代码改变世界

PayPal接口文档

2010-10-18 22:12  xchit  阅读(3301)  评论(1编辑  收藏  举报

1.到https://developer.paypal.com注册一个开发帐号,好了之后再进入Sandbox建立测试用的Paypal虚拟帐号(至少应该建立一个Business的和一个Personal的),信息可以是假的,注意:这里的至少两个测试帐号是在你所建立的开发帐号里面建立的,不要注册错了;
2.测试是很麻烦,但是是必不可少的,因为如果客户买过一次出错之后,就不会来第二次了,所以花半天时间做测试是很重要的;

ASP.Net/C#

 


using System;
using System.IO;
using System.Text;
using System.Net;
using System.Web;
public partial class csIPNexample :System.Web.UI.Page
{
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")
{
// log for manual investigation
}
else
{
//log response/ipn data for manual investigation
}
}
}
1。买家和卖家必须是paypal(贝宝)的注册用户。
2。从银行卡提取资金到贝宝帐户上,也叫充值。此后网上付费不再跟银行卡有关。

实现网上购物
卖家从贝宝网上获取按钮。

主要参数:
add:一次只能购买单种商品
upload:可以购买多种商品

购物车
<input type="hidden" name="cmd" value="_cart">

卖家的帐户
<input type="hidden" name="business" value="jiangjun_0_1999@163.com">

商品名
<input type="hidden" name="item_name" value="Java">

商品编号
<input type="hidden" name="item_number" value="1001">

商品价格
<input type="hidden" name="amount" value="1.00">

商品数量-针对每一种商品
<input type="hidden" name="quantity" value="2">

货币类型 CNY:人民币,USD:美元
<input type="hidden" name="currency_code" value="CNY">

取消交易并要返回的位置
<input type="hidden" name="cancel_return" value=" http://localhost/wwwShop.php ">

交易完后自动返回的位置
<input type="hidden" name="return" value="http://localhost/wwwShop.php">

 

付款数据传输 (PDT)
返回的参数
atm:交易金额
tx:交易号
st:状态    value=Completed为交易完成标志
cc=CNY:应该是货币类型
cm=
sig=
二次返回
二次返回在程序中执行
cmd=_notify-synch(必须这么写),tx和at组合用于二次返回的参数,
at是身份标记,不是返回的参数,而是需要手动添加。
实例:
Buy.php
<input type="hidden" name="return" value="http://localhost/paypal.php">
paypal.php
<?
$req = 'cmd=_notify-synch';

$tx_token = $_GET['tx'];
$auth_token = "SJIKB64NeDhAFQg_h1H2YjTrW4EoLeDYXx-iWOvQ3uvhZs5zER1aL8EZbvW";
$req .= "&tx=$tx_token&at=$auth_token";

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0 ";
$header .= "User-Agent: PHP/".phpversion()." ";
$header .= "Content-Type: application/x-www-form-urlencoded ";
$header .= "Content-Length: " . strlen($req) . " ";
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
?>
二次返回的参数

成功标志
SUCCESS

first_name=%E8%80%81
last_name=%E7%89%87
address_status=unconfirmed
address_zip=100001
address_street:
address_country=China
address_country_code=CN
address_name=%E7%89%87%E8%80%81
address_city=%E5%8C%97%E4%BA%AC
address_state=%E5%8C%97%E4%BA%AC
contact_phone=01058472654    
payer_email=jinold_2%40163.com


tax=0.00
payer_id=T85NNQLW2BHJQ
payment_date=22%3A35%3A00+Sep+09%2C+2006+PDT
payment_status=Completed
charset=UTF-8

custom=
payer_status=unverified
business=jiangjun_0_1999%40163.com                       商家账号
receiver_email=jiangjun_0_1999%40163.com                  商家帐户中注册的邮件
txn_id=28267733YJ662041W                               唯一贝宝交易号,和tx意义一样
payment_type=instant
receiver_id=8LFB8Q7WADTW4
txn_type=cart
mc_currency=CNY
residence_country=CN
payment_gross=

商品信息
num_cart_items=3                                   商品种类
mc_gross=3.00                                      总额
mc_shipping=0.00
mc_handling=0.00
item_number1=1001                                  商品1的编号
item_number2=1002
item_number3=1003
item_name1=Java                                    商品1 的名称
item_name2=C%2B%2B
item_name3=Oracle
quantity1=1                                         商品1的数量
quantity2=1
quantity3=1
mc_shipping1=0.00
mc_shipping2=0.00
mc_shipping3=0.00
mc_handling1=0.00
mc_handling2=0.00
mc_handling3=0.00
mc_gross_1=1.00                                     商品1的总额
mc_gross_2=1.00
mc_gross_3=1.00

示例代码
付款数据传输
https://www.paypal.com/us/cgi-bin/webscr?cmd=p/xcl/rec/pdt-code

即时付款通知-IPN 示例代码
https://www.paypal.com/cn/cgi-bin/webscr?cmd=p/xcl/rec/ipn-code

即时付款通知工具*
https://www.paypal.com/cn/cgi-bin/webscr?cmd=p/pdn/3p-solutions-ipntools

还有本人写的Paypal_end.php,虽然不怎样。但会有帮助的。因为按照paypal提供的代码,会出现302重定向错误。我没能解决,所以套用了网上搜索的内容
重 复 购 买
关于重复购买
Paypal不提供商家自动扣除买家资金的功能。但是会在定期的发送邮件给用户提醒用户该缴费了。
咨询电话:02128913401

纠正:paypal和贝宝的区别
贝宝是paypal在上海设立的分公司。
在功能上与paypal有一些区别
主要有:贝宝只能支持人民币业务,而paypal支持多国货币(50多种)
贝宝不支持租用和循环支付,而paypal支持。
贝宝提议,如果想要了解更详细的内容,跟paypal取得联系。

循环付款-也就是重复购买(看《租费和循环付款手册.pdf》)
租用:是对某个产品的长期租用,长期租用过程中需要定期交付租费,通过这个功能可以实现重复购买的。其实我们的标签或分类就是租给用户。而且租用时每次只能租用一个,不能像购买物品那样成批的执行,如果要租用几个标签或分类,需要多次租用。

主要参数:
<input type="hidden" name="cmd" value="_xclick-subscriptions">        必须这么写,表示这是租用

<input type="hidden" name="business" value="jinold_1@126.com">
<input type="hidden" name="item_name" value="C#Book">
<input type="hidden" name="item_number" value="20001">

<input type="hidden" name="a1" value="0.01">
<input type="hidden" name="p1" value="1">
<input type="hidden" name="t1" value="D">
<input type="hidden" name="a2" value="0.01">
<input type="hidden" name="p2" value="1">
<input type="hidden" name="t2" value="D">

<input type="hidden" name="a3" value="0.01">
<input type="hidden" name="p3" value="1">
<input type="hidden" name="t3" value="D">
*注:如果不需要试用,就直接用3就可以了,1和2是可选的
<input type="hidden" name="src" value="1">
<input type="hidden" name="sra" value="1">
<input type="hidden" name="srt" value="5">
一次返回值
tx=0HN606257Y398373R&
st=Completed&
amt=1.00&
cc=
cm=&
sig=

二次返回值
SUCCESS
payment_date=00%3A21%3A18+Sep+14%2C+2006+PDT
txn_type=subscr_payment
subscr_id=S-5XR56068625362743                           租用号
last_name=jin
residence_country=C2
item_name=C%23Book
payment_gross=1.00
mc_currency=USD
business=jinold_1%40126.com                               商家帐号
payment_type=instant
payer_status=unverified
payer_email=jinold_2%40126.com                            租用者帐号
txn_id=0HN606257Y398373R                               交易号
receiver_email=jinold_1%40126.com
first_name=old
payer_id=K89UE33TA6M3W
receiver_id=LVKVBHZKL8MFY
contact_phone=%2B86+58472654
item_number=20001
payment_status=Completed
payment_fee=0.33
mc_fee=0.33
mc_gross=1.00
charset=gb2312