NotifyController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Essensoft.AspNetCore.Payment.Alipay;
  5. using Essensoft.AspNetCore.Payment.Alipay.Notify;
  6. using Essensoft.AspNetCore.Payment.JDPay;
  7. using Essensoft.AspNetCore.Payment.JDPay.Notify;
  8. using Essensoft.AspNetCore.Payment.LianLianPay;
  9. using Essensoft.AspNetCore.Payment.LianLianPay.Notify;
  10. using Essensoft.AspNetCore.Payment.QPay;
  11. using Essensoft.AspNetCore.Payment.QPay.Notify;
  12. using Essensoft.AspNetCore.Payment.UnionPay;
  13. using Essensoft.AspNetCore.Payment.UnionPay.Notify;
  14. using Essensoft.AspNetCore.Payment.WeChatPay;
  15. using Essensoft.AspNetCore.Payment.WeChatPay.Notify;
  16. using Microsoft.AspNetCore.Mvc;
  17. using Microsoft.Extensions.Options;
  18. namespace WebApplicationSample.Controllers
  19. {
  20. #region 支付宝异步通知
  21. [Route("notify/alipay")]
  22. public class AlipayNotifyController : Controller
  23. {
  24. private readonly IAlipayNotifyClient _client;
  25. private readonly IOptions<AlipayOptions> _optionsAccessor;
  26. public AlipayNotifyController(IAlipayNotifyClient client, IOptions<AlipayOptions> optionsAccessor)
  27. {
  28. _client = client;
  29. _optionsAccessor = optionsAccessor;
  30. }
  31. /// <summary>
  32. /// 扫码支付异步通知
  33. /// </summary>
  34. /// <returns></returns>
  35. [Route("precreate")]
  36. [HttpPost]
  37. public async Task<IActionResult> Precreate()
  38. {
  39. try
  40. {
  41. var parameters = new Dictionary<string, string>();
  42. foreach (var iter in Request.Form)
  43. {
  44. parameters.Add(iter.Key, iter.Value);
  45. }
  46. var notify = await _client.ExecuteAsync<AlipayTradePrecreateNotify>(parameters, _optionsAccessor.Value);
  47. if ("TRADE_SUCCESS" == notify.TradeStatus)
  48. {
  49. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  50. return AlipayNotifyResult.Success;
  51. }
  52. return NoContent();
  53. }
  54. catch
  55. {
  56. return NoContent();
  57. }
  58. }
  59. /// <summary>
  60. /// APP支付异步通知
  61. /// </summary>
  62. /// <returns></returns>
  63. [Route("apppay")]
  64. [HttpPost]
  65. public async Task<IActionResult> AppPay()
  66. {
  67. try
  68. {
  69. var parameters = new Dictionary<string, string>();
  70. foreach (var iter in Request.Form)
  71. {
  72. parameters.Add(iter.Key, iter.Value);
  73. }
  74. var notify = await _client.ExecuteAsync<AlipayTradeAppPayNotify>(parameters, _optionsAccessor.Value);
  75. if ("TRADE_SUCCESS" == notify.TradeStatus)
  76. {
  77. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  78. return AlipayNotifyResult.Success;
  79. }
  80. return NoContent();
  81. }
  82. catch
  83. {
  84. return NoContent();
  85. }
  86. }
  87. /// <summary>
  88. /// 电脑网站支付异步通知
  89. /// </summary>
  90. /// <returns></returns>
  91. [Route("pagepay")]
  92. [HttpPost]
  93. public async Task<IActionResult> PagePay()
  94. {
  95. try
  96. {
  97. var parameters = new Dictionary<string, string>();
  98. foreach (var iter in Request.Form)
  99. {
  100. parameters.Add(iter.Key, iter.Value);
  101. }
  102. var notify = await _client.ExecuteAsync<AlipayTradePagePayNotify>(parameters, _optionsAccessor.Value);
  103. if ("TRADE_SUCCESS" == notify.TradeStatus)
  104. {
  105. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  106. return AlipayNotifyResult.Success;
  107. }
  108. return NoContent();
  109. }
  110. catch
  111. {
  112. return NoContent();
  113. }
  114. }
  115. /// <summary>
  116. /// 手机网站支付异步通知
  117. /// </summary>
  118. /// <returns></returns>
  119. [Route("wappay")]
  120. [HttpPost]
  121. public async Task<IActionResult> WapPay()
  122. {
  123. try
  124. {
  125. var parameters = new Dictionary<string, string>();
  126. foreach (var iter in Request.Form)
  127. {
  128. parameters.Add(iter.Key, iter.Value);
  129. }
  130. var notify = await _client.ExecuteAsync<AlipayTradeWapPayNotify>(parameters, _optionsAccessor.Value);
  131. if ("TRADE_SUCCESS" == notify.TradeStatus)
  132. {
  133. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  134. return AlipayNotifyResult.Success;
  135. }
  136. return NoContent();
  137. }
  138. catch
  139. {
  140. return NoContent();
  141. }
  142. }
  143. }
  144. #endregion
  145. #region 微信支付异步通知
  146. [Route("notify/wechatpay")]
  147. public class WeChatPayNotifyController : Controller
  148. {
  149. private readonly IWeChatPayNotifyClient _client;
  150. public WeChatPayNotifyController(IWeChatPayNotifyClient client)
  151. {
  152. _client = client;
  153. }
  154. /// <summary>
  155. /// 统一下单支付结果通知
  156. /// </summary>
  157. /// <returns></returns>
  158. [Route("unifiedorder")]
  159. [HttpPost]
  160. public async Task<IActionResult> Unifiedorder()
  161. {
  162. try
  163. {
  164. var notify = await _client.ExecuteAsync<WeChatPayUnifiedOrderNotify>(Request);
  165. if (notify.ReturnCode == "SUCCESS")
  166. {
  167. if (notify.ResultCode == "SUCCESS")
  168. {
  169. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  170. return WeChatPayNotifyResult.Success;
  171. }
  172. }
  173. return NoContent();
  174. }
  175. catch
  176. {
  177. return NoContent();
  178. }
  179. }
  180. /// <summary>
  181. /// 退款结果通知
  182. /// </summary>
  183. /// <returns></returns>
  184. [Route("refund")]
  185. [HttpPost]
  186. public async Task<IActionResult> Refund()
  187. {
  188. try
  189. {
  190. var notify = await _client.ExecuteAsync<WeChatPayRefundNotify>(Request);
  191. if (notify.ReturnCode == "SUCCESS")
  192. {
  193. if (notify.RefundStatus == "SUCCESS")
  194. {
  195. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  196. return WeChatPayNotifyResult.Success;
  197. }
  198. }
  199. return NoContent();
  200. }
  201. catch
  202. {
  203. return NoContent();
  204. }
  205. }
  206. }
  207. #endregion
  208. #region QQ钱包异步通知
  209. [Route("notify/qpay")]
  210. public class QPayNotifyController : Controller
  211. {
  212. private readonly IQPayNotifyClient _client;
  213. private readonly IOptions<QPayOptions> _optionsAccessor;
  214. public QPayNotifyController(IQPayNotifyClient client, IOptions<QPayOptions> optionsAccessor)
  215. {
  216. _client = client;
  217. _optionsAccessor = optionsAccessor;
  218. }
  219. /// <summary>
  220. /// 统一下单支付结果通知
  221. /// </summary>
  222. /// <returns></returns>
  223. [Route("unifiedorder")]
  224. [HttpPost]
  225. public async Task<IActionResult> Unifiedorder()
  226. {
  227. try
  228. {
  229. var notify = await _client.ExecuteAsync<QPayUnifiedOrderNotify>(Request, _optionsAccessor.Value);
  230. if ("SUCCESS" == notify.TradeState)
  231. {
  232. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  233. return QPayNotifyResult.Success;
  234. }
  235. return NoContent();
  236. }
  237. catch
  238. {
  239. return NoContent();
  240. }
  241. }
  242. /// <summary>
  243. /// 付款码支付结果通知
  244. /// </summary>
  245. /// <returns></returns>
  246. [Route("micropay")]
  247. [HttpPost]
  248. public async Task<IActionResult> MicroPay()
  249. {
  250. try
  251. {
  252. var notify = await _client.ExecuteAsync<QPayMicroPayNotify>(Request, _optionsAccessor.Value);
  253. if ("SUCCESS" == notify.TradeState)
  254. {
  255. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  256. return QPayNotifyResult.Success;
  257. }
  258. return NoContent();
  259. }
  260. catch
  261. {
  262. return NoContent();
  263. }
  264. }
  265. /// <summary>
  266. /// 企业付款 - 用户到账通知
  267. /// </summary>
  268. /// <returns></returns>
  269. [Route("b2cpay")]
  270. [HttpPost]
  271. public async Task<IActionResult> B2CPay()
  272. {
  273. try
  274. {
  275. var notify = await _client.ExecuteAsync<QPayEPayB2CNotify>(Request, _optionsAccessor.Value);
  276. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  277. return QPayNotifyResult.Success;
  278. }
  279. catch
  280. {
  281. return NoContent();
  282. }
  283. }
  284. }
  285. #endregion
  286. #region 京东支付异步通知
  287. [Route("notify/jdpay")]
  288. public class JDPayNotifyController : Controller
  289. {
  290. private readonly IJDPayNotifyClient _client;
  291. private readonly IOptions<JDPayOptions> _optionsAccessor;
  292. public JDPayNotifyController(IJDPayNotifyClient client, IOptions<JDPayOptions> optionsAccessor)
  293. {
  294. _client = client;
  295. _optionsAccessor = optionsAccessor;
  296. }
  297. [Route("async")]
  298. [HttpPost]
  299. public async Task<IActionResult> Async()
  300. {
  301. try
  302. {
  303. var notify = await _client.ExecuteAsync<JDPayAsyncNotify>(Request, _optionsAccessor.Value);
  304. Console.WriteLine("TradeNum: " + notify.TradeNum + " tradeType :" + notify.TradeType);// notify.TradeType 0-消费 1-退款
  305. return JDPayNotifyResult.Success;
  306. }
  307. catch
  308. {
  309. return NoContent();
  310. }
  311. }
  312. [Route("defraypay")]
  313. [HttpPost]
  314. public async Task<IActionResult> DefrayPay()
  315. {
  316. try
  317. {
  318. var notify = await _client.ExecuteAsync<JDPayDefrayPayNotify>(Request, _optionsAccessor.Value);
  319. Console.WriteLine("trade_no: " + notify.TradeNo + " trade_amount :" + notify.TradeAmount);
  320. return JDPayNotifyResult.Success;
  321. }
  322. catch
  323. {
  324. return NoContent();
  325. }
  326. }
  327. }
  328. #endregion
  329. #region 连连支付异步通知
  330. [Route("notify/lianlianpay")]
  331. public class LianLianPayNotifyController : Controller
  332. {
  333. private readonly ILianLianPayNotifyClient _client;
  334. private readonly IOptions<LianLianPayOptions> _optionsAccessor;
  335. public LianLianPayNotifyController(ILianLianPayNotifyClient client, IOptions<LianLianPayOptions> optionsAccessor)
  336. {
  337. _client = client;
  338. _optionsAccessor = optionsAccessor;
  339. }
  340. [Route("receivemoney")]
  341. [HttpPost]
  342. public async Task<IActionResult> ReceiveMoney()
  343. {
  344. try
  345. {
  346. var notify = await _client.ExecuteAsync<LianLianPayReceiveMoneyNotify>(Request, _optionsAccessor.Value);
  347. Console.WriteLine("NoOrder: " + notify.NoOrder);
  348. return LianLianPayNotifyResult.Success;
  349. }
  350. catch
  351. {
  352. return NoContent();
  353. }
  354. }
  355. [Route("refund")]
  356. [HttpPost]
  357. public async Task<IActionResult> Refund()
  358. {
  359. try
  360. {
  361. var notify = await _client.ExecuteAsync<LianLianPayRefundNotify>(Request, _optionsAccessor.Value);
  362. Console.WriteLine("NoRefund: " + notify.NoRefund);
  363. return LianLianPayNotifyResult.Success;
  364. }
  365. catch
  366. {
  367. return NoContent();
  368. }
  369. }
  370. }
  371. #endregion
  372. #region 银联支付异步通知
  373. [Route("notify/unionpay")]
  374. public class UnionPayNotifyController : Controller
  375. {
  376. private readonly IUnionPayNotifyClient _client;
  377. public UnionPayNotifyController(IUnionPayNotifyClient client)
  378. {
  379. _client = client;
  380. }
  381. /// <summary>
  382. /// 网关支付 - 跳转网关页面支付通知
  383. /// </summary>
  384. /// <returns></returns>
  385. [Route("gatewaypayfrontconsume")]
  386. [HttpPost]
  387. public async Task<IActionResult> GatewayPayFrontConsume()
  388. {
  389. try
  390. {
  391. var notify = await _client.ExecuteAsync<UnionPayGatewayPayFrontConsumeNotify>(Request);
  392. Console.WriteLine("OrderId: " + notify.OrderId + " respCode :" + notify.RespCode);
  393. return UnionPayNotifyResult.Success;
  394. }
  395. catch
  396. {
  397. return NoContent();
  398. }
  399. }
  400. }
  401. #endregion
  402. }