Paypal学习 2 -- IPN (Instant Payment Notification)

在上一片文章中,笔者介绍了buy now 按钮的内容,由于buy now按钮将页面直接重定向到了paypal的页面,也就是说已经脱离了我们自己的系统,我们怎样才能知道用户是否付款成功了呢?答案是Paypal为我们提供了IPN的功能,这个功能可以理解为:当用户支付成功之后,paypal可以给某一个url提交一个post请求,来表明某个用户已经完成了一个支付。下面笔者将会详细介绍怎么实现这个功能。


      1)登录到开发者的后台dashbord

            https://developer.paypal.com/webapps/developer/applications/myapps 

      2) IPN功能默认情况下是只对专业用户开启的,如果我们只是破铜的business账户是没有此功能的。我们需要将business账号升级到pro级别。具体步骤:

             2.1  点击sandbox下的accounts

             2.2  选择你自己的business测试账号,点击profile

             2.3  在profile的弹出框下的profile tab中找到account type,后面有一个upgrade to pro 按钮,点击这个按钮就能够完成升级的工作,升级之后应该是这样的:

                     

              

       3) 完成了升级到专业用户的工作,我们就可以配置IPN了,具体步骤如下:

             3.1 使用business测试账号登录到sandbox系统中

             3.2 点击 My Account --> Profile

             3.3 选择 Selling preferences 中的 Instant Payment Notification preferences

                   

             3.4  点击Choose IPN settings 

             3.5  填写Notification URL中的内容,这个url就应该是你自己系统中接受paypal提交的post请求的地址,对于php开发者,这个url可以是一个php的页面,java可以使一个action/ controller

                    需要指出的是,这个url不能是localhost/ 127.0.0.1 下的,需要时公网上面的一个地址,这样paypal才能找得到。

         4)完成了上面的工作,我们就可在每次收到用户的付款后,收到一个从paypal发过来的post请求。

         5)接收这个post请求需要完成一个验证的步骤,具体的验证流程可以参考 https://developer.paypal.com/docs/classic/ipn/gs_IPN/

               怎么具体接收这个post请求参考: https://www.youtube.com/watch?v=t_7GA1BZXPs   

               具体代码如下: 

               Paypal_IPN.php 

<?php
class Paypal_IPN {
	
	/** @var string $_url The paypal url to go to through cURL
	 private $_url;
	
	 /**
	 * @param string $mode 'live' or 'sandbox'
	 */
	public function __construct($mode = 'live')
	{
		if ($mode == 'live')
			$this->_url = 'https://www.paypal.com/cgi-bin/webscr';
	
		else
			$this->_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
	}
	
	public function run()
	{
		$postFields = 'cmd=_notify-validate';
	
		foreach($_POST as $key => $value)
		{
			$postFields .= "&$key=".urlencode($value);
		}
	
		$ch = curl_init();
	
		curl_setopt_array($ch, array(
			CURLOPT_URL => $this->_url,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_SSL_VERIFYPEER => false,
			CURLOPT_POST => true,
			CURLOPT_POSTFIELDS => $postFields
		));
	
		// the result will be either "VERIFIED" or "INVALID"
		$result = curl_exec($ch);
		curl_close($ch);
	
		$fh = fopen('result.txt', 'w');
		fwrite($fh, $result . ' -- ' . $postFields);
		fclose($fh);
	
		echo $result;
	}
	
	
	
}

?>
       

               TestIPN.php

<?php
require 'Paypal_IPN.php';
$paypal = new Paypal_IPN('sandbox');
$paypal->run();
?>

        6) 这里的TestIPN.php是用来接受post请求的类。

  

                             



 

    






            

             

       

            

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PP_IPN_PDT_Guide_CHN.pdf PaypalIPN和PDT变量对照表(中文版) Instant Payment Notification (即时付款通知) & Payment Data Transfer (付款数据传输) 说明 目录 1 付款数据传输 PDT.....................................................................................................................3 1.1 什么是PDT........................................................................................................................3 1.2 如何启用PDT...................................................................................................................3 1.3 PDT 示例代码....................................................................................................................6 1.3.1 PDT for PHP 示例代码...........................................................................................6 1.3.2 PDT for ASP 示例代码............................................................................................7 2 即时付款通知 IPN ....................................................................................................................10 2.1 什么是即时付款通知 IPN .............................................................................................10 2.2 如何启用IPN? ..............................................................................................................11 2.3 通知确认 - 给PayPal 的HTTPS 回发.........................................................................12 2.4 处理IPN 的示例代码.....................................................................................................13 2.4.1 IPN for JAVA 示例代码........................................................................................13 2.4.2 IPN for Perl 示例代码..........................................................................................14 2.4.3 IPN for PHP 示例代码.........................................................................................16 2.4.4 IPN for ASP/VBScript 示例代码.........................................................................17 2.4.5 IPN for .NET/VB 示例代码.................................................................................18 2.4.6 IPN for .NET/C# 示例代码..................................................................................19 3 集成PayPal 建议........................................................................................................................21 4 附录5:IPN 和PDT 变量列表.................................................................................................21

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值