我试图实现Paypal快速结账,但发现了一个问题。我不知道如何正确地实现IPN。这是我的表单代码:
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<!-- Identify your business so that you can collect the payments. -->
<input type="hidden" name="business" value="myemail@mydomain.com">
<!-- Specify a Buy Now button. -->
<input type="hidden" name="cmd" value="_xclick" />
<!-- Specify details about the item that buyers will purchase. -->
<input type="hidden" name="item_name" value="Payment order" />
<input type="hidden" name="amount" value=7 />
<input type="hidden" name="currency_code" value="EUR" />
<input type="hidden" name="notify_url" value="http://mysite/checkout/paypal_ipn" />
<!-- Display the payment button. -->
<input type="image" name="submit" border="0" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif" alt="PayPal - The safer, easier way to pay online">
<img alt="" border="0" width="1" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >
</form>
当我使用CodeIgniter时,我在控制器中创建了一个函数来处理来自Paypal的响应。这是代码:
function paypal_ipn() {
header('HTTP/1.1 200 OK');
// Assign payment notification values to local variables
$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'];
// Build the required acknowledgement message out of the notification just received
$req = 'cmd=_notify-validate'; // Add 'cmd=_notify-validate' to beginning of the acknowledgement
foreach ($_POST as $key => $value) { // Loop through the notification NV pairs
$value = urlencode(stripslashes($value)); // Encode these values
$req .= "&$key=$value"; // Add the NV pairs to the acknowledgement
}
// Set up the acknowledgement request headers
$header = "POST /cgi-bin/webscr HTTP/1.1rn"; // HTTP POST request
$header .= "Content-Type: application/x-www-form-urlencodedrn";
$header .= "Content-Length: " . strlen($req) . "rnrn";
// Open a socket for the acknowledgement request
$fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
// Send the HTTP POST request back to PayPal for validation
fputs($fp, $header . $req);
while (!feof($fp)) { // While not EOF
$res = fgets($fp, 1024); // Get the acknowledgement response
if (strcmp($res, "VERIFIED") == 0) { // Response contains VERIFIED - process notification
echo "VERIVIED";
} else if (strcmp($res, "INVALID") == 0) { //Response contains INVALID - reject notification
// Authentication protocol is complete - begin error handling
echo "INVALID";
}
}
fclose($fp); // Close the file
}
我预计买家在Paypal中完成付款后,它将被重定向到我在表格中声明的notify_url,如果它已验证或无效,我会收到消息。但事实上,它留在了贝宝的"谢谢"页面。
我对代码做了什么错误,所以我无法收到通知。
IPN不是这样工作的,IPN是异步的,与用户无关。
客户流量:
您的网站的购物车->Paypal支付->Paypal感谢您或重定向到您的订单完成
IPN流量:
等待交易->将数据张贴到您的url->您的网站验证数据->在DB 中插入/更新订单
@Abaij-在您的IPN使用者代码中添加一个记录器,该记录器将状态消息写入本地文件。确保它是IPN使用者代码中的第一行代码,这样在出现异常时就不会错过它。确保您的站点对记录器打算写入的驱动器\文件夹具有写入权限。使用IPN模拟器也发送一个测试IPN也你的网站,看看是否写了日志。如果你得到一个日志,那么你就可以确认消费者受到了打击。如果是这样的话,那么你也需要进一步调查你是否也验证了PayPal是否按预期工作。如果您没有得到日志文件,请检查您的站点端口80是否从外部可见。
Br