WeChatPayScoreNotifyController.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Threading.Tasks;
  3. using Essensoft.AspNetCore.Payment.WeChatPay;
  4. using Essensoft.AspNetCore.Payment.WeChatPay.Payscore;
  5. using Essensoft.AspNetCore.Payment.WeChatPay.Payscore.Notify;
  6. using Essensoft.AspNetCore.Payment.WeChatPay.V3;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.Extensions.Options;
  9. namespace WebApplicationSample.Controllers
  10. {
  11. [Route("wechatpay/score/notify")]
  12. public class WeChatPayScoreNotifyController : Controller
  13. {
  14. private readonly IWeChatPayNotifyClient _client;
  15. private readonly IOptions<WeChatPayOptions> _optionsAccessor;
  16. public WeChatPayScoreNotifyController(IWeChatPayNotifyClient client, IOptions<WeChatPayOptions> optionsAccessor)
  17. {
  18. _client = client;
  19. _optionsAccessor = optionsAccessor;
  20. }
  21. /// <summary>
  22. /// 开启/解除授权服务回调通知
  23. /// </summary>
  24. [Route("Permissions")]
  25. [HttpPost]
  26. public async Task<IActionResult> Permissions()
  27. {
  28. try
  29. {
  30. var notify = await _client.ExecuteAsync<WeChatPayScorePermissionsOpenOrCancelNotify>(Request, _optionsAccessor.Value);
  31. if (notify.UserServiceStatus == WeChatPayScoreUserServiceStatus.Opened ||
  32. notify.UserServiceStatus == WeChatPayScoreUserServiceStatus.Closed)
  33. {
  34. Console.WriteLine("Permissions body: " + notify.Body);
  35. return WeChatPayNotifyResult.Success;
  36. }
  37. return WeChatPayNotifyResult.Failure;
  38. }
  39. catch (WeChatPayException ex)
  40. {
  41. Console.WriteLine("出现异常: " + ex.Message);
  42. return WeChatPayNotifyResult.Failure;
  43. }
  44. }
  45. /// <summary>
  46. /// 确认订单回调通知
  47. /// </summary>
  48. [Route("orderconfirm")]
  49. [HttpPost]
  50. public async Task<IActionResult> ServiceOrderConfirm()
  51. {
  52. try
  53. {
  54. var notify = await _client.ExecuteAsync<WeChatPayScoreServiceOrderConfirmNotify>(Request, _optionsAccessor.Value);
  55. if (notify.State == ServiceOrderState.Doing)
  56. {
  57. Console.WriteLine("ServiceOrderConfirm body: " + notify.Body);
  58. return WeChatPayNotifyResult.Success;
  59. }
  60. return WeChatPayNotifyResult.Failure;
  61. }
  62. catch (WeChatPayException ex)
  63. {
  64. Console.WriteLine("出现异常: " + ex.Message);
  65. return WeChatPayNotifyResult.Failure;
  66. }
  67. }
  68. /// <summary>
  69. /// 订单支付成功回调通知
  70. /// </summary>
  71. [Route("orderpaid")]
  72. [HttpPost]
  73. public async Task<IActionResult> ServiceOrderPaid()
  74. {
  75. try
  76. {
  77. var notify = await _client.ExecuteAsync<WeChatPayScoreServiceOrderPaidNotify>(Request, _optionsAccessor.Value);
  78. if (notify.State == ServiceOrderState.Done)
  79. {
  80. Console.WriteLine("ServiceOrderPaid body: " + notify.Body);
  81. return WeChatPayNotifyResult.Success;
  82. }
  83. return WeChatPayNotifyResult.Failure;
  84. }
  85. catch (WeChatPayException ex)
  86. {
  87. Console.WriteLine("出现异常: " + ex.Message);
  88. return WeChatPayNotifyResult.Failure;
  89. }
  90. }
  91. /// <summary>
  92. /// 订单 确认 或 支付成功 回调通知
  93. /// </summary>
  94. [Route("confirmorpaid")]
  95. [HttpPost]
  96. public async Task<IActionResult> ServiceOrderConfirmOrPaid()
  97. {
  98. try
  99. {
  100. var notify = await _client.ExecuteAsync<WeChatPayScoreServiceOrderPaidNotify>(Request, _optionsAccessor.Value);
  101. if (notify.State == ServiceOrderState.Doing || notify.State == ServiceOrderState.Done)
  102. {
  103. Console.WriteLine("ServiceOrderConfirmOrPaid body: " + notify.Body);
  104. return WeChatPayNotifyResult.Success;
  105. }
  106. return WeChatPayNotifyResult.Failure;
  107. }
  108. catch (WeChatPayException ex)
  109. {
  110. Console.WriteLine("出现异常: " + ex.Message);
  111. return WeChatPayNotifyResult.Failure;
  112. }
  113. }
  114. }
  115. }