首页 > 解决方案 > PayPal 自动返回脚本总是被调用两次

问题描述

我目前正在使用 PayPal 进行付款。直到最近,一切都很顺利。我有一个订阅按钮。我已经设置了返回 URL 来处理从交易中获取数据然后将其邮寄给用户。我需要的所有数据都已成功检索,但是,问题是我的函数在返回时运行了两次。我收到了我应该收到两次的电子邮件。几周前这工作正常,所以现在我想知道发生了什么,为什么它突然出现了这个错误。

我在这里读过:Paypal 自适应支付返回 url 调用了两次,这可能是因为我点击了“单击此处”按钮,即使它还没有 10 秒。我已经尝试单击而不是单击它,但我仍然得到相同的结果。

我还读到这可能是因为它在付款时被调用,然后在订阅开始时被调用。知道我该如何处理吗?

$pp_hostname = "www.sandbox.paypal.com"; // Change to www.sandbox.paypal.com to test against sandbox
$req = 'cmd=_notify-synch';

$tx_token = $_GET['tx'];
$auth_token = "my_token_from_paypal";
$req .= "&tx=$tx_token&at=$auth_token";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://$pp_hostname/cgi-bin/webscr");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
//set cacert.pem verisign certificate path in curl using 'CURLOPT_CAINFO' field here,
//if your server does not bundled with default verisign certificates.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: $pp_hostname"));
$res = curl_exec($ch);
curl_close($ch);
if(!$res){
    //HTTP ERROR
}else{
     // parse the data
    $lines = explode("\n", trim($res));
    $keyarray = array();
    if (strcmp ($lines[0], "SUCCESS") == 0) 
    {
        for ($i = 1; $i < count($lines); $i++) {
            $temp = explode("=", $lines[$i],2);
            $keyarray[urldecode($temp[0])] = urldecode($temp[1]);
         }

         // code for mail handling
    }
    else if (strcmp ($lines[0], "FAIL") == 0) {
        echo 'there has been an error';
    }
}

标签: phppaypalpaypal-sandbox

解决方案


推荐阅读