PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/catalog/controller/payment/pp_standard.php

https://github.com/MariusRugan/OpenCart-Secured
PHP | 146 lines | 115 code | 31 blank | 0 comment | 31 complexity | ec34f99c44a695b26e829621b1d2986b MD5 | raw file
  1. <?php
  2. class ControllerPaymentPPStandard extends Controller {
  3. protected function index() {
  4. $this->data['button_confirm'] = $this->language->get('button_confirm');
  5. $this->data['button_back'] = $this->language->get('button_back');
  6. if (!$this->config->get('pp_standard_test')) {
  7. $this->data['action'] = 'https://www.paypal.com/cgi-bin/webscr';
  8. } else {
  9. $this->data['action'] = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
  10. }
  11. $this->load->model('checkout/order');
  12. $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
  13. $this->data['business'] = $this->config->get('pp_standard_email');
  14. $this->data['item_name'] = html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8');
  15. $this->data['currency_code'] = $order_info['currency'];
  16. $this->data['amount'] = $this->currency->format($order_info['total'], $order_info['currency'], $order_info['value'], FALSE);
  17. $this->data['first_name'] = html_entity_decode($order_info['payment_firstname'], ENT_QUOTES, 'UTF-8');
  18. $this->data['last_name'] = html_entity_decode($order_info['payment_lastname'], ENT_QUOTES, 'UTF-8');
  19. $this->data['address1'] = html_entity_decode($order_info['payment_address_1'], ENT_QUOTES, 'UTF-8');
  20. $this->data['address2'] = html_entity_decode($order_info['payment_address_2'], ENT_QUOTES, 'UTF-8');
  21. $this->data['city'] = html_entity_decode($order_info['payment_city'], ENT_QUOTES, 'UTF-8');
  22. $this->data['zip'] = html_entity_decode($order_info['payment_postcode'], ENT_QUOTES, 'UTF-8');
  23. $this->data['country'] = $order_info['payment_iso_code_2'];
  24. $this->data['notify_url'] = HTTP_SERVER . 'index.php?route=payment/pp_standard/callback';
  25. $this->data['email'] = $order_info['email'];
  26. $this->data['invoice'] = $this->session->data['order_id'] . ' - ' . html_entity_decode($order_info['payment_firstname'], ENT_QUOTES, 'UTF-8') . ' ' . html_entity_decode($order_info['payment_lastname'], ENT_QUOTES, 'UTF-8');
  27. $this->data['lc'] = $this->session->data['language'];
  28. if (!$this->config->get('pp_standard_transaction')) {
  29. $this->data['paymentaction'] = 'authorization';
  30. } else {
  31. $this->data['paymentaction'] = 'sale';
  32. }
  33. $this->data['return'] = HTTPS_SERVER . 'index.php?route=checkout/success';
  34. if ($this->request->get['route'] != 'checkout/guest_step_3') {
  35. $this->data['cancel_return'] = HTTPS_SERVER . 'index.php?route=checkout/payment';
  36. } else {
  37. $this->data['cancel_return'] = HTTPS_SERVER . 'index.php?route=checkout/guest_step_2';
  38. }
  39. $this->load->library('encryption');
  40. $encryption = new Encryption($this->config->get('config_encryption'));
  41. $this->data['custom'] = $encryption->encrypt($this->session->data['order_id']);
  42. if ($this->request->get['route'] != 'checkout/guest_step_3') {
  43. $this->data['back'] = HTTPS_SERVER . 'index.php?route=checkout/payment';
  44. } else {
  45. $this->data['back'] = HTTPS_SERVER . 'index.php?route=checkout/guest_step_2';
  46. }
  47. $this->id = 'payment';
  48. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/pp_standard.tpl')) {
  49. $this->template = $this->config->get('config_template') . '/template/payment/pp_standard.tpl';
  50. } else {
  51. $this->template = 'default/template/payment/pp_standard.tpl';
  52. }
  53. $this->render();
  54. }
  55. public function callback() {
  56. $this->load->library('encryption');
  57. $encryption = new Encryption($this->config->get('config_encryption'));
  58. if (isset($this->request->post['custom'])) {
  59. $order_id = $encryption->decrypt($this->request->post['custom']);
  60. } else {
  61. $order_id = 0;
  62. }
  63. $this->load->model('checkout/order');
  64. $order_info = $this->model_checkout_order->getOrder($order_id);
  65. if ($order_info) {
  66. $request = 'cmd=_notify-validate';
  67. foreach ($this->request->post as $key => $value) {
  68. $request .= '&' . $key . '=' . urlencode(stripslashes(html_entity_decode($value, ENT_QUOTES, 'UTF-8')));
  69. }
  70. if (extension_loaded('curl')) {
  71. if (!$this->config->get('pp_standard_test')) {
  72. $ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
  73. } else {
  74. $ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
  75. }
  76. curl_setopt($ch, CURLOPT_POST, true);
  77. curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
  78. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  79. curl_setopt($ch, CURLOPT_HEADER, false);
  80. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  81. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  82. $response = curl_exec($ch);
  83. if (strcmp($response, 'VERIFIED') == 0 || $this->request->post['payment_status'] == 'Completed') {
  84. $this->model_checkout_order->confirm($order_id, $this->config->get('pp_standard_order_status_id'));
  85. } else {
  86. $this->model_checkout_order->confirm($order_id, $this->config->get('config_order_status_id'));
  87. }
  88. curl_close($ch);
  89. } else {
  90. $header = 'POST /cgi-bin/webscr HTTP/1.0' . "\r\n";
  91. $header .= 'Content-Type: application/x-www-form-urlencoded' . "\r\n";
  92. $header .= 'Content-Length: ' . strlen(utf8_decode($request)) . "\r\n";
  93. $header .= 'Connection: close' ."\r\n\r\n";
  94. if (!$this->config->get('pp_standard_test')) {
  95. $fp = fsockopen('www.paypal.com', 80, $errno, $errstr, 30);
  96. } else {
  97. $fp = fsockopen('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
  98. }
  99. if ($fp) {
  100. fputs($fp, $header . $request);
  101. while (!feof($fp)) {
  102. $response = fgets($fp, 1024);
  103. if (strcmp($response, 'VERIFIED') == 0) {
  104. $this->model_checkout_order->confirm($order_id, $this->config->get('pp_standard_order_status_id'));
  105. } else {
  106. $this->model_checkout_order->confirm($order_id, $this->config->get('config_order_status_id'));
  107. }
  108. }
  109. fclose($fp);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. ?>