我从未进行过订阅贝宝处理,因此不确定我做错了什么。在paypal帐户中正确设置了paypal ipn网址,下面的代码是我拼凑而成的。它在沙盒上完美运行,但沙盒不支持订阅。
我通过购买进行了实际测试,但无法正常工作。付款在Paypal上处理,状态未验证,但是在必须接收IPN的页面上没有任何反应。我将信息转储到文件中进行测试,这样我可以看到发生了什么,但是什么也没发生,没有使用信息创建文件,这必须表示没有发送IPN吗?
任何帮助表示赞赏!
$enableSandbox = false;
$paypalUrl = $enableSandbox ? 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr' : 'https://ipnpb.paypal.com/cgi-bin/webscr';
function verifyTransaction($data) {
global $paypalUrl;
$req = 'cmd=_notify-validate';
foreach ($data as $key => $value) {
$value = urlencode(stripslashes($value));
$value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i', '${1}%0D%0A${3}', $value); // IPN fix
$req .= "&$key=$value";
}
$ch = curl_init($paypalUrl);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
$res = curl_exec($ch);
if (!$res) {
$errno = curl_errno($ch);
$errstr = curl_error($ch);
curl_close($ch);
throw new Exception("cURL error: [$errno] $errstr");
}
$info = curl_getinfo($ch);
// Check the http response
$httpCode = $info['http_code'];
if ($httpCode != 200) {
throw new Exception("PayPal responded with http code $httpCode");
}
curl_close($ch);
//return $res === 'VERIFIED';
return $res;
}
// Handle the PayPal response.
// Assign posted variables to local data array.
$data = [
'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'],
'payer_id' => $_POST['payer_id'],
'custom' => $_POST['custom'],
'txn_type' => $_POST['txn_type'],
'subscr_id' => $_POST['subscr_id'],
'txn_id' => $_POST['txn_id'],
];
if (verifyTransaction($_POST)) {
if($data['receiver_email'] == "PAYPAL ACCOUNT EMAIL OMMITTED"){
if($data['payment_status'] == "Completed" || $data['txn_type'] == "subscr_payment"){
if($data['custom'] != ""){
//code removed
}
}
}
}