You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 regels
4.9 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. using System.IO;
  7. namespace WxPayAPI
  8. {
  9. /// <summary>
  10. /// 扫码支付模式一回调处理类
  11. /// 接收微信支付后台发送的扫码结果,调用统一下单接口并将下单结果返回给微信支付后台
  12. /// </summary>
  13. public class NativeNotify : Notify
  14. {
  15. public NativeNotify(Page page)
  16. : base(page)
  17. {
  18. }
  19. public override void ProcessNotify()
  20. {
  21. WriteLog("不错");
  22. WxPayData notifyData = GetNotifyData();
  23. //检查openid和product_id是否返回
  24. if (!notifyData.IsSet("openid") || !notifyData.IsSet("product_id"))
  25. {
  26. WxPayData res = new WxPayData();
  27. res.SetValue("return_code", "FAIL");
  28. res.SetValue("return_msg", "回调数据异常");
  29. Log.Info(this.GetType().ToString(), "The data WeChat post is error : " + res.ToXml());
  30. page.Response.Write(res.ToXml());
  31. page.Response.End();
  32. }
  33. //调统一下单接口,获得下单结果
  34. string openid = notifyData.GetValue("openid").ToString();
  35. string product_id = notifyData.GetValue("product_id").ToString();
  36. WxPayData unifiedOrderResult = new WxPayData();
  37. try
  38. {
  39. unifiedOrderResult = UnifiedOrder(openid, product_id);
  40. }
  41. catch (Exception ex)//若在调统一下单接口时抛异常,立即返回结果给微信支付后台
  42. {
  43. WxPayData res = new WxPayData();
  44. res.SetValue("return_code", "FAIL");
  45. res.SetValue("return_msg", "统一下单失败");
  46. Log.Error(this.GetType().ToString(), "UnifiedOrder failure : " + res.ToXml());
  47. page.Response.Write(res.ToXml());
  48. page.Response.End();
  49. }
  50. //若下单失败,则立即返回结果给微信支付后台
  51. if (!unifiedOrderResult.IsSet("appid") || !unifiedOrderResult.IsSet("mch_id") || !unifiedOrderResult.IsSet("prepay_id"))
  52. {
  53. WxPayData res = new WxPayData();
  54. res.SetValue("return_code", "FAIL");
  55. res.SetValue("return_msg", "统一下单失败");
  56. Log.Error(this.GetType().ToString(), "UnifiedOrder failure : " + res.ToXml());
  57. page.Response.Write(res.ToXml());
  58. page.Response.End();
  59. }
  60. //统一下单成功,则返回成功结果给微信支付后台
  61. WxPayData data = new WxPayData();
  62. data.SetValue("return_code", "SUCCESS");
  63. data.SetValue("return_msg", "OK");
  64. data.SetValue("appid", WxPayConfig.APPID);
  65. data.SetValue("mch_id", WxPayConfig.MCHID);
  66. data.SetValue("nonce_str", WxPayApi.GenerateNonceStr());
  67. data.SetValue("prepay_id", unifiedOrderResult.GetValue("prepay_id"));
  68. data.SetValue("result_code", "SUCCESS");
  69. data.SetValue("err_code_des", "OK");
  70. data.SetValue("sign", data.MakeSign());
  71. Log.Info(this.GetType().ToString(), "UnifiedOrder success , send data to WeChat : " + data.ToXml());
  72. page.Response.Write(data.ToXml());
  73. page.Response.End();
  74. }
  75. private WxPayData UnifiedOrder(string openId, string productId)
  76. {
  77. //统一下单
  78. WxPayData req = new WxPayData();
  79. req.SetValue("body", "test");
  80. req.SetValue("attach", "test");
  81. req.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo());
  82. req.SetValue("total_fee", 1);
  83. req.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
  84. req.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));
  85. req.SetValue("goods_tag", "test");
  86. req.SetValue("trade_type", "NATIVE");
  87. req.SetValue("openid", openId);
  88. req.SetValue("product_id", productId);
  89. WxPayData result = WxPayApi.UnifiedOrder(req);
  90. return result;
  91. }
  92. /// <summary>
  93. /// 写入日志
  94. /// </summary>
  95. /// <param name="readme"></param>
  96. public void WriteLog(string readme)
  97. {
  98. string _Date = System.DateTime.Now.Date.ToString("yyyy-MM-dd");
  99. StreamWriter dout = new StreamWriter(@"D:/wwwroot/wxpay.ivhua.com/logs/" + _Date + ".txt", true);
  100. dout.Write("事件:" + readme + " 操作时间:" + System.DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
  101. dout.Write(System.Environment.NewLine); //换行
  102. dout.Close();
  103. }
  104. }
  105. }