За последние 24 часа нас посетили 60328 программистов и 1818 роботов. Сейчас ищут 2344 программиста ...

Проблема с IPN Paypal

Тема в разделе "Прочие вопросы по PHP", создана пользователем ericsoft, 27 апр 2015.

  1. ericsoft

    ericsoft Активный пользователь

    С нами с:
    24 апр 2015
    Сообщения:
    13
    Симпатии:
    1
    Здравствуйте, уважаемые форумчане.
    Прошу вашей помощи разобраться со скриптом IPN Paypal. С песочницей работает прекрасно, но почему-то упрямо отказывается работать с живыми деньгами.
    Скрипт взят с сайта paypal, вот он
    Код (PHP):
    1. <?php
    2. // CONFIG: Enable debug mode. This means we'll log requests into 'ipn.log' in the same directory.
    3. // Especially useful if you encounter network errors or other intermittent problems with IPN (validation).
    4. // Set this to 0 once you go live or don't require logging.
    5. define("DEBUG", 1);
    6. // Set to 0 once you're ready to go live
    7. define("USE_SANDBOX", 1);
    8. define("LOG_FILE", "./ipn.log");
    9. // Read POST data
    10. // reading posted data directly from $_POST causes serialization
    11. // issues with array data in POST. Reading raw POST data from input stream instead.
    12. $raw_post_data = file_get_contents('php://input');
    13. $raw_post_array = explode('&', $raw_post_data);
    14. $myPost = array();
    15. foreach ($raw_post_array as $keyval) {
    16.     $keyval = explode ('=', $keyval);
    17.     if (count($keyval) == 2)
    18.         $myPost[$keyval[0]] = urldecode($keyval[1]);
    19. }
    20. // read the post from PayPal system and add 'cmd'
    21. $req = 'cmd=_notify-validate';
    22. if(function_exists('get_magic_quotes_gpc')) {
    23.     $get_magic_quotes_exists = true;
    24. }
    25. foreach ($myPost as $key => $value) {
    26.     if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
    27.         $value = urlencode(stripslashes($value));
    28.     } else {
    29.         $value = urlencode($value);
    30.     }
    31.     $req .= "&$key=$value";
    32. }
    33. // Post IPN data back to PayPal to validate the IPN data is genuine
    34. // Without this step anyone can fake IPN data
    35. if(USE_SANDBOX == true) {
    36.     $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
    37. } else {
    38.     $paypal_url = "https://www.paypal.com/cgi-bin/webscr";
    39. }
    40. $ch = curl_init($paypal_url);
    41. if ($ch == FALSE) {
    42.     return FALSE;
    43. }
    44. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    45. curl_setopt($ch, CURLOPT_POST, 1);
    46. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    47. curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
    48. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    49. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    50. curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    51. if(DEBUG == true) {
    52.     curl_setopt($ch, CURLOPT_HEADER, 1);
    53.     curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
    54. }
    55. // CONFIG: Optional proxy configuration
    56. //curl_setopt($ch, CURLOPT_PROXY, $proxy);
    57. //curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
    58. // Set TCP timeout to 30 seconds
    59. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    60. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
    61. // CONFIG: Please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path
    62. // of the certificate as shown below. Ensure the file is readable by the webserver.
    63. // This is mandatory for some environments.
    64. //$cert = __DIR__ . "./cacert.pem";
    65. //curl_setopt($ch, CURLOPT_CAINFO, $cert);
    66. $res = curl_exec($ch);
    67. if (curl_errno($ch) != 0) // cURL error
    68.     {
    69.     if(DEBUG == true) {    
    70.         error_log(date('[Y-m-d H:i e] '). "Can't connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
    71.     }
    72.     curl_close($ch);
    73.     exit;
    74. } else {
    75.         // Log the entire HTTP response if debug is switched on.
    76.         if(DEBUG == true) {
    77.             error_log(date('[Y-m-d H:i e] '). "HTTP request of validation request:". curl_getinfo($ch, CURLINFO_HEADER_OUT) ." for IPN payload: $req" . PHP_EOL, 3, LOG_FILE);
    78.             error_log(date('[Y-m-d H:i e] '). "HTTP response of validation request: $res" . PHP_EOL, 3, LOG_FILE);
    79.         }
    80.         curl_close($ch);
    81. }
    82. // Inspect IPN validation result and act accordingly
    83. // Split response headers and payload, a better way for strcmp
    84. $tokens = explode("\r\n\r\n", trim($res));
    85. $res = trim(end($tokens));
    86. //Здесь код подключения к БД
    87. if (strcmp ($res, "VERIFIED") == 0) {
    88.     // check whether the payment_status is Completed
    89.     // check that txn_id has not been previously processed
    90.     // check that receiver_email is your PayPal email
    91.     // check that payment_amount/payment_currency are correct
    92.     // process payment and mark item as paid.
    93.     // assign posted variables to local variables
    94.     $item_name = $_POST['item_name'];
    95.     $item_number = $_POST['item_number'];
    96.     $payment_status = $_POST['payment_status'];
    97.     //$payment_amount = $_POST['mc_gross'];
    98.     //$payment_currency = $_POST['mc_currency'];
    99.     $txn_id = $_POST['txn_id'];
    100.     $subscr_id = $_POST["subscr_id"]; 
    101.     //$receiver_email = $_POST['receiver_email'];
    102.     //$payer_email = $_POST['payer_email'];
    103.     $subscr_date = date("Y-m-d H:i:s");
    104.  
    105. //Здесь вносятся изменения в БД
    106.  
    107.  
    108.     if(DEBUG == true) {
    109.         error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE);
    110.     }
    111. } else if (strcmp ($res, "INVALID") == 0) {
    112.     // log for manual investigation
    113.     // Add business logic here which deals with invalid IPN messages
    114.     if(DEBUG == true) {
    115.         error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
    116.     }
    117. }
    118. ?>
    При работе с песочницей все запросы в БД проходят отлично, а при работе с живыми деньгами в лог пишется следующее
    Код (Text):
    1.  
    2. for IPN payload: cmd=_notify-validate....здесь данные об операции...
    3. <HTML><HEAD>
    4. <TITLE>Access Denied</TITLE>
    5. </HEAD><BODY>
    6. <H1>Access Denied</H1>
    7.  You don't have permission to access "http&#58;&#47;&#47;www&#46;paypal&#46;com&#47;cgi&#45;bin&#47;webscr" on this server.<P>
    8. Reference&#32;&#35;18&#46;98643e17&#46;1429891496&#46;6d65ea
    9. </BODY>
    10. </HTML>
    Права доступа к файлу менял и так и сяк, не могу понять в чем же причина. Заранее спасибо за ответы.

    Добавлено спустя 44 минуты 13 секунд:
    Может проблема в настройках IPN уведомлений в аккаунте? Хотя, в песочнице они тоже не настроены, даже не знаю, всю голову сломал уже...
     
  2. denis01

    denis01 Суперстар
    Команда форума Модератор

    С нами с:
    9 дек 2014
    Сообщения:
    12.227
    Симпатии:
    1.714
    Адрес:
    Молдова, г.Кишинёв