NotifyController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 Essensoft.AspNetCore.Payment.WeChatPay;
  8. using Essensoft.AspNetCore.Payment.WeChatPay.Notify;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Microsoft.Extensions.Options;
  11. namespace WebApplicationSample.Controllers
  12. {
  13. #region 支付宝异步通知
  14. [Route("notify/alipay")]
  15. public class AlipayNotifyController : Controller
  16. {
  17. private readonly IAlipayNotifyClient _client;
  18. private readonly IOptions<AlipayOptions> _optionsAccessor;
  19. public AlipayNotifyController(IAlipayNotifyClient client, IOptions<AlipayOptions> optionsAccessor)
  20. {
  21. _client = client;
  22. _optionsAccessor = optionsAccessor;
  23. }
  24. /// <summary>
  25. /// 应用网关
  26. /// </summary>
  27. /// <returns></returns>
  28. [Route("gateway")]
  29. [HttpPost]
  30. public async Task<IActionResult> Gateway()
  31. {
  32. try
  33. {
  34. var service = Request.Form["service"].ToString();
  35. switch (service)
  36. {
  37. // 激活开发者模式
  38. case "alipay.service.check":
  39. {
  40. var options = _optionsAccessor.Value;
  41. // 获取参数
  42. var parameters = _client.GetParameters(Request);
  43. var sign = parameters["sign"];
  44. parameters.Remove("sign");
  45. var signContent = AlipaySignature.GetSignContent(parameters);
  46. // 验签
  47. var isSuccess = AlipaySignature.RSACheckContent(signContent, sign, options.AlipayPublicKey, "GBK", options.SignType);
  48. // 组XML响应内容
  49. var response = MakeVerifyGWResponse(isSuccess, options.AlipayPublicKey, options.AppPrivateKey, "GBK", options.SignType);
  50. return Content(response, "text/xml");
  51. }
  52. }
  53. var msg_method = Request.Form["msg_method"].ToString();
  54. switch (msg_method)
  55. {
  56. // 资金单据状态变更通知
  57. case "alipay.fund.trans.order.changed":
  58. {
  59. var notify = await _client.CertificateExecuteAsync<AlipayFundTransOrderChangedNotify>(Request, _optionsAccessor.Value);
  60. return AlipayNotifyResult.Success;
  61. }
  62. // 第三方应用授权取消消息
  63. case "alipay.open.auth.appauth.cancelled":
  64. {
  65. var notify = await _client.CertificateExecuteAsync<AlipayOpenAuthAppauthCancelledNotify>(Request, _optionsAccessor.Value);
  66. return AlipayNotifyResult.Success;
  67. }
  68. // 用户授权取消消息
  69. case "alipay.open.auth.userauth.cancelled":
  70. {
  71. var notify = await _client.CertificateExecuteAsync<AlipayOpenAuthUserauthCancelledNotify>(Request, _optionsAccessor.Value);
  72. return AlipayNotifyResult.Success;
  73. }
  74. // 小程序审核通过通知
  75. case "alipay.open.mini.version.audit.passed":
  76. {
  77. var notify = await _client.CertificateExecuteAsync<AlipayOpenMiniVersionAuditPassedNotify>(Request, _optionsAccessor.Value);
  78. return AlipayNotifyResult.Success;
  79. }
  80. // 用户授权取消消息
  81. case "alipay.open.mini.version.audit.rejected":
  82. {
  83. var notify = await _client.CertificateExecuteAsync<AlipayOpenMiniVersionAuditRejectedNotify>(Request, _optionsAccessor.Value);
  84. return AlipayNotifyResult.Success;
  85. }
  86. // 收单资金结算到银行账户,结算退票的异步通知
  87. case "alipay.trade.settle.dishonoured":
  88. {
  89. var notify = await _client.CertificateExecuteAsync<AlipayTradeSettleDishonouredNotify>(Request, _optionsAccessor.Value);
  90. return AlipayNotifyResult.Success;
  91. }
  92. // 收单资金结算到银行账户,结算失败的异步通知
  93. case "alipay.trade.settle.fail":
  94. {
  95. var notify = await _client.CertificateExecuteAsync<AlipayTradeSettleFailNotify>(Request, _optionsAccessor.Value);
  96. return AlipayNotifyResult.Success;
  97. }
  98. // 收单资金结算到银行账户,结算成功的异步通知
  99. case "alipay.trade.settle.success":
  100. {
  101. var notify = await _client.CertificateExecuteAsync<AlipayTradeSettleSuccessNotify>(Request, _optionsAccessor.Value);
  102. return AlipayNotifyResult.Success;
  103. }
  104. }
  105. return NoContent();
  106. }
  107. catch
  108. {
  109. return NoContent();
  110. }
  111. }
  112. /// <summary>
  113. /// 扫码支付异步通知
  114. /// </summary>
  115. [Route("precreate")]
  116. [HttpPost]
  117. public async Task<IActionResult> Precreate()
  118. {
  119. try
  120. {
  121. var notify = await _client.CertificateExecuteAsync<AlipayTradePrecreateNotify>(Request, _optionsAccessor.Value);
  122. if (notify.TradeStatus == AlipayTradeStatus.Success)
  123. {
  124. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  125. return AlipayNotifyResult.Success;
  126. }
  127. return NoContent();
  128. }
  129. catch
  130. {
  131. return NoContent();
  132. }
  133. }
  134. /// <summary>
  135. /// APP支付异步通知
  136. /// </summary>
  137. [Route("apppay")]
  138. [HttpPost]
  139. public async Task<IActionResult> AppPay()
  140. {
  141. try
  142. {
  143. var notify = await _client.CertificateExecuteAsync<AlipayTradeAppPayNotify>(Request, _optionsAccessor.Value);
  144. if (notify.TradeStatus == AlipayTradeStatus.Success)
  145. {
  146. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  147. return AlipayNotifyResult.Success;
  148. }
  149. return NoContent();
  150. }
  151. catch
  152. {
  153. return NoContent();
  154. }
  155. }
  156. /// <summary>
  157. /// 电脑网站支付异步通知
  158. /// </summary>
  159. [Route("pagepay")]
  160. [HttpPost]
  161. public async Task<IActionResult> PagePay()
  162. {
  163. try
  164. {
  165. var notify = await _client.CertificateExecuteAsync<AlipayTradePagePayNotify>(Request, _optionsAccessor.Value);
  166. if (notify.TradeStatus == AlipayTradeStatus.Success)
  167. {
  168. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  169. return AlipayNotifyResult.Success;
  170. }
  171. return NoContent();
  172. }
  173. catch
  174. {
  175. return NoContent();
  176. }
  177. }
  178. /// <summary>
  179. /// 手机网站支付异步通知
  180. /// </summary>
  181. [Route("wappay")]
  182. [HttpPost]
  183. public async Task<IActionResult> WapPay()
  184. {
  185. try
  186. {
  187. var notify = await _client.CertificateExecuteAsync<AlipayTradeWapPayNotify>(Request, _optionsAccessor.Value);
  188. if (notify.TradeStatus == AlipayTradeStatus.Success)
  189. {
  190. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  191. return AlipayNotifyResult.Success;
  192. }
  193. return NoContent();
  194. }
  195. catch
  196. {
  197. return NoContent();
  198. }
  199. }
  200. private string MakeVerifyGWResponse(bool isSuccess, string certPublicKey, string appPrivateKey, string charset, string signType)
  201. {
  202. var xmlDoc = new XmlDocument(); //创建实例
  203. var xmldecl = xmlDoc.CreateXmlDeclaration("1.0", "GBK", null);
  204. xmlDoc.AppendChild(xmldecl);
  205. var xmlElem = xmlDoc.CreateElement("alipay"); //新建元素
  206. xmlDoc.AppendChild(xmlElem); //添加元素
  207. var alipay = xmlDoc.SelectSingleNode("alipay");
  208. var response = xmlDoc.CreateElement("response");
  209. var success = xmlDoc.CreateElement("success");
  210. if (isSuccess)
  211. {
  212. success.InnerText = "true";//设置文本节点
  213. response.AppendChild(success);//添加到<Node>节点中
  214. }
  215. else
  216. {
  217. success.InnerText = "false";//设置文本节点
  218. response.AppendChild(success);//添加到<Node>节点中
  219. var err = xmlDoc.CreateElement("error_code");
  220. err.InnerText = "VERIFY_FAILED";
  221. response.AppendChild(err);
  222. }
  223. var biz_content = xmlDoc.CreateElement("biz_content");
  224. biz_content.InnerText = certPublicKey;
  225. response.AppendChild(biz_content);
  226. alipay.AppendChild(response);
  227. var sign = xmlDoc.CreateElement("sign");
  228. sign.InnerText = AlipaySignature.RSASignContent(response.InnerXml, appPrivateKey, charset, signType);
  229. alipay.AppendChild(sign);
  230. var sign_type = xmlDoc.CreateElement("sign_type");
  231. sign_type.InnerText = signType;
  232. alipay.AppendChild(sign_type);
  233. return xmlDoc.InnerXml;
  234. }
  235. }
  236. #endregion
  237. #region 微信支付异步通知
  238. [Route("notify/wechatpay")]
  239. public class WeChatPayNotifyController : Controller
  240. {
  241. private readonly IWeChatPayNotifyClient _client;
  242. private readonly IOptions<WeChatPayOptions> _optionsAccessor;
  243. public WeChatPayNotifyController(IWeChatPayNotifyClient client, IOptions<WeChatPayOptions> optionsAccessor)
  244. {
  245. _client = client;
  246. _optionsAccessor = optionsAccessor;
  247. }
  248. /// <summary>
  249. /// 统一下单支付结果通知
  250. /// </summary>
  251. [Route("unifiedorder")]
  252. [HttpPost]
  253. public async Task<IActionResult> Unifiedorder()
  254. {
  255. try
  256. {
  257. var notify = await _client.ExecuteAsync<WeChatPayUnifiedOrderNotify>(Request, _optionsAccessor.Value);
  258. if (notify.ReturnCode == WeChatPayCode.Success)
  259. {
  260. if (notify.ResultCode == WeChatPayCode.Success)
  261. {
  262. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  263. return WeChatPayNotifyResult.Success;
  264. }
  265. }
  266. return NoContent();
  267. }
  268. catch
  269. {
  270. return NoContent();
  271. }
  272. }
  273. /// <summary>
  274. /// 退款结果通知
  275. /// </summary>
  276. [Route("refund")]
  277. [HttpPost]
  278. public async Task<IActionResult> Refund()
  279. {
  280. try
  281. {
  282. var notify = await _client.ExecuteAsync<WeChatPayRefundNotify>(Request, _optionsAccessor.Value);
  283. if (notify.ReturnCode == WeChatPayCode.Success)
  284. {
  285. if (notify.RefundStatus == WeChatPayCode.Success)
  286. {
  287. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  288. return WeChatPayNotifyResult.Success;
  289. }
  290. }
  291. return NoContent();
  292. }
  293. catch
  294. {
  295. return NoContent();
  296. }
  297. }
  298. /// <summary>
  299. /// 统一下单V3支付结果通知
  300. /// </summary>
  301. [Route("v3/transactions")]
  302. [HttpPost]
  303. public async Task<IActionResult> Transactions()
  304. {
  305. try
  306. {
  307. return NoContent();
  308. }
  309. catch
  310. {
  311. return NoContent();
  312. }
  313. }
  314. }
  315. #endregion
  316. }