隐藏

PayPal IPN接口集成经验分享

发布:2016/12/20 0:43:23作者:管理员 来源:本站 浏览次数:1142

如果你的系统需要得知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>  
  17. 解释一下几个重要的参数:
     
    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
  18. 解释一下几个重要的参数:
     
    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的通知消息