.NET C#支付宝条码支付接口详解
网络编程 2021-07-04 22:41www.168986.cn编程入门
这篇文章主要为大家详细介绍了.NET C#支付宝条码支付接口的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
支付宝条码支付接口使用,供大家参考,具体内容如下
应用场景实例
收银员使用扫码设备读取用户支付宝钱包“付款码”后,将二维码或条码信息通过本接口上送至支付宝发起支付。
支付宝提供3种开发语言的SDK,选择自己的开发语言下载,项目中会有很多示例。本文选择.NET2010版本。
将SDK项目中的AopSdk.dll文件引用到自己的项目中。
支付类代码
简略版 数据需自行获取
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) { //直接确认,否则打不开 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 { //请根据实际请求需要_content参数 #region _content参数 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"; //支付授权码 orderList.auth_code = txtPaymentCode; //卖家支付宝用户ID orderList.seller_id = ""; //订单总金额 //orderList.total_amount = osaled_amount.ToString("#0.00"); //商品明细 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; //订单标题 orderList.subject = goodsName; //订单描述 orderList.body = ""; //商户操作员编号 orderList.operator_id = user.user_employeeNo; //商户门店编号 orderList.store_id = ""; //支付宝店铺编号 orderList.alipay_store_id = ""; //机具终端编号 orderList.terminal_id = ""; //支付超时时间 string expire_time = System.DateTime.Now.AddMinutes(30).ToString("yyyy-MM-dd HH:mm:ss"); orderList.time_expire = expire_time; #endregion _content参数 string _content = FormatToJson.Serialize(orderList); StoreEntity store = StoreBLL.GetStoreEntityByStore_Code(user.user_pany); //开发者的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(_content); string result = payResponse.Body; if (payResponse != null) { switch (payResponse.Code) { case ToAlipayResultCode.SUCCESS://支付成功 //业务处理 resp.Message = "支付成功"; resp.Status = true; break; case ToAlipayResultCode.INRROCESS://业务处理中 StringBuilder sb1 = new StringBuilder(); sb1.Append("{\"out_trade_no\":\"" + out_trade_no + "\"}"); //调用查询接口需要进行轮询订单支付结果 AlipayTradeQueryResponse queryResponse = LoopQuery(sb1.ToString()); //用订单号trade_no进行轮询也是可以的。 if (queryResponse != null) { if (queryResponse.Code == ToAlipayResultCode.SUCCESS) { //支付成功或交易结束 if (queryResponse.TradeStatus == "TRADE_SUCCESS" || queryResponse.TradeStatus == "TRADE_FINISHED") { //业务处理 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 = "操作失败,未查询到订单信息,请联系管理员!"; resp.Status = false; } } catch (Exception ex) { ExceptionLog.ToAlipayLog(ex.Message);//记录日志 resp.Message = ex.Message; resp.Status = false; } return resp; } private static AlipayTradePayResponse Pay(string _content) { AlipayTradePayRequest payRequst = new AlipayTradePayRequest(); payRequst.BizContent = _content; AlipayTradePayResponse payResponse = client.Execute(payRequst); return payResponse; } private static AlipayTradeCancelResponse Cancel(string _content) { AlipayTradeCancelRequest cancelRequest = new AlipayTradeCancelRequest(); cancelRequest.BizContent = _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 = _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 _content) { AlipayTradeQueryRequest payRequst = new AlipayTradeQueryRequest(); payRequst.BizContent = _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 + "\"}"); _content = param.ToString(); Cancel(_content); } return payResponse; } }
前端效果图
扫描枪自动提交,input输入框内“onkeyup=()”方法即可。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程