net平台c#语言如何实现支付宝payto接口
2014-10-11 21:08
447 查看
软件现在这种支付方式比较多象网银在线等使用的方法都是url验证,就是通过url参数和一个这些url参数的md5编码来确认这个连接的正确性,支付宝在
你购买成功后跳转自定义连接的时候会传2次过来,第一次是数据底层请求,第二次是web请求,而只有第一次有验证码,这个只能通过记录下来才看的到,因为
两次请求间隔很小,如果光显示的话最后的结果是被第二次覆盖了的。所以在接收的时候就要设定接收条件,一种是没有notify_type参数的,一种是有
的。
代码如下:
1
t1=ConfigurationSettings.AppSettings["interface"];//支付接口,就是给的一个连接地址
2
t2=ConfigurationSettings.AppSettings["account"];//支付宝帐户你的帐户
3
t3=ConfigurationSettings.AppSettings["password"];//安全校验码,设置的商家验证码
4
t4="images/logo_zfbsmall.gif";//按钮图片地址
5
t5="test";//悬停说明
6
cmd="0001";//默认
7
subject="item";//商品名称
8
body="decrip";//描述
9
order_no=;//定单号,用户自己生成,方便自己管理 prices=100;//价格0.01~50000.00
10
rurl="http://www.xxx.com/";//商品展示网址
11
types="1";//1:商品购买2:服务购买3:网络拍卖4:捐赠
12
number="1";//购买数量
13
transport="3";//1:平邮2:快递3:虚拟物品
14
ordinary_fee="";//平邮运费
15
express_fee="";//快递运费
16
readonlys="true";//交易信息是否只读
17
buyer_msg="";//买家给卖家的留言
18
buyer="";//买家Email
19
buyer_name="";//买家姓名
20
buyer_address="";//买家地址
21
buyer_zipcode="";//买家邮编
22
buyer_tel="";//买家电话号码
23
buyer_mobile="";//买家手机号码
24
partner=ConfigurationSettings.AppSettings["partenid"];//合作伙伴ID,这个是固定的上面就是要提供得基本信息,然后就是生成支付宝得连接,也就是给支付宝提供一条带验证的购买信息。
1
public string creatAlipayItemURL(string t1,string t2,string t3,string t4,string t5,string cmd,string subject,string body,string order_no,string prices,string rurl,string types,string number,string transport,string ordinary_fee,string express_fee,string readonlys,string buyer_msg,string buyer,string buyer_name,string buyer_address,string buyer_zipcode,string buyer_tel,string buyer_mobile,string partner)
2
{
3
string itemURL,str2CreateAc,acCode;
4
string INTERFACE_URL,sellerEmail,keyCode,imgsrc,imgtitle,AlipayItemURL;
5
//初始化各必要变量
6
INTERFACE_URL=t1+t2;//支付接口
7
sellerEmail=t2;//商户支付宝账户(改成你自己的)
8
keyCode=t3;//安全校验码(改成你自己的)
9
imgsrc=t4;//支付宝按钮图片
10
imgtitle=t5;//按钮悬停说明
11
12
str2CreateAc="cmd" + cmd + "subject" + subject;
13
str2CreateAc=str2CreateAc + "body" + body;
14
str2CreateAc=str2CreateAc + "order_no" + order_no;
15
str2CreateAc=str2CreateAc + "price" + prices;
16
//str2CreateAc=str2CreateAc + "url" + rurl;
17
str2CreateAc=str2CreateAc + "type" + types;
18
str2CreateAc=str2CreateAc + "number" + number;
19
str2CreateAc=str2CreateAc + "transport" + transport;
20
/*str2CreateAc=str2CreateAc + "ordinary_fee" + ordinary_fee;
21
str2CreateAc=str2CreateAc + "express_fee" + express_fee;
22
str2CreateAc=str2CreateAc + "readonly" + readonlys;
23
str2CreateAc=str2CreateAc + "buyer_msg" + buyer_msg;*/
24
str2CreateAc=str2CreateAc + "seller" + sellerEmail;
25
/*str2CreateAc=str2CreateAc + "buyer" + buyer;
26
str2CreateAc=str2CreateAc + "buyer_name" + buyer_name;
27
str2CreateAc=str2CreateAc + "buyer_address" + buyer_address;
28
str2CreateAc=str2CreateAc + "buyer_zipcode" + buyer_zipcode;
29
str2CreateAc=str2CreateAc + "buyer_tel" + buyer_tel;
30
str2CreateAc=str2CreateAc + "buyer_mobile" + buyer_mobile;*/
31
str2CreateAc=str2CreateAc + "partner" + partner;
32
str2CreateAc=str2CreateAc + keyCode;
33
34
//acCode=FormsAuthentication.HashPasswordForStoringInConfigFile(str2CreateAc,"MD5");
35
acCode=this.GetMD5(str2CreateAc,"gb2312");
36
itemURL=INTERFACE_URL + "?cmd=" + cmd;
37
itemURL=itemURL + "&subject=" + HttpUtility.UrlEncode(subject);
38
itemURL=itemURL + "&body=" + HttpUtility.UrlEncode(body);
39
itemURL=itemURL + "&order_no=" + order_no;
40
itemURL=itemURL + "&price=" + prices;
41
//itemURL=itemURL + "&url=" + rurl;
42
itemURL=itemURL + "&type=" + types;
43
itemURL=itemURL + "&number=" + number;
44
itemURL=itemURL + "&transport=" + transport;
45
/*itemURL=itemURL + "&ordinary_fee=" + ordinary_fee;
46
itemURL=itemURL + "&express_fee=" + express_fee;
47
itemURL=itemURL + "&readonly=" + readonlys;
48
itemURL=itemURL + "&buyer_msg=" + HttpUtility.UrlEncode(buyer_msg);
49
itemURL=itemURL + "&buyer=" + HttpUtility.UrlEncode(buyer);
50
itemURL=itemURL + "&buyer_name=" + HttpUtility.UrlEncode(buyer_name);
51
itemURL=itemURL + "&buyer_address=" + HttpUtility.UrlEncode(buyer_address);
52
itemURL=itemURL + "&buyer_zipcode=" + buyer_zipcode;
53
itemURL=itemURL + "&buyer_tel=" + buyer_tel;
54
itemURL=itemURL + "&buyer_mobile=" + buyer_mobile;*/
55
itemURL=itemURL + "&partner=" + partner;
56
itemURL=itemURL + "&ac=" + acCode;
57
AlipayItemURL=itemURL;
58
return AlipayItemURL;
59
}
这个函数就是返回生成的地址,里面注释掉的看你自己需要可以添加进去,然后就是md5码的问题,现在用默认的md5生成程序对中文的支持只限于
GB2312,而支付宝使用的是GBK,虽然两个编码的内容GBK兼容GB2312但是毕竟两个编码方式不同,所以会产生错误,如果用英文或者数字不会有
问题。上面下载里面带的一个md5.asp的算法支持中文。
现在已经可以跳转到支付宝的页面了,而我们这边就要自己记录用户的信息已经生成的定单编号,这样在支付宝返回信息的时候来查询。在设定了返回地址后,我们就要看接收页面了。
1
string msg_id,order_no,gross,buyer_email,buyer_name,buyer_address,buyer_zipcode,buyer_tel,buyer_mobile,action,s_date,ac,notify_type;
2
3
string returnTxt;//返回给支付宝通知接口的结果
4
string alipayNotifyURL;//支付宝查询接口URL
5
string myalipayEmail;//商户的支付宝Email
6
string ResponseTxt="";
7
8
9
returnTxt = "N";
10
alipayNotifyURL = ConfigurationSettings.AppSettings["interfaceback"];//支付宝查询接口地址
11
myalipayEmail = ConfigurationSettings.AppSettings["account"];//填写您的支付宝帐号
12
13
14
//检查支付宝通知接口传递过来的参数是否合法
15
msg_id = newop.DelStr(Request["msg_id"]);
16
order_no = newop.DelStr(Request["order_no"]);
17
gross = newop.DelStr(Request["gross"]);
18
buyer_email = newop.DelStr(Request["buyer_email"]);
19
buyer_name = newop.DelStr(Request["buyer_name"]);
20
buyer_address = newop.DelStr(Request["buyer_address"]);
21
buyer_zipcode = newop.DelStr(Request["buyer_zipcode"]);
22
buyer_tel = newop.DelStr(Request["buyer_tel"]);
23
buyer_mobile = newop.DelStr(Request["buyer_mobile"]);
24
action = newop.DelStr(Request["action"]);
25
s_date = newop.DelStr(Request["date"]);
26
ac = newop.DelStr(Request["ac"]);
27
notify_type = newop.DelStr(Request["notify_type"]);
28
29
alipayNotifyURL = alipayNotifyURL + "msg_id=" + msg_id + "&email=" + myalipayEmail + "&order_no=" + order_no;
30
31
System.Net.WebClient isClient= new System.Net.WebClient();
32
Stream isStream = isClient.OpenRead(alipayNotifyURL);
33
StreamReader isReader = new StreamReader(isStream,System.Text.Encoding.GetEncoding("GB2312"));
34
ResponseTxt = isReader.ReadToEnd();
35
36
if(action == "test")//测试商户网站URL是否正确安装
37
{
38
returnTxt = "Y";
39
}
40
else if((action=="sendOff")&&(msg_id!=""))//发货通知
41
{
42
returnTxt = "N";
43
if((ResponseTxt == "true")||(ResponseTxt == "false"))
44
{
45
//更新数据在商户系统里的订单数据;如果已经发货,则将returnTxt置为Y,否则为N
46
47
}
48
else
49
{
50
//非法数据,不做更新
51
returnTxt="Error";
52
}
53
}
54
else if((action=="sendOff")&&(notify_type=="web"))
55
{
56
//检查是否已经付帐,并记录 }
57
else if((action=="checkOut")&&(msg_id!=""))//交易结束通知
58
{
59
returnTxt = "Y";
60
if((ResponseTxt=="true")||(ResponseTxt == "false"))
61
{
62
//更新数据在商户系统里的订单数据;如果数据更新成功,则将returnTxt置为Y,否则为N
63
//更新数据
64
65
//你的代码,更新你这边数据
66
returnTxt= "Y";
67
}
68
else
69
{
70
//非法数据,不做更新
71
returnTxt = "Error";
72
}
73
}
74
else
75
{
76
returnTxt="Error";
77
}
78
Response.Write(returnTxt);
代码编写完毕。
你购买成功后跳转自定义连接的时候会传2次过来,第一次是数据底层请求,第二次是web请求,而只有第一次有验证码,这个只能通过记录下来才看的到,因为
两次请求间隔很小,如果光显示的话最后的结果是被第二次覆盖了的。所以在接收的时候就要设定接收条件,一种是没有notify_type参数的,一种是有
的。
代码如下:
1
t1=ConfigurationSettings.AppSettings["interface"];//支付接口,就是给的一个连接地址
2
t2=ConfigurationSettings.AppSettings["account"];//支付宝帐户你的帐户
3
t3=ConfigurationSettings.AppSettings["password"];//安全校验码,设置的商家验证码
4
t4="images/logo_zfbsmall.gif";//按钮图片地址
5
t5="test";//悬停说明
6
cmd="0001";//默认
7
subject="item";//商品名称
8
body="decrip";//描述
9
order_no=;//定单号,用户自己生成,方便自己管理 prices=100;//价格0.01~50000.00
10
rurl="http://www.xxx.com/";//商品展示网址
11
types="1";//1:商品购买2:服务购买3:网络拍卖4:捐赠
12
number="1";//购买数量
13
transport="3";//1:平邮2:快递3:虚拟物品
14
ordinary_fee="";//平邮运费
15
express_fee="";//快递运费
16
readonlys="true";//交易信息是否只读
17
buyer_msg="";//买家给卖家的留言
18
buyer="";//买家Email
19
buyer_name="";//买家姓名
20
buyer_address="";//买家地址
21
buyer_zipcode="";//买家邮编
22
buyer_tel="";//买家电话号码
23
buyer_mobile="";//买家手机号码
24
partner=ConfigurationSettings.AppSettings["partenid"];//合作伙伴ID,这个是固定的上面就是要提供得基本信息,然后就是生成支付宝得连接,也就是给支付宝提供一条带验证的购买信息。
1
public string creatAlipayItemURL(string t1,string t2,string t3,string t4,string t5,string cmd,string subject,string body,string order_no,string prices,string rurl,string types,string number,string transport,string ordinary_fee,string express_fee,string readonlys,string buyer_msg,string buyer,string buyer_name,string buyer_address,string buyer_zipcode,string buyer_tel,string buyer_mobile,string partner)
2
{
3
string itemURL,str2CreateAc,acCode;
4
string INTERFACE_URL,sellerEmail,keyCode,imgsrc,imgtitle,AlipayItemURL;
5
//初始化各必要变量
6
INTERFACE_URL=t1+t2;//支付接口
7
sellerEmail=t2;//商户支付宝账户(改成你自己的)
8
keyCode=t3;//安全校验码(改成你自己的)
9
imgsrc=t4;//支付宝按钮图片
10
imgtitle=t5;//按钮悬停说明
11
12
str2CreateAc="cmd" + cmd + "subject" + subject;
13
str2CreateAc=str2CreateAc + "body" + body;
14
str2CreateAc=str2CreateAc + "order_no" + order_no;
15
str2CreateAc=str2CreateAc + "price" + prices;
16
//str2CreateAc=str2CreateAc + "url" + rurl;
17
str2CreateAc=str2CreateAc + "type" + types;
18
str2CreateAc=str2CreateAc + "number" + number;
19
str2CreateAc=str2CreateAc + "transport" + transport;
20
/*str2CreateAc=str2CreateAc + "ordinary_fee" + ordinary_fee;
21
str2CreateAc=str2CreateAc + "express_fee" + express_fee;
22
str2CreateAc=str2CreateAc + "readonly" + readonlys;
23
str2CreateAc=str2CreateAc + "buyer_msg" + buyer_msg;*/
24
str2CreateAc=str2CreateAc + "seller" + sellerEmail;
25
/*str2CreateAc=str2CreateAc + "buyer" + buyer;
26
str2CreateAc=str2CreateAc + "buyer_name" + buyer_name;
27
str2CreateAc=str2CreateAc + "buyer_address" + buyer_address;
28
str2CreateAc=str2CreateAc + "buyer_zipcode" + buyer_zipcode;
29
str2CreateAc=str2CreateAc + "buyer_tel" + buyer_tel;
30
str2CreateAc=str2CreateAc + "buyer_mobile" + buyer_mobile;*/
31
str2CreateAc=str2CreateAc + "partner" + partner;
32
str2CreateAc=str2CreateAc + keyCode;
33
34
//acCode=FormsAuthentication.HashPasswordForStoringInConfigFile(str2CreateAc,"MD5");
35
acCode=this.GetMD5(str2CreateAc,"gb2312");
36
itemURL=INTERFACE_URL + "?cmd=" + cmd;
37
itemURL=itemURL + "&subject=" + HttpUtility.UrlEncode(subject);
38
itemURL=itemURL + "&body=" + HttpUtility.UrlEncode(body);
39
itemURL=itemURL + "&order_no=" + order_no;
40
itemURL=itemURL + "&price=" + prices;
41
//itemURL=itemURL + "&url=" + rurl;
42
itemURL=itemURL + "&type=" + types;
43
itemURL=itemURL + "&number=" + number;
44
itemURL=itemURL + "&transport=" + transport;
45
/*itemURL=itemURL + "&ordinary_fee=" + ordinary_fee;
46
itemURL=itemURL + "&express_fee=" + express_fee;
47
itemURL=itemURL + "&readonly=" + readonlys;
48
itemURL=itemURL + "&buyer_msg=" + HttpUtility.UrlEncode(buyer_msg);
49
itemURL=itemURL + "&buyer=" + HttpUtility.UrlEncode(buyer);
50
itemURL=itemURL + "&buyer_name=" + HttpUtility.UrlEncode(buyer_name);
51
itemURL=itemURL + "&buyer_address=" + HttpUtility.UrlEncode(buyer_address);
52
itemURL=itemURL + "&buyer_zipcode=" + buyer_zipcode;
53
itemURL=itemURL + "&buyer_tel=" + buyer_tel;
54
itemURL=itemURL + "&buyer_mobile=" + buyer_mobile;*/
55
itemURL=itemURL + "&partner=" + partner;
56
itemURL=itemURL + "&ac=" + acCode;
57
AlipayItemURL=itemURL;
58
return AlipayItemURL;
59
}
这个函数就是返回生成的地址,里面注释掉的看你自己需要可以添加进去,然后就是md5码的问题,现在用默认的md5生成程序对中文的支持只限于
GB2312,而支付宝使用的是GBK,虽然两个编码的内容GBK兼容GB2312但是毕竟两个编码方式不同,所以会产生错误,如果用英文或者数字不会有
问题。上面下载里面带的一个md5.asp的算法支持中文。
现在已经可以跳转到支付宝的页面了,而我们这边就要自己记录用户的信息已经生成的定单编号,这样在支付宝返回信息的时候来查询。在设定了返回地址后,我们就要看接收页面了。
1
string msg_id,order_no,gross,buyer_email,buyer_name,buyer_address,buyer_zipcode,buyer_tel,buyer_mobile,action,s_date,ac,notify_type;
2
3
string returnTxt;//返回给支付宝通知接口的结果
4
string alipayNotifyURL;//支付宝查询接口URL
5
string myalipayEmail;//商户的支付宝Email
6
string ResponseTxt="";
7
8
9
returnTxt = "N";
10
alipayNotifyURL = ConfigurationSettings.AppSettings["interfaceback"];//支付宝查询接口地址
11
myalipayEmail = ConfigurationSettings.AppSettings["account"];//填写您的支付宝帐号
12
13
14
//检查支付宝通知接口传递过来的参数是否合法
15
msg_id = newop.DelStr(Request["msg_id"]);
16
order_no = newop.DelStr(Request["order_no"]);
17
gross = newop.DelStr(Request["gross"]);
18
buyer_email = newop.DelStr(Request["buyer_email"]);
19
buyer_name = newop.DelStr(Request["buyer_name"]);
20
buyer_address = newop.DelStr(Request["buyer_address"]);
21
buyer_zipcode = newop.DelStr(Request["buyer_zipcode"]);
22
buyer_tel = newop.DelStr(Request["buyer_tel"]);
23
buyer_mobile = newop.DelStr(Request["buyer_mobile"]);
24
action = newop.DelStr(Request["action"]);
25
s_date = newop.DelStr(Request["date"]);
26
ac = newop.DelStr(Request["ac"]);
27
notify_type = newop.DelStr(Request["notify_type"]);
28
29
alipayNotifyURL = alipayNotifyURL + "msg_id=" + msg_id + "&email=" + myalipayEmail + "&order_no=" + order_no;
30
31
System.Net.WebClient isClient= new System.Net.WebClient();
32
Stream isStream = isClient.OpenRead(alipayNotifyURL);
33
StreamReader isReader = new StreamReader(isStream,System.Text.Encoding.GetEncoding("GB2312"));
34
ResponseTxt = isReader.ReadToEnd();
35
36
if(action == "test")//测试商户网站URL是否正确安装
37
{
38
returnTxt = "Y";
39
}
40
else if((action=="sendOff")&&(msg_id!=""))//发货通知
41
{
42
returnTxt = "N";
43
if((ResponseTxt == "true")||(ResponseTxt == "false"))
44
{
45
//更新数据在商户系统里的订单数据;如果已经发货,则将returnTxt置为Y,否则为N
46
47
}
48
else
49
{
50
//非法数据,不做更新
51
returnTxt="Error";
52
}
53
}
54
else if((action=="sendOff")&&(notify_type=="web"))
55
{
56
//检查是否已经付帐,并记录 }
57
else if((action=="checkOut")&&(msg_id!=""))//交易结束通知
58
{
59
returnTxt = "Y";
60
if((ResponseTxt=="true")||(ResponseTxt == "false"))
61
{
62
//更新数据在商户系统里的订单数据;如果数据更新成功,则将returnTxt置为Y,否则为N
63
//更新数据
64
65
//你的代码,更新你这边数据
66
returnTxt= "Y";
67
}
68
else
69
{
70
//非法数据,不做更新
71
returnTxt = "Error";
72
}
73
}
74
else
75
{
76
returnTxt="Error";
77
}
78
Response.Write(returnTxt);
代码编写完毕。
相关文章推荐
- .Net语言 APP开发平台——Smobiler学习日志:如何快速实现类似于微信的悬浮显示二维码效果
- c#.net实现支付宝Payto接口
- C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 基于数据库资源的多语言实现
- .Net语言 APP开发平台——Smobiler学习日志:如何快速实现手机上的资源上传功能
- .Net语言 APP开发平台——Smobiler学习日志:如何快速实现手机上的资源上传功能
- .Net语言 APP开发平台——Smobiler学习日志:如何快速实现手机上的资源上传功能
- 在ASP.NET中如何用C#.NET实现基于表单的验证
- 如何使用 C# .NET 在 ASP.NET 应用程序中实现基于窗体的身份验证
- [转]在ASP.NET中如何用C#.NET实现基于表单的验证(一)
- [转]在ASP.NET中如何用C#.NET实现基于表单的验证(一)
- 在ASP.NET中如何用C#.NET实现基于表单的验证
- 如何在 Microsoft Visual C# .NET 中实现 Microsoft Excel 自动化
- 在ASP.NET中如何用C#.NET实现基于表单的验证
- 请问在.net framework精简版,使用C#语言如何实现MD5和SHA1算法
- 如何使用 C# .NET 在 ASP.NET 应用程序中实现基于窗体的身份验证
- 支付宝Payto接口的c#.net实现
- 在ASP.NET中如何用C#.NET实现基于表单的验证
- 在ASP.NET中如何用C#.NET实现基于表单的验证
- [转]在ASP.NET中如何用C#.NET实现基于表单的验证(二)
- 支付宝Payto接口的c#.net实现