支付宝条码支付接口使用,供大家参考,具体内容如下
应用场景实例
收银员使用扫码设备读取用户支付宝钱包“付款码”后,将二维码或条码信息通过本接口上送至支付宝发起支付。
SDK下载
支付宝提供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=()”方法即可。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“.NET C#支付宝条码支付接口详解”评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。