WxpayController.cs 3.6 KB

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