手機網站支付接口,請參考支付寶官方文檔:https://b.alipay.com/signing/productSet.htm?navKey=all
1.需要提供應用ID、應用私鑰、支付寶公鑰
2.代碼實現:
下載官方SDK,並編譯引用到項目中去。
初始化對象:private readonly IAopClient _client= new DefaultAopClient("https://openapi.alipay.com/gateway.do", "應用ID", "應用私鑰", "json", "1.0", "簽名類型", "支付寶公鑰", "字符編碼格式,支持GBK和UTF-8", false);
支付接口:
1>調用支付寶支付網關
/// <summary> /// 手機網站支付 /// </summary> /// <param name="postProcessPaymentRequest"></param> private void AlipayTradeWapPayment(訂單實體 order) { if (order == null) { Log.Error(this.GetType().ToString(),"訂單數據不能為空"); return; } Log.Info(this.GetType().ToString(), "AlipayTradeWapPayment Start......"); string responseText = string.Empty; try { #region 調用支付寶支付接口 AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); request.SetReturnUrl(http://XXXXXXXXX/foxconn/AliPayment/AppPaymentReturn);//頁面跳轉同步通知頁面路徑 request.SetNotifyUrl("http://XXXXXXXXX/foxconn/AliPayment/AppPaymentNotify");//服務器異步通知頁面路徑 request.BizContent = "{" + " \"body\":\"\"," + " \"subject\":\"" + order.訂單名稱述 + "\"," + " \"out_trade_no\":\"" + order.訂單編號 + "\"," + " \"timeout_express\":\"90m\"," + " \"total_amount\":\"" + order.訂單金額 + "\"," + " \"seller_id\":\"" + _alipayPaymentSettings.SellerId + "\"," + " \"product_code\":\"QUICK_WAP_PAY\"" + " }"; AlipayTradeWapPayResponse response = _client.pageExecute(request); responseText = response.Body; _httpContext.Response.Clear(); _httpContext.Response.Write(responseText); _httpContext.Response.End(); #endregion } catch (Exception ex) { Log.Error(this.GetType().ToString(), "Exception: " + ex.Message); } Log.Info(this.GetType().ToString(), "AlipayTradeWapPayment End......"); }
2>接收支付寶同步通知(get)
/// <summary> /// 支付寶手機網站支付同步通知 /// </summary> /// <returns></returns> [HttpGet] public ActionResult AppPaymentReturn() { string result = string.Empty; Log.Info(this.GetType().ToString(), "AppPaymentReturn End......"); try { SortedDictionary<string, string> sPara = GetRequestGet();//將異步通知中收到的所有參數都存放到map中 if (sPara.Count > 0)//判斷是否有帶返回參數 { bool verifyResult = AlipaySignature.RSACheckV1(sPara, _aliPaymentSettings.App_Alipay_Public_Key, _aliPaymentSettings.App_Charset, _aliPaymentSettings.App_SignType, false); //調用SDK驗證簽名 if (verifyResult) { //驗簽成功后,按照支付結果異步通知中的描述,對支付結果中的業務內容進行二次校驗,校驗成功后在response中返回success並繼續商戶自身業務處理,校驗失敗返回failure //商戶訂單號 string out_trade_no = Request.QueryString["out_trade_no"]; //支付寶交易號 string trade_no = Request.QueryString["trade_no"]; //交易狀態 string trade_status = Request.QueryString["trade_status"]; if (Request.QueryString["trade_status"] == "TRADE_FINISHED" || Request.QueryString["trade_status"] == "TRADE_SUCCESS") { //判斷該筆訂單是否在商戶網站中已經做過處理 //如果沒有做過處理,根據訂單號(out_trade_no)在商戶網站的訂單系統中查到該筆訂單的詳細,並執行商戶的業務程序 //如果有做過處理,不執行商戶的業務程序 } else { result = "trade_status=" + Request.QueryString["trade_status"]; } //打印頁面 result = "驗證成功"; } else//驗證失敗 { //驗簽失敗則記錄異常日志,並在response中返回failure. result = "驗證失敗"; } } else { result = "無返回參數"; } Log.Info(this.GetType().ToString(), "result:" + result); } catch (Exception ex) { Log.Info(this.GetType().ToString(), "Exception:" + ex.Message); } Log.Info(this.GetType().ToString(), "AppPaymentReturn End......"); return 跳轉頁面; }
3>接收支付寶異步通知(post)
/// <summary> /// 支付寶手機網站支付異步通知 /// </summary> /// <returns></returns> [ValidateInput(false)] public ActionResult AppPaymentNotify() { string result = string.Empty; try { Log.Info(this.GetType().ToString(), "AppPaymentNotify Start......"); SortedDictionary<string, string> sPara = GetRequestPost(); if (sPara.Count > 0)//判斷是否有帶返回參數 { bool verifyResult = AlipaySignature.RSACheckV1(sPara, _aliPaymentSettings.App_Alipay_Public_Key, _aliPaymentSettings.App_Charset, _aliPaymentSettings.App_SignType, false); //調用SDK驗證簽名 Log.Info(this.GetType().ToString(), "verifyResult:" + verifyResult); if (verifyResult)//驗證成功 { bool isRefund = false; string batch_no = string.Empty; if(sPara.ContainsKey("out_biz_no")) { isRefund = true; batch_no = sPara["out_biz_no"]; } //商戶訂單號 string out_trade_no = Request.Form["out_trade_no"]; //支付寶交易號 string trade_no = Request.Form["trade_no"]; //交易狀態 string trade_status = Request.Form["trade_status"]; string buyer_id = Request.Form["buyer_id"]; string buyer_emial = Request.Form["buyer_logon_id"]; if (string.IsNullOrEmpty(out_trade_no)) { throw new Exception("商戶訂單號不能為空"); } Log.Debug(this.GetType().ToString(), string.Format("out_trade_no:【{0}】-trade_no:【{1}】-trade_status:【{2}】", out_trade_no, trade_no, trade_status)); if (trade_status == "TRADE_FINISHED") { //判斷該筆訂單是否在商戶網站中已經做過處理 //如果沒有做過處理,根據訂單號(out_trade_no)在商戶網站的訂單系統中查到該筆訂單的詳細,並執行商戶的業務程序 //請務必判斷請求時的total_fee、seller_id與通知時獲取的total_fee、seller_id為一致的 //如果有做過處理,不執行商戶的業務程序 //處理業務邏輯 //注意: //退款日期超過可退款期限后(如三個月可退款),支付寶系統發送該交易狀態通知 } else if (trade_status == "TRADE_SUCCESS") { //判斷該筆訂單是否在商戶網站中已經做過處理 //如果沒有做過處理,根據訂單號(out_trade_no)在商戶網站的訂單系統中查到該筆訂單的詳細,並執行商戶的業務程序 //請務必判斷請求時的total_fee、seller_id與通知時獲取的total_fee、seller_id為一致的 //如果有做過處理,不執行商戶的業務程序 if (isRefund) { //處理退款業務 Log.Info(this.GetType().ToString(), "退款成功"); } else { //處理訂單業務 Log.Info(this.GetType().ToString(), "付款成功"); } //注意: //付款完成后,支付寶系統發送該交易狀態通知 } else if (trade_status == "TRADE_CLOSED")//未付款交易超時關閉,或支付完成后全額退款 { if (isRefund) { //處理退款業務 Log.Info(this.GetType().ToString(), "退款成功"); } } //——請根據您的業務邏輯來編寫程序(以上代碼僅作參考)—— Response.Write("success"); //請不要修改或刪除 ///////////////////////////////////////////////////////////////////////////////////////////////////////////// } else//驗證失敗 { Response.Write("failure"); } } else { Response.Write("無通知參數"); Log.Info(this.GetType().ToString(), "result:無通知參數"); } } catch (Exception ex) { Response.Write("failure"); Log.Info(this.GetType().ToString(), "Exception:" + ex.Message); } Response.End(); Log.Info(this.GetType().ToString(), "AppPaymentNotify End......"); return Content("fail"); }
退款接口:
/// <summary> /// 退款 /// </summary> /// <param name="refundPaymentRequest"></param> /// <returns></returns> private bool AlipayTradeWapRefund(退款實體 refundModel) { var result = false; if (refundModel == null) { Log.Error(this.GetType().ToString(), "退款數據不能為空"); return result; } string responseText = string.Empty; try { #region 調用支付寶退款接口 AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();//創建API對應的request類 //request.SetNotifyUrl(_alipayPaymentSettings.App_RefundNotifyUrl);//服務器異步通知頁面路徑 string batch_no = refundModel.退款編號; request.BizContent = "{" + " \"out_trade_no\":\"" + refundModel.訂單編號 + "\"," + " \"trade_no\":\"" + refundModel.交易號 + "\"," + " \"out_request_no\":\"" + batch_no + "\"," + " \"refund_amount\":\"" + refundModel.退款金額 + "\"" + " }";//設置業務參數 AlipayTradeRefundResponse response = _client.Execute(request);//通過alipayClient調用API,獲得對應的response類 //根據response中的結果繼續業務邏輯處理 if (response.Code == "10000")//接口調用成功 { } #endregion } catch (Exception ex) { responseText = ex.Message; Log.Error(this.GetType().ToString(), "Exception: " + ex.Message); } Log.Info(this.GetType().ToString(), "Refund End......"); return result; }
查詢接口:
/// <summary> /// 查詢訂單在支付寶的狀態 /// </summary> /// <param name="queryOrderRequest"></param> /// <returns></returns> private bool AlipayTradeWapQuery(訂單查詢實體 queryOrder) { var result = false; if (queryOrder == null) { Log.Error(this.GetType().ToString(), "查詢數據不能為空"); return result; } Log.Info(this.GetType().ToString(), "AlipayTradeWapQuery Start......"); string responseText = string.Empty; try { #region 調用支付寶支付查詢接口 AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); request.BizContent = "{" + " \"out_trade_no\":\"" + queryOrder.訂單編號 + "\"," + " \"trade_no\":\"" + queryOrder.交易號 + "\"" + " }"; AlipayTradeQueryResponse response = _client.Execute(request); string tradeStatus = response.TradeStatus; responseText = tradeStatus; if (tradeStatus == "TRADE_SUCCESS" || tradeStatus == "TRADE_CLOSED" || tradeStatus == "TRADE_FINISHED") { //處理支付業務 result=true; Log.Info(this.GetType().ToString(), "支付成功"); } else { Log.Info(this.GetType().ToString(), "支付失敗"); } #endregion } catch (Exception ex) { responseText = ex.Message; Log.Error(this.GetType().ToString(), "Exception: " + ex.Message); } Log.Debug(this.GetType().ToString(), string.Format("支付寶手機網站支付查詢跟蹤信息。GetForm:{0}", responseText); Log.Info(this.GetType().ToString(), "AlipayTradeWapQuery End......"); return result; }
