1
0

NotifyController.cs 9.4 KB

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