.net接入支付寶的支付接口
H5手機(jī)網(wǎng)站接入支付寶的支付接口,推薦使用支付寶提供的SDK來(lái)快速開(kāi)發(fā)
我使用的是SDK開(kāi)發(fā)
引用命名空間
using Aop.Api; using Aop.Api.Request; using Aop.Api.Response; using Aop.Api.Util;
首頁(yè)需要定義一些常量
static string serverUrl = "https://openapi.alipaydev.com/gateway.do"; static string app_id = "**"; //開(kāi)發(fā)者的應(yīng)用ID static string format = "JSON"; static string charset = "utf-8"; static string sign_type = "RSA2"; //簽名格式 static string version = "1.0"; string UID = "2088102169707816";//賣(mài)家支付寶賬戶(hù)號(hào) //商戶(hù)私鑰 static string merchant_private_key = "***"; //支付寶公鑰 static string alipay_public_key = "***";
這里的app_id,merchant_private_key,alipay_public_key 我就沒(méi)有列出來(lái)了,獲取的方法需要自己去支付寶平臺(tái)完成一些操作進(jìn)行獲取
在用戶(hù)點(diǎn)擊網(wǎng)站付款時(shí),我們需要喚醒支付寶,來(lái)進(jìn)行支付
public string H5RequestPayWay(OrderPO order) { IAopClient client = new DefaultAopClient(serverUrl, app_id, merchant_private_key, format, version, sign_type, alipay_public_key, charset, false); AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); string address= "http://m." + PathLogic1.RootDomain; request.SetReturnUrl(address+ "/WebPay/AlipayPayResult");//同步請(qǐng)求 request.SetNotifyUrl(address + "/WebPay/AsyncPay");//異步請(qǐng)求 var lstDetail = Context.Data.OrderDetail.Where(x => x.OrderNo == order.OrderNo).ToSelectList(x=>new { x.SkuName}); StringBuilder sb = new StringBuilder(); for (int i = 0; i < lstDetail.Count(); i++) { sb.Append(lstDetail[i].SkuName + ","); } request.BizContent = "{" + "\"body\":\""+sb.ToString().Substring(0,sb.Length-1)+"\"," + "\"subject\":\"袋鼠巴巴商品支付\"," + "\"out_trade_no\":\""+order.OrderNo+"\"," + "\"timeout_express\":\"90m\"," + "\"total_amount\":"+(order.TotalAmount.Value+order.TotalFreight.Value)+"," + "\"product_code\":\"QUICK_WAP_PAY\"" + " }"; AlipayTradeWapPayResponse response = client.pageExecute(request); return response.Body; }
具體發(fā)送給支付寶的參數(shù),自行去查看
執(zhí)行方法后,買(mǎi)家輸入自己的支付寶賬號(hào)密碼進(jìn)行支付,支付成功的結(jié)果,支付寶會(huì)以post的方式異步請(qǐng)求你的SetNotifyUrl的地址
這個(gè)SetNotifyUrl的地址必須要外網(wǎng)可以訪(fǎng)問(wèn),支付寶的請(qǐng)求才能進(jìn)來(lái)
買(mǎi)家支付成功,商家修改訂單狀態(tài)和數(shù)據(jù)庫(kù)的操作,都在異步請(qǐng)求中執(zhí)行
同步請(qǐng)求
public ActionResult AlipayPayResult() { ViewBag.result = "success"; return View("PayResult"); }
/// <summary> /// 驗(yàn)證通知數(shù)據(jù)的正確性 /// </summary> /// <param name="out_trade_no"></param> /// <param name="total_amount"></param> /// <param name="seller_id"></param> /// <returns></returns> private SortedDictionary<string, string> GetRequestPost() { int i = 0; SortedDictionary<string, string> sArray = new SortedDictionary<string, string>(); NameValueCollection coll; //Load Form variables into NameValueCollection variable. coll = Request.Form; // Get names of all forms into a string array. String[] requestItem = coll.AllKeys; for (i = 0; i < requestItem.Length; i++) { sArray.Add(requestItem[i], Request.Form[requestItem[i]]); } return sArray; }
/// <summary> /// 驗(yàn)簽 /// </summary> /// <param name="inputPara"></param> /// <returns></returns> public Boolean Verify(SortedDictionary<string, string> inputPara) { Dictionary<string, string> sPara = new Dictionary<string, string>(); Boolean verifyResult = AlipaySignature.RSACheckV1(inputPara, alipay_public_key, charset,sign_type,false); return verifyResult; }
異步請(qǐng)求:
[HttpPost] public void AsyncPay() { SortedDictionary<string, string> sPara = GetRequestPost();//將post請(qǐng)求過(guò)來(lái)的參數(shù)傳化為SortedDictionary if (sPara.Count > 0) { AlipayTradeWayPayServer pay = new AlipayTradeWayPayServer(); Boolean VerifyResult = pay.Verify(sPara);//驗(yàn)簽if (VerifyResult) { try { //商戶(hù)訂單號(hào) string out_trade_no = Request.Form["out_trade_no"]; //支付寶交易號(hào) string trade_no = Request.Form["trade_no"]; //支付金額 decimal total_amount = Request.Form["total_amount"].ConvertType(Decimal.Zero); //實(shí)收金額 //decimal receipt_amount = Request.Form["receipt_amount"].ConvertType(Decimal.Zero); //交易狀態(tài) string trade_status = Request.Form["trade_status"]; //賣(mài)家支付寶賬號(hào) string seller_id = Request.Form["seller_id"]; //商品描述 string body = Request.Form["body"]; //交易創(chuàng)建時(shí)間 DateTime gmt_create = DateTime.Parse(Request.Form["gmt_create"]); //交易付款時(shí)間 DateTime gmt_payment = DateTime.Parse(Request.Form["gmt_payment"]); string appid = Request.Form["app_id"]; WriteError("驗(yàn)證參數(shù)開(kāi)始"); Boolean DataValidity = pay.CheckInform(out_trade_no, total_amount, seller_id, appid);//商家判斷參數(shù)時(shí)候是否匹配if (DataValidity) { if (Request.Form["trade_status"] == "TRADE_FINISHED") { AlipayWayPayPO model = CreateAlipayWayPay(out_trade_no, trade_no, trade_status, gmt_create, gmt_payment); pay.PaySuccess(out_trade_no, model, Server.MapPath("~/" + DateTime.Today.ToString("yyMMdd") + ".txt"));//修改訂單 //注意: //退款日期超過(guò)可退款期限后(如三個(gè)月可退款),支付寶系統(tǒng)發(fā)送該交易狀態(tài)通知 } else if (Request.Form["trade_status"] == "TRADE_SUCCESS") { AlipayWayPayPO model = CreateAlipayWayPay(out_trade_no, trade_no, trade_status, gmt_create, gmt_payment); pay.PaySuccess(out_trade_no, model, Server.MapPath("~/" + DateTime.Today.ToString("yyMMdd") + ".txt"));//修改訂單 //注意: //付款完成后,支付寶系統(tǒng)發(fā)送該交易狀態(tài)通知 } else { } //——請(qǐng)根據(jù)您的業(yè)務(wù)邏輯來(lái)編寫(xiě)程序(以上代碼僅作參考)—— Response.Write("success"); //請(qǐng)不要修改或刪除 ///////////////////////////////////////////////////////////////////////////////////////////////////////////// } } catch (Exception ex) { } } else//驗(yàn)證失敗 { Response.Write("fail"); } } else { Response.Write("無(wú)通知參數(shù)"); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決ASP.NET中Type.GetType方法總返回空的問(wèn)題
今天做練習(xí)的時(shí)候用到了Type.GetType方法。。 可是他總返回null。。2008-03-03巧用ASP.NET預(yù)編譯Web應(yīng)用程序規(guī)避調(diào)用延遲的方法
ASP.NET 1.x的開(kāi)發(fā)人員常常聽(tīng)到用戶(hù)抱怨首次調(diào)用應(yīng)用程序的時(shí)候會(huì)碰到初始化延遲。畢竟,初次請(qǐng)求會(huì)引發(fā)一個(gè)系列過(guò)程,包括運(yùn)行庫(kù)初始化、分析、把ASPX頁(yè)面編譯成中間語(yǔ)言、把方法即時(shí)編譯成本地代碼等等。2011-08-08asp.net使用ashx生成圖形驗(yàn)證碼的方法示例
這篇文章主要介紹了asp.net使用ashx生成圖形驗(yàn)證碼的方法,結(jié)合實(shí)例形式分析了asp.net生成圖形驗(yàn)證碼的步驟、實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07使用ASP.NET?Web?API構(gòu)建Restful?API
這篇文章介紹了使用ASP.NET?Web?API構(gòu)建Restful?API的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04Asp.net 基于Cookie簡(jiǎn)易的權(quán)限判斷
基于Cookie簡(jiǎn)易的權(quán)限判斷代碼,需要的朋友可以參考下。2010-01-01