1

我已经设置了 Paypal 帐户通知 URL 以转到此脚本:

    // Read the notification from PayPal which comes in the form of a POST array and create the acknowledgement response
    $req = 'cmd=_notify-validate';               // add 'cmd' to beginning of the acknowledgement you send back to PayPal

    foreach ($_POST as $key => $value) 
    { // Loop through the notification NV pairs
    $value = urlencode(stripslashes($value));  // Encode the values
    $req .= "&$key=$value";                    // Add the NV pairs to the acknowledgement
    }


    // Assign the paypal payment notification values to local variables
    if($_POST){
    $item_name = $_POST['item_name'];
    $item_number = $_POST['item_number'];
    $payment_status = $_POST['payment_status'];
    $payment_amount = $_POST['mc_gross'];
    $payment_currency = $_POST['mc_currency'];
    $txn_id = $_POST['txn_id'];
    $receiver_email = $_POST['receiver_email'];
    $payer_email = $_POST['payer_email'];}

    //Set up the acknowledgement request headers (this is the updated version for http 1.1)
    $header .= "POST /cgi-bin/webscr HTTP/1.1\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Host: www.paypal.com\r\n";
    $header .= "Connection: close\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
    //Open a socket for the acknowledgement request
    $fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);

    if(!$fp){
        echo "HTTP ERROR";
    }
    else
    {//start 1

    // Post request back to PayPal for validation
    fputs ($fp, $header . $req);

    //once paypal receives the acknowledgement response, another message will be send containing the single word VERIFIED or INVALID

    while (!feof($fp)) 
        { //start 2, while not EndOfFile
    $res = fgets ($fp, 1024); // Get the acknowledgement response
    $res = trim($res);
        if (strcmp ($res, "VERIFIED") == 0) 
            {// start 3, Response is OK

            if ($payment_status == "Completed")
                {//start 4

                //send email announcing success

                $from = "Rupert Heath Literary Agency";
                $to = $payer_email;
                $subject = "Ebook";
                $body = "It works";
                mail($to, $subject, $body, $from);

                }//end 4
            }//end 3
            else if(strcmp ($res, "INVALID") == 0)
                {//start 5

                //send email announcing failure
                //$error_log .= 'Line 57'

                $from = "Guide Test Page";
                $to = $payer_email;
                $subject = "INVALID IPN";
                $body = "Doesn't work";
                mail($to, $subject, $body, $from);
                }//end 5


    } //end 2
fclose ($fp);  //close file pointer
} //end 1

它基于许多 Web 托管的示例,适合升级到 HTTP 1.1

该脚本作为测试,根据来自 Paypal 的 VERIFIED 或 INVALID 响应发送成功或失败电子邮件。问题是我总是收到一封无效的电子邮件并且不明白为什么。我查看了 Paypal IPN 历史记录,HTTP 响应代码为 200,这似乎表明 IPN 交换工作正常,因此 Paypal 可能正在响应 VERIFIED 但我的脚本有错误。

IPN 历史详细信息如下:

即时付款通知 (IPN) 详细信息

消息 ID69025489S2598613V

创建日期/时间 18/07/2013 23:22 PDT

原件/重新发送原件

最迟交付尝试日期/时间 18/07/2013 23:22 PDT

通知 URL http://www.rupertheath.com/ipn/ipn_script

HTTP 响应代码 200

发送状态 已发送

重试次数 0

交易 ID4D0877596N038120Y

IPN 类型进行的交易

IPN Message mc_gross=0.01&protection_eligibility=Eligible&address_status=confirmed&payer_id=C3USV8A4Q2QDW&tax=0.00&address_street=Ramsey House 34 Fowlers Road&payment_date=23:22:44 Jul 18, 2013 PDT&payment_status=Completed&charset=windows-1252&address_zip=SP1 2QU&first_name=Michael&mc_fee=0.01&address_country_code=GB&address_name=Michael Heath¬ify_version=3.7&custom=&payer_status=verified&business=emailagency@rupertheath.com&address_country=United Kingdom&address_city=Salisbury&quantity=1&verify_sign=AhKyCHsfiy2frgZNNoQmGHQ3LhKMAboweJqZzYCdqp30Hb7b99tF.04a&payer_email=msheath@btinternet.com&txn_id=4D0877596N038120Y&payment_type=instant&last_name=Heath&address_state=Wiltshire&receiver_email=emailagency@rupertheath.com&payment_fee=&receiver_id=BRM2TYMP4ACZ8&txn_type=web_accept&item_name=Ebook&mc_currency=GBP&item_number=&residence_country=GB&handling_amount=0.00&transaction_subject=Ebook&payment_gross=&shipping=0.00&ipn_track_id=b0a3b4ae3c51c

谁能帮我调试这个问题?

4

2 回答 2

1

最好使用原始发布的数据,而不是自己重建。这是我个人使用的代码,到目前为止运行良好:

file_get_contents(
    'https://www.paypal.com/cgi-bin/webscr?cmd=_notify-validate',
    false,
    stream_context_create(array(
        'http'=>array(
            'method'=>'POST',
            'header' => 'Content-Type: application/x-www-form-urlencoded',
            'content' => file_get_contents('php://input'),
        )
    ))
);

它也可以很容易地变成 cURL。

于 2013-07-19T07:13:38.057 回答
0

这条线很可能是一个问题:

$value = urlencode(stripslashes($value));

具体来说,stripslashes在那里使用。stripslashes如果你的 PHP 配置有(上帝帮助你)Magic Quotes on,你只需要包括。值得庆幸的是,从 PHP 5.4 起,Magic Quotes 已被删除,因此您可以安全地stripslashes从该行中删除,使其成为:

$value = urlencode($value);

信不信由你,Paypal 允许用户数据包含反斜杠。因此,如果 IPN 有任何反斜杠,并且您使用 删除它们stripslashes,那么当您将 IPN 数据发回 PayPal 进行验证时,Paypal 会给您 INVALID 响应,因为数据将不匹配。

于 2015-03-03T04:52:37.957 回答