PageRenderTime 27ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/opencart/trunk/upload/catalog/controller/payment/sagepay.php

http://coderstalk.googlecode.com/
PHP | 260 lines | 196 code | 64 blank | 0 comment | 44 complexity | 380f3c4cfdf0b79d33f3dd0e57170630 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, AGPL-3.0
  1. <?php
  2. class ControllerPaymentSagepay extends Controller {
  3. protected function index() {
  4. $this->language->load('payment/sagepay');
  5. $this->data['button_confirm'] = $this->language->get('button_confirm');
  6. $this->data['button_back'] = $this->language->get('button_back');
  7. if ($this->config->get('sagepay_test') == 'live') {
  8. $this->data['action'] = 'https://live.sagepay.com/gateway/service/vspform-register.vsp';
  9. } elseif ($this->config->get('sagepay_test') == 'test') {
  10. $this->data['action'] = 'https://test.sagepay.com/gateway/service/vspform-register.vsp';
  11. } elseif ($this->config->get('sagepay_test') == 'sim') {
  12. $this->data['action'] = 'https://test.sagepay.com/simulator/vspformgateway.asp';
  13. }
  14. $vendor = $this->config->get('sagepay_vendor');
  15. $password = $this->config->get('sagepay_password');
  16. $this->load->model('checkout/order');
  17. $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
  18. $data = array();
  19. $data['VendorTxCode'] = $this->session->data['order_id'];
  20. $data['ReferrerID'] = 'E511AF91-E4A0-42DE-80B0-09C981A3FB61';
  21. $data['Amount'] = $this->currency->format($order_info['total'], $order_info['currency'], $order_info['value'], FALSE);
  22. $data['Currency'] = $order_info['currency'];
  23. $data['Description'] = sprintf($this->language->get('text_description'), date($this->language->get('date_format_short')), $this->session->data['order_id']);
  24. $data['SuccessURL'] = html_entity_decode($this->url->https('payment/sagepay/success&order_id=' . $this->session->data['order_id']));
  25. if ($this->request->get['route'] != 'checkout/guest/confirm') {
  26. $this->data['FailureURL'] = $this->url->https('checkout/payment');
  27. } else {
  28. $this->data['FailureURL'] = $this->url->https('checkout/guest');
  29. }
  30. $data['CustomerName'] = $order_info['payment_firstname'] . ' ' . $order_info['payment_lastname'];
  31. $data['SendEMail'] = '1';
  32. $data['CustomerEMail'] = $order_info['email'];
  33. $data['VendorEMail'] = $this->config->get('config_email');
  34. $data['BillingFirstnames'] = $order_info['payment_firstname'];
  35. $data['BillingSurname'] = $order_info['payment_lastname'];
  36. $data['BillingAddress1'] = $order_info['payment_address_1'];
  37. if ($order_info['payment_address_2']) {
  38. $data['BillingAddress2'] = $order_info['payment_address_2'];
  39. }
  40. $data['BillingCity'] = $order_info['payment_city'];
  41. $data['BillingPostCode'] = $order_info[payment_'postcode'];
  42. $data['BillingCountry'] = $order_info['payment_iso_code_2'];
  43. if ($order_info['payment_iso_code_2'] == 'US') {
  44. $data['BillingState'] = $order_info['payment_zone_code'];
  45. }
  46. $data['BillingPhone=' . $order_info['telephone'];
  47. if ($this->cart->hasShipping()) {
  48. $data['DeliveryFirstnames'] = $order_info['shipping_firstname'];
  49. $data['DeliverySurname'] = $order_info['shipping_lastname'];
  50. $data['DeliveryAddress1'] = $order_info['shipping_address_1'];
  51. if ($order_info['shipping_address_2']) {
  52. $data['DeliveryAddress2'] = $order_info['shipping_address_2'];
  53. }
  54. $data['DeliveryCity'] = $order_info['shipping_city'];
  55. $data['DeliveryPostCode'] = $order_info['shipping_postcode'];
  56. $data['DeliveryCountry'] = $order_info['shipping_iso_code_2'];
  57. if ($order_info['shipping_iso_code_2'] == 'US') {
  58. $data['DeliveryState'] = $order_info['shipping_zone_code'];
  59. }
  60. $data['DeliveryPhone'] = $order_info['telephone'];
  61. } else {
  62. $data['DeliveryFirstnames'] = $order_info['payment_firstname'];
  63. $data['DeliverySurname'] = $order_info['payment_lastname'];
  64. $data['DeliveryAddress1'] = $order_info['payment_address_1'];
  65. if ($order_info['payment_address_2']) {
  66. $data['DeliveryAddress2'] = $order_info['payment_address_2'];
  67. }
  68. $data['DeliveryCity'] = $order_info['payment_city'];
  69. $data['DeliveryPostCode'] = $order_info['payment_postcode'];
  70. $data['DeliveryCountry'] = $order_info['payment_iso_code_2'];
  71. if ($order_info['$payment_iso_code_2'] == 'US') {
  72. $data['DeliveryState'] = $order_info['payment_zone_code'];
  73. }
  74. $data['DeliveryPhone'] = $order_info['telephone'];
  75. }
  76. $data['AllowGiftAid'] = '0';
  77. if (!$this->config->get('sagepay_transaction')) {
  78. $data['ApplyAVSCV2'] = '0';
  79. }
  80. $data['Apply3DSecure'] = '0';
  81. $this->data['transaction'] = $this->config->get('sagepay_transaction');
  82. $this->data['vendor'] = $vendor;
  83. $this->data['crypt'] = base64_encode($this->simpleXor(http_build_query($data), $password));
  84. if ($this->request->get['route'] != 'checkout/guest/confirm') {
  85. $this->data['back'] = $this->url->https('checkout/payment');
  86. } else {
  87. $this->data['back'] = $this->url->https('checkout/guest');
  88. }
  89. $this->id = 'payment';
  90. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/sagepay.tpl')) {
  91. $this->template = $this->config->get('config_template') . '/template/payment/sagepay.tpl';
  92. } else {
  93. $this->template = 'default/template/payment/sagepay.tpl';
  94. }
  95. $this->render();
  96. }
  97. public function success() {
  98. if (isset($this->request->get['crypt'])) {
  99. $string = base64_decode(str_replace(' ', '+', $this->request->get['crypt']));
  100. $password = $this->config->get('sagepay_password');
  101. $output = $this->simpleXor($string, $password);
  102. $data = $this->getToken($output);
  103. if ($data) {
  104. $this->load->model('checkout/order');
  105. $this->model_checkout_order->confirm($this->request->get['order_id'], $this->config->get('sagepay_order_status_id'));
  106. $message = '';
  107. if (isset($data['VPSTxId'])) {
  108. $message .= 'VPSTxId: ' . $data['VPSTxId'] . "\n";
  109. }
  110. if (isset($data['TxAuthNo'])) {
  111. $message .= 'TxAuthNo: ' . $data['TxAuthNo'] . "\n";
  112. }
  113. if (isset($data['AVSCV2'])) {
  114. $message .= 'AVSCV2: ' . $data['AVSCV2'] . "\n";
  115. }
  116. if (isset($data['AddressResult'])) {
  117. $message .= 'AddressResult: ' . $data['AddressResult'] . "\n";
  118. }
  119. if (isset($data['PostCodeResult'])) {
  120. $message .= 'PostCodeResult: ' . $data['PostCodeResult'] . "\n";
  121. }
  122. if (isset($data['CV2Result'])) {
  123. $message .= 'CV2Result: ' . $data['CV2Result'] . "\n";
  124. }
  125. if (isset($data['3DSecureStatus'])) {
  126. $message .= '3DSecureStatus: ' . $data['3DSecureStatus'] . "\n";
  127. }
  128. if (isset($data['CAVV'])) {
  129. $message .= 'CAVV: ' . $data['CAVV'] . "\n";
  130. }
  131. if (isset($data['CardType'])) {
  132. $message .= 'CardType: ' . $data['CardType'] . "\n";
  133. }
  134. if (isset($data['Last4Digits'])) {
  135. $message .= 'Last4Digits: ' . $data['Last4Digits'] . "\n";
  136. }
  137. $this->model_checkout_order->update($this->request->get['order_id'], $this->config->get('sagepay_order_status_id'), $message, FALSE);
  138. $this->redirect($this->url->http('checkout/success'));
  139. }
  140. }
  141. }
  142. private function simpleXor($string, $password) {
  143. $data = array();
  144. for ($i = 0; $i < strlen(utf8_decode($password)); $i++) {
  145. $data[$i] = ord(substr($password, $i, 1));
  146. }
  147. $output = '';
  148. for ($i = 0; $i < strlen(utf8_decode($string)); $i++) {
  149. $output .= chr(ord(substr($string, $i, 1)) ^ ($data[$i % strlen(utf8_decode($password))]));
  150. }
  151. return $output;
  152. }
  153. private function getToken($string) {
  154. $tokens = array(
  155. 'Status',
  156. 'StatusDetail',
  157. 'VendorTxCode',
  158. 'VPSTxId',
  159. 'TxAuthNo',
  160. 'Amount',
  161. 'AVSCV2',
  162. 'AddressResult',
  163. 'PostCodeResult',
  164. 'CV2Result',
  165. 'GiftAid',
  166. '3DSecureStatus',
  167. 'CAVV',
  168. 'AddressStatus',
  169. 'CardType',
  170. 'Last4Digits',
  171. 'PayerStatus',
  172. 'CardType'
  173. );
  174. $output = array();
  175. $data = array();
  176. for ($i = count($tokens) - 1; $i >= 0; $i--){
  177. $start = strpos($string, $tokens[$i]);
  178. if ($start){
  179. $data[$i]['start'] = $start;
  180. $data[$i]['token'] = $tokens[$i];
  181. }
  182. }
  183. sort($data);
  184. for ($i = 0; $i < count($data); $i++){
  185. $start = $data[$i]['start'] + strlen($data[$i]['token']) + 1;
  186. if ($i == (count($data) - 1)) {
  187. $output[$data[$i]['token']] = substr($string, $start);
  188. } else {
  189. $length = $data[$i+1]['start'] - $data[$i]['start'] - strlen($data[$i]['token']) - 2;
  190. $output[$data[$i]['token']] = substr($string, $start, $length);
  191. }
  192. }
  193. return $output;
  194. }
  195. }
  196. ?>