NotifyController.cs 13 KB

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