c# 实现的支付宝支付
C#支付宝扫码支付示的具体代码,供大家参考,具体内容如下
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Security.Cryptography; usingSystem.Text; usingSystem.Web; namespaceZK_ECommerce.Shop.pay.alipay { publicclassAliPay { publicstaticstringGetMD5(strings,string_input_charset) { //////与ASP兼容的MD5加密算法 /// MD5md5=newMD5CryptoServiceProvider(); byte[]t=md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s)); StringBuildersb=newStringBuilder(32); for(inti=0;i///冒泡排序法 /// inti,j;//交换标志 stringtemp; boolexchange; for(i=0;i =i;j--) { if(System.String.CompareOrdinal(r[j+1],r[j])<0)//交换条件 { temp=r[j+1]; r[j+1]=r[j]; r[j]=temp; exchange=true;//发生了交换,故将交换标志置为真 } } if(!exchange)//本趟排序未发生交换,提前终止算法 { break; } } returnr; } publicstringCreatUrl( stringgateway, stringservice, stringpartner, stringsign_type, stringout_trade_no, stringsubject, stringbody, stringpayment_type, stringtotal_fee, stringshow_url, stringseller_email, stringkey, stringreturn_url, string_input_charset, stringnotify_url, stringextra_common_param="" ) { returnCreatUrl( gateway, service, partner, sign_type, out_trade_no, subject, body, payment_type, total_fee, show_url, seller_email, key, return_url, _input_charset, notify_url, "", extra_common_param); } publicstringCreatUrl( stringgateway, stringservice, stringpartner, stringsign_type, stringout_trade_no, stringsubject, stringbody, stringpayment_type, stringtotal_fee, stringshow_url, stringseller_email, stringkey, stringreturn_url, string_input_charset, stringnotify_url, stringtoken, stringextra_common_param="" ) { /// ///2015年11月17日14:08:00 /// inti; string[]Oristr; //构造数组; if(!string.IsNullOrEmpty(token)) { if(extra_common_param=="COD2MOTOPAY") { Oristr=new[]{ "service="+service, "partner="+partner, "subject="+subject, "body="+body, "out_trade_no="+out_trade_no, "total_fee="+total_fee, "show_url="+show_url, "payment_type="+payment_type, "seller_email="+seller_email, "notify_url="+notify_url, "_input_charset="+_input_charset, "return_url="+return_url, "token="+token, "extra_common_param="+extra_common_param }; } else { Oristr=new[]{ "service="+service, "partner="+partner, "subject="+subject, "body="+body, "out_trade_no="+out_trade_no, "total_fee="+total_fee, "show_url="+show_url, "payment_type="+payment_type, "seller_email="+seller_email, "notify_url="+notify_url, "_input_charset="+_input_charset, "return_url="+return_url, "token="+token }; } } else { if(extra_common_param=="COD2MOTOPAY") { Oristr=new[]{ "service="+service, "partner="+partner, "subject="+subject, "body="+body, "out_trade_no="+out_trade_no, "total_fee="+total_fee, "show_url="+show_url, "payment_type="+payment_type, "seller_email="+seller_email, "notify_url="+notify_url, "_input_charset="+_input_charset, "return_url="+return_url, "extra_common_param="+extra_common_param }; } else { Oristr=new[]{ "service="+service, "partner="+partner, "subject="+subject, "body="+body, "out_trade_no="+out_trade_no, "total_fee="+total_fee, "show_url="+show_url, "payment_type="+payment_type, "seller_email="+seller_email, "notify_url="+notify_url, "_input_charset="+_input_charset, "return_url="+return_url }; } } //进行排序; string[]Sortedstr=BubbleSort(Oristr); //构造待md5摘要字符串; StringBuilderprestr=newStringBuilder(); for(i=0;i///createdbysunzhizhi2006.5.21,sunzhizhi@msn.com。 /// inti; //构造数组; string[]Oristr={ "service="+service, "partner="+partner, "out_trade_no="+out_trade_no, "_input_charset="+_input_charset }; //进行排序; string[]Sortedstr=BubbleSort(Oristr); //构造待md5摘要字符串; StringBuilderprestr=newStringBuilder(); for(i=0;i 页面处理:
usingSystem; usingSystem.Collections.Generic; usingSystem.Collections.Specialized; usingSystem.IO; usingSystem.Linq; usingSystem.Net; usingSystem.Security.Cryptography; usingSystem.Text; usingSystem.Web; usingSystem.Web.Mvc; usingZK_ECommerce.Shop.pay.alipay; namespaceAlipay.Controllers { publicclassApliayDemoController:Controller { //////支付页面 /// ////// publicActionResultIndex(stringordercode) { stringout_trade_no=ordercode; stringgateway="https://mapi.alipay.com/gateway.do?";//支付接口 stringservice="create_direct_pay_by_user"; stringpartner="";//partner合作伙伴ID保留字段 stringsign_type="MD5"; stringpayment_type="1";//支付类型 stringshow_url="http://www.letao.com"; stringseller_email="";//卖家账号 stringkey="";//partner账户的支付宝安全校验码 stringreturn_url="http://localhost:1396/ApliayDemo/ApliyReturn";//服务器通知返回接口 stringnotify_url="http://localhost:1396/ApliayDemo/AlipayNotify";//服务器通知返回接口 string_input_charset="utf-8"; stringextra_common_param="COD2MOTOPAY";//表示快捷支付 stringtoken=string.Empty; if(string.IsNullOrEmpty(token)) token=""; stringsubject="康康体检套餐";//subject商品名称 stringbody="测试商品";//商品描述 stringtotal_fee="0.01"; AliPayap=newAliPay(); stringaliay_url=ap.CreatUrl(gateway,service,partner,sign_type,out_trade_no,subject,body,payment_type, total_fee,show_url,seller_email,key,return_url,_input_charset,notify_url,token,extra_common_param); returnRedirect(aliay_url); } /// ///返回通知页面 /// ///publicActionResultApliyReturn() { stringalipayNotifyURL="https://mapi.alipay.com/gateway.do?"; stringkey="";//partner的对应交易安全校验码(必须填写) string_input_charset="utf-8"; stringpartner="";//partner合作伙伴id(必须填写) alipayNotifyURL=alipayNotifyURL+"service=notify_verify"+"&partner="+partner+"¬ify_id="+Request.QueryString["notify_id"]; //获取支付宝ATN返回结果,true是正确的订单信息,false是无效的 stringresponseTxt=Get_Http(alipayNotifyURL,120000); inti; NameValueCollectioncoll; coll=Request.QueryString; String[]requestarr=coll.AllKeys; //进行排序; string[]Sortedstr=BubbleSort(requestarr); //构造待md5摘要字符串; StringBuilderprestr=newStringBuilder(); for(i=0;i Result:responseTxt="+responseTxt); Response.Write("
Result:mysign="+mysign); Response.Write("
Result:sign="+sign); } returnView(); } //////支付回调页面 /// ///publicActionResultAlipayNotify() { stringalipayNotifyURL="https://mapi.alipay.com/gateway.do?"; stringpartner="";//partner合作伙伴id(必须填写) stringkey="";//partner的对应交易安全校验码(必须填写) alipayNotifyURL=alipayNotifyURL+"service=notify_verify"+"&partner="+partner+"¬ify_id="+Request.Form["notify_id"]; //获取支付宝ATN返回结果,true是正确的订单信息,false是无效的 stringresponseTxt=Get_Http(alipayNotifyURL,120000); inti; NameValueCollectioncoll; //LoadFormvariablesintoNameValueCollectionvariable. coll=Request.Form; //Getnamesofallformsintoastringarray. String[]requestarr=coll.AllKeys; //进行排序; string[]Sortedstr=BubbleSort(requestarr); //构造待md5摘要字符串; StringBuilderprestr=newStringBuilder(); for(i=0;i Result:responseTxt="+responseTxt); Response.Write("
Result:mysign="+mysign); Response.Write("
Result:sign="+sign); } returnView(); } publicstaticstringGetMD5(strings,string_input_charset) { //////与ASP兼容的MD5加密算法 /// MD5md5=newMD5CryptoServiceProvider(); byte[]t=md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s)); StringBuildersb=newStringBuilder(32); for(inti=0;i///与ASP兼容的MD5加密算法 /// MD5md5=newMD5CryptoServiceProvider(); byte[]t=md5.ComputeHash(Encoding.GetEncoding("utf-8").GetBytes(s)); StringBuildersb=newStringBuilder(32); for(inti=0;i ///冒泡排序法 /// inti,j;//交换标志 stringtemp; boolexchange; for(i=0;i =i;j--) { if(System.String.CompareOrdinal(r[j+1],r[j])<0)//交换条件 { temp=r[j+1]; r[j+1]=r[j]; r[j]=temp; exchange=true;//发生了交换,故将交换标志置为真 } } if(!exchange)//本趟排序未发生交换,提前终止算法 { break; } } returnr; } //获取远程服务器ATN结果 publicStringGet_Http(Stringa_strUrl,inttimeout) { stringstrResult; try { HttpWebRequestmyReq=(HttpWebRequest)HttpWebRequest.Create(a_strUrl); myReq.Timeout=timeout; HttpWebResponseHttpWResp=(HttpWebResponse)myReq.GetResponse(); StreammyStream=HttpWResp.GetResponseStream(); StreamReadersr=newStreamReader(myStream,Encoding.Default); StringBuilderstrBuilder=newStringBuilder(); while(-1!=sr.Peek()) { strBuilder.Append(sr.ReadLine()); } strResult=strBuilder.ToString(); } catch(Exceptionexp) { strResult="错误:"+exp.Message; } returnstrResult; } } } 以上就是c#实现的支付宝支付的详细内容,更多关于c#支付宝支付的资料请关注毛票票其它相关文章!