|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- using BPA.KitChen.GroupMealOrder.Core.Cache;
- using BPA.KitChen.GroupMealOrder.Core.CacheOption;
- using BPA.KitChen.GroupMealOrder.Core.Entity;
- using BPA.KitChen.GroupMealOrder.SqlSugar;
- using BPA.KitChen.WeChat.WechatServer.Dtos;
- using Essensoft.Paylink.WeChatPay;
- using Essensoft.Paylink.WeChatPay.V2;
- using Essensoft.Paylink.WeChatPay.V2.Notify;
- using Essensoft.Paylink.WeChatPay.V2.Parser;
- using Furion;
- using Furion.DependencyInjection;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
-
- namespace BPA.KitChen.WeChat.WechatServer.Service
- {
- public class WechatNotityService : IWechatNotityService, ITransient
- {
-
-
- public readonly IWechatPayService _wechatPayService;
- private readonly IOptions<WeChatPayOptions> _optionsAccessor;
- private readonly IWeChatPayNotifyClient _client;
- public WechatNotityService(
- IWechatPayService wechatPayService,
- IWeChatPayNotifyClient client,
- IOptions<WeChatPayOptions> optionsAccessor)
- {
- _wechatPayService = wechatPayService;
- _optionsAccessor = optionsAccessor;
- _client = client;
- }
- /// <summary>
- /// 支付回调
- /// </summary>
- /// <returns></returns>
- public async Task<IActionResult> PayNotity()
- {
- Console.WriteLine("支付成功回调函数");
- using (var reader = new StreamReader(App.HttpContext.Request.Body, Encoding.UTF8, true, 1024, true))
- {
- var body = await reader.ReadToEndAsync();
- if (string.IsNullOrEmpty(body))
- {
- return WeChatPayNotifyResult.Failure;
- }
- var parser = new WeChatPayNotifyXmlParser<WeChatPayUnifiedOrderNotify>();
- var notify = parser.Parse(body);
- var config = ApolloApplication.Configs.Find(x => x.Key == notify.AppId);
-
- if (notify.ReturnCode == WeChatPayCode.Success)
- {
- if (notify.ResultCode == WeChatPayCode.Success)
- {
-
- //查询订单
- var order = await SqlSugarDb.Db.Queryable<BPA_WeighOrder>()
- .ClearFilter()
- .FirstAsync(x => x.OrderNumber == notify.OutTradeNo);
- if (order==null)
- {
- return WeChatPayNotifyResult.Failure;
- }
-
- if (order.States==1&&order.PayStates==1&&!string.IsNullOrEmpty(order.TransactionId))
- {
- return WeChatPayNotifyResult.Failure;
- }
-
- order.States = 1;
- order.PayStates = 1;
- order.TransactionId = notify.TransactionId;
- var res = await SqlSugarDb.Db.Updateable(order).ExecuteCommandAsync();
- if (res<=0)
- {
- return WeChatPayNotifyResult.Failure;
- }
- RedisHelper.RedisConn.GetDatabase().KeyDelete("order" + order.CreateId);
- return WeChatPayNotifyResult.Success;
- }
- }
- }
- return WeChatPayNotifyResult.Failure;
- }
-
- /// <summary>
- /// 退款回调
- /// </summary>
- public async Task<IActionResult> RefundNotity()
- {
- WeChatPayRefundNotify notify=new();
- WeChatPayOptions weChatPayOptions = new();
- ApolloApplicationConfig config=new();
- try
- {
- using (var reader = new StreamReader(App.HttpContext.Request.Body, Encoding.UTF8, true, 1024, true))
- {
- var body = await reader.ReadToEndAsync();
- if (string.IsNullOrEmpty(body))
- {
- return WeChatPayNotifyResult.Failure;
- }
- var parser = new WeChatPayNotifyXmlParser<WeChatPayRefundNotify>();
- notify = parser.Parse(body);
- config = ApolloApplication.Configs.Find(x => x.Key == notify.AppId);
- var notifyClient = await _client.ExecuteAsync<WeChatPayRefundNotify>(body, config.WechatPayConfig);
- if (notify.ReturnCode == WeChatPayCode.Success)
- {
- if (notify.RefundStatus == WeChatPayCode.Success)
- {
-
- var order = await SqlSugarDb.Db.Queryable<BPA_RefundWeighOrder>()
- .FirstAsync(x => x.WeighOrderId == notify.OutRefundNo);
-
- order.States = 1;
-
- return WeChatPayNotifyResult.Success;
- }
- }
- else
- {
- return WeChatPayNotifyResult.Failure;
- }
- }
-
- }
- catch (Exception ex)
- {
- return WeChatPayNotifyResult.Failure;
- }
- finally
- {
- notify = null;
- weChatPayOptions = null;
- config = null;
- }
-
- return WeChatPayNotifyResult.Failure;
- }
- }
- }
|