AlipayNotifyController.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Threading.Tasks;
  3. using Essensoft.AspNetCore.Payment.Alipay;
  4. using Essensoft.AspNetCore.Payment.Alipay.Notify;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Options;
  7. namespace WebApplicationSample.Controllers
  8. {
  9. [Route("alipay/notify")]
  10. public class AlipayNotifyController : Controller
  11. {
  12. private readonly IAlipayNotifyClient _client;
  13. private readonly IOptions<AlipayOptions> _optionsAccessor;
  14. public AlipayNotifyController(IAlipayNotifyClient client, IOptions<AlipayOptions> optionsAccessor)
  15. {
  16. _client = client;
  17. _optionsAccessor = optionsAccessor;
  18. }
  19. /// <summary>
  20. /// 应用网关
  21. /// </summary>
  22. /// <returns></returns>
  23. [Route("gateway")]
  24. [HttpPost]
  25. public async Task<IActionResult> Gateway()
  26. {
  27. try
  28. {
  29. var msg_method = Request.Form["msg_method"].ToString();
  30. switch (msg_method)
  31. {
  32. // 资金单据状态变更通知
  33. case "alipay.fund.trans.order.changed":
  34. {
  35. var notify = await _client.CertificateExecuteAsync<AlipayFundTransOrderChangedNotify>(Request, _optionsAccessor.Value);
  36. return AlipayNotifyResult.Success;
  37. }
  38. // 第三方应用授权取消消息
  39. case "alipay.open.auth.appauth.cancelled":
  40. {
  41. var notify = await _client.CertificateExecuteAsync<AlipayOpenAuthAppauthCancelledNotify>(Request, _optionsAccessor.Value);
  42. return AlipayNotifyResult.Success;
  43. }
  44. // 用户授权取消消息
  45. case "alipay.open.auth.userauth.cancelled":
  46. {
  47. var notify = await _client.CertificateExecuteAsync<AlipayOpenAuthUserauthCancelledNotify>(Request, _optionsAccessor.Value);
  48. return AlipayNotifyResult.Success;
  49. }
  50. // 小程序审核通过通知
  51. case "alipay.open.mini.version.audit.passed":
  52. {
  53. var notify = await _client.CertificateExecuteAsync<AlipayOpenMiniVersionAuditPassedNotify>(Request, _optionsAccessor.Value);
  54. return AlipayNotifyResult.Success;
  55. }
  56. // 用户授权取消消息
  57. case "alipay.open.mini.version.audit.rejected":
  58. {
  59. var notify = await _client.CertificateExecuteAsync<AlipayOpenMiniVersionAuditRejectedNotify>(Request, _optionsAccessor.Value);
  60. return AlipayNotifyResult.Success;
  61. }
  62. // 收单资金结算到银行账户,结算退票的异步通知
  63. case "alipay.trade.settle.dishonoured":
  64. {
  65. var notify = await _client.CertificateExecuteAsync<AlipayTradeSettleDishonouredNotify>(Request, _optionsAccessor.Value);
  66. return AlipayNotifyResult.Success;
  67. }
  68. // 收单资金结算到银行账户,结算失败的异步通知
  69. case "alipay.trade.settle.fail":
  70. {
  71. var notify = await _client.CertificateExecuteAsync<AlipayTradeSettleFailNotify>(Request, _optionsAccessor.Value);
  72. return AlipayNotifyResult.Success;
  73. }
  74. // 收单资金结算到银行账户,结算成功的异步通知
  75. case "alipay.trade.settle.success":
  76. {
  77. var notify = await _client.CertificateExecuteAsync<AlipayTradeSettleSuccessNotify>(Request, _optionsAccessor.Value);
  78. return AlipayNotifyResult.Success;
  79. }
  80. }
  81. return NoContent();
  82. }
  83. catch
  84. {
  85. return NoContent();
  86. }
  87. }
  88. /// <summary>
  89. /// 扫码支付异步通知
  90. /// </summary>
  91. [Route("precreate")]
  92. [HttpPost]
  93. public async Task<IActionResult> Precreate()
  94. {
  95. try
  96. {
  97. var notify = await _client.CertificateExecuteAsync<AlipayTradePrecreateNotify>(Request, _optionsAccessor.Value);
  98. if (notify.TradeStatus == AlipayTradeStatus.Success)
  99. {
  100. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  101. return AlipayNotifyResult.Success;
  102. }
  103. return NoContent();
  104. }
  105. catch
  106. {
  107. return NoContent();
  108. }
  109. }
  110. /// <summary>
  111. /// APP支付异步通知
  112. /// </summary>
  113. [Route("apppay")]
  114. [HttpPost]
  115. public async Task<IActionResult> AppPay()
  116. {
  117. try
  118. {
  119. var notify = await _client.CertificateExecuteAsync<AlipayTradeAppPayNotify>(Request, _optionsAccessor.Value);
  120. if (notify.TradeStatus == AlipayTradeStatus.Success)
  121. {
  122. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  123. return AlipayNotifyResult.Success;
  124. }
  125. return NoContent();
  126. }
  127. catch
  128. {
  129. return NoContent();
  130. }
  131. }
  132. /// <summary>
  133. /// 电脑网站支付异步通知
  134. /// </summary>
  135. [Route("pagepay")]
  136. [HttpPost]
  137. public async Task<IActionResult> PagePay()
  138. {
  139. try
  140. {
  141. var notify = await _client.CertificateExecuteAsync<AlipayTradePagePayNotify>(Request, _optionsAccessor.Value);
  142. if (notify.TradeStatus == AlipayTradeStatus.Success)
  143. {
  144. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  145. return AlipayNotifyResult.Success;
  146. }
  147. return NoContent();
  148. }
  149. catch
  150. {
  151. return NoContent();
  152. }
  153. }
  154. /// <summary>
  155. /// 手机网站支付异步通知
  156. /// </summary>
  157. [Route("wappay")]
  158. [HttpPost]
  159. public async Task<IActionResult> WapPay()
  160. {
  161. try
  162. {
  163. var notify = await _client.CertificateExecuteAsync<AlipayTradeWapPayNotify>(Request, _optionsAccessor.Value);
  164. if (notify.TradeStatus == AlipayTradeStatus.Success)
  165. {
  166. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  167. return AlipayNotifyResult.Success;
  168. }
  169. return NoContent();
  170. }
  171. catch
  172. {
  173. return NoContent();
  174. }
  175. }
  176. /// <summary>
  177. /// 交易关闭异步通知
  178. /// </summary>
  179. [Route("close")]
  180. [HttpPost]
  181. public async Task<IActionResult> Close()
  182. {
  183. try
  184. {
  185. var notify = await _client.CertificateExecuteAsync<AlipayTradeCloseNotify>(Request, _optionsAccessor.Value);
  186. if (notify.TradeStatus == AlipayTradeStatus.Wait)
  187. {
  188. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  189. return AlipayNotifyResult.Success;
  190. }
  191. return NoContent();
  192. }
  193. catch
  194. {
  195. return NoContent();
  196. }
  197. }
  198. }
  199. }