c#.net实现支付宝Payto接口

      它现在这种支付方式比较多象网银在线等使用的方法都是url验证,就是通过url参数和一个这些url参数的md5编码来确认这个连接的正确性,支付宝在你购买成功后跳转自定义连接的时候会传2次过来,第一次是数据底层请求,第二次是web请求,而只有第一次有验证码,这个只能通过记录下来才看的到,因为两次请求间隔很小,如果光显示的话最后的结果是被第二次覆盖了的。所以在接收的时候就要设定接收条件,一种是没有notify_type参数的,一种是有的。
 1None.gift1=ConfigurationSettings.AppSettings["interface"];//支付接口,就是给的一个连接地址
 2None.gif            t2=ConfigurationSettings.AppSettings["account"];//支付宝帐户你的帐户
 3None.gif            t3=ConfigurationSettings.AppSettings["password"];//安全校验码,设置的商家验证码
 4None.gif            t4="images/logo_zfbsmall.gif";//按钮图片地址
 5None.gif            t5="test";//悬停说明
 6None.gif            cmd="0001";//默认
 7None.gif            subject="item";//商品名称
 8None.gif            body="decrip";//描述
 9None.gif            order_no=;//定单号,用户自己生成,方便自己管理                prices=100;//价格0.01~50000.00
10None.gif            rurl="http://www.xxx.com/";//商品展示网址
11None.gif            types="1";//1:商品购买2:服务购买3:网络拍卖4:捐赠
12None.gif            number="1";//购买数量
13None.gif            transport="3";//1:平邮2:快递3:虚拟物品
14None.gif            ordinary_fee="";//平邮运费
15None.gif            express_fee="";//快递运费
16None.gif            readonlys="true";//交易信息是否只读
17None.gif            buyer_msg="";//买家给卖家的留言
18None.gif            buyer="";//买家Email
19None.gif            buyer_name="";//买家姓名
20None.gif            buyer_address="";//买家地址
21None.gif            buyer_zipcode="";//买家邮编
22None.gif            buyer_tel="";//买家电话号码
23None.gif            buyer_mobile="";//买家手机号码
24None.gif            partner=ConfigurationSettings.AppSettings["partenid"];//合作伙伴ID,这个是固定的
上面就是要提供得基本信息,然后就是生成支付宝得连接,也就是给支付宝提供一条带验证的购买信息。
 1None.gifpublic 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)
 2ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
 3InBlock.gif            string itemURL,str2CreateAc,acCode;
 4InBlock.gif            string INTERFACE_URL,sellerEmail,keyCode,imgsrc,imgtitle,AlipayItemURL;
 5InBlock.gif            //初始化各必要变量
 6InBlock.gif            INTERFACE_URL=t1+t2;//支付接口
 7InBlock.gif            sellerEmail=t2;//商户支付宝账户(改成你自己的)
 8InBlock.gif            keyCode=t3;//安全校验码(改成你自己的)
 9InBlock.gif            imgsrc=t4;//支付宝按钮图片
10InBlock.gif            imgtitle=t5;//按钮悬停说明
11InBlock.gif
12InBlock.gif            str2CreateAc="cmd" + cmd + "subject" + subject;
13InBlock.gif            str2CreateAc=str2CreateAc + "body" + body;
14InBlock.gif            str2CreateAc=str2CreateAc + "order_no" + order_no;
15InBlock.gif            str2CreateAc=str2CreateAc + "price" + prices;
16InBlock.gif            //str2CreateAc=str2CreateAc + "url" + rurl;
17InBlock.gif            str2CreateAc=str2CreateAc + "type" + types;
18InBlock.gif            str2CreateAc=str2CreateAc + "number" + number;
19InBlock.gif            str2CreateAc=str2CreateAc + "transport" + transport;
20ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//*str2CreateAc=str2CreateAc + "ordinary_fee" + ordinary_fee;
21InBlock.gif            str2CreateAc=str2CreateAc + "express_fee" + express_fee;
22InBlock.gif            str2CreateAc=str2CreateAc + "readonly" + readonlys;
23ExpandedSubBlockEnd.gif            str2CreateAc=str2CreateAc + "buyer_msg" + buyer_msg;*/

24InBlock.gif            str2CreateAc=str2CreateAc + "seller" + sellerEmail;
25ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//*str2CreateAc=str2CreateAc + "buyer" + buyer;
26InBlock.gif            str2CreateAc=str2CreateAc + "buyer_name" + buyer_name;
27InBlock.gif            str2CreateAc=str2CreateAc + "buyer_address" + buyer_address;
28InBlock.gif            str2CreateAc=str2CreateAc + "buyer_zipcode" + buyer_zipcode;
29InBlock.gif            str2CreateAc=str2CreateAc + "buyer_tel" + buyer_tel;
30ExpandedSubBlockEnd.gif            str2CreateAc=str2CreateAc + "buyer_mobile" + buyer_mobile;*/

31InBlock.gif            str2CreateAc=str2CreateAc + "partner" + partner;
32InBlock.gif            str2CreateAc=str2CreateAc + keyCode;
33InBlock.gif
34InBlock.gif            //acCode=FormsAuthentication.HashPasswordForStoringInConfigFile(str2CreateAc,"MD5");
35InBlock.gif            acCode=this.GetMD5(str2CreateAc,"gb2312");
36InBlock.gif            itemURL=INTERFACE_URL + "?cmd=" + cmd;
37InBlock.gif            itemURL=itemURL + "&subject=" + HttpUtility.UrlEncode(subject);
38InBlock.gif            itemURL=itemURL + "&body=" + HttpUtility.UrlEncode(body);
39InBlock.gif            itemURL=itemURL + "&order_no=" + order_no;
40InBlock.gif            itemURL=itemURL + "&price=" + prices;
41InBlock.gif            //itemURL=itemURL + "&url=" + rurl;
42InBlock.gif            itemURL=itemURL + "&type=" + types;
43InBlock.gif            itemURL=itemURL + "&number=" + number;
44InBlock.gif            itemURL=itemURL + "&transport=" + transport;
45ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//*itemURL=itemURL + "&ordinary_fee=" + ordinary_fee;
46InBlock.gif            itemURL=itemURL + "&express_fee=" + express_fee;
47InBlock.gif            itemURL=itemURL + "&readonly=" + readonlys;
48InBlock.gif            itemURL=itemURL + "&buyer_msg=" + HttpUtility.UrlEncode(buyer_msg);
49InBlock.gif            itemURL=itemURL + "&buyer=" + HttpUtility.UrlEncode(buyer);
50InBlock.gif            itemURL=itemURL + "&buyer_name=" + HttpUtility.UrlEncode(buyer_name);
51InBlock.gif            itemURL=itemURL + "&buyer_address=" + HttpUtility.UrlEncode(buyer_address);
52InBlock.gif            itemURL=itemURL + "&buyer_zipcode=" + buyer_zipcode;
53InBlock.gif            itemURL=itemURL + "&buyer_tel=" + buyer_tel;
54ExpandedSubBlockEnd.gif            itemURL=itemURL + "&buyer_mobile=" + buyer_mobile;*/

55InBlock.gif            itemURL=itemURL + "&partner=" + partner;
56InBlock.gif            itemURL=itemURL + "&ac=" + acCode;
57InBlock.gif            AlipayItemURL=itemURL;
58InBlock.gif                        return AlipayItemURL;
59ExpandedBlockEnd.gif        }

这个函数就是返回生成的地址,里面注释掉的看你自己需要可以添加进去,然后就是md5码的问题,现在用默认的md5生成程序对中文的支持只限于GB2312,而支付宝使用的是GBK,虽然两个编码的内容GBK兼容GB2312但是毕竟两个编码方式不同,所以会产生错误,如果用英文或者数字不会有问题。上面下载里面带的一个md5.asp的算法支持中文。
        现在已经可以跳转到支付宝的页面了,而我们这边就要自己记录用户的信息已经生成的定单编号,这样在支付宝返回信息的时候来查询。在设定了返回地址后,我们就要看接收页面了。
 1None.gifstring msg_id,order_no,gross,buyer_email,buyer_name,buyer_address,buyer_zipcode,buyer_tel,buyer_mobile,action,s_date,ac,notify_type;
 2None.gif    
 3None.gif            string returnTxt;//返回给支付宝通知接口的结果
 4None.gif            string alipayNotifyURL;//支付宝查询接口URL
 5None.gif            string myalipayEmail;//商户的支付宝Email
 6None.gif            string ResponseTxt="";
 7None.gif    
 8None.gif
 9None.gif            returnTxt            = "N";
10None.gif            alipayNotifyURL        = ConfigurationSettings.AppSettings["interfaceback"];//支付宝查询接口地址
11None.gif            myalipayEmail        = ConfigurationSettings.AppSettings["account"];//填写您的支付宝帐号
12None.gif
13None.gif    
14None.gif            //检查支付宝通知接口传递过来的参数是否合法
15None.gif            msg_id            = newop.DelStr(Request["msg_id"]);
16None.gif            order_no        = newop.DelStr(Request["order_no"]);
17None.gif            gross            = newop.DelStr(Request["gross"]);
18None.gif            buyer_email        = newop.DelStr(Request["buyer_email"]);
19None.gif            buyer_name        = newop.DelStr(Request["buyer_name"]);
20None.gif            buyer_address    = newop.DelStr(Request["buyer_address"]);
21None.gif            buyer_zipcode    = newop.DelStr(Request["buyer_zipcode"]);
22None.gif            buyer_tel        = newop.DelStr(Request["buyer_tel"]);
23None.gif            buyer_mobile    = newop.DelStr(Request["buyer_mobile"]);
24None.gif            action            = newop.DelStr(Request["action"]);
25None.gif            s_date            = newop.DelStr(Request["date"]);
26None.gif            ac                = newop.DelStr(Request["ac"]);
27None.gif            notify_type     = newop.DelStr(Request["notify_type"]);
28None.gif
29None.gif            alipayNotifyURL    = alipayNotifyURL + "msg_id=" + msg_id + "&email=" + myalipayEmail + "&order_no=" + order_no;
30None.gif    
31None.gif            System.Net.WebClient isClient= new System.Net.WebClient();
32None.gif            Stream isStream = isClient.OpenRead(alipayNotifyURL);
33None.gif            StreamReader isReader = new StreamReader(isStream,System.Text.Encoding.GetEncoding("GB2312"));
34None.gif            ResponseTxt = isReader.ReadToEnd();
35None.gif
36None.gifif(action == "test")//测试商户网站URL是否正确安装
37ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
38InBlock.gif                returnTxt    = "Y";
39ExpandedBlockEnd.gif            }

40None.gif            else if((action=="sendOff")&&(msg_id!=""))//发货通知
41ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
42InBlock.gif                returnTxt        = "N";
43InBlock.gif                if((ResponseTxt == "true")||(ResponseTxt == "false"))
44ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
45InBlock.gif                    //更新数据在商户系统里的订单数据;如果已经发货,则将returnTxt置为Y,否则为N
46InBlock.gif        
47ExpandedSubBlockEnd.gif                }

48InBlock.gif                else
49ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
50InBlock.gif                    //非法数据,不做更新
51InBlock.gif                    returnTxt="Error";
52ExpandedSubBlockEnd.gif                }

53ExpandedBlockEnd.gif            }

54None.gif            else if((action=="sendOff")&&(notify_type=="web"))
55ExpandedBlockStart.gifContractedBlock.gif            dot.gif{
56InBlock.gif                //检查是否已经付帐,并记录            }
57InBlock.gif            else if((action=="checkOut")&&(msg_id!=""))//交易结束通知
58ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
59InBlock.gif                returnTxt    = "Y";
60InBlock.gif                if((ResponseTxt=="true")||(ResponseTxt == "false"))
61ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
62InBlock.gif                    //更新数据在商户系统里的订单数据;如果数据更新成功,则将returnTxt置为Y,否则为N
63InBlock.gif                    //更新数据
64InBlock.gif                    
65InBlock.gif//你的代码,更新你这边数据
66InBlock.gif                    returnTxt= "Y";
67ExpandedSubBlockEnd.gif                }

68InBlock.gif                else
69ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
70InBlock.gif                    //非法数据,不做更新
71InBlock.gif                    returnTxt    = "Error";
72ExpandedSubBlockEnd.gif                }
    
73ExpandedSubBlockEnd.gif            }

74InBlock.gif            else
75ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
76InBlock.gif                returnTxt="Error";
77ExpandedSubBlockEnd.gif            }

78InBlock.gif            Response.Write(returnTxt);

转载于:https://www.cnblogs.com/Chailizi/archive/2007/08/13/853142.html

支付宝接口 asp.net c#支付宝接口详细代码   支付宝Payto接口C#.net实现方法。支付宝现在这种支付方式比较多象网银在线等使用的方法都是url验证,就是通过url参数和一个这些url参数的md5编码来确认这个连接的正确性,支付宝在你购买成功后跳转自定义连接的时候会传2次过来,第一次是数据底层请求,第二次是web请求,而只有第一次有验证码,这个只能通过记录下来才看的到,因为两次请求间隔很小,如果光显示的话最后的结果是被第二次覆盖了的。所以在接收的时候就要设定接收条件,一种是没有notify_type参数的,一种是有的。   我们先来看一下创建一个连接地址 t1=ConfigurationSettings.AppSettings["interface"];//支付接口,就是给的一个连接地址   t2=ConfigurationSettings.AppSettings["account"];//支付宝帐户你的帐户   t3=ConfigurationSettings.AppSettings["password"];//安全校验码,设置的商家验证码   t4="images/logo_zfbsmall.gif";//按钮图片地址   t5="test";//悬停说明   cmd="0001";//默认   subject="item";//商品名称   body="decrip";//描述   order_no=;//定单号,用户自己生成,方便自己管理 prices=100;//价格0.01~50000.00   rurl="http://www.xxx.com/";//商品展示网址   types="1";//1:商品购买2:服务购买3:网络拍卖4:捐赠   number="1";//购买数量   transport="3";//1:平邮2:快递3:虚拟物品   ordinary_fee="";//平邮运费   express_fee="";//快递运费   readonlys="true";//交易信息是否只读   buyer_msg="";//买家给卖家的留言   buyer="";//买家Email   buyer_name="";//买家姓名   buyer_address="";//买家地址   buyer_zipcode="";//买家邮编   buyer_tel="";//买家电话号码   buyer_mobile="";//买家手机号码   partner=ConfigurationSettings.AppSettings["partenid"];//合作伙伴ID,这个是固定的   上面就是要提供得基本信息,然后就是生成支付宝得连接,也就是给支付宝提供一条带验证的购买信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值