PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://coderstalk.googlecode.com/
PHP | 192 lines | 146 code | 46 blank | 0 comment | 17 complexity | c3c8d62d1be3a9e493d4b719d7a97f74 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, AGPL-3.0
  1. <?php
  2. class ControllerPaymentPayPalDirect extends Controller {
  3. protected function index() {
  4. $this->language->load('payment/paypal_direct');
  5. $this->data['text_credit_card'] = $this->language->get('text_credit_card');
  6. $this->data['text_start_date'] = $this->language->get('text_start_date');
  7. $this->data['text_issue_number'] = $this->language->get('text_issue_number');
  8. $this->data['entry_credit_card_type'] = $this->language->get('entry_credit_card_type');
  9. $this->data['entry_credit_card_number'] = $this->language->get('entry_credit_card_number');
  10. $this->data['entry_start_date'] = $this->language->get('entry_start_date');
  11. $this->data['entry_expire_date'] = $this->language->get('entry_expire_date');
  12. $this->data['entry_cvv2_number'] = $this->language->get('entry_cvv2_number');
  13. $this->data['entry_issue_number'] = $this->language->get('entry_issue_number');
  14. $this->data['button_confirm'] = $this->language->get('button_confirm');
  15. $this->data['button_back'] = $this->language->get('button_back');
  16. $this->data['cards'] = array();
  17. $this->data['cards'][] = array(
  18. 'text' => 'Visa',
  19. 'value' => 'VISA'
  20. );
  21. $this->data['cards'][] = array(
  22. 'text' => 'MasterCard',
  23. 'value' => 'MASTERCARD'
  24. );
  25. $this->data['cards'][] = array(
  26. 'text' => 'Discover Card',
  27. 'value' => 'DISCOVER'
  28. );
  29. $this->data['cards'][] = array(
  30. 'text' => 'American Express',
  31. 'value' => 'AMEX'
  32. );
  33. $this->data['cards'][] = array(
  34. 'text' => 'Maestro',
  35. 'value' => 'SWITCH'
  36. );
  37. $this->data['cards'][] = array(
  38. 'text' => 'Solo',
  39. 'value' => 'SOLO'
  40. );
  41. $this->data['months'] = array();
  42. for ($i = 1; $i <= 12; $i++) {
  43. $this->data['months'][] = array(
  44. 'text' => strftime('%B', mktime(0, 0, 0, $i, 1, 2000)),
  45. 'value' => sprintf('%02d', $i)
  46. );
  47. }
  48. $today = getdate();
  49. $this->data['year_valid'] = array();
  50. for ($i = $today['year'] - 10; $i < $today['year'] + 1; $i++) {
  51. $this->data['year_valid'][] = array(
  52. 'text' => strftime('%Y', mktime(0, 0, 0, 1, 1, $i)),
  53. 'value' => strftime('%Y', mktime(0, 0, 0, 1, 1, $i))
  54. );
  55. }
  56. $this->data['year_expire'] = array();
  57. for ($i = $today['year']; $i < $today['year'] + 10; $i++) {
  58. $this->data['year_expire'][] = array(
  59. 'text' => strftime('%Y', mktime(0, 0, 0, 1, 1, $i)),
  60. 'value' => strftime('%Y', mktime(0, 0, 0, 1, 1, $i))
  61. );
  62. }
  63. $this->data['back'] = $this->url->https('checkout/payment');
  64. $this->id = 'payment';
  65. $this->template = $this->config->get('config_template') . 'payment/paypal_direct.tpl';
  66. $this->render();
  67. }
  68. public function send() {
  69. if (!$this->config->get('paypal_direct_test')) {
  70. $api_endpoint = 'https://api-3t.paypal.com/nvp';
  71. } else {
  72. $api_endpoint = 'https://api-3t.sandbox.paypal.com/nvp';
  73. }
  74. if (!$this->config->get('paypal_direct_transaction')) {
  75. $payment_type = 'Authorization';
  76. } else {
  77. $payment_type = 'Sale';
  78. }
  79. $this->load->model('checkout/order');
  80. $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
  81. $payment_address = $this->customer->getAddress($this->session->data['payment_address_id']);
  82. $ch = curl_init();
  83. curl_setopt($ch, CURLOPT_URL, $api_endpoint);
  84. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  85. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  86. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  87. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  88. curl_setopt($ch, CURLOPT_POST, 1);
  89. $payment_data = array(
  90. 'METHOD' => 'DoDirectPayment',
  91. 'VERSION' => '51.0',
  92. 'PWD' => $this->config->get('paypal_direct_password'),
  93. 'USER' => $this->config->get('paypal_direct_username'),
  94. 'SIGNATURE' => $this->config->get('paypal_direct_signature'),
  95. 'CUSTREF' => $order_info['order_id'],
  96. 'PAYMENTACTION' => $payment_type,
  97. 'AMT' => $this->currency->format($order_info['total'], $order_info['currency'], 1.00000, FALSE),
  98. 'CREDITCARDTYPE' => $this->request->post['credit_card_type'],
  99. 'ACCT' => str_replace(' ', '', $this->request->post['credit_card_number']),
  100. 'CARDSTART' => $this->request->post['start_date_month'] . $this->request->post['start_date_year'],
  101. 'EXPDATE' => $this->request->post['expire_date_month'] . $this->request->post['expire_date_year'],
  102. 'CVV2' => $this->request->post['cvv2_number'],
  103. 'CARDISSUE' => $this->request->post['issue_number'],
  104. 'FIRSTNAME' => $order_info['payment_firstname'],
  105. 'LASTNAME' => $order_info['payment_lastname'],
  106. 'EMAIL' => $order_info['email'],
  107. 'PHONENUM' => $order_info['telephone'],
  108. 'IPADDRESS' => $this->request->server['REMOTE_ADDR'],
  109. 'STREET' => $order_info['payment_address_1'],
  110. 'CITY' => $order_info['payment_city'],
  111. 'STATE' => $order_info['payment_zone'],
  112. 'ZIP' => $order_info['payment_postcode'],
  113. 'COUNTRYCODE' => $payment_address['iso_code_2'],
  114. 'CURRENCYCODE' => $order_info['currency']
  115. );
  116. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payment_data));
  117. $response = curl_exec($ch);
  118. if (!$response) {
  119. exit('DoDirectPayment failed: ' . curl_error($ch) . '(' . curl_errno($ch) . ')');
  120. }
  121. $response_data = array();
  122. parse_str($response, $response_data);
  123. $json = array();
  124. if ($response_data['ACK'] == 'Success') {
  125. $this->model_checkout_order->confirm($this->session->data['order_id'], $this->config->get('config_order_status_id'));
  126. $message = '';
  127. if (isset($response_data['AVSCODE'])) {
  128. $message .= 'AVSCODE: ' . $response_data['AVSCODE'] . "\n";
  129. }
  130. if (isset($response_data['CVV2MATCH'])) {
  131. $message .= 'CVV2MATCH: ' . $response_data['CVV2MATCH'] . "\n";
  132. }
  133. if (isset($response_data['TRANSACTIONID'])) {
  134. $message .= 'TRANSACTIONID: ' . $response_data['TRANSACTIONID'] . "\n";
  135. }
  136. $this->model_checkout_order->update($this->session->data['order_id'], $this->config->get('paypal_direct_order_status_id'), $message, FALSE);
  137. $json['success'] = TRUE;
  138. }
  139. if (($response_data['ACK'] != 'Success') && ($response_data['ACK'] != 'SuccessWithWarning')) {
  140. $json['error'] = $response_data['L_LONGMESSAGE0'];
  141. }
  142. $this->load->library('json');
  143. $this->response->setOutput(Json::encode($json));
  144. }
  145. }
  146. ?>