您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān).NET C#支付寶條碼支付接口的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
應(yīng)用場景實(shí)例
收銀員使用掃碼設(shè)備讀取用戶支付寶錢包“付款碼”后,將二維碼或條碼信息通過本接口上送至支付寶發(fā)起支付。
支付寶提供3種開發(fā)語言的SDK,選擇自己的開發(fā)語言下載,項(xiàng)目中會有很多示例。本文選擇.NET2010版本。
將SDK項(xiàng)目中的AopSdk.dll文件引用到自己的項(xiàng)目中。
支付類代碼
簡略版 數(shù)據(jù)需自行獲取
public class ToAlipayBLL { private static readonly ToAlipayDAL dal = new ToAlipayDAL(); static IAopClient client = null; public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { //直接確認(rèn),否則打不開 return true; } /// <summary> /// 支付寶條碼支付 /// </summary> /// <param name="osaleh_osalehID"></param> /// <param name="txtPaymentCode">付款碼</param> /// <param name="orderType"></param> /// <param name="user"></param> /// <returns></returns> public static ResponseDTO GetAlipayRequestExecute(string osaleh_osalehID, string str_osaled_osaledID, string txtPaymentCode, string orderType, UsersEntity user) { ResponseDTO resp = new ResponseDTO(); try { //請根據(jù)實(shí)際請求需要biz_content參數(shù) #region biz_content參數(shù) OrderListModel orderList = new OrderListModel(); List<ProductDetailModel> goodsList = new List<ProductDetailModel>(); StringBuilder param = new StringBuilder(); //商戶訂單號 string out_trade_no = System.DateTime.Now.ToString("yyyyMMddHHmmss") + "0000" + osaleh_osalehID; //商戶唯一訂單號 orderList.out_trade_no = out_trade_no; //支付場景 orderList.scene = "bar_code"; //支付授權(quán)碼 orderList.auth_code = txtPaymentCode; //賣家支付寶用戶ID orderList.seller_id = ""; //訂單總金額 //orderList.total_amount = osaled_amount.ToString("#0.00"); //商品明細(xì) string goodsName = string.Empty; bool IsFlag = true; foreach (var item in OsaledList) { ProductDetailModel detailModel = new ProductDetailModel(); detailModel.goods_id = "0"; detailModel.goods_name = "default"; detailModel.quantity = ((int)item.osaled_qty).ToString(); detailModel.price = item.osaled_amount.ToString("#0.00"); detailModel.goods_category = ""; goodsList.Add(detailModel); } orderList.goods_detail = goodsList; //訂單標(biāo)題 orderList.subject = goodsName; //訂單描述 orderList.body = ""; //商戶操作員編號 orderList.operator_id = user.user_employeeNo; //商戶門店編號 orderList.store_id = ""; //支付寶店鋪編號 orderList.alipay_store_id = ""; //機(jī)具終端編號 orderList.terminal_id = ""; //支付超時時間 string expire_time = System.DateTime.Now.AddMinutes(30).ToString("yyyy-MM-dd HH:mm:ss"); orderList.time_expire = expire_time; #endregion biz_content參數(shù) string biz_content = FormatToJson.Serialize(orderList); StoreEntity store = StoreBLL.GetStoreEntityByStore_Code(user.user_company); //開發(fā)者的AppId string alipay_appId = ""; ConfigHelper.alipay_merchant_private_key = string.Format(ConfigHelper.alipay_merchant_private_key, store.Area_Code); ConfigHelper.alipay_public_key = string.Format(ConfigHelper.alipay_public_key, store.Area_Code); ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); client = new DefaultAopClient(ConfigHelper.alipay_serverUrl, alipay_appId, ConfigHelper.alipay_merchant_private_key, "json", ConfigHelper.alipay_version, ConfigHelper.alipay_sign_type, ConfigHelper.alipay_public_key, ConfigHelper.alipay_charset); AlipayTradePayResponse payResponse = Pay(biz_content); string result = payResponse.Body; if (payResponse != null) { switch (payResponse.Code) { case ToAlipayResultCode.SUCCESS://支付成功 //業(yè)務(wù)處理 resp.Message = "支付成功"; resp.Status = true; break; case ToAlipayResultCode.INRROCESS://業(yè)務(wù)處理中 StringBuilder sb1 = new StringBuilder(); sb1.Append("{\"out_trade_no\":\"" + out_trade_no + "\"}"); //調(diào)用查詢接口需要進(jìn)行輪詢訂單支付結(jié)果 AlipayTradeQueryResponse queryResponse = LoopQuery(sb1.ToString()); //用訂單號trade_no進(jìn)行輪詢也是可以的。 if (queryResponse != null) { if (queryResponse.Code == ToAlipayResultCode.SUCCESS) { //支付成功或交易結(jié)束 if (queryResponse.TradeStatus == "TRADE_SUCCESS" || queryResponse.TradeStatus == "TRADE_FINISHED") { //業(yè)務(wù)處理 resp.Message = "支付成功"; resp.Status = true; } else { //支付超時 resp.Message = "支付超時"; resp.Status = false; } } else { //支付失敗 resp.Message = "支付失敗,錯誤代碼:" + queryResponse.SubCode + "。描述:" + queryResponse.SubMsg; resp.Status = false; } } break; case ToAlipayResultCode.FAIL: StringBuilder sb2 = new StringBuilder(); sb2.Append("{\"out_trade_no\":\"" + out_trade_no + "\"}"); AlipayTradeCancelResponse cancelResponse = Cancel(sb2.ToString()); if (!string.IsNullOrEmpty(cancelResponse.SubCode)) { resp.Message = "支付失敗,錯誤代碼:" + cancelResponse.SubCode + "。描述:" + cancelResponse.SubMsg; } else { resp.Message = "支付失敗,錯誤代碼:" + payResponse.SubCode + "。描述:" + payResponse.SubMsg; } //支付失敗 resp.Status = false; break; } } } else { resp.Message = "操作失敗,未查詢到訂單信息,請聯(lián)系管理員!"; resp.Status = false; } } catch (Exception ex) { ExceptionLog.ToAlipayLog(ex.Message);//記錄日志 resp.Message = ex.Message; resp.Status = false; } return resp; } private static AlipayTradePayResponse Pay(string biz_content) { AlipayTradePayRequest payRequst = new AlipayTradePayRequest(); payRequst.BizContent = biz_content; AlipayTradePayResponse payResponse = client.Execute(payRequst); return payResponse; } private static AlipayTradeCancelResponse Cancel(string biz_content) { AlipayTradeCancelRequest cancelRequest = new AlipayTradeCancelRequest(); cancelRequest.BizContent = biz_content; AlipayTradeCancelResponse cancelResponse = client.Execute(cancelRequest); if (null != cancelResponse) { if (cancelResponse.Code == ToAlipayResultCode.FAIL && cancelResponse.RetryFlag == "Y") { // 新開一個線程重試撤銷 ParameterizedThreadStart ParStart = new ParameterizedThreadStart(cancelOrderRetry); Thread myThread = new Thread(ParStart); object o = biz_content; myThread.Start(o); } } return cancelResponse; } private static void cancelOrderRetry(object o) { int retryCount = 10; for (int i = 0; i < retryCount; ++i) { Thread.Sleep(5000); AlipayTradeCancelRequest cancelRequest = new AlipayTradeCancelRequest(); cancelRequest.BizContent = o.ToString(); AlipayTradeCancelResponse cancelResponse = client.Execute(cancelRequest); if (null != cancelResponse) { if (cancelResponse.Code == ToAlipayResultCode.FAIL) { if (cancelResponse.RetryFlag == "N") { break; } } if ((cancelResponse.Code == ToAlipayResultCode.SUCCESS)) { break; } } } } private static AlipayTradeQueryResponse LoopQuery(string biz_content) { AlipayTradeQueryRequest payRequst = new AlipayTradeQueryRequest(); payRequst.BizContent = biz_content; Dictionary<string, string> paramsDict = (Dictionary<string, string>)payRequst.GetParameters(); AlipayTradeQueryResponse payResponse = null; for (int i = 1; i <= 6; i++) { Thread.Sleep(5000); payResponse = client.Execute(payRequst); if (string.Compare(payResponse.Code, ToAlipayResultCode.SUCCESS, false) == 0) { if (payResponse.TradeStatus == "TRADE_FINISHED" || payResponse.TradeStatus == "TRADE_SUCCESS" || payResponse.TradeStatus == "TRADE_CLOSED") // return payResponse; break; } } //未付款交易超時或等待超時。 if (payResponse.Code == ToAlipayResultCode.FAIL || payResponse.TradeStatus == "TRADE_CLOSED" || payResponse.TradeStatus == "WAIT_BUYER_PAY") { //撤銷訂單 StringBuilder param = new StringBuilder(); param.Append("{\"out_trade_no\":\"" + payResponse.OutTradeNo + "\"}"); biz_content = param.ToString(); Cancel(biz_content); } return payResponse; } }
前端效果圖
掃描槍自動提交,input輸入框內(nèi)“onkeyup=()”方法即可。
感謝各位的閱讀!關(guān)于“.NET C#支付寶條碼支付接口的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。