PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/www/shop/engine/Shopware/Plugins/Default/Frontend/SwagPaymentBillsafe/Controllers/Frontend/PaymentBillsafe.php

https://bitbucket.org/weberlars/sot-shopware
PHP | 366 lines | 236 code | 26 blank | 104 comment | 44 complexity | 9cb4fef0dbdd967d887b714697eb16ac MD5 | raw file
Possible License(s): AGPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * Shopware 4.0
  4. * Copyright Š 2012 shopware AG
  5. *
  6. * According to our dual licensing model, this program can be used either
  7. * under the terms of the GNU Affero General Public License, version 3,
  8. * or under a proprietary license.
  9. *
  10. * The texts of the GNU Affero General Public License with an additional
  11. * permission and of our proprietary license can be found at and
  12. * in the LICENSE file you have received along with this program.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * "Shopware" is a registered trademark of shopware AG.
  20. * The licensing of the program under the AGPLv3 does not imply a
  21. * trademark license. Therefore any rights, title and interest in
  22. * our trademarks remain entirely with us.
  23. *
  24. * @category Shopware
  25. * @package Shopware_Plugins
  26. * @subpackage Plugin
  27. * @copyright Copyright (c) 2012, shopware AG (http://www.shopware.de)
  28. * @version $Id$
  29. * @author Heiner Lohaus
  30. * @author $Author$
  31. */
  32. /**
  33. * Billsafe payment controller
  34. */
  35. class Shopware_Controllers_Frontend_PaymentBillsafe extends Shopware_Controllers_Frontend_Payment
  36. {
  37. /**
  38. * Pre dispatch method
  39. */
  40. public function preDispatch()
  41. {
  42. $this->View()->setScope(Enlight_Template_Manager::SCOPE_PARENT);
  43. }
  44. /**
  45. * Index action method.
  46. *
  47. * Forwards to correct the action.
  48. */
  49. public function indexAction()
  50. {
  51. if($this->getPaymentShortName() == 'billsafe_invoice') {
  52. $this->redirect(array('action' => 'gateway', 'forceSecure' => true));
  53. } else {
  54. $this->redirect(array('controller' => 'checkout'));
  55. }
  56. }
  57. /**
  58. * Returns the article list parameter data.
  59. *
  60. * @return array
  61. */
  62. protected function getArticleListParameter()
  63. {
  64. $user = $this->getUser();
  65. $basket = $this->getBasket();
  66. $articleList = array();
  67. foreach ($basket['content'] as $item) {
  68. if (!empty($user['additional']['charge_vat']) && !empty($item['amountWithTax'])){
  69. $price = round($item['amountWithTax'] / $item['quantity'], 2);
  70. } else {
  71. $price = str_replace(',', '.', $item['price']);
  72. }
  73. $article = array(
  74. 'number' => $item['ordernumber'],
  75. 'name' => strlen($item['articlename']) > 100 ? substr($item['articlename'], 0, 90) . '...' : $item['articlename'] ,
  76. 'description' => '',
  77. 'quantity' => $item['quantity'],
  78. 'grossPrice' => $price,
  79. 'quantityShipped' => 0,
  80. );
  81. if(empty($article['grossPrice']) || empty($user['additional']['charge_vat'])) {
  82. $article['tax'] = 0;
  83. } elseif(!empty($item['taxPercent'])) {
  84. $article['tax'] = $item['taxPercent'];
  85. } else {
  86. if (!empty($user['additional']['charge_vat']) && !empty($item['amountWithTax'])){
  87. $amount = $item['amountWithTax'];
  88. } else {
  89. $amount = str_replace(',', '.', $item['amount']);
  90. }
  91. $article['tax'] = round(
  92. $amount / str_replace(',', '.', $item['amountnet'])
  93. , 2) * 100 - 100;
  94. }
  95. // Detect kind of article
  96. if($item['modus'] == 4) {
  97. $article['type'] = 'handling';
  98. } else {
  99. $article['type'] = $price >= 0 ? 'goods' : 'voucher';
  100. }
  101. $testKey = $article['type'] === 'goods' ? null : $article['type'];
  102. if($testKey === null) {
  103. $articleList[] = $article;
  104. } elseif(!isset($articleList[$testKey])) {
  105. $articleList[$testKey] = $article;
  106. } else {
  107. $articleList[$testKey]['grossPrice'] += $article['grossPrice'];
  108. }
  109. }
  110. if(!empty($basket['sShippingcosts'])) {
  111. $article = array(
  112. 'number' => 'shipment',
  113. 'name' => 'Versandkosten',
  114. 'description' => '',
  115. 'quantity' => 1,
  116. 'grossPrice' => $this->getShipment(),
  117. 'tax' => $this->getTaxShipment(),
  118. 'type' => 'shipment',
  119. );
  120. $articleList[] = $article;
  121. }
  122. return array_values($articleList);
  123. }
  124. /**
  125. * Returns the prepared customer parameter data.
  126. *
  127. * @return array
  128. */
  129. protected function getCustomerParameter()
  130. {
  131. $user = $this->getUser();
  132. $customer = array(
  133. 'id' => $user['billingaddress']['customernumber'],
  134. 'company' => $user['billingaddress']['company'],
  135. 'gender' => $user['billingaddress']['salutation'] == 'ms' ? 'f' : 'm',
  136. 'firstname' => $user['billingaddress']['firstname'],
  137. 'lastname' => $user['billingaddress']['lastname'],
  138. 'street' => $user['billingaddress']['street'],
  139. 'houseNumber' => $user['billingaddress']['streetnumber'],
  140. 'postcode' => $user['billingaddress']['zipcode'],
  141. 'city' => $user['billingaddress']['city'],
  142. 'country' => $user['additional']['country']['countryiso'],
  143. 'email' => $user['additional']['user']['email'],
  144. 'phone' => $user['billingaddress']['phone'],
  145. );
  146. if(!empty($user['billingaddress']['birthday']) && $user['billingaddress']['birthday'] != '0000-00-00') {
  147. $customer['dateOfBirth'] = $user['billingaddress']['birthday'];
  148. }
  149. if(!empty($user['billingaddress']['company'])) {
  150. $customer['company'] = $user['billingaddress']['company'];
  151. }
  152. return $customer;
  153. }
  154. /**
  155. * Gateway action method.
  156. *
  157. * Collects the payment information and transmit it to the payment provider.
  158. */
  159. public function gatewayAction()
  160. {
  161. $router = $this->Front()->Router();
  162. $config = $this->Config();
  163. $articleList = $this->getArticleListParameter();
  164. $customer = $this->getCustomerParameter();
  165. $parameter = array(
  166. 'order' => array(
  167. 'amount' => $this->getAmount(),
  168. 'taxAmount' => $this->getTaxAmount(),
  169. 'currencyCode' => $this->getCurrencyShortName()
  170. ),
  171. 'customer' => $customer,
  172. 'articleList' => $articleList,
  173. 'product' => 'invoice',
  174. 'url' => array(
  175. 'return' => $router->assemble(array('action' => 'return', 'forceSecure' => true)),
  176. 'cancel' => $router->assemble(array('action' => 'cancel', 'forceSecure' => true)),
  177. 'image' => $this->View()->fetch('string:{link file=' . var_export($config->logo, true) . ' fullPath}')
  178. ),
  179. 'sessionId' => Shopware()->SessionID(),
  180. 'custom' => array(
  181. $this->getAmount(),
  182. $this->createPaymentUniqueId()
  183. )
  184. );
  185. $response = Shopware()->BillsafeClient()->prepareOrder($parameter);
  186. if($config->debug) {
  187. Shopware()->Log()->info($parameter);
  188. Shopware()->Log()->info($response);
  189. }
  190. $this->View()->BillsafeResponse = $response;
  191. $this->View()->BillsafeConfig = $config;
  192. if(empty($response->token)) {
  193. $this->forward('cancel');
  194. }
  195. }
  196. /**
  197. * Return action method
  198. *
  199. * Reads the transactionResult and represents it for the customer.
  200. */
  201. public function returnAction()
  202. {
  203. $token = $this->Request()->getParam('token');
  204. $client = Shopware()->BillsafeClient();
  205. $config = $this->Config();
  206. $transactionResult = $client->getTransactionResult(array('token' => $token));
  207. if ($transactionResult->ack === 'ERROR'
  208. && isset(Shopware()->Session()->BillsafeResult)) {
  209. $transactionResult = Shopware()->Session()->BillsafeResult;
  210. } else {
  211. Shopware()->Session()->BillsafeResult = $transactionResult;
  212. }
  213. if ($transactionResult->status == 'ACCEPTED') {
  214. $amount = $transactionResult->custom[0];
  215. $secret = $transactionResult->custom[1];
  216. if ($amount == $this->getAmount()) {
  217. $paymentStatusId = !empty($config->paymentStatusId) ? $config->paymentStatusId : 17;
  218. } else {
  219. $paymentStatusId = 21;
  220. }
  221. $orderNumber = $this->saveOrder($transactionResult->transactionId, $secret, $paymentStatusId);
  222. $client->setOrderNumber(array(
  223. 'transactionId' => $transactionResult->transactionId,
  224. 'orderNumber' => $orderNumber
  225. ));
  226. $this->redirect(array('controller' => 'checkout', 'action' => 'finish', 'sUniqueID' => $secret));
  227. } else {
  228. $this->View()->BillsafeResponse = $transactionResult;
  229. $this->forward('cancel');
  230. }
  231. }
  232. /**
  233. * Cancel action method
  234. *
  235. * Reads the payment config
  236. */
  237. public function cancelAction()
  238. {
  239. $this->View()->BillsafeConfig = $this->Config();
  240. }
  241. /**
  242. * Returns the payment plugin config data.
  243. *
  244. * @return Shopware_Models_Plugin_Config
  245. */
  246. public function Config()
  247. {
  248. return Shopware()->Plugins()->Frontend()->SwagPaymentBillsafe()->Config();
  249. }
  250. /**
  251. * Sets order comment by order number
  252. *
  253. * @param $orderNumber
  254. * @param $comment
  255. * @return void
  256. */
  257. public function setOrderComment($orderNumber, $comment)
  258. {
  259. $sql = '
  260. UPDATE s_order SET comment=? WHERE ordernumber=?
  261. ';
  262. Shopware()->Db()->query($sql, array(
  263. $comment,
  264. $orderNumber
  265. ));
  266. }
  267. /**
  268. * Returns basket tax amount as float
  269. *
  270. * @return float
  271. */
  272. public function getShipment()
  273. {
  274. $user = $this->getUser();
  275. $basket = $this->getBasket();
  276. if (!empty($user['additional']['charge_vat'])){
  277. return $basket['sShippingcostsWithTax'];
  278. } else {
  279. return str_replace(',', '.', $basket['sShippingcosts']);
  280. }
  281. }
  282. /**
  283. * Returns basket tax amount as float
  284. *
  285. * @return float
  286. */
  287. public function getTaxShipment()
  288. {
  289. $user = $this->getUser();
  290. $basket = $this->getBasket();
  291. if (!empty($user['additional']['charge_vat'])){
  292. return round(
  293. $basket['sShippingcostsWithTax'] / $basket['sShippingcostsNet']
  294. , 2) * 100 - 100;
  295. } else {
  296. return 0;
  297. }
  298. }
  299. /**
  300. * Returns basket tax amount as float
  301. *
  302. * @return float
  303. */
  304. public function getTaxAmount()
  305. {
  306. $user = $this->getUser();
  307. $basket = $this->getBasket();
  308. if (!empty($user['additional']['charge_vat'])){
  309. return $basket['sAmountTax'];
  310. } else {
  311. return 0;
  312. }
  313. }
  314. /**
  315. * Returns the full user data as array
  316. *
  317. * @return array
  318. */
  319. public function getUser()
  320. {
  321. if(!empty(Shopware()->Session()->sOrderVariables['sUserData'])) {
  322. return Shopware()->Session()->sOrderVariables['sUserData'];
  323. } else {
  324. return null;
  325. }
  326. }
  327. /**
  328. * Returns the full basket data as array
  329. *
  330. * @return array
  331. */
  332. public function getBasket()
  333. {
  334. if(!empty(Shopware()->Session()->sOrderVariables['sBasket'])) {
  335. return Shopware()->Session()->sOrderVariables['sBasket'];
  336. } else {
  337. return null;
  338. }
  339. }
  340. }