团餐订单
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

WechatNotityService.cs 5.5 KiB

9ヶ月前
9ヶ月前
9ヶ月前
9ヶ月前
9ヶ月前
9ヶ月前
9ヶ月前
9ヶ月前
9ヶ月前
9ヶ月前
9ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using BPA.KitChen.GroupMealOrder.Core.Cache;
  2. using BPA.KitChen.GroupMealOrder.Core.CacheOption;
  3. using BPA.KitChen.GroupMealOrder.Core.Entity;
  4. using BPA.KitChen.GroupMealOrder.SqlSugar;
  5. using BPA.KitChen.WeChat.WechatServer.Dtos;
  6. using Essensoft.Paylink.WeChatPay;
  7. using Essensoft.Paylink.WeChatPay.V2;
  8. using Essensoft.Paylink.WeChatPay.V2.Notify;
  9. using Essensoft.Paylink.WeChatPay.V2.Parser;
  10. using Furion;
  11. using Furion.DependencyInjection;
  12. using Microsoft.AspNetCore.Mvc;
  13. using Microsoft.Extensions.Options;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. namespace BPA.KitChen.WeChat.WechatServer.Service
  21. {
  22. public class WechatNotityService : IWechatNotityService, ITransient
  23. {
  24. public readonly IWechatPayService _wechatPayService;
  25. private readonly IOptions<WeChatPayOptions> _optionsAccessor;
  26. private readonly IWeChatPayNotifyClient _client;
  27. public WechatNotityService(
  28. IWechatPayService wechatPayService,
  29. IWeChatPayNotifyClient client,
  30. IOptions<WeChatPayOptions> optionsAccessor)
  31. {
  32. _wechatPayService = wechatPayService;
  33. _optionsAccessor = optionsAccessor;
  34. _client = client;
  35. }
  36. /// <summary>
  37. /// 支付回调
  38. /// </summary>
  39. /// <returns></returns>
  40. public async Task<IActionResult> PayNotity()
  41. {
  42. Console.WriteLine("支付成功回调函数");
  43. using (var reader = new StreamReader(App.HttpContext.Request.Body, Encoding.UTF8, true, 1024, true))
  44. {
  45. var body = await reader.ReadToEndAsync();
  46. if (string.IsNullOrEmpty(body))
  47. {
  48. return WeChatPayNotifyResult.Failure;
  49. }
  50. var parser = new WeChatPayNotifyXmlParser<WeChatPayUnifiedOrderNotify>();
  51. var notify = parser.Parse(body);
  52. var config = ApolloApplication.Configs.Find(x => x.Key == notify.AppId);
  53. if (notify.ReturnCode == WeChatPayCode.Success)
  54. {
  55. if (notify.ResultCode == WeChatPayCode.Success)
  56. {
  57. //查询订单
  58. var order = await SqlSugarDb.Db.Queryable<BPA_WeighOrder>()
  59. .ClearFilter()
  60. .FirstAsync(x => x.OrderNumber == notify.OutTradeNo);
  61. if (order==null)
  62. {
  63. return WeChatPayNotifyResult.Failure;
  64. }
  65. if (order.States==1&&order.PayStates==1&&!string.IsNullOrEmpty(order.TransactionId))
  66. {
  67. return WeChatPayNotifyResult.Failure;
  68. }
  69. order.States = 1;
  70. order.PayStates = 1;
  71. order.TransactionId = notify.TransactionId;
  72. var res = await SqlSugarDb.Db.Updateable(order).ExecuteCommandAsync();
  73. if (res<=0)
  74. {
  75. return WeChatPayNotifyResult.Failure;
  76. }
  77. RedisHelper.RedisConn.GetDatabase().KeyDelete("order" + order.CreateId);
  78. return WeChatPayNotifyResult.Success;
  79. }
  80. }
  81. }
  82. return WeChatPayNotifyResult.Failure;
  83. }
  84. /// <summary>
  85. /// 退款回调
  86. /// </summary>
  87. public async Task<IActionResult> RefundNotity()
  88. {
  89. WeChatPayRefundNotify notify=new();
  90. WeChatPayOptions weChatPayOptions = new();
  91. ApolloApplicationConfig config=new();
  92. try
  93. {
  94. using (var reader = new StreamReader(App.HttpContext.Request.Body, Encoding.UTF8, true, 1024, true))
  95. {
  96. var body = await reader.ReadToEndAsync();
  97. if (string.IsNullOrEmpty(body))
  98. {
  99. return WeChatPayNotifyResult.Failure;
  100. }
  101. var parser = new WeChatPayNotifyXmlParser<WeChatPayRefundNotify>();
  102. notify = parser.Parse(body);
  103. config = ApolloApplication.Configs.Find(x => x.Key == notify.AppId);
  104. var notifyClient = await _client.ExecuteAsync<WeChatPayRefundNotify>(body, config.WechatPayConfig);
  105. if (notify.ReturnCode == WeChatPayCode.Success)
  106. {
  107. if (notify.RefundStatus == WeChatPayCode.Success)
  108. {
  109. var order = await SqlSugarDb.Db.Queryable<BPA_RefundWeighOrder>()
  110. .FirstAsync(x => x.WeighOrderId == notify.OutRefundNo);
  111. order.States = 1;
  112. return WeChatPayNotifyResult.Success;
  113. }
  114. }
  115. else
  116. {
  117. return WeChatPayNotifyResult.Failure;
  118. }
  119. }
  120. }
  121. catch (Exception ex)
  122. {
  123. return WeChatPayNotifyResult.Failure;
  124. }
  125. finally
  126. {
  127. notify = null;
  128. weChatPayOptions = null;
  129. config = null;
  130. }
  131. return WeChatPayNotifyResult.Failure;
  132. }
  133. }
  134. }