[英]Paypal IPN - How does it work?
我需要有关Paypal IPN如何工作的简短说明。 没有任何细节,只是基本内容。
我应该如何设置HTML变量以及如何获取数据以验证来自Paypal的付款? 这就是我所需要知道的,我无法在某处对此进行快速解释。
如果可能的话,也向我展示一些代码行,绝对有帮助。
谢谢。
IPN是一种消息服务, PayPal使用它来发送有关特定事件的通知,例如:
在许多情况下,触发IPN事件的操作是您网站上的用户操作。 但是,其他操作可能会触发IPN。 例如,您网站的后台流程可能会调用可退款的PayPal API,或者客户可能会将有争议的费用通知PayPal。
您使用侦听器(有时称为处理程序)接收和处理IPN消息。 此侦听器基本上是您在服务器上创建的始终处于活动状态的网页或Web应用程序,并具有允许其接受和验证从PayPal发送的IPN消息,然后根据来自以下位置的信息在服务器上调用后端服务的代码: IPN消息。 该Web应用程序等待IPN,然后(通常)将其传递给适当响应的管理过程。 PayPal提供了示例代码,可以对其进行修改以实现用于处理从消息PayPal发送的IPN的侦听器。 有关详细信息,请参见实现IPN侦听器 。
有关详细信息和帮助,请访问: 贝宝即时付款通知指南
希望这可以帮助。
我已经多次使用了等效的C#(和PHP版本看起来非常相似)。
https://www.x.com/developers/PayPal/documentation-tools/code-sample/216623
<?php
//reading raw POST data from input stream. reading pot data from $_POST may cause serialization issues since POST data may contain arrays
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval)
{
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc'))
{
$get_magic_quotes_exits = true;
}
foreach ($myPost as $key => $value)
{
if($get_magic_quotes_exits == true && get_magic_quotes_gpc() == 1)
{
$value = urlencode(stripslashes($value));
}
else
{
$value = urlencode($value);
}
$req .= "&$key=$value";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.paypal.com/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);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.paypal.com'));
// In wamp like environment where the root authority certificate doesn't comes in the bundle, you need
// to download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path
// of the certificate as shown below.
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
$res = curl_exec($ch);
curl_close($ch);
// assign posted variables 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'];
if (strcmp ($res, "VERIFIED") == 0) {
// 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
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
?>
基本上,贝宝会与您联系,您会做出回应; 这使您可以验证是PayPal调用了IPN处理程序,而不是恶意方。 在验证步骤之后,您可以继续处理结果。 如您所知,在付款发生后会进行IPN调用(也可以针对付款生命周期中的其他事件进行配置)。 您可以使用IPN更新系统状态(例如,解锁购买的产品)。
我发现这个PHP类非常有用(且易于使用):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.