NotifyController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. public QPayNotifyController(IQPayNotifyClient client)
  214. {
  215. _client = client;
  216. }
  217. /// <summary>
  218. /// 统一下单支付结果通知
  219. /// </summary>
  220. /// <returns></returns>
  221. [Route("unifiedorder")]
  222. [HttpPost]
  223. public async Task<IActionResult> Unifiedorder()
  224. {
  225. try
  226. {
  227. var notify = await _client.ExecuteAsync<QPayUnifiedOrderNotify>(Request);
  228. if ("SUCCESS" == notify.TradeState)
  229. {
  230. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  231. return QPayNotifyResult.Success;
  232. }
  233. return NoContent();
  234. }
  235. catch
  236. {
  237. return NoContent();
  238. }
  239. }
  240. /// <summary>
  241. /// 付款码支付结果通知
  242. /// </summary>
  243. /// <returns></returns>
  244. [Route("micropay")]
  245. [HttpPost]
  246. public async Task<IActionResult> MicroPay()
  247. {
  248. try
  249. {
  250. var notify = await _client.ExecuteAsync<QPayMicroPayNotify>(Request);
  251. if ("SUCCESS" == notify.TradeState)
  252. {
  253. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  254. return QPayNotifyResult.Success;
  255. }
  256. return NoContent();
  257. }
  258. catch
  259. {
  260. return NoContent();
  261. }
  262. }
  263. /// <summary>
  264. /// 企业付款 - 用户到账通知
  265. /// </summary>
  266. /// <returns></returns>
  267. [Route("b2cpay")]
  268. [HttpPost]
  269. public async Task<IActionResult> B2CPay()
  270. {
  271. try
  272. {
  273. var notify = await _client.ExecuteAsync<QPayEPayB2CNotify>(Request);
  274. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  275. return QPayNotifyResult.Success;
  276. }
  277. catch
  278. {
  279. return NoContent();
  280. }
  281. }
  282. }
  283. #endregion
  284. #region 京东支付异步通知
  285. [Route("notify/jdpay")]
  286. public class JDPayNotifyController : Controller
  287. {
  288. private readonly IJDPayNotifyClient _client;
  289. private readonly IOptions<JDPayOptions> _optionsAccessor;
  290. public JDPayNotifyController(IJDPayNotifyClient client, IOptions<JDPayOptions> optionsAccessor)
  291. {
  292. _client = client;
  293. _optionsAccessor = optionsAccessor;
  294. }
  295. [Route("async")]
  296. [HttpPost]
  297. public async Task<IActionResult> Async()
  298. {
  299. try
  300. {
  301. var notify = await _client.ExecuteAsync<JDPayAsyncNotify>(Request, _optionsAccessor.Value);
  302. Console.WriteLine("TradeNum: " + notify.TradeNum + " tradeType :" + notify.TradeType);// notify.TradeType 0-消费 1-退款
  303. return JDPayNotifyResult.Success;
  304. }
  305. catch
  306. {
  307. return NoContent();
  308. }
  309. }
  310. [Route("defraypay")]
  311. [HttpPost]
  312. public async Task<IActionResult> DefrayPay()
  313. {
  314. try
  315. {
  316. var notify = await _client.ExecuteAsync<JDPayDefrayPayNotify>(Request, _optionsAccessor.Value);
  317. Console.WriteLine("trade_no: " + notify.TradeNo + " trade_amount :" + notify.TradeAmount);
  318. return JDPayNotifyResult.Success;
  319. }
  320. catch
  321. {
  322. return NoContent();
  323. }
  324. }
  325. }
  326. #endregion
  327. #region 连连支付异步通知
  328. [Route("notify/lianlianpay")]
  329. public class LianLianPayNotifyController : Controller
  330. {
  331. private readonly ILianLianPayNotifyClient _client;
  332. public LianLianPayNotifyController(ILianLianPayNotifyClient client)
  333. {
  334. _client = client;
  335. }
  336. [Route("receivemoney")]
  337. [HttpPost]
  338. public async Task<IActionResult> ReceiveMoney()
  339. {
  340. try
  341. {
  342. var notify = await _client.ExecuteAsync<LianLianPayReceiveMoneyNotify>(Request);
  343. Console.WriteLine("NoOrder: " + notify.NoOrder);
  344. return LianLianPayNotifyResult.Success;
  345. }
  346. catch
  347. {
  348. return NoContent();
  349. }
  350. }
  351. [Route("refund")]
  352. [HttpPost]
  353. public async Task<IActionResult> Refund()
  354. {
  355. try
  356. {
  357. var notify = await _client.ExecuteAsync<LianLianPayRefundNotify>(Request);
  358. Console.WriteLine("NoRefund: " + notify.NoRefund);
  359. return LianLianPayNotifyResult.Success;
  360. }
  361. catch
  362. {
  363. return NoContent();
  364. }
  365. }
  366. }
  367. #endregion
  368. #region 银联支付异步通知
  369. [Route("notify/unionpay")]
  370. public class UnionPayNotifyController : Controller
  371. {
  372. private readonly IUnionPayNotifyClient _client;
  373. public UnionPayNotifyController(IUnionPayNotifyClient client)
  374. {
  375. _client = client;
  376. }
  377. /// <summary>
  378. /// 网关支付 - 跳转网关页面支付通知
  379. /// </summary>
  380. /// <returns></returns>
  381. [Route("gatewaypayfrontconsume")]
  382. [HttpPost]
  383. public async Task<IActionResult> GatewayPayFrontConsume()
  384. {
  385. try
  386. {
  387. var notify = await _client.ExecuteAsync<UnionPayGatewayPayFrontConsumeNotify>(Request);
  388. Console.WriteLine("OrderId: " + notify.OrderId + " respCode :" + notify.RespCode);
  389. return UnionPayNotifyResult.Success;
  390. }
  391. catch
  392. {
  393. return NoContent();
  394. }
  395. }
  396. }
  397. #endregion
  398. }