.NET C#支付宝条码支付接口详解
程序员文章站
2023-02-21 14:31:56
支付宝条码支付接口使用,供大家参考,具体内容如下
应用场景实例
收银员使用扫码设备读取用户支付宝钱包“付款码”后,将二维码或条码信息通过本接口上送至支付宝发起支付。...
支付宝条码支付接口使用,供大家参考,具体内容如下
应用场景实例
收银员使用扫码设备读取用户支付宝钱包“付款码”后,将二维码或条码信息通过本接口上送至支付宝发起支付。
支付宝提供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 { //请根据实际请求需要biz_content参数 #region biz_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 biz_content参数 string biz_content = formattojson.serialize(orderlist); storeentity store = storebll.getstoreentitybystore_code(user.user_company); //开发者的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://支付成功 //业务处理 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 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输入框内“onkeyup=()”方法即可。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 简单网络编程如何用python来实现