PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/cursos/modules/paypal/validation.php

https://github.com/hecbuma/quali-fisioterapia
PHP | 139 lines | 94 code | 13 blank | 32 comment | 26 complexity | 18a3b5a22d595b4a283e0ab5bcb9dc24 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2011 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Academic Free License (AFL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/afl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2011 PrestaShop SA
  23. * @version Release: $Revision: 1.4 $
  24. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. */
  27. include(dirname(__FILE__).'/../../config/config.inc.php');
  28. include(dirname(__FILE__).'/paypal.php');
  29. $errors = '';
  30. $result = false;
  31. $paypal = new Paypal();
  32. // Fill params
  33. $params = 'cmd=_notify-validate';
  34. foreach ($_POST AS $key => $value)
  35. $params .= '&'.$key.'='.urlencode(stripslashes($value));
  36. // PayPal Server
  37. $paypalServer = 'www.'.(Configuration::get('PAYPAL_SANDBOX') ? 'sandbox.' : '').'paypal.com';
  38. // Getting PayPal data...
  39. if (function_exists('curl_exec'))
  40. {
  41. // curl ready
  42. $ch = curl_init('https://' . $paypalServer . '/cgi-bin/webscr');
  43. // If the above fails, then try the url with a trailing slash (fixes problems on some servers)
  44. if (!$ch)
  45. $ch = curl_init('https://' . $paypalServer . '/cgi-bin/webscr/');
  46. if (!$ch)
  47. $errors .= $paypal->getL('connect').' '.$paypal->getL('curlmethodfailed');
  48. else
  49. {
  50. curl_setopt($ch, CURLOPT_POST, true);
  51. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  52. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  53. curl_setopt($ch, CURLOPT_HEADER, false);
  54. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  55. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  56. $result = curl_exec($ch);
  57. if (strtoupper($result) != 'VERIFIED')
  58. $errors .= $paypal->getL('curlmethod').$result.' cURL error:'.curl_error($ch);
  59. curl_close($ch);
  60. }
  61. }
  62. elseif (($fp = @fsockopen('ssl://' . $paypalServer, 443, $errno, $errstr, 30)) || ($fp = @fsockopen($paypalServer, 80, $errno, $errstr, 30)))
  63. {
  64. // fsockopen ready
  65. $header = 'POST /cgi-bin/webscr HTTP/1.0'."\r\n" .
  66. 'Host: '.$paypalServer."\r\n".
  67. 'Content-Type: application/x-www-form-urlencoded'."\r\n".
  68. 'Content-Length: '.Tools::strlen($params)."\r\n".
  69. 'Connection: close'."\r\n\r\n";
  70. fputs($fp, $header.$params);
  71. $read = '';
  72. while (!feof($fp))
  73. {
  74. $reading = trim(fgets($fp, 1024));
  75. $read .= $reading;
  76. if (strtoupper($reading) == 'VERIFIED' OR strtoupper($reading) == 'INVALID')
  77. {
  78. $result = $reading;
  79. break;
  80. }
  81. }
  82. if (strtoupper($result) != 'VERIFIED')
  83. $errors .= $paypal->getL('socketmethod').$result;
  84. fclose($fp);
  85. }
  86. else
  87. $errors = $paypal->getL('connect').$paypal->getL('nomethod');
  88. if (isset($_POST['custom']))
  89. $cart_secure = explode('_', $_POST['custom']);
  90. else
  91. $cart_secure = array();
  92. // Printing errors...
  93. if (strtoupper($result) == 'VERIFIED')
  94. {
  95. if (!isset($_POST['mc_gross']))
  96. $errors .= $paypal->getL('mc_gross').'<br />';
  97. if (!isset($_POST['payment_status']))
  98. $errors .= $paypal->getL('payment_status').'<br />';
  99. elseif (strtoupper($_POST['payment_status']) != 'COMPLETED')
  100. $errors .= $paypal->getL('payment').$_POST['payment_status'].'<br />';
  101. if (!isset($_POST['custom']))
  102. $errors .= $paypal->getL('custom').'<br />';
  103. if (!isset($_POST['txn_id']))
  104. $errors .= $paypal->getL('txn_id').'<br />';
  105. if (!isset($_POST['mc_currency']))
  106. $errors .= $paypal->getL('mc_currency').'<br />';
  107. if (empty($errors))
  108. {
  109. $cart = new Cart((int)($cart_secure[0]));
  110. if (!$cart->id)
  111. $errors = $paypal->getL('cart').'<br />';
  112. elseif (Order::getOrderByCartId((int)($cart_secure[0])))
  113. $errors = $paypal->getL('order').'<br />';
  114. else
  115. $paypal->validateOrder((int)$cart_secure[0], _PS_OS_PAYMENT_, (float)($_POST['mc_gross']), $paypal->displayName, $paypal->getL('transaction').$_POST['txn_id'], array('transaction_id' => $_POST['txn_id'], 'payment_status' => $_POST['payment_status']), NULL, false, $cart_secure[1]);
  116. }
  117. }
  118. else
  119. $errors .= $paypal->getL('verified');
  120. if (!empty($errors) AND isset($_POST['custom']))
  121. {
  122. if (strtoupper($_POST['payment_status']) == 'PENDING')
  123. $paypal->validateOrder((int)$cart_secure[0], _PS_OS_PAYPAL_, (float)($_POST['mc_gross']), $paypal->displayName, $paypal->getL('transaction').$_POST['txn_id'].'<br />'.$errors, array('transaction_id' => $_POST['txn_id'], 'payment_status' => $_POST['payment_status']), NULL, false, $cart_secure[1]);
  124. else
  125. $paypal->validateOrder((int)$cart_secure[0], _PS_OS_ERROR_, 0, $paypal->displayName, $errors.'<br />', array(), NULL, false, $cart_secure[1]);
  126. }