AlipayNotifyController.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Xml;
  4. using Essensoft.AspNetCore.Payment.Alipay;
  5. using Essensoft.AspNetCore.Payment.Alipay.Notify;
  6. using Essensoft.AspNetCore.Payment.Alipay.Utility;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.Extensions.Options;
  9. namespace WebApplicationSample.Controllers
  10. {
  11. [Route("alipay/notify")]
  12. public class AlipayNotifyController : Controller
  13. {
  14. private readonly IAlipayNotifyClient _client;
  15. private readonly IOptions<AlipayOptions> _optionsAccessor;
  16. public AlipayNotifyController(IAlipayNotifyClient client, IOptions<AlipayOptions> optionsAccessor)
  17. {
  18. _client = client;
  19. _optionsAccessor = optionsAccessor;
  20. }
  21. /// <summary>
  22. /// 应用网关
  23. /// </summary>
  24. /// <returns></returns>
  25. [Route("gateway")]
  26. [HttpPost]
  27. public async Task<IActionResult> Gateway()
  28. {
  29. try
  30. {
  31. var service = Request.Form["service"].ToString();
  32. switch (service)
  33. {
  34. // 激活开发者模式
  35. case "alipay.service.check":
  36. {
  37. var options = _optionsAccessor.Value;
  38. // 获取参数
  39. var parameters = _client.GetParameters(Request);
  40. var sign = parameters["sign"];
  41. parameters.Remove("sign");
  42. var signContent = AlipaySignature.GetSignContent(parameters);
  43. // 验签
  44. var isSuccess = AlipaySignature.RSACheckContent(signContent, sign, options.AlipayPublicKey, "GBK", options.SignType);
  45. // 组XML响应内容
  46. var response = MakeVerifyGWResponse(isSuccess, options.AlipayPublicKey, options.AppPrivateKey, "GBK", options.SignType);
  47. return Content(response, "text/xml");
  48. }
  49. }
  50. var msg_method = Request.Form["msg_method"].ToString();
  51. switch (msg_method)
  52. {
  53. // 资金单据状态变更通知
  54. case "alipay.fund.trans.order.changed":
  55. {
  56. var notify = await _client.CertificateExecuteAsync<AlipayFundTransOrderChangedNotify>(Request, _optionsAccessor.Value);
  57. return AlipayNotifyResult.Success;
  58. }
  59. // 第三方应用授权取消消息
  60. case "alipay.open.auth.appauth.cancelled":
  61. {
  62. var notify = await _client.CertificateExecuteAsync<AlipayOpenAuthAppauthCancelledNotify>(Request, _optionsAccessor.Value);
  63. return AlipayNotifyResult.Success;
  64. }
  65. // 用户授权取消消息
  66. case "alipay.open.auth.userauth.cancelled":
  67. {
  68. var notify = await _client.CertificateExecuteAsync<AlipayOpenAuthUserauthCancelledNotify>(Request, _optionsAccessor.Value);
  69. return AlipayNotifyResult.Success;
  70. }
  71. // 小程序审核通过通知
  72. case "alipay.open.mini.version.audit.passed":
  73. {
  74. var notify = await _client.CertificateExecuteAsync<AlipayOpenMiniVersionAuditPassedNotify>(Request, _optionsAccessor.Value);
  75. return AlipayNotifyResult.Success;
  76. }
  77. // 用户授权取消消息
  78. case "alipay.open.mini.version.audit.rejected":
  79. {
  80. var notify = await _client.CertificateExecuteAsync<AlipayOpenMiniVersionAuditRejectedNotify>(Request, _optionsAccessor.Value);
  81. return AlipayNotifyResult.Success;
  82. }
  83. // 收单资金结算到银行账户,结算退票的异步通知
  84. case "alipay.trade.settle.dishonoured":
  85. {
  86. var notify = await _client.CertificateExecuteAsync<AlipayTradeSettleDishonouredNotify>(Request, _optionsAccessor.Value);
  87. return AlipayNotifyResult.Success;
  88. }
  89. // 收单资金结算到银行账户,结算失败的异步通知
  90. case "alipay.trade.settle.fail":
  91. {
  92. var notify = await _client.CertificateExecuteAsync<AlipayTradeSettleFailNotify>(Request, _optionsAccessor.Value);
  93. return AlipayNotifyResult.Success;
  94. }
  95. // 收单资金结算到银行账户,结算成功的异步通知
  96. case "alipay.trade.settle.success":
  97. {
  98. var notify = await _client.CertificateExecuteAsync<AlipayTradeSettleSuccessNotify>(Request, _optionsAccessor.Value);
  99. return AlipayNotifyResult.Success;
  100. }
  101. }
  102. return NoContent();
  103. }
  104. catch
  105. {
  106. return NoContent();
  107. }
  108. }
  109. /// <summary>
  110. /// 扫码支付异步通知
  111. /// </summary>
  112. [Route("precreate")]
  113. [HttpPost]
  114. public async Task<IActionResult> Precreate()
  115. {
  116. try
  117. {
  118. var notify = await _client.CertificateExecuteAsync<AlipayTradePrecreateNotify>(Request, _optionsAccessor.Value);
  119. if (notify.TradeStatus == AlipayTradeStatus.Success)
  120. {
  121. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  122. return AlipayNotifyResult.Success;
  123. }
  124. return NoContent();
  125. }
  126. catch
  127. {
  128. return NoContent();
  129. }
  130. }
  131. /// <summary>
  132. /// APP支付异步通知
  133. /// </summary>
  134. [Route("apppay")]
  135. [HttpPost]
  136. public async Task<IActionResult> AppPay()
  137. {
  138. try
  139. {
  140. var notify = await _client.CertificateExecuteAsync<AlipayTradeAppPayNotify>(Request, _optionsAccessor.Value);
  141. if (notify.TradeStatus == AlipayTradeStatus.Success)
  142. {
  143. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  144. return AlipayNotifyResult.Success;
  145. }
  146. return NoContent();
  147. }
  148. catch
  149. {
  150. return NoContent();
  151. }
  152. }
  153. /// <summary>
  154. /// 电脑网站支付异步通知
  155. /// </summary>
  156. [Route("pagepay")]
  157. [HttpPost]
  158. public async Task<IActionResult> PagePay()
  159. {
  160. try
  161. {
  162. var notify = await _client.CertificateExecuteAsync<AlipayTradePagePayNotify>(Request, _optionsAccessor.Value);
  163. if (notify.TradeStatus == AlipayTradeStatus.Success)
  164. {
  165. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  166. return AlipayNotifyResult.Success;
  167. }
  168. return NoContent();
  169. }
  170. catch
  171. {
  172. return NoContent();
  173. }
  174. }
  175. /// <summary>
  176. /// 手机网站支付异步通知
  177. /// </summary>
  178. [Route("wappay")]
  179. [HttpPost]
  180. public async Task<IActionResult> WapPay()
  181. {
  182. try
  183. {
  184. var notify = await _client.CertificateExecuteAsync<AlipayTradeWapPayNotify>(Request, _optionsAccessor.Value);
  185. if (notify.TradeStatus == AlipayTradeStatus.Success)
  186. {
  187. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  188. return AlipayNotifyResult.Success;
  189. }
  190. return NoContent();
  191. }
  192. catch
  193. {
  194. return NoContent();
  195. }
  196. }
  197. private string MakeVerifyGWResponse(bool isSuccess, string certPublicKey, string appPrivateKey, string charset, string signType)
  198. {
  199. var xmlDoc = new XmlDocument(); //创建实例
  200. var xmldecl = xmlDoc.CreateXmlDeclaration("1.0", "GBK", null);
  201. xmlDoc.AppendChild(xmldecl);
  202. var xmlElem = xmlDoc.CreateElement("alipay"); //新建元素
  203. xmlDoc.AppendChild(xmlElem); //添加元素
  204. var alipay = xmlDoc.SelectSingleNode("alipay");
  205. var response = xmlDoc.CreateElement("response");
  206. var success = xmlDoc.CreateElement("success");
  207. if (isSuccess)
  208. {
  209. success.InnerText = "true";//设置文本节点
  210. response.AppendChild(success);//添加到<Node>节点中
  211. }
  212. else
  213. {
  214. success.InnerText = "false";//设置文本节点
  215. response.AppendChild(success);//添加到<Node>节点中
  216. var err = xmlDoc.CreateElement("error_code");
  217. err.InnerText = "VERIFY_FAILED";
  218. response.AppendChild(err);
  219. }
  220. var biz_content = xmlDoc.CreateElement("biz_content");
  221. biz_content.InnerText = certPublicKey;
  222. response.AppendChild(biz_content);
  223. alipay.AppendChild(response);
  224. var sign = xmlDoc.CreateElement("sign");
  225. sign.InnerText = AlipaySignature.RSASignContent(response.InnerXml, appPrivateKey, charset, signType);
  226. alipay.AppendChild(sign);
  227. var sign_type = xmlDoc.CreateElement("sign_type");
  228. sign_type.InnerText = signType;
  229. alipay.AppendChild(sign_type);
  230. return xmlDoc.InnerXml;
  231. }
  232. }
  233. }