Paypal IPN - 检查付款状态是不是已完成?
Posted
技术标签:
【中文标题】Paypal IPN - 检查付款状态是不是已完成?【英文标题】:Paypal IPN - Check if payment status is completed?Paypal IPN - 检查付款状态是否已完成? 【发布时间】:2014-01-18 10:17:18 【问题描述】:我有一个电子书销售网站,用户可以在购买后下载一本书。我从 Paypal 收到 IPN,据我了解,如果用户选择信用卡作为付款方式,有时付款状态会显示为“进行中”或“处理中”。由于 Paypal 会在付款完成后重新发送 IPN,为了避免在您的数据库中重复,检查付款状态是否完成的最佳方法是什么?
【问题讨论】:
我已经通过保存交易ID并检查它来完成它.. 【参考方案1】:这些是 Paypal 返回给您的 IPN 侦听器的可能状态
Canceled_Reversal
Completed
Denied
Failed
Refunded
Reversed
Voided
In-Progress
Pending
Processed
您应该检查Completed
状态以确保付款已完成。下面是检查不同状态的IPN Listener
示例
<?php
//Build the data to post back to Paypal
$postback = 'cmd=_notify-validate';
// go through each of the posted vars and add them to the postback variable
foreach ($_POST as $key => $value)
$value = urlencode(stripslashes($value));
$postback .= "&$key=$value";
// build the header string to post back to PayPal system to validate
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($postback) . "\r\n\r\n";
// Send to paypal or the sandbox depending on whether you're live or developing
// comment out one of the following lines
//$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);//open the connection
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
// or use port 443 for an SSL connection
//$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
if (!$fp)
// HTTP ERROR Failed to connect
//error handling or email here
else // if we've connected OK
fputs ($fp, $header . $postback);//post the data back
while (!feof($fp))
$response = fgets ($fp, 1024);
if (strcmp ($response, "VERIFIED") == 0) //It's verified
// assign posted variables to local variables, apply urldecode to them all at this point as well, makes things simpler later
$payment_status = $_POST['payment_status'];//read the payment details and the account holder
if($payment_status == 'Completed')
//Do something
else if($payment_status == 'Denied' || $payment_status == 'Failed' || $payment_status == 'Refunded' || $payment_status == 'Reversed' || $payment_status == 'Voided')
//Do something
else if($payment_status == 'In-Progress' || $payment_status == 'Pending' || $payment_status == 'Processed')
//Do something
else if($payment_status == 'Canceled_Reversal')
//Do something
else if (strcmp ($response, "INVALID") == 0)
//the Paypal response is INVALID, not VERIFIED
//end of while
fclose ($fp);
?>
【讨论】:
是的,我知道如何成为听众。我想问的是如何处理这种情况:'else if($payment_status == 'In-Progress' || $payment_status == 'Pending' || $payment_status == 'Processed')'?问题是 - 我会将其存储在数据库中,稍后付款完成时 - 贝宝将重新发送 IPN,然后“if($payment_status == 'Completed')”将触发。那么如何检查欺诈者是否没有使用相同的交易 ID 重新提供此请求?因为交易 id 将保持不变。 谢谢@KhawerZeshan!此信息也可以在 PayPal 官方文档中找到,详细信息:developer.paypal.com/docs/classic/ipn/integration-guide/…(支付信息变量)以上是关于Paypal IPN - 检查付款状态是不是已完成?的主要内容,如果未能解决你的问题,请参考以下文章
Paypal IPN payment_status 说已完成但付款被拒绝