PayPal(贝宝)支付接口、文档、IPN

   PayPal是国外流行的一个在线支付,就像我们国内的支付宝、快钱、财富通等。

A、PayPal国际版与PayPal国内版

   在PayPal 进军中国之前,它只有一个全世界通用的版本,可称之为国际版;但 PayPal 为了进军国内电子支付市场,推出的一个具有中国特色的版本,称之为贝宝,也可称之为国内版。

   国内版贝宝与国际版 PayPal 相比,主要区别如下:国内版可以绑定银行借记卡(即普通银行卡);国内版使用人民币进行交易;国内版只能在国内使用,在世界上可以使用 PayPal 国际版的另外190多个和国家,国内版贝宝均无法使用。国内版与国际版公用数据库,也就是说,如果你的 email 帐号已经申请了国内版贝宝,也就无法再用来申请国际版 PayPal 。
国际的只支付 美元 收付 ! 国内的支持 人民币 收付。

B、网站付款标准版和网站付款专业版的区别
   标准版主要包括以下几种基础产品:
   • 立即购买按钮(Buy Now Buttons):适用于购买单件物品;
   • PayPal 购物车(PayPal Shopping Cart):适用于多件物品的付款; 
   • 上传购物车(Cart Upload ):适用于提交第三方购物车中的商品; 
   • 捐赠按钮(Donation Buttons):适用于慈善事业或者接受捐赠; 
   • 订阅按钮(Subscription Buttons):适用于周期性付款;

更多内容参考:http://paypal.ebay.cn/integrationcenter/list__center_2.html

这里我们具体介绍 只向PayPal提交购物车总金额

<form action="https://www.paypal.com/cgi-bin/webscr" method="post"> 
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="PayPal账户中的email地址">
<input type="hidden" name="item_name" value="商品描述">
<input type="hidden" name="item_number" value="商品编号">
<input type="hidden" name="currency_code" value="货币单位,如USD,EUR等">
<input type="hidden" name="amount" value="商品金额或订单总价">
<input type="hidden" name="notify_url" value="交易后paypal返回网站地址" />
<input type="hidden" name="cancel_return" value="客户取消交易后返回地址" />
<input type="hidden" name="return" value="客户交易返回地址" />
<input type="submit" value="PayPal">
</form>

 cmd:_xclick表示该按钮为“立即购买”或“捐赠”按钮。

    _xclick_subscription表示该按钮为“订阅”按钮。

       _cart表示为“购物车”相关按钮。

   s_x-click表示该按钮为加密按钮。

notify_url:IPN通知地址


其他参数设置参考:http://paypal.ebay.cn/integrationcenter/list__resource_6_1.html

C、 即时付款通知IPN 

什么是即时付款通知IPN ?
当您收到新的付款交易或者已发生的付款交易的状态发生变化时,PayPal都将异步(即不作为网站付款流程的一部分) 发送付款详细数据到您所指定的URL,以便您了解买家付款的具体情况并做出相应的响应。这个过程我们称作即时付款通知(简称 IPN)。示意图如下:

贝宝付款通知IPN流程图

•  客户点击“付款”按钮向您的账户付款; 
•  PayPal接受到客户的付款后,向您的服务器指定的URL通过POST方式发送IPN;
•  在您的服务器收到IPN之后,您必须将收到的POST信息对原样返回给PayPal进行验证,PayPal通过此方法帮您防范欺骗或“中间人”攻击;(对IPN信息的验证过程我们称之为通知确认); 
•  PayPal返回验证信息,通过验证为VERIFIED,不通过则为INVALD; 
•  根据验证信息处理付款明细;
 然后在IPN地址里处理就可以了,这里用以C#为例:

protected void Page_Load(object sender, EventArgs e)
{
//Post back to either sandbox or live
string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
string strLive = "https://www.paypal.com/cgi-bin/webscr";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);

// Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;

//for proxy
//WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
//req.Proxy = proxy;

//Send the request to PayPal and get the response
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();

if (strResponse == "VERIFIED")
{

// 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 (strResponse == "INVALID")
{
//do somethings
}
else
{
//log response/ipn data for manual investigation
}

}

D、调试问题

  调试是个头疼的问题,因为你可能要支付很多次才能调试成功,那将浪费你很多美元,HOHO,因为要手续费滴。。。paypal提供了测试接口。
  请去https://developer.paypal.com注册个会员, 然后在这个会员里,可以注册买家和卖家。然后登录买家和卖家帐户(在买家卖家管理界面上,有登录按钮)。这个虚拟的买家和卖家,登录后的操作,就像操作真 实的paypal帐户一样了。然后可以在买家和卖家帐号里充值,要多少充多少,点充值明细的那个过程按钮,系统自动马上帮你充值成功。你就有用不完的钱可 以用来测试接口了。
把接口及代码中所有“business”设置为你注册的虚拟卖家帐号。https://www.paypal.com/cgi-bin/webscr均换为https://www.sandbox.paypal.com/cgi-bin/webscr,就可以了。测试吧,接口可以正常使用以后,把他们再换回正式的帐号和地址就完工了。




posted @ 2011-12-01 13:24  charje  阅读(25794)  评论(9编辑  收藏  举报