cURL Paypal IPN 脚本不工作

Posted

技术标签:

【中文标题】cURL Paypal IPN 脚本不工作【英文标题】:cURL Paypal IPN script not working 【发布时间】:2012-12-15 20:19:24 【问题描述】:

我一直试图找出我的 ipn 脚本出了什么问题。我尝试了许多不同的方法,我也尝试了 fsockopen 但我没有让它工作。现在我正在使用 cURL,但它仍然无法正常工作。我已经安装了 cURL,它应该可以正常工作。

我的脚本(只是一个简单的例子,我删除了一些检查只是为了测试):

<?php
$ACTIVE_CONNECTION = mysql_connect('127.0.0.1', 'mysqluser', 'mysqlpassword') or die("Could not connect to server.");
mysql_select_db('mysqldb', $ACTIVE_CONNECTION) or die("Could not connect to database.");

$tid = $_GET['tx'];
$auth_token = "aqiopfsawdaisytrgkl";

$url = "https://www.sandbox.paypal.com/cgi-bin/webscr";

$post_vars = "cmd=_notify-synch&tx=" . $tid . "&at=" . $auth_token;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_vars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'cURL/PHP');

$fetched = curl_exec($ch);



$lines = explode("\n", $fetched);
$keyarray = array();
if (strcmp ($lines[0], "SUCCESS") == 0) 
for ($i=1; $i<count($lines);$i++)
list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = urldecode($val);

// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process payment
$payment_amount = $keyarray['mc_gross'];
$payment_status = $keyarray['payment_status'];
$payment_currency = $keyarray['mc_currency'];
$txn_id = $keyarray['txn_id'];
$receiver_email = $keyarray['receiver_email'];
$payer_email = $keyarray['payer_email'];
$username = mysql_real_escape_string($keyarray['custom']);

    if ($payment_status == 'Completed')
            mysql_query("UPDATE `users` SET vip = '1' WHERE `username` = '".$username."'");
        

else if (strcmp ($lines[0], "FAIL") == 0) 
// manual investigation here

?>

【问题讨论】:

需要更多信息而不是“它仍然无法正常工作”。到底出了什么问题?您是否收到来自 PayPal 的无效回复?你的脚本被击中了吗? 同意@Andrew Angell。 “它仍然不起作用”不足以继续下去。当你回显$fetched 时会发生什么?如果什么都没有打印,是$fetched FALSE?如果是这样,curl_error($ch) 告诉你什么? 我试过了,$fetched 打印了“FAIL”。 【参考方案1】:

试试这个来测试 Sanbox IPN

https://developer.paypal.com/cgi-bin/devscr?cmd=_ipn-link-session

替换

if (strcmp ($lines[0], "SUCCESS") == 0) 

if (strcmp ($lines[0], "SUCCESS") == 0 || 1 == 1) 

【讨论】:

如果您使用实际沙箱进行测试,则不必使用 1==1。仅当您从自己的表单帖子进行测试并且希望它为测试目的进行验证时,才需要这样做。但是,如果数据来自 PayPal 沙箱,则可以正常验证。 是的,你是对的,这种方法在你使用“外部”工具“Sandbox IPN”时很有用,请查看我发送的链接以了解我的意思。

以上是关于cURL Paypal IPN 脚本不工作的主要内容,如果未能解决你的问题,请参考以下文章

PayPal IPN 模拟器测试产生一个空的 curl 结果。在现场工作正常

PayPal:IPN 和 PDT 脚本同时工作

Curl / php 来自 PayPal IPN url 的空响应

从 cURL 支付向 paypal IPN 添加自定义变量

用于 paypal ipn 的 curl 或 fsockopen

PayPal IPN 已停止工作