JSAPIPay.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Newtonsoft.Json;
  2. using Sugar.Enties;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. namespace WxPayAPI
  7. {
  8. public class JSAPIPay
  9. {
  10. /// <summary>
  11. /// nlog日志
  12. /// </summary>
  13. private static NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger();
  14. /// <summary>
  15. /// 生成JSAPI所需的数据 (先下单,然后获取返回的数据)
  16. /// </summary>
  17. /// <param name="productId">商品ID</param>
  18. /// <param name="total_fee">订单金额,单位为分,类型为整数</param>
  19. /// <param name="out_trade_no">从商户传来的订单号</param>
  20. /// <returns>模式二URL</returns>
  21. public WxPayDTO GetJSAPIWxPayData(int total_fee, string openid, string out_trade_no)
  22. {
  23. //统一下单: 调用统一下单接口
  24. Log.Info(this.GetType().ToString() + "JSAPI-GetPayUrl");
  25. WxPayData data = new WxPayData();
  26. data.SetValue("body", "缴费"); //商品描述
  27. //data.SetValue("detail", "缴费"); //商品详情
  28. data.SetValue("out_trade_no", out_trade_no); //商户订单号
  29. data.SetValue("total_fee", Convert.ToDouble(total_fee).ToString()); //订单总金额,以分为单位
  30. data.SetValue("trade_type", "JSAPI");//交易类型
  31. data.SetValue("openid", openid);
  32. //string xml = data.ToXml(); //转换成XML
  33. //var startTime = DateTime.Now; //开始时间
  34. Log.Info($"【GetJSAPIWxPayData】data组装:{WxPayConfig.NOTIFY_URL}");
  35. WxPayData result = WxPayApi.UnifiedOrder(data);//调用统一下单接口
  36. //获取微信返回数据
  37. TimeSpan timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1));
  38. string appId = result.GetValue("appid") == null ? "" : result.GetValue("appid").ToString();
  39. string timeStamp = ((int)timeSpan.TotalSeconds).ToString() + "";
  40. string nonceStr = result.GetValue("nonce_str") == null ? "" : result.GetValue("nonce_str").ToString();
  41. string package = result.GetValue("prepay_id") == null ? "prepay_id=" : "prepay_id=" + result.GetValue("prepay_id").ToString();
  42. string signType = "MD5";
  43. string prepay_id = result.GetValue("prepay_id") == null ? "" : result.GetValue("prepay_id").ToString();
  44. string result_code = result.GetValue("result_code") == null ? "" : result.GetValue("result_code").ToString();
  45. string return_code = result.GetValue("return_code") == null ? "" : result.GetValue("return_code").ToString();
  46. string err_code = "";
  47. string err_code_des = "";
  48. if (return_code == "SUCCESS" || result_code == "FAIL")
  49. {
  50. err_code = result.GetValue("err_code") == null ? "" : result.GetValue("err_code").ToString();
  51. err_code_des = result.GetValue("err_code_des") == null ? "" : result.GetValue("err_code_des").ToString();
  52. }
  53. //获取微信签名
  54. WxPayData wxPaydata = new WxPayData();
  55. wxPaydata.SetValue("appId", appId); //公众号名称
  56. wxPaydata.SetValue("timeStamp", timeStamp); //时间戳,自1970年以来的秒数
  57. wxPaydata.SetValue("nonceStr", nonceStr); //随机串
  58. wxPaydata.SetValue("package", package); //商品描述
  59. wxPaydata.SetValue("signType", signType); //微信签名方式
  60. string sign = wxPaydata.MakeSign();
  61. //wxPaydata.SetValue("sign", sign);//微信签名
  62. WxPayDTO wxpaydto = new WxPayDTO();
  63. wxpaydto.AppId = appId;
  64. wxpaydto.TimeStamp = timeStamp;
  65. wxpaydto.NonceStr = nonceStr;
  66. wxpaydto.Package = package;
  67. wxpaydto.SignType = signType;
  68. wxpaydto.PaySign = sign;
  69. wxpaydto.PrepayId = prepay_id;
  70. wxpaydto.ReturnCode = return_code;
  71. wxpaydto.ResultCode = result_code;
  72. wxpaydto.ErrCode = err_code;
  73. wxpaydto.ErrCodeDes = err_code_des;
  74. wxpaydto.OutTradeNo = out_trade_no;
  75. //string mweb_url = result.GetValue("mweb_url") == null ? "" : result.GetValue("mweb_url").ToString();//mweb_url为拉起微信支付收银台的中间页面,可通过访问该url来拉起微信客户端,完成支付,mweb_url的有效期为5分钟。
  76. return wxpaydto;
  77. }
  78. /// <summary>
  79. /// 取消支付或其他原因,关闭预支付订单
  80. /// </summary>
  81. /// <param name="out_trade_no"></param>
  82. /// <returns></returns>
  83. public WxPayDTO CloseOrder(string out_trade_no)
  84. {
  85. //统一下单: 调用统一下单接口
  86. Log.Info(this.GetType().ToString() + "JSAPI-Reverse");
  87. WxPayData data = new WxPayData();
  88. data.SetValue("out_trade_no", out_trade_no); //商户订单号
  89. Log.Info($"【CloseOrder】data组装:{JsonConvert.SerializeObject(data)}");
  90. WxPayData result = WxPayApi.CloseOrder(data);//调用撤销订单
  91. WxPayDTO wxpaydto = new WxPayDTO();
  92. wxpaydto.AppId = result.GetValue("appid") == null ? "" : result.GetValue("appid").ToString();
  93. wxpaydto.ResultCode = result.GetValue("result_code") == null ? "" : result.GetValue("result_code").ToString();
  94. wxpaydto.ReturnCode = result.GetValue("return_code") == null ? "" : result.GetValue("return_code").ToString();
  95. //wxpaydto.TimeStamp = timeStamp;
  96. //wxpaydto.NonceStr = nonceStr;
  97. //wxpaydto.Package = package;
  98. //wxpaydto.SignType = signType;
  99. //wxpaydto.PaySign = sign;
  100. //wxpaydto.PrepayId = prepay_id;
  101. //wxpaydto.ReturnMsg = return_msg;
  102. return wxpaydto;
  103. }
  104. }
  105. }