NativeNotify.cs 4.1 KB

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