支付寶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,這個是固定的

上面就是要提供得基本信息,然後就是生成支付寶得連接,也就是給支付寶提供一條帶驗證的購買信息。
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)
        {
            
string itemURL,str2CreateAc,acCode;
            
string INTERFACE_URL,sellerEmail,keyCode,imgsrc,imgtitle,AlipayItemURL;
            
//初始化各必要變量
            INTERFACE_URL=t1+t2;//支付接口
            sellerEmail=t2;//商戶支付寶賬戶(改成你自己的)
            keyCode=t3;//安全校驗碼(改成你自己的)
            imgsrc=t4;//支付寶按鈕圖片
            imgtitle=t5;//按鈕懸停說明

            str2CreateAc
="cmd" + cmd + "subject" + subject;
            str2CreateAc
=str2CreateAc + "body" + body;
            str2CreateAc
=str2CreateAc + "order_no" + order_no;
            str2CreateAc
=str2CreateAc + "price" + prices;
            
//str2CreateAc=str2CreateAc + "url" + rurl;
            str2CreateAc=str2CreateAc + "type" + types;
            str2CreateAc
=str2CreateAc + "number" + number;
            str2CreateAc
=str2CreateAc + "transport" + transport;
            
/*str2CreateAc=str2CreateAc + "ordinary_fee" + ordinary_fee;
            str2CreateAc=str2CreateAc + "express_fee" + express_fee;
            str2CreateAc=str2CreateAc + "readonly" + readonlys;
            str2CreateAc=str2CreateAc + "buyer_msg" + buyer_msg;
*/
            str2CreateAc
=str2CreateAc + "seller" + sellerEmail;
            
/*str2CreateAc=str2CreateAc + "buyer" + buyer;
            str2CreateAc=str2CreateAc + "buyer_name" + buyer_name;
            str2CreateAc=str2CreateAc + "buyer_address" + buyer_address;
            str2CreateAc=str2CreateAc + "buyer_zipcode" + buyer_zipcode;
            str2CreateAc=str2CreateAc + "buyer_tel" + buyer_tel;
            str2CreateAc=str2CreateAc + "buyer_mobile" + buyer_mobile;
*/
            str2CreateAc
=str2CreateAc + "partner" + partner;
            str2CreateAc
=str2CreateAc + keyCode;

            
//acCode=FormsAuthentication.HashPasswordForStoringInConfigFile(str2CreateAc,"MD5");
            acCode=this.GetMD5(str2CreateAc,"gb2312");
            itemURL
=INTERFACE_URL + "?cmd=" + cmd;
            itemURL
=itemURL + "&subject=" + HttpUtility.UrlEncode(subject);
            itemURL
=itemURL + "&body=" + HttpUtility.UrlEncode(body);
            itemURL
=itemURL + "&order_no=" + order_no;
            itemURL
=itemURL + "&price=" + prices;
            
//itemURL=itemURL + "&url=" + rurl;
            itemURL=itemURL + "&type=" + types;
            itemURL
=itemURL + "&number=" + number;
            itemURL
=itemURL + "&transport=" + transport;
            
/*itemURL=itemURL + "&ordinary_fee=" + ordinary_fee;
            itemURL=itemURL + "&express_fee=" + express_fee;
            itemURL=itemURL + "&readonly=" + readonlys;
            itemURL=itemURL + "&buyer_msg=" + HttpUtility.UrlEncode(buyer_msg);
            itemURL=itemURL + "&buyer=" + HttpUtility.UrlEncode(buyer);
            itemURL=itemURL + "&buyer_name=" + HttpUtility.UrlEncode(buyer_name);
            itemURL=itemURL + "&buyer_address=" + HttpUtility.UrlEncode(buyer_address);
            itemURL=itemURL + "&buyer_zipcode=" + buyer_zipcode;
            itemURL=itemURL + "&buyer_tel=" + buyer_tel;
            itemURL=itemURL + "&buyer_mobile=" + buyer_mobile;
*/
            itemURL
=itemURL + "&partner=" + partner;
            itemURL
=itemURL + "&ac=" + acCode;
            AlipayItemURL
=itemURL;
                        
return AlipayItemURL;
        }

這個函數就是返回生成的地址,裏面註釋掉的看你自己需要可以添加進去,然後就是md5碼的問題,現在用默認的md5生成程序對中文的支持只限於GB2312,而支付寶使用的是GBK,雖然兩個編碼的內容GBK兼容GB2312但是畢竟兩個編碼方式不同,所以會產生錯誤,如果用英文或者數字不會有問題。上面下載裏面帶的一個md5.asp的算法支持中文。
        現在已經可以跳轉到支付寶的頁面了,而我們這邊就要自己記錄用戶的信息已經生成的定單編號,這樣在支付寶返回信息的時候來查詢。在設定了返回地址後,我們就要看接收頁面了。
string msg_id,order_no,gross,buyer_email,buyer_name,buyer_address,buyer_zipcode,buyer_tel,buyer_mobile,action,s_date,ac,notify_type;
    
            
string returnTxt;//返回給支付寶通知接口的結果
            string alipayNotifyURL;//支付寶查詢接口URL
            string myalipayEmail;//商戶的支付寶Email
            string ResponseTxt="";
    

            returnTxt            
= "N";
            alipayNotifyURL        
= ConfigurationSettings.AppSettings["interfaceback"];//支付寶查詢接口地址
            myalipayEmail        = ConfigurationSettings.AppSettings["account"];//填寫您的支付寶帳號

    
            
//檢查支付寶通知接口傳遞過來的參數是否合法
            msg_id            = newop.DelStr(Request["msg_id"]);
            order_no        
= newop.DelStr(Request["order_no"]);
            gross            
= newop.DelStr(Request["gross"]);
            buyer_email        
= newop.DelStr(Request["buyer_email"]);
            buyer_name        
= newop.DelStr(Request["buyer_name"]);
            buyer_address    
= newop.DelStr(Request["buyer_address"]);
            buyer_zipcode    
= newop.DelStr(Request["buyer_zipcode"]);
            buyer_tel        
= newop.DelStr(Request["buyer_tel"]);
            buyer_mobile    
= newop.DelStr(Request["buyer_mobile"]);
            action            
= newop.DelStr(Request["action"]);
            s_date            
= newop.DelStr(Request["date"]);
            ac                
= newop.DelStr(Request["ac"]);
            notify_type     
= newop.DelStr(Request["notify_type"]);

            alipayNotifyURL    
= alipayNotifyURL + "msg_id=" + msg_id + "&email=" + myalipayEmail + "&order_no=" + order_no;
    
            System.Net.WebClient isClient
= new System.Net.WebClient();
            Stream isStream 
= isClient.OpenRead(alipayNotifyURL);
            StreamReader isReader 
= new StreamReader(isStream,System.Text.Encoding.GetEncoding("GB2312"));
            ResponseTxt 
= isReader.ReadToEnd();

if(action == "test")//測試商戶網站URL是否正確安裝
            {
                returnTxt    
= "Y";
            }
            
else if((action=="sendOff")&&(msg_id!=""))//發貨通知
            {
                returnTxt        
= "N";
                
if((ResponseTxt == "true")||(ResponseTxt == "false"))
                {
                    
//更新數據在商戶系統裏的訂單數據;如果已經發貨,則將returnTxt置爲Y,否則爲N
        
                }
                
else
                {
                    
//非法數據,不做更新
                    returnTxt="Error";
                }
            }
            
else if((action=="sendOff")&&(notify_type=="web"))
            {
                
//檢查是否已經付帳,並記錄            }
            else if((action=="checkOut")&&(msg_id!=""))//交易結束通知
            {
                returnTxt    
= "Y";
                
if((ResponseTxt=="true")||(ResponseTxt == "false"))
                {
                    
//更新數據在商戶系統裏的訂單數據;如果數據更新成功,則將returnTxt置爲Y,否則爲N
                    
//更新數據
                    
//你的代碼,更新你這邊數據
                    returnTxt= "Y";
                }
                
else
                {
                    
//非法數據,不做更新
                    returnTxt    = "Error";
                }    
            }
            
else
            {
                returnTxt
="Error";
            }
            Response.Write(returnTxt);
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章