WxpayController.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Text;
  5. using GxPress.Common.WechatPay;
  6. using Microsoft.AspNetCore.Authorization;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.AspNetCore.Mvc;
  9. using System.IO;
  10. using System.Threading.Tasks;
  11. namespace GxPress.Api.WebControllers
  12. {
  13. [Route("api/web/wxpay")]
  14. [ApiController]
  15. [Authorize]
  16. public class WxpayController : Controller
  17. {
  18. private IHttpContextAccessor _contextAccessor;
  19. private HttpContext _context { get { return _contextAccessor.HttpContext; } }
  20. public WxpayController(IHttpContextAccessor contextAccessor)
  21. {
  22. _contextAccessor = contextAccessor;
  23. }
  24. [HttpGet()]
  25. [AllowAnonymous]
  26. public string GetNativePayUrl()
  27. {
  28. var nativePay = new NativePay();
  29. return nativePay.GetPayUrl("637263608658642540","汪峰讲故事",7);
  30. }
  31. /// <summary>
  32. /// 回调地址
  33. /// </summary>
  34. [HttpPost("notify")]
  35. [AllowAnonymous]
  36. public async Task<string> Callback()
  37. {
  38. var contentType = Request.ContentType;
  39. var notifyData = await GetNotifyData();
  40. //检查支付结果中transaction_id是否存在
  41. if (!notifyData.IsSet("transaction_id"))
  42. {
  43. //若transaction_id不存在,则立即返回结果给微信支付后台
  44. WxPayData res = new WxPayData();
  45. res.SetValue("return_code", "FAIL");
  46. res.SetValue("return_msg", "支付结果中微信订单号不存在");
  47. return (res.ToXml());
  48. }
  49. string transaction_id = notifyData.GetValue("transaction_id").ToString();
  50. //查询订单,判断订单真实性
  51. if (!Common.WechatPay.WxPayApi.QueryOrder(transaction_id))
  52. {
  53. //若订单查询失败,则立即返回结果给微信支付后台
  54. WxPayData res = new WxPayData();
  55. res.SetValue("return_code", "FAIL");
  56. res.SetValue("return_msg", "订单查询失败");
  57. return (res.ToXml());
  58. }
  59. //查询订单成功
  60. else
  61. {
  62. WxPayData res = new WxPayData();
  63. res.SetValue("return_code", "SUCCESS");
  64. res.SetValue("return_msg", "OK");
  65. return (res.ToXml());
  66. }
  67. }
  68. public async Task<WxPayData> GetNotifyData()
  69. {
  70. var body = string.Empty;
  71. //接收从微信后台POST过来的数据
  72. using (var reader = new StreamReader(Request.Body))
  73. {
  74. body = await reader.ReadToEndAsync();
  75. }
  76. //转换数据格式并验证签名
  77. WxPayData data = new WxPayData();
  78. if (string.IsNullOrEmpty(body))
  79. {
  80. data.SetValue("return_code", "FAIL");
  81. return data;
  82. }
  83. try
  84. {
  85. data.FromXml(body);
  86. }
  87. catch (WxPayException ex)
  88. {
  89. //若签名错误,则立即返回结果给微信支付后台
  90. WxPayData res = new WxPayData();
  91. res.SetValue("return_code", "FAIL");
  92. res.SetValue("return_msg", ex.Message);
  93. // Log.Error(this.GetType().ToString(), "Sign check error : " + res.ToXml());
  94. // page.Response.Write(res.ToXml());
  95. // page.Response.End();
  96. }
  97. // Log.Info(this.GetType().ToString(), "Check sign success");
  98. return data;
  99. }
  100. }
  101. }