NotifyController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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.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. public WeChatPayNotifyController(IWeChatPayNotifyClient client)
  130. {
  131. _client = client;
  132. }
  133. /// <summary>
  134. /// 统一下单支付结果通知
  135. /// </summary>
  136. /// <returns></returns>
  137. [Route("unifiedorder")]
  138. [HttpPost]
  139. public async Task<IActionResult> Unifiedorder()
  140. {
  141. try
  142. {
  143. var notify = await _client.ExecuteAsync<WeChatPayUnifiedOrderNotify>(Request);
  144. if (notify.ReturnCode == "SUCCESS")
  145. {
  146. if (notify.ResultCode == "SUCCESS")
  147. {
  148. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  149. return WeChatPayNotifyResult.Success;
  150. }
  151. }
  152. return NoContent();
  153. }
  154. catch
  155. {
  156. return NoContent();
  157. }
  158. }
  159. /// <summary>
  160. /// 退款结果通知
  161. /// </summary>
  162. /// <returns></returns>
  163. [Route("refund")]
  164. [HttpPost]
  165. public async Task<IActionResult> Refund()
  166. {
  167. try
  168. {
  169. var notify = await _client.ExecuteAsync<WeChatPayRefundNotify>(Request);
  170. if (notify.ReturnCode == "SUCCESS")
  171. {
  172. if (notify.RefundStatus == "SUCCESS")
  173. {
  174. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  175. return WeChatPayNotifyResult.Success;
  176. }
  177. }
  178. return NoContent();
  179. }
  180. catch
  181. {
  182. return NoContent();
  183. }
  184. }
  185. }
  186. #endregion
  187. #region QQ钱包异步通知
  188. [Route("notify/qpay")]
  189. public class QPayNotifyController : Controller
  190. {
  191. private readonly IQPayNotifyClient _client;
  192. private readonly IOptions<QPayOptions> _optionsAccessor;
  193. public QPayNotifyController(IQPayNotifyClient client, IOptions<QPayOptions> optionsAccessor)
  194. {
  195. _client = client;
  196. _optionsAccessor = optionsAccessor;
  197. }
  198. /// <summary>
  199. /// 统一下单支付结果通知
  200. /// </summary>
  201. /// <returns></returns>
  202. [Route("unifiedorder")]
  203. [HttpPost]
  204. public async Task<IActionResult> Unifiedorder()
  205. {
  206. try
  207. {
  208. var notify = await _client.ExecuteAsync<QPayUnifiedOrderNotify>(Request, _optionsAccessor.Value);
  209. if ("SUCCESS" == notify.TradeState)
  210. {
  211. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  212. return QPayNotifyResult.Success;
  213. }
  214. return NoContent();
  215. }
  216. catch
  217. {
  218. return NoContent();
  219. }
  220. }
  221. /// <summary>
  222. /// 付款码支付结果通知
  223. /// </summary>
  224. /// <returns></returns>
  225. [Route("micropay")]
  226. [HttpPost]
  227. public async Task<IActionResult> MicroPay()
  228. {
  229. try
  230. {
  231. var notify = await _client.ExecuteAsync<QPayMicroPayNotify>(Request, _optionsAccessor.Value);
  232. if ("SUCCESS" == notify.TradeState)
  233. {
  234. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  235. return QPayNotifyResult.Success;
  236. }
  237. return NoContent();
  238. }
  239. catch
  240. {
  241. return NoContent();
  242. }
  243. }
  244. /// <summary>
  245. /// 企业付款 - 用户到账通知
  246. /// </summary>
  247. /// <returns></returns>
  248. [Route("b2cpay")]
  249. [HttpPost]
  250. public async Task<IActionResult> B2CPay()
  251. {
  252. try
  253. {
  254. var notify = await _client.ExecuteAsync<QPayEPayB2CNotify>(Request, _optionsAccessor.Value);
  255. Console.WriteLine("OutTradeNo: " + notify.OutTradeNo);
  256. return QPayNotifyResult.Success;
  257. }
  258. catch
  259. {
  260. return NoContent();
  261. }
  262. }
  263. }
  264. #endregion
  265. #region 京东支付异步通知
  266. [Route("notify/jdpay")]
  267. public class JDPayNotifyController : Controller
  268. {
  269. private readonly IJDPayNotifyClient _client;
  270. private readonly IOptions<JDPayOptions> _optionsAccessor;
  271. public JDPayNotifyController(IJDPayNotifyClient client, IOptions<JDPayOptions> optionsAccessor)
  272. {
  273. _client = client;
  274. _optionsAccessor = optionsAccessor;
  275. }
  276. [Route("async")]
  277. [HttpPost]
  278. public async Task<IActionResult> Async()
  279. {
  280. try
  281. {
  282. var notify = await _client.ExecuteAsync<JDPayAsyncNotify>(Request, _optionsAccessor.Value);
  283. Console.WriteLine("TradeNum: " + notify.TradeNum + " tradeType :" + notify.TradeType);// notify.TradeType 0-消费 1-退款
  284. return JDPayNotifyResult.Success;
  285. }
  286. catch
  287. {
  288. return NoContent();
  289. }
  290. }
  291. [Route("defraypay")]
  292. [HttpPost]
  293. public async Task<IActionResult> DefrayPay()
  294. {
  295. try
  296. {
  297. var notify = await _client.ExecuteAsync<JDPayDefrayPayNotify>(Request, _optionsAccessor.Value);
  298. Console.WriteLine("trade_no: " + notify.TradeNo + " trade_amount :" + notify.TradeAmount);
  299. return JDPayNotifyResult.Success;
  300. }
  301. catch
  302. {
  303. return NoContent();
  304. }
  305. }
  306. }
  307. #endregion
  308. #region 连连支付异步通知
  309. [Route("notify/lianlianpay")]
  310. public class LianLianPayNotifyController : Controller
  311. {
  312. private readonly ILianLianPayNotifyClient _client;
  313. private readonly IOptions<LianLianPayOptions> _optionsAccessor;
  314. public LianLianPayNotifyController(ILianLianPayNotifyClient client, IOptions<LianLianPayOptions> optionsAccessor)
  315. {
  316. _client = client;
  317. _optionsAccessor = optionsAccessor;
  318. }
  319. [Route("receivemoney")]
  320. [HttpPost]
  321. public async Task<IActionResult> ReceiveMoney()
  322. {
  323. try
  324. {
  325. var notify = await _client.ExecuteAsync<LianLianPayReceiveMoneyNotify>(Request, _optionsAccessor.Value);
  326. Console.WriteLine("NoOrder: " + notify.NoOrder);
  327. return LianLianPayNotifyResult.Success;
  328. }
  329. catch
  330. {
  331. return NoContent();
  332. }
  333. }
  334. [Route("refund")]
  335. [HttpPost]
  336. public async Task<IActionResult> Refund()
  337. {
  338. try
  339. {
  340. var notify = await _client.ExecuteAsync<LianLianPayRefundNotify>(Request, _optionsAccessor.Value);
  341. Console.WriteLine("NoRefund: " + notify.NoRefund);
  342. return LianLianPayNotifyResult.Success;
  343. }
  344. catch
  345. {
  346. return NoContent();
  347. }
  348. }
  349. }
  350. #endregion
  351. #region 银联支付异步通知
  352. [Route("notify/unionpay")]
  353. public class UnionPayNotifyController : Controller
  354. {
  355. private readonly IUnionPayNotifyClient _client;
  356. public UnionPayNotifyController(IUnionPayNotifyClient client)
  357. {
  358. _client = client;
  359. }
  360. /// <summary>
  361. /// 网关支付 - 跳转网关页面支付通知
  362. /// </summary>
  363. /// <returns></returns>
  364. [Route("gatewaypayfrontconsume")]
  365. [HttpPost]
  366. public async Task<IActionResult> GatewayPayFrontConsume()
  367. {
  368. try
  369. {
  370. var notify = await _client.ExecuteAsync<UnionPayGatewayPayFrontConsumeNotify>(Request);
  371. Console.WriteLine("OrderId: " + notify.OrderId + " respCode :" + notify.RespCode);
  372. return UnionPayNotifyResult.Success;
  373. }
  374. catch
  375. {
  376. return NoContent();
  377. }
  378. }
  379. }
  380. #endregion
  381. }