C#的支付寶Payto接口代碼

2010-08-28 10:53:02來源:西部e網(wǎng)作者:

  它現(xiàn)在這種支付方式比較多象網(wǎng)銀在線等使用的方法都是url驗(yàn)證,就是通過url參數(shù)和一個(gè)這些url參數(shù)的md5編碼來確認(rèn)這個(gè)連接的正確性,支付寶在你購買成功后跳轉(zhuǎn)自定義連接的時(shí)候會(huì)傳2次過來,第一次是數(shù)據(jù)底層請(qǐng)求,第二次是web請(qǐng)求,而只有第一次有驗(yàn)證碼,這個(gè)只能通過記錄下來才看的到,因?yàn)閮纱握?qǐng)求間隔很小,如果光顯示的話最后的結(jié)果是被第二次覆蓋了的。所以在接收的時(shí)候就要設(shè)定接收條件,一種是沒有notify_type參數(shù)的,一種是有的。
 
  我們先來看一下創(chuàng)建一個(gè)連接地址

  t1=ConfigurationSettings.AppSettings["interface"];//支付接口,就是給的一個(gè)連接地址

  t2=ConfigurationSettings.AppSettings["account"];//支付寶帳戶你的帳戶

  t3=ConfigurationSettings.AppSettings["password"];//安全校驗(yàn)碼,設(shè)置的商家驗(yàn)證碼

  t4="images/logo_zfbsmall.gif";//按鈕圖片地址

  t5="test";//懸停說明

  cmd="0001";//默認(rèn)

  subject="item";//商品名稱

  body="decrip";//描述

  order_no=;//定單號(hào),用戶自己生成,方便自己管理                prices=100;//價(jià)格0.01~50000.00

  rurl="商品展示網(wǎng)址

  types="1";//1:商品購買2:服務(wù)購買3:網(wǎng)絡(luò)拍賣4:捐贈(zèng)

  number="1";//購買數(shù)量

  transport="3";//1:平郵2:快遞3:虛擬物品

  ordinary_fee="";//平郵運(yùn)費(fèi)

  express_fee="";//快遞運(yùn)費(fèi)

  readonlys="true";//交易信息是否只讀

  buyer_msg="";//買家給賣家的留言

  buyer="";//買家Email

  buyer_name="";//買家姓名

  buyer_address="";//買家地址

  buyer_zipcode="";//買家郵編

  buyer_tel="";//買家電話號(hào)碼

  buyer_mobile="";//買家手機(jī)號(hào)碼

  partner=ConfigurationSettings.AppSettings["partenid"];//合作伙伴ID,這個(gè)是固定的

  上面就是要提供得基本信息,然后就是生成支付寶得連接,也就是給支付寶提供一條帶驗(yàn)證的購買信息。

  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;//安全校驗(yàn)碼(改成你自己的)

  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;

  }

  這個(gè)函數(shù)就是返回生成的地址,里面注釋掉的看你自己需要可以添加進(jìn)去,然后就是md5碼的問題,現(xiàn)在用默認(rèn)的md5生成程序?qū)χ形牡闹С种幌抻贕B2312,而支付寶使用的是GBK,雖然兩個(gè)編碼的內(nèi)容GBK兼容GB2312但是畢竟兩個(gè)編碼方式不同,所以會(huì)產(chǎn)生錯(cuò)誤,如果用英文或者數(shù)字不會(huì)有問題。上面下載里面帶的一個(gè)md5.asp的算法支持中文。

  現(xiàn)在已經(jīng)可以跳轉(zhuǎn)到支付寶的頁面了,而我們這邊就要自己記錄用戶的信息已經(jīng)生成的定單編號(hào),這樣在支付寶返回信息的時(shí)候來查詢。在設(shè)定了返回地址后,我們就要看接收頁面了。

  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;//返回給支付寶通知接口的結(jié)果

  string alipayNotifyURL;//支付寶查詢接口URL

  string myalipayEmail;//商戶的支付寶Email

  string ResponseTxt="";

  returnTxt            = "N";

  alipayNotifyURL        = ConfigurationSettings.AppSettings["interfaceback"];//支付寶查詢接口地址

  myalipayEmail        = ConfigurationSettings.AppSettings["account"];//填寫您的支付寶帳號(hào)

  //檢查支付寶通知接口傳遞過來的參數(shù)是否合法

  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")//測(cè)試商戶網(wǎng)站URL是否正確安裝

  {

  returnTxt    = "Y";

  }

  else if((action=="sendOff")&&(msg_id!=""))//發(fā)貨通知

  {

  returnTxt        = "N";

  if((ResponseTxt == "true")||(ResponseTxt == "false"))

  {

  //更新數(shù)據(jù)在商戶系統(tǒng)里的訂單數(shù)據(jù);如果已經(jīng)發(fā)貨,則將returnTxt置為Y,否則為N

  }

  else

  {

  //非法數(shù)據(jù),不做更新

  returnTxt="Error";

  }

  }

  else if((action=="sendOff")&&(notify_type=="web"))

  {

  //檢查是否已經(jīng)付帳,并記錄            }

  else if((action=="checkOut")&&(msg_id!=""))//交易結(jié)束通知

  {

  returnTxt    = "Y";

  if((ResponseTxt=="true")||(ResponseTxt == "false"))

  {

  //更新數(shù)據(jù)在商戶系統(tǒng)里的訂單數(shù)據(jù);如果數(shù)據(jù)更新成功,則將returnTxt置為Y,否則為N

  //更新數(shù)據(jù)

  //你的代碼,更新你這邊數(shù)據(jù)

  returnTxt= "Y";

  }

  else

  {

  //非法數(shù)據(jù),不做更新

  returnTxt    = "Error";

  }

  }

  else

  {

  returnTxt="Error";

  }

  Response.Write(returnTxt);

關(guān)鍵詞:C#

贊助商鏈接: