Paypal IPN-它是如何工作的


Paypal IPN - How does it work?

我需要一个关于Paypal IPN如何工作的简短解释。没有任何细节,只是基本的东西。

我应该如何设置我的HTML变量,以及我应该如何获得数据来验证Paypal的付款?这就是我需要知道的全部,我找不到关于这一点的快速解释。

如果可能的话,也给我看一些代码行,肯定会有所帮助。

谢谢。

IPNPayPal用于发送有关特定事件的通知的消息服务,例如:

  • 即时支付,包括快速结账、直接信用卡付款和授权(授权的交易付款但尚未收集)
  • eCheck付款和相关状态,如挂起、已完成或被拒绝,以及由于其他原因(例如审查潜在的欺诈行为
  • 经常性付款和订阅操作
  • 与交易

在许多情况下,触发IPN事件的操作是您网站上的用户操作。但是,其他操作可能会触发IPN。例如,您网站的后台流程可能会调用PayPal API来退款,或者客户可能会通知PayPal有争议的费用。

您使用侦听器(有时称为处理程序)接收和处理IPN消息。这个监听器基本上是您在服务器上创建的一个网页或web应用程序,它始终处于活动状态,并具有允许它接受和验证从PayPal发送的IPN消息的代码,然后根据IPN消息中的信息调用服务器上的后端服务。web应用程序等待IPN,并(通常)将它们传递给适当响应的管理进程。PayPal提供了可以修改的示例代码,以实现一个侦听器,该侦听器处理PayPal消息发送的IPN。有关详细信息,请参阅实现IPN侦听器

有关详细信息和帮助,请访问:PayPal即时支付通知指南

希望这能有所帮助。

代码

我已经使用过很多次类似的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会联系您并作出回应;这允许您验证它是PayPal调用您的IPN处理程序,而不是恶意方。在验证步骤之后,您可以继续处理结果。我相信你知道,IPN呼叫是在支付发生后进行的(也可以为支付生命周期中的其他事件进行配置)。您可以使用IPN更新系统状态(例如解锁购买的产品)。

其他东西

  • 我为PayPal使用的最后一个开发URL是https://www.sandbox.paypal.com/cgi-bin/webscr(可能仍然有效)
  • IPN页面/处理程序需要公开,以便PayPal调用
  • 您需要在PayPal开发者UI中配置IPN通知(主要包括向他们提供IPN页面的URL)
  • 您可以将自定义信息与PayPal将发送回IPN处理程序的原始交易一起发送到PayPal。我相信它是在一个被称为"习俗"的领域中通过的

我发现这个PHP类非常有用(而且很容易使用):