PayPal IPN接口集成经验分享

如果你的系统需要得知PayPal支付的最终结果(成功、失败),那么PayPal的IPN接口是最简单的解决方案。

 
IPN通知示意图
 
 
下面分享一下我的经验:
 
1)如何申请PayPal IPN接口?
 
你只需要拥有一个普通的PayPal账户即可,IPN接口不需要单独申请开通。
 
 
2)准备工作:申请一个沙盒账户。
 
在系统开发阶段,我们不需要用真实的PayPal账户来测试。
到https://developer.paypal.com注册一个开发人员账户(免费)。
登录系统,添加一个虚拟卖家(例如,seller_1316102076_biz@qq.com)和一个买家(例如,buyer_1316102290_per@qq.com)。
 
3)制作一个订单表单。
 
如下:
  1. <form action="https://www.sandbox.paypal.com/cgi-bin/websc" method="post">  
  2.   <input type="hidden" name="business" value="seller_1316102076_biz@qq.com">  
  3.   <input type="hidden" name="item_name" value="海底捞优惠券">  
  4.   <input type="hidden" name="amount" value="30">  
  5.   <input type="hidden" name="no_note" value="1">  
  6.   <input type="hidden" name="return"  value="http://adsl.redicecn.com/order.php?status=ok">  
  7.   <input type="hidden" name="cancel_return" value="http://adsl.redicecn.com/">  
  8.   <input type="hidden" name="custom" value="1">  
  9.   <input type="hidden" name="notify_url" value="http://adsl.redicecn.com/ipn.php">  
  10.     
  11.   <input type="hidden" name="cmd" value="_xclick">  
  12.   <input type="hidden" name="currency_code" value="CAD">  
  13.   <input type="hidden" name="charset" value="utf-8" />  
  14.   <input type="hidden" name="rm" value="1" />  
  15.  <input type="submit" value="支付" />  
  16.   </form>  
 
解释一下几个重要的参数:
 
business = 收款者的PayPal账户
cmd = 立即购买按钮(_xclick),PayPal 购物车(_cart)
return = 支付成功后跳转地址
cancel_return = 取消支付后跳转地址
notify_url = 接收通知的接口(在第4部中我们实现该接口)
rm = PayPal跳转到return和cancel_return时的方式 (1=get, 2=post)
currency_code = 货币单位(美元-USD,加币-CAD)
 
item_name = 商品的描述信息
amount = 商品的总额
custom = 自定义值(可以存放订单编号,PayPal的通知消息中将包含该参数)。
charset = 指定参数采用的字符编码(设置不正确在PayPal账单上将显示乱码,特别是商品描述。)
 
关于这些参数的更详细说明请参考这里: http://paypal.ebay.cn/integrationcenter/list__center_2.html 。
另外需要注意的是,用沙盒账户测试时表单提交的地址用https://www.sandbox.paypal.com/cgi-bin/websc,正式使用时用https://www.paypal.com/cgi-bin/webscr。
 
 
4)写一个通知接口,用于接收PayPal返回的消息(判断支付状态)。
 
下面是一个PHP的例子:
ipn.php
  1. <?  
  2.   
  3. // read the post from PayPal system and add 'cmd'  
  4. $req = 'cmd=_notify-validate';  
  5.   
  6. foreach ($_POST as $key => $value) {  
  7. $value = urlencode(stripslashes($value));  
  8. $req .= "&$key=$value";  
  9. }  
  10.   
  11. // post back to PayPal system to validate  
  12. $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";  
  13. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";  
  14. $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";  
  15.   
  16. $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno$errstr, 30); // 沙盒用  
  17. //$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); // 正式用  
  18.   
  19. // assign posted variables to local variables  
  20. $item_name = $_POST['item_name'];  
  21. $item_number = $_POST['item_number'];  
  22. $payment_status = $_POST['payment_status'];  
  23. $payment_amount = $_POST['mc_gross'];  
  24. $payment_currency = $_POST['mc_currency'];  
  25. $txn_id = $_POST['txn_id'];  
  26. $receiver_email = $_POST['receiver_email'];  
  27. $payer_email = $_POST['payer_email'];  
  28. $mc_gross = $_POST['mc_gross ']; // 付款金额  
  29. $custom = $_POST['custom ']; // 得到订单号  
  30.   
  31. if (!$fp) {  
  32. // HTTP ERROR  
  33. else {  
  34. fputs ($fp$header . $req);  
  35. while (!feof($fp)) {  
  36. $res = fgets ($fp, 1024);  
  37. if (strcmp ($res"VERIFIED") == 0) {  
  38. // check the payment_status is Completed  
  39. // check that txn_id has not been previously processed  
  40. // check that receiver_email is your Primary PayPal email  
  41. // check that payment_amount/payment_currency are correct  
  42. // process payment  
  43. // 验证通过。付款成功了,在这里进行逻辑处理(修改订单状态,邮件提醒,自动发货等)  
  44. }  
  45. else if (strcmp ($res"INVALID") == 0) {  
  46. // log for manual investigation  
  47. // 验证失败,可以不处理。  
  48. }  
  49. }  
  50. fclose ($fp);  
  51. }  
  52. ?>  
 
5)安全性的考虑:
 
A)如何防止客户修改购物表单里面的参数(特别是amount)。
在ipn.php中,判断付款金额$mc_gross是否与订单所需支付金额一致(根据订单号查数据库)。
 
B)有没有可能客户通过直接给ipn.php提交数据,绕过支付?
不可能。在ipn.php的一开始就对接收到的数据进行了校验(将接收到的数据原样发给PayPal验证,只有当PayPal检查认为是它自己发出的数据时才会返回"VERIFIED",否则返回"INVALID")。
 
给出几个截图,展示整个支付过程:
 
 第一步:
 
 第二步:
 
第三步:
 
第四步:
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值