NotifyController.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using System.Xml;
  6. using Essensoft.AspNetCore.Payment.Alipay;
  7. using Essensoft.AspNetCore.Payment.Alipay.Notify;
  8. using Essensoft.AspNetCore.Payment.Alipay.Utility;
  9. using Essensoft.AspNetCore.Payment.WeChatPay;
  10. using Essensoft.AspNetCore.Payment.WeChatPay.Notify;
  11. using Microsoft.AspNetCore.Mvc;
  12. using Microsoft.Extensions.Options;
  13. namespace WebApplicationSample.Controllers
  14. {
  15. #region 支付宝异步通知
  16. [Route("notify/alipay")]
  17. public class AlipayNotifyController : Controller
  18. {
  19. private readonly IAlipayNotifyClient _client;
  20. private readonly IOptions<AlipayOptions> _optionsAccessor;
  21. public AlipayNotifyController(IAlipayNotifyClient client, IOptions<AlipayOptions> optionsAccessor)
  22. {
  23. _client = client;
  24. _optionsAccessor = optionsAccessor;
  25. }
  26. /// <summary>
  27. /// 应用网关
  28. /// </summary>
  29. /// <returns></returns>
  30. [Route("gateway")]
  31. [HttpPost]
  32. public async Task<IActionResult> Gateway()
  33. {
  34. try
  35. {
  36. // 获取参数
  37. var Dic = new Dictionary<string, string>();
  38. if (Request.Method == "POST")
  39. {
  40. foreach (var iter in Request.Form)
  41. {
  42. Dic.Add(iter.Key, iter.Value);
  43. }
  44. }
  45. // 激活开发者模式
  46. if ("alipay.service.check".Equals(Dic["service"]))
  47. {
  48. var options = _optionsAccessor.Value;
  49. var signStr = Dic["sign"];
  50. Dic.Remove("sign");
  51. var signContent = AlipaySignature.GetSignContent(Dic);
  52. // 加签方式为公钥证书时,从公钥证书获取的RSA公钥 options.AlipayPublicCertKey
  53. var isSuccess = AlipaySignature.RSACheckContent(signContent, signStr, options.AlipayPublicCertKey, options.Charset, options.SignType);
  54. var response = MakeVerifyGWResponse(isSuccess, options.AlipayPublicCertKey, options.AppPrivateKey, options.Charset, options.SignType);
  55. return Content(response, "text/xml");
  56. }
  57. return NoContent();
  58. }
  59. catch
  60. {
  61. return NoContent();
  62. }
  63. }
  64. /// <summary>
  65. /// 扫码支付异步通知
  66. /// </summary>
  67. [Route("precreate")]
  68. [HttpPost]
  69. public async Task<IActionResult> Precreate()
  70. {
  71. try
  72. {
  73. var notify = await _client.CertificateExecuteAsync<AlipayTradePrecreateNotify>(Request, _optionsAccessor.Value);
  74. if (notify.TradeStatus == AlipayTradeStatus.Success)
  75. {
  76. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  77. return AlipayNotifyResult.Success;
  78. }
  79. return NoContent();
  80. }
  81. catch
  82. {
  83. return NoContent();
  84. }
  85. }
  86. /// <summary>
  87. /// APP支付异步通知
  88. /// </summary>
  89. [Route("apppay")]
  90. [HttpPost]
  91. public async Task<IActionResult> AppPay()
  92. {
  93. try
  94. {
  95. var notify = await _client.CertificateExecuteAsync<AlipayTradeAppPayNotify>(Request, _optionsAccessor.Value);
  96. if (notify.TradeStatus == AlipayTradeStatus.Success)
  97. {
  98. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  99. return AlipayNotifyResult.Success;
  100. }
  101. return NoContent();
  102. }
  103. catch
  104. {
  105. return NoContent();
  106. }
  107. }
  108. /// <summary>
  109. /// 电脑网站支付异步通知
  110. /// </summary>
  111. [Route("pagepay")]
  112. [HttpPost]
  113. public async Task<IActionResult> PagePay()
  114. {
  115. try
  116. {
  117. var notify = await _client.CertificateExecuteAsync<AlipayTradePagePayNotify>(Request, _optionsAccessor.Value);
  118. if (notify.TradeStatus == AlipayTradeStatus.Success)
  119. {
  120. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  121. return AlipayNotifyResult.Success;
  122. }
  123. return NoContent();
  124. }
  125. catch
  126. {
  127. return NoContent();
  128. }
  129. }
  130. /// <summary>
  131. /// 手机网站支付异步通知
  132. /// </summary>
  133. [Route("wappay")]
  134. [HttpPost]
  135. public async Task<IActionResult> WapPay()
  136. {
  137. try
  138. {
  139. var notify = await _client.CertificateExecuteAsync<AlipayTradeWapPayNotify>(Request, _optionsAccessor.Value);
  140. if (notify.TradeStatus == AlipayTradeStatus.Success)
  141. {
  142. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  143. return AlipayNotifyResult.Success;
  144. }
  145. return NoContent();
  146. }
  147. catch
  148. {
  149. return NoContent();
  150. }
  151. }
  152. private string MakeVerifyGWResponse(bool isSuccess, string certPublicKey, string appPrivateKey, string charset, string signType)
  153. {
  154. var xmlDoc = new XmlDocument(); //创建实例
  155. var xmldecl = xmlDoc.CreateXmlDeclaration("1.0", "GBK", null);
  156. xmlDoc.AppendChild(xmldecl);
  157. var xmlElem = xmlDoc.CreateElement("alipay"); //新建元素
  158. xmlDoc.AppendChild(xmlElem); //添加元素
  159. var alipay = xmlDoc.SelectSingleNode("alipay");
  160. var response = xmlDoc.CreateElement("response");
  161. var success = xmlDoc.CreateElement("success");
  162. if (isSuccess)
  163. {
  164. success.InnerText = "true";//设置文本节点
  165. response.AppendChild(success);//添加到<Node>节点中
  166. }
  167. else
  168. {
  169. success.InnerText = "false";//设置文本节点
  170. response.AppendChild(success);//添加到<Node>节点中
  171. var err = xmlDoc.CreateElement("error_code");
  172. err.InnerText = "VERIFY_FAILED";
  173. response.AppendChild(err);
  174. }
  175. var biz_content = xmlDoc.CreateElement("biz_content");
  176. biz_content.InnerText = certPublicKey;
  177. response.AppendChild(biz_content);
  178. alipay.AppendChild(response);
  179. var sign = xmlDoc.CreateElement("sign");
  180. sign.InnerText = AlipaySignature.RSASignContent(response.InnerXml, appPrivateKey, charset, signType);
  181. alipay.AppendChild(sign);
  182. var sign_type = xmlDoc.CreateElement("sign_type");
  183. sign_type.InnerText = signType;
  184. alipay.AppendChild(sign_type);
  185. return xmlDoc.InnerXml;
  186. }
  187. }
  188. #endregion
  189. #region 微信支付异步通知
  190. [Route("notify/wechatpay")]
  191. public class WeChatPayNotifyController : Controller
  192. {
  193. private readonly IWeChatPayNotifyClient _client;
  194. private readonly IOptions<WeChatPayOptions> _optionsAccessor;
  195. public WeChatPayNotifyController(IWeChatPayNotifyClient client, IOptions<WeChatPayOptions> optionsAccessor)
  196. {
  197. _client = client;
  198. _optionsAccessor = optionsAccessor;
  199. }
  200. /// <summary>
  201. /// 统一下单支付结果通知
  202. /// </summary>
  203. [Route("unifiedorder")]
  204. [HttpPost]
  205. public async Task<IActionResult> Unifiedorder()
  206. {
  207. try
  208. {
  209. var notify = await _client.ExecuteAsync<WeChatPayUnifiedOrderNotify>(Request, _optionsAccessor.Value);
  210. if (notify.ReturnCode == WeChatPayCode.Success)
  211. {
  212. if (notify.ResultCode == WeChatPayCode.Success)
  213. {
  214. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  215. return WeChatPayNotifyResult.Success;
  216. }
  217. }
  218. return NoContent();
  219. }
  220. catch
  221. {
  222. return NoContent();
  223. }
  224. }
  225. /// <summary>
  226. /// 退款结果通知
  227. /// </summary>
  228. [Route("refund")]
  229. [HttpPost]
  230. public async Task<IActionResult> Refund()
  231. {
  232. try
  233. {
  234. var notify = await _client.ExecuteAsync<WeChatPayRefundNotify>(Request, _optionsAccessor.Value);
  235. if (notify.ReturnCode == WeChatPayCode.Success)
  236. {
  237. if (notify.RefundStatus == WeChatPayCode.Success)
  238. {
  239. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  240. return WeChatPayNotifyResult.Success;
  241. }
  242. }
  243. return NoContent();
  244. }
  245. catch
  246. {
  247. return NoContent();
  248. }
  249. }
  250. }
  251. #endregion
  252. }