ResultNotify.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 ResultNotify:Notify
  13. {
  14. public ResultNotify(Page page):base(page)
  15. {
  16. }
  17. public override void ProcessNotify()
  18. {
  19. WxPayData notifyData = GetNotifyData();
  20. //检查支付结果中transaction_id是否存在
  21. if (!notifyData.IsSet("transaction_id"))
  22. {
  23. //若transaction_id不存在,则立即返回结果给微信支付后台
  24. WxPayData res = new WxPayData();
  25. res.SetValue("return_code", "FAIL");
  26. res.SetValue("return_msg", "支付结果中微信订单号不存在");
  27. Log.Error(this.GetType().ToString(), "The Pay result is error : " + res.ToXml());
  28. page.Response.Write(res.ToXml());
  29. page.Response.End();
  30. }
  31. string transaction_id = notifyData.GetValue("transaction_id").ToString();
  32. //查询订单,判断订单真实性
  33. if (!QueryOrder(transaction_id))
  34. {
  35. //若订单查询失败,则立即返回结果给微信支付后台
  36. WxPayData res = new WxPayData();
  37. res.SetValue("return_code", "FAIL");
  38. res.SetValue("return_msg", "订单查询失败");
  39. Log.Error(this.GetType().ToString(), "Order query failure : " + res.ToXml());
  40. page.Response.Write(res.ToXml());
  41. page.Response.End();
  42. }
  43. //查询订单成功
  44. else
  45. {
  46. WxPayData res = new WxPayData();
  47. res.SetValue("return_code", "SUCCESS");
  48. res.SetValue("return_msg", "OK");
  49. Log.Info(this.GetType().ToString(), "order query success : " + res.ToXml());
  50. page.Response.Write(res.ToXml());
  51. page.Response.End();
  52. }
  53. }
  54. //查询订单
  55. private bool QueryOrder(string transaction_id)
  56. {
  57. WxPayData req = new WxPayData();
  58. req.SetValue("transaction_id", transaction_id);
  59. WxPayData res = WxPayApi.OrderQuery(req);
  60. if (res.GetValue("return_code").ToString() == "SUCCESS" &&
  61. res.GetValue("result_code").ToString() == "SUCCESS")
  62. {
  63. return true;
  64. }
  65. else
  66. {
  67. return false;
  68. }
  69. }
  70. }
  71. }