服务器之家

服务器之家 > 正文

.NET C#支付宝条码支付接口详解

时间:2020-05-27 12:19     来源/作者:下一秒_待续

支付宝条码支付接口使用,供大家参考,具体内容如下

应用场景实例

收银员使用扫码设备读取用户支付宝钱包“付款码”后,将二维码或条码信息通过本接口上送至支付宝发起支付。

SDK下载

支付宝提供3种开发语言的SDK,选择自己的开发语言下载,项目中会有很多示例。本文选择.NET2010版本。

将SDK项目中的AopSdk.dll文件引用到自己的项目中。

.NET C#支付宝条码支付接口详解

支付类代码

简略版 数据需自行获取 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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;
 
 }
}

前端效果图

.NET C#支付宝条码支付接口详解

扫描枪自动提交,input输入框内“onkeyup=()”方法即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/chinaplan/article/details/49180367

相关文章

热门资讯

歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
返回顶部