.net中微信、支付宝回调

需求:自助机调用接口生成二维码,用户扫描二维码付款后,回调方法里写入到数据库,自助机轮询查数据库判断用户是否付款。

  1 using bk.Services.Log;
  2 using bk.web.Common.Alipay;
  3 using bk.web.Common.Weixin;
  4 using System.Collections.Generic;
  5 using System.Collections.Specialized;
  6 using System.Text;
  7 using System.Web;
  8 using System.Web.Mvc;
  9 using Com.Alipay;
 10 using Com.Alipay.Business;
 11 using Com.Alipay.Domain;
 12 using Com.Alipay.Model;
 13 using System;
 14 using bk.web.Models;
 15 using bk.Core.Domain;
 16 using System.IO;
 17 using bk.Core;
 18 using bk.Services.Admin;
 19 using System.Threading.Tasks;
 20 using bk.Core.Infrastructure;
 21 using System.Linq;
 22 using System.Configuration;
 23 
 24 namespace bk.web.Controllers
 25 {
 26     public class PayNotifyController : Controller
 27     {
 28         private readonly IAgent_User_ImgService _userService;
 29         private readonly IAgentOperationPayService _payService;
 30         private readonly IAgentOperationService _operateService;
 31         private readonly IAgentDevice_PayService _amountService;
 32         public PayNotifyController(
 33             IAgent_User_ImgService userService,
 34             IAgentOperationPayService payService,
 35             IAgentOperationService operateService,
 36             IAgentDevice_PayService amountService)
 37         {
 38             this._userService = userService;
 39             _payService = payService;
 40             _operateService = operateService;
 41             _amountService = amountService;
 42         }
 43 
 44         // GET: PayNotify
 45         public ActionResult Index()
 46         {
 47             return View();
 48         }
 49 
 50         #region 微信支付回调
 51         /// <summary>
 52         /// 支付结果通知回调处理类
 53         /// 负责接收微信支付后台发送的支付结果并对订单有效性进行验证,将验证结果反馈给微信支付后台
 54         /// </summary>
 55         public void wxNotify()
 56         {
 57             Log.Info(this.GetType().ToString(), "微信支付回调开始");
 58 
 59             WxPayData notifyData = GetNotifyData();
 60             //检查支付结果中transaction_id是否存在
 61             if (!notifyData.IsSet("transaction_id"))
 62             {
 63                 //若transaction_id不存在,则立即返回结果给微信支付后台
 64                 WxPayData res = new WxPayData();
 65                 res.SetValue("return_code", "FAIL");
 66                 res.SetValue("return_msg", "支付结果中微信订单号不存在");
 67                 Log.Error(this.GetType().ToString(), "The Pay result is error : " + res.ToXml());
 68                 Response.Write(res.ToXml());
 69                 Response.End();
 70             }
 71 
 72             string transaction_id = notifyData.GetValue("transaction_id").ToString();
 73 
 74             //查询订单,判断订单真实性
 75             if (!QueryOrder(transaction_id))
 76             {
 77                 //若订单查询失败,则立即返回结果给微信支付后台
 78                 WxPayData res = new WxPayData();
 79                 res.SetValue("return_code", "FAIL");
 80                 res.SetValue("return_msg", "订单查询失败");
 81                 Log.Error(this.GetType().ToString(), "Order query failure : " + res.ToXml());
 82                 Response.Write(res.ToXml());
 83                 Response.End();
 84             }
 85             //查询订单成功
 86             else
 87             {
 88                 //更新数据库
 89                 //WriteLog.Debug("notifyData begin:");
 90                 //var dicValue = notifyData.GetValues();
 91                 //foreach (var item in dicValue)
 92                 //{
 93                 //    WriteLog.Debug(item.Key + " : " + item.Value);
 94                 //}
 95                 //WriteLog.Debug("notifyData end;");
 96                 string out_trade_no = notifyData.GetValue("out_trade_no").ToString();
 97                 UpdatePayData(1, out_trade_no, transaction_id);
 98 
 99                 WxPayData res = new WxPayData();
100                 res.SetValue("return_code", "SUCCESS");
101                 res.SetValue("return_msg", "OK");
102                 Log.Info(this.GetType().ToString(), "order query success : " + res.ToXml());
103 
104                 Response.Write(res.ToXml());
105                 Response.End();
106             }
107 
108         }
109 
110         private void UpdatePayData(int payType, string out_trade_no, string transaction_id)
111         {
112             //更新AgentOperationPay
113             M_AgentOperationPay m = _payService.GetByOrderNo(out_trade_no);
114             if (m == null)
115             {
116 
117             }
118             else
119             {
120                 m.SerialNo = transaction_id;
121                 m.Payment = payType;
122                 m.PayStatus = 1;
123                 _payService.Update(m);
124 
125                 //更新AgentOperation
126                 M_AgentOperation om = _operateService.GetByOperationNo(m.OperationNo);
127                 om.Status = 1;
128                 _operateService.Update(om);
129             }
130         }
131 
132         /// <summary>
133         /// 接收从微信支付后台发送过来的数据并验证签名
134         /// </summary>
135         /// <returns>微信支付后台返回的数据</returns>
136         public WxPayData GetNotifyData()
137         {
138             //接收从微信后台POST过来的数据
139             System.IO.Stream s = Request.InputStream;
140             int count = 0;
141             byte[] buffer = new byte[1024];
142             StringBuilder builder = new StringBuilder();
143             while ((count = s.Read(buffer, 0, 1024)) > 0)
144             {
145                 builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
146             }
147             s.Flush();
148             s.Close();
149             s.Dispose();
150 
151             WriteLog.Debug("Receive data from WeChat : " + builder.ToString());
152 
153             //转换数据格式并验证签名
154             WxPayData data = new WxPayData();
155             try
156             {
157                 data.FromXml(builder.ToString());
158             }
159             catch (WxPayException ex)
160             {
161                 //若签名错误,则立即返回结果给微信支付后台
162                 WxPayData res = new WxPayData();
163                 res.SetValue("return_code", "FAIL");
164                 res.SetValue("return_msg", ex.Message);
165                 WriteLog.Debug("Sign check error : " + res.ToXml());
166                 Response.Write(res.ToXml());
167                 Response.End();
168             }
169 
170             WriteLog.Debug("Check sign success");
171             return data;
172         }
173         //查询订单
174         private bool QueryOrder(string transaction_id)
175         {
176             WxPayData req = new WxPayData();
177             req.SetValue("transaction_id", transaction_id);
178             WxPayData res = WxPayApi.OrderQuery(req);
179             if (res.GetValue("return_code").ToString() == "SUCCESS" &&
180                 res.GetValue("result_code").ToString() == "SUCCESS")
181             {
182                 return true;
183             }
184             else
185             {
186                 return false;
187             }
188         }
189         #endregion
190 
191         #region 支付宝回调
192         public void aliNotify()
193         {
194             WriteLog.Debug("支付宝回调开始");
195             WriteLog.Debug("aliNotify : begin");
196             SortedDictionary<string, string> sPara = GetRequestPost();
197 
198             if (sPara.Count > 0)//判断是否有带返回参数
199             {
200                 //Notify aliNotify = new Notify();
201                 Com.Alipay.Notify aliNotify = new Com.Alipay.Notify(Config.charset, Config.sign_type, Config.pid, Config.mapiUrl, Config.alipay_public_key);
202 
203                 //对异步通知进行验签
204                 bool verifyResult = aliNotify.Verify(sPara, Request.Form["notify_id"], Request.Form["sign"]);
205                 //对验签结果
206                 //bool isSign = Aop.Api.Util.AlipaySignature.RSACheckV2(sPara, Config.alipay_public_key ,Config.charset,Config.sign_type,false );
207 
208                 if (verifyResult && CheckParams()) //验签成功 && 关键业务参数校验成功
209                 {
210                     /////////////////////////////////////////////////////////////////////////////////////////////////////////////
211                     //请在这里加上商户的业务逻辑程序代码
212 
213 
214                     //——请根据您的业务逻辑来编写程序(以下代码仅作参考)——
215                     //获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表
216 
217                     //商户订单号
218                     string out_trade_no = Request.Form["out_trade_no"];
219 
220 
221                     //支付宝交易号
222                     string trade_no = Request.Form["trade_no"];
223 
224                     //交易状态
225                     //在支付宝的业务通知中,只有交易通知状态为TRADE_SUCCESS或TRADE_FINISHED时,才是买家付款成功。
226                     string trade_status = Request.Form["trade_status"];
227 
228                     //更新数据库
229                     if (trade_status == "TRADE_SUCCESS" || trade_status == "TRADE_FINISHED")
230                     {
231                         UpdatePayData(2, out_trade_no, trade_no);
232                     }
233                     
234                     //判断是否在商户网站中已经做过了这次通知返回的处理
235                     //如果没有做过处理,那么执行商户的业务程序
236                     //如果有做过处理,那么不执行商户的业务程序
237 
238                     WriteLog.Debug("aliNotify : success");
239                     Response.Write("success");  //请不要修改或删除
240 
241                     //——请根据您的业务逻辑来编写程序(以上代码仅作参考)——
242 
243                     /////////////////////////////////////////////////////////////////////////////////////////////////////////////
244                 }
245                 else//验证失败
246                 {
247                     WriteLog.Debug("aliNotify : fail");
248                     Response.Write("fail");
249                 }
250             }
251             else
252             {
253                 WriteLog.Debug("aliNotify : 无通知参数");
254                 Response.Write("无通知参数");
255             }
256         }
257         /// <summary>
258         /// 获取支付宝POST过来通知消息,并以“参数名=参数值”的形式组成数组
259         /// </summary>
260         /// <returns>request回来的信息组成的数组</returns>
261         public SortedDictionary<string, string> GetRequestPost()
262         {
263             int i = 0;
264             SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
265             NameValueCollection coll;
266             //Load Form variables into NameValueCollection variable.
267             coll = Request.Form;
268 
269             // Get names of all forms into a string array.
270             string[] requestItem = coll.AllKeys;
271 
272             for (i = 0; i < requestItem.Length; i++)
273             {
274                 sArray.Add(requestItem[i], Request.Form[requestItem[i]]);
275                 WriteLog.Debug("aliNotify : GetRequestPost, key="+ requestItem[i] + ",value="+ Request.Form[requestItem[i]]);
276             }
277 
278             return sArray;
279         }
280 
281 
282         /// <summary>
283         /// 对支付宝异步通知的关键参数进行校验
284         /// </summary>
285         /// <returns></returns>
286         private bool CheckParams()
287         {
288             bool ret = true;
289 
290             //获得商户订单号out_trade_no
291             string out_trade_no = Request.Form["out_trade_no"];
292             //TODO 商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,
293 
294             //获得支付总金额total_amount
295             string total_amount = Request.Form["total_amount"];
296             //TODO 判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额),
297 
298             //获得卖家账号seller_email
299             string seller_email = Request.Form["seller_email"];
300             //TODO 校验通知中的seller_email(或者seller_id) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id / seller_email)
301 
302             //获得调用方的appid;
303             //如果是非授权模式,appid是商户的appid;如果是授权模式(token调用),appid是系统商的appid
304             string app_id = Request.Form["app_id"];
305             //TODO 验证app_id是否是调用方的appid;。
306 
307             //验证上述四个参数,完全吻合则返回参数校验成功
308             return ret;
309 
310         }
311 
312         #endregion
313 
314         #region 微信、支付宝生成二维码url
315         public JsonResult GetWxAliCodeUrl(WxAliPayRequestOnlyUrl req)
316         {
317             WriteLog.Debug("计时:开始时间=" + DateTime.Now.ToString());
318             WxAliPayResponse result = new WxAliPayResponse();
319 
320             #region 业务参数判断 和 签名判断--占不加业务
321 
322             //bool have_err = false;
323             //string err_msg = "缺少参数:";
324             //if (req == null)
325             //{
326             //    result.Code = "1000";
327             //    result.Msg = "传入参数为空";
328             //    return Json(result);
329             //}
330 
331             //if (string.IsNullOrEmpty(req.file_name))
332             //{
333             //    have_err = true;
334             //    err_msg += "文件名称不能为空";
335             //}
336             //if (string.IsNullOrEmpty(req.sign))
337             //{
338             //    have_err = true;
339             //    err_msg += "、签名不能为空";
340             //}
341             //if (string.IsNullOrEmpty(req.avatar))
342             //{
343             //    have_err = true;
344             //    err_msg += "、照片不能为空";
345             //}
346             //if (req.time_stamp <= 0)
347             //{
348             //    have_err = true;
349             //    err_msg += "、请传入正确的时间戳格式";
350             //}
351             //if (string.IsNullOrEmpty(req.SFZMHM))
352             //{
353             //    have_err = true;
354             //    err_msg += "身份证明号码不能为空";
355             //}
356             //if (string.IsNullOrEmpty(req.SFZMMC))
357             //{
358             //    have_err = true;
359             //    err_msg += "身份证明名称不能为空";
360             //}
361             //if (string.IsNullOrEmpty(req.SFZMLX))
362             //{
363             //    have_err = true;
364             //    err_msg += "身份证明类型不能为空";
365             //}
366             //if (req.Img_Type != 1 && req.Img_Type != 2 && req.Img_Type != 3)
367             //{
368             //    have_err = true;
369             //    err_msg += "照片类型格式不正确";
370             //}
371             //if (string.IsNullOrEmpty(req.OperationNo))
372             //{
373             //    have_err = true;
374             //    err_msg += "操作编号不能为空";
375             //}
376             //if (have_err)
377             //{
378             //    //commonHelper.write_log("1001图片接口上传失败", err_msg);
379             //    result.Code = "1001";
380             //    result.Msg = err_msg;
381             //    return Json(result);
382             //}
383             //string strtime_stamp = ConfigurationManager.AppSettings["time_stamp"];
384             //int intTimeStampCfg = 600;
385             //int.TryParse(strtime_stamp, out intTimeStampCfg);
386             //if (Math.Abs(CommonHelper.GetCurrentTimeStamp() - req.time_stamp) > intTimeStampCfg)
387             //{
388             //    //commonHelper.write_log("1002图片接口上传失败", "请求过期----当前时间:" + commonHelper.GetCurrentTimeStamp() + "------传入时间:" + model.time_stamp);
389             //    result.Code = "1002";
390             //    result.Msg = "请求过期";
391             //    return Json(result);
392             //}
393             //string api_user_key = ConfigurationManager.AppSettings["upload_img_key"]; 
394             //string mysign = string.Format("apikey={0}&time_stamp={1}&file_name={2}&operationNo={3}", api_user_key, req.time_stamp, req.file_name, req.OperationNo);
395             //mysign = CommonHelper.MD5Encrypt(mysign, 32);
396             //mysign = mysign.ToLower();
397             //WriteLog.Debug("GetWxAliCodeUrl: mysign="+ mysign + ", 签名参数sign="+ req.sign.ToLower());
398             //if (req.sign.ToLower() != mysign)
399             //{
400             //    //commonHelper.write_log("1003图片接口上传失败", "签名不匹配----传入的签名:" + model.sign.ToLower() + "------正确的签名:" + mysign);
401             //    result.Code = "1003";
402             //    result.Msg = "签名不匹配";
403             //    return Json(result);
404             //}
405             //WriteLog.Debug("业务参数判断 和 签名判断 成功");
406 
407             #endregion
408 
409             #region 判断参数
410             if (req == null)
411             {
412                 result.Code = "1000";
413                 result.Msg = "传入参数为空";
414                 return Json(result);
415             }
416             if (string.IsNullOrEmpty(req.deviceNo))
417             {
418                 result.Code = "1001";
419                 result.Msg = "设备号不能为空";
420                 return Json(result);
421             }
422             if (string.IsNullOrEmpty(req.busyCode))
423             {
424                 result.Code = "1002";
425                 result.Msg = "业务编号不能为空";
426                 return Json(result);
427             }
428             if (string.IsNullOrEmpty(req.OperationNo))
429             {
430                 result.Code = "1003";
431                 result.Msg = "操作编号不能为空";
432                 return Json(result);
433             }
434             if (string.IsNullOrEmpty(req.sign))
435             {
436                 result.Code = "1004";
437                 result.Msg = "签名不能为空";
438                 return Json(result);
439             }
440             if (req.time_stamp <= 0)
441             {
442                 result.Code = "1005";
443                 result.Msg = "请传入正确的时间戳格式";
444                 return Json(result);
445             }
446             string strtime_stamp = ConfigurationManager.AppSettings["time_stamp"];
447             int intTimeStampCfg = 600;
448             int.TryParse(strtime_stamp, out intTimeStampCfg);
449             if (Math.Abs(CommonHelper.GetCurrentTimeStamp() - req.time_stamp) > intTimeStampCfg)
450             {
451                 //commonHelper.write_log("1002图片接口上传失败", "请求过期----当前时间:" + commonHelper.GetCurrentTimeStamp() + "------传入时间:" + model.time_stamp);
452                 result.Code = "1006";
453                 result.Msg = "请求过期";
454                 return Json(result);
455             }
456             string api_user_key = ConfigurationManager.AppSettings["upload_img_key"];
457             string mysign = string.Format("apikey={0}&deviceNo={1}&busyCode={2}&OperationNo={3}&time_stamp={4}", api_user_key, req.deviceNo, req.busyCode, req.OperationNo, req.time_stamp);
458             mysign = CommonHelper.MD5Encrypt(mysign, 32);
459             mysign = mysign.ToLower();
460             if (!req.sign.ToLower().Equals(mysign))
461             {
462                 //commonHelper.write_log("1003图片接口上传失败", "签名不匹配----传入的签名:" + model.sign.ToLower() + "------正确的签名:" + mysign);
463                 result.Code = "1007";
464                 result.Msg = "签名不匹配";
465                 return Json(result);
466             }
467             #endregion
468 
469             string WxCode = "", WxCodeUrl = "", AliCodeUrl = "", AliCode = "", Str_Out_trade_no = "";
470             //订单号
471             Str_Out_trade_no = GetOut_trade_no();
472             //商品ID
473             string Str_productId = GetRandom(9);
474             //金额
475             string strAmount = GetAmount(req.deviceNo, req.busyCode, req.isTest);
476             string Str_total_amount = strAmount;// "0.01";//支付宝支付金额,以 元 为单位
477             int Int_total_amount = ParIntForWeixin(strAmount);// 1;//微信支付金额,以 分 为单位
478 
479             #region weixin
480             NativePay nativePay = new NativePay();
481 
482             //生成扫码支付模式二url
483             string url2 = nativePay.GetPayUrl(Str_Out_trade_no, Str_productId, Int_total_amount);
484 
485             Log.Info(this.GetType().ToString(), "WeiXinNativePay url2 : " + url2);
486 
487             WxCode = "SUCCESS";
488             WxCodeUrl = HttpUtility.UrlEncode(url2);
489 
490             #endregion
491 
492             #region ali
493             AlipayTradePrecreateContentBuilder builder = BuildPrecreateContent(Str_Out_trade_no, Str_productId, Str_total_amount);
494             //string out_trade_no = builder.out_trade_no;
495 
496             //推荐使用轮询撤销机制,不推荐使用异步通知,避免单边账问题发生。
497             IAlipayTradeService serviceClient = F2FBiz.CreateClientInstance(Config.serverUrl, Config.appId, Config.merchant_private_key, Config.version,
498                             Config.sign_type, Config.alipay_public_key, Config.charset);
499 
500             string notify_url = Config.NotifyUrl; //商户接收异步通知的地址
501             AlipayF2FPrecreateResult precreateResult = serviceClient.tradePrecreate(builder, notify_url);
502 
503             //以下返回结果的处理供参考。
504             //payResponse.QrCode即二维码对于的链接
505             //将链接用二维码工具生成二维码,顾客可以用支付宝钱包扫码支付。
506             switch (precreateResult.Status)
507             {
508                 case ResultEnum.SUCCESS:
509                     AliCode = "SUCCESS";
510                     AliCodeUrl = HttpUtility.UrlEncode(precreateResult.response.QrCode);
511                     break;
512                 case ResultEnum.FAILED:
513                     AliCode = "FAILED";
514                     AliCodeUrl = precreateResult.response.Body;
515                     break;
516                 case ResultEnum.UNKNOWN:
517                     AliCode = "UNKNOWN";
518                     if (precreateResult.response == null)
519                     {
520                         AliCodeUrl = "配置或网络异常,请检查后重试";
521                     }
522                     else
523                     {
524                         AliCodeUrl = "系统异常,请更新外部订单后重新发起请求";
525                     }
526                     break;
527             }
528 
529             #endregion
530 
531             //处理业务数据,存图片、写数据库
532             Task.Run(()=> WritePayDataNew(req, Str_Out_trade_no));
533 
534             WriteLog.Debug("计时:结束时间=" + DateTime.Now.ToString());
535             result.Code = "200";
536             result.Msg = "成功";
537             result.WxCode = WxCode;
538             result.WxCodeUrl = WxCodeUrl;
539             result.AliCode = AliCode;
540             result.AliCodeUrl = AliCodeUrl;
541             result.Out_trade_no = Str_Out_trade_no;
542             return Json(result);
543         }
544 
545         private int ParIntForWeixin(string strAmount)
546         {
547             double p = 0;
548             double.TryParse(strAmount, out p);
549             int intp = (int)(p * 100);
550             if (intp == 0)
551                 return 20 * 100;
552             else
553                 return intp;
554         }
555 
556         private string GetAmount(string deviceNo, string busyCode, string isTest)
557         {
558             var strTest = string.IsNullOrEmpty(isTest) ? "" : isTest;
559             if (strTest.Equals("1"))
560             {
561                 //测试用,金额为配置的0.01
562                 string TestamountPrice = ConfigurationManager.AppSettings["TestamountPrice"];
563                 if (string.IsNullOrEmpty(TestamountPrice))
564                     return "0.01";
565                 else
566                     return TestamountPrice;
567             }
568             else
569             {
570                 IQueryable<M_AgentDevice_Pay> q = _amountService.GetByNoCode(deviceNo, busyCode);
571                 var ls = q.ToList();
572                 if (ls == null || ls.Count < 1)
573                 {
574                     string amountPrice = ConfigurationManager.AppSettings["amountPrice"];
575                     if (string.IsNullOrEmpty(amountPrice))
576                         return "20";
577                     else
578                         return amountPrice;
579                 }
580                 else
581                 {
582                     return ls[0].PayAmount;
583                 }
584             }
585         }
586 
587         /// <summary>
588         /// 支付宝 构造支付请求数据
589         /// </summary>
590         /// <returns>请求数据集</returns>
591         private AlipayTradePrecreateContentBuilder BuildPrecreateContent(string Str_Out_trade_no, string Str_productId, string Str_total_amount)
592         {
593             //bkin 三个参数:out_trade_no total_amount subject
594 
595             //线上联调时,请输入真实的外部订单号。
596             string out_trade_no = Str_Out_trade_no;
597 
598             AlipayTradePrecreateContentBuilder builder = new AlipayTradePrecreateContentBuilder();
599             //收款账号
600             builder.seller_id = Config.pid;
601             //订单编号
602             builder.out_trade_no = out_trade_no;
603             //订单总金额
604             builder.total_amount = Str_total_amount;
605             //参与优惠计算的金额
606             //builder.discountable_amount = "";
607             //不参与优惠计算的金额
608             //builder.undiscountable_amount = "";
609             //订单名称
610             builder.subject = "证件照";
611             //自定义超时时间
612             builder.timeout_express = "5m";
613             //订单描述
614             builder.body = "";
615             //门店编号,很重要的参数,可以用作之后的营销
616             builder.store_id = "test store id";
617             //操作员编号,很重要的参数,可以用作之后的营销
618             builder.operator_id = "证件照";
619 
620             //传入商品信息详情
621             List<GoodsInfo> gList = new List<GoodsInfo>();
622             GoodsInfo goods = new GoodsInfo();
623             goods.goods_id = Str_productId;
624             goods.goods_name = "证件照";
625             goods.price = Str_total_amount;
626             goods.quantity = "1";
627             gList.Add(goods);
628             builder.goods_detail = gList;
629 
630             //系统商接入可以填此参数用作返佣
631             //ExtendParams exParam = new ExtendParams();
632             //exParam.sysServiceProviderId = "20880000000000";
633             //builder.extendParams = exParam;
634 
635             return builder;
636         }
637 
638         private void WritePayData(WxAliPayRequest req, string Str_Out_trade_no)
639         {
640             WriteLog.Debug("WritePayData begin");
641             if (req == null)
642                 return;
643             var filePath = Base64_to_File(req.avatar);
644 
645             //
646             var _payService1 = EngineContext.Current.Resolve<IAgent_User_ImgService>(); 
647             var _payService2 = EngineContext.Current.Resolve<IAgentOperationPayService>();
648             var _payService3 = EngineContext.Current.Resolve<IAgentOperationService>();
649 
650             //插入 Agent_User_Img 表
651             var m = new M_Agent_User_Img()
652             {
653                 SFZMHM = req.SFZMHM,
654                 SFZMMC = req.SFZMMC,
655                 SFZMLX = req.SFZMLX,
656                 Img_Type = req.Img_Type,
657                 Img_Url = filePath,
658                 OperationNo = req.OperationNo,
659                 Img_Spec = req.Img_Spec,
660                 Create_Time = DateTime.Now,
661                 IsDeleted = false
662             };
663             _payService1.Insert(m);
664 
665             //插入 AgentOperationPay 表
666             var m2 = new M_AgentOperationPay()
667             {
668                 Payment = 0,//支付成功回调更新为 (1, "微信支付");(2, "支付宝支付");(3, "银联支付");(4, "现金支付");
669                 Amount = (decimal)0.01,
670                 Quantity = 1,
671                 PayStatus = 0,//支付成功回调更新为1
672                 AddTime = DateTime.Now,
673                 IsDeleted = false,
674                 OrderNo = Str_Out_trade_no,
675                 SerialNo = Str_Out_trade_no,
676                 OperationNo = req.OperationNo
677             };
678             _payService2.Insert(m2);
679 
680             //插入 AgentOperation 表
681             var m3 = new M_AgentOperation()
682             {
683                 AgentNo = "bsr_zxj",
684                 OperationNo = req.OperationNo,
685                 AgentDeviceNo = req.deviceNo,
686                 BusinessNo = "dgnzxj",
687                 OperationStatus = true,
688                 AddTime = DateTime.Now,
689                 IsDeleted = false,
690                 ValidateCode = "1111",
691                 Status = 0    //支付成功回调更新为1
692             };
693             _payService3.Insert(m3);
694 
695 
696         }
697         private void WritePayDataNew(WxAliPayRequestOnlyUrl req, string Str_Out_trade_no)
698         {
699             WriteLog.Debug("WritePayData begin");
700             if (req == null)
701                 return;
702             //
703             var _payService2 = EngineContext.Current.Resolve<IAgentOperationPayService>();
704             var _payService3 = EngineContext.Current.Resolve<IAgentOperationService>();
705 
706             //插入 AgentOperationPay 表
707             var m2 = new M_AgentOperationPay()
708             {
709                 Payment = 0,//支付成功回调更新为 (1, "微信支付");(2, "支付宝支付");(3, "银联支付");(4, "现金支付");
710                 Amount = (decimal)0.01,
711                 Quantity = 1,
712                 PayStatus = 0,//支付成功回调更新为1
713                 AddTime = DateTime.Now,
714                 IsDeleted = false,
715                 OrderNo = Str_Out_trade_no,
716                 SerialNo = Str_Out_trade_no,
717                 OperationNo = req.OperationNo
718             };
719             _payService2.Insert(m2);
720 
721             //插入 AgentOperation 表
722             var m3 = new M_AgentOperation()
723             {
724                 AgentNo = "bsr_zxj",
725                 OperationNo = req.OperationNo,
726                 AgentDeviceNo = req.deviceNo,
727                 BusinessNo = "dgnzxj",
728                 OperationStatus = true,
729                 AddTime = DateTime.Now,
730                 IsDeleted = false,
731                 ValidateCode = "1111",
732                 Status = 0    //支付成功回调更新为1
733             };
734             _payService3.Insert(m3);
735 
736 
737         }
738         private void WritePayData2(WxAliPayRequest req, string Str_Out_trade_no)
739         {
740             WriteLog.Debug("WritePayData begin");
741             if (req == null)
742                 return;
743             var filePath = Base64_to_File(req.avatar);
744 
745             //插入 Agent_User_Img 表
746             var m = new M_Agent_User_Img()
747             {
748                 SFZMHM = req.SFZMHM,
749                 SFZMMC = req.SFZMMC,
750                 SFZMLX = req.SFZMLX,
751                 Img_Type = req.Img_Type,
752                 Img_Url = filePath,
753                 OperationNo = req.OperationNo,
754                 Img_Spec = req.Img_Spec,
755                 Create_Time = DateTime.Now,
756                 IsDeleted = false
757             };
758             _userService.Insert(m);
759 
760             //插入 AgentOperationPay 表
761             var m2 = new M_AgentOperationPay()
762             {
763                 Payment = 1,
764                 Amount = (decimal)0.01,
765                 Quantity = 1,
766                 PayStatus = 0,
767                 AddTime = DateTime.Now,
768                 IsDeleted = false,
769                 OrderNo = Str_Out_trade_no,
770                 SerialNo = Str_Out_trade_no,
771                 OperationNo = req.OperationNo
772             };
773             _payService.Insert(m2);
774 
775             //插入 AgentOperation 表
776             var m3 = new M_AgentOperation()
777             {
778                 AgentNo = "bsr_zxj",
779                 OperationNo = req.OperationNo,
780                 AgentDeviceNo = req.deviceNo,
781                 BusinessNo = "dgnzxj",
782                 OperationStatus = true,
783                 AddTime = DateTime.Now,
784                 IsDeleted = false,
785                 ValidateCode = "1111",
786                 Status = 1
787             };
788             _operateService.Insert(m3);
789 
790 
791         }
792 
793         private string GetOut_trade_no()
794         {
795             return System.DateTime.Now.ToString("yyyyMMddHHmmss") + GetRandom(10);
796         }
797 
798         private string Base64_to_File(string imageCode)
799         {
800             if (string.IsNullOrEmpty(imageCode))
801                 return "";
802 
803             byte[] arr2 = Convert.FromBase64String(imageCode);
804             using (MemoryStream ms2 = new MemoryStream(arr2))
805             {
806                 string dir = "/user_img/" + DateTime.Now.ToString("yyyyMMdd") + "/";
807                 if (!Directory.Exists(Server.MapPath(dir)))
808                 {
809                     Directory.CreateDirectory(Server.MapPath(dir));
810                 }
811                 string fileName = Guid.NewGuid() + ".jpg";
812                 string dirFile = Server.MapPath(dir) + fileName;
813 
814                 System.Drawing.Bitmap bmp2 = new System.Drawing.Bitmap(ms2);
815                 bmp2.Save(dirFile, System.Drawing.Imaging.ImageFormat.Jpeg);
816                 //bmp2.Save(filePath + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
817                 //bmp2.Save(filePath + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
818                 //bmp2.Save(filePath + ".png", System.Drawing.Imaging.ImageFormat.Png);
819                 return dir + fileName;
820             }
821         }
822 
823         //生成N位的随机数 全数字
824         private string GetRandom(int len)
825         {
826             string k = "";
827             Random rand = new Random(GetRandomSeed());
828             for (int i = 0; i < len; i++)
829             {
830                 k += rand.Next(0, 10).ToString();
831             }
832             return k;
833         }
834         //取随机数种子
835         private int GetRandomSeed()
836         {
837             byte[] bytes = new byte[4];
838             System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
839             rng.GetBytes(bytes);
840             return BitConverter.ToInt32(bytes, 0);
841         }
842         #endregion
843 
844         #region 客户端轮询此接口,查是否已经付款
845         //根据订单号来查询
846         [HttpPost]
847         public JsonResult CheckIsPay(string out_trade_no)
848         {
849             var m = _payService.GetByOrderNo(out_trade_no);
850             if (m == null)
851             {
852                 return Json(new { code = "0", msg = "未支付" });
853             }
854             else if (m.PayStatus == 1)
855             {
856                 return Json(new { code = "1", msg = "已支付" });
857             }
858             else 
859             {
860                 return Json(new { code = "0", msg = "未支付" });
861             }
862             
863         }
864         #endregion
865     }
866 }

支付宝调用需要引用官网的一些dll, 微信的需要添加一些文件:

 

posted on 2019-12-25 11:06  bkin的笔记  阅读(1840)  评论(0编辑  收藏  举报