using GDZZ.Application.Entity; using GDZZ.Core.Entity; using GDZZ.Core; using Microsoft.AspNetCore.Mvc; using System; using System.Threading.Tasks; using Furion.EventBus; using Microsoft.Extensions.Options; using Microsoft.AspNetCore.Http; using Furion.DependencyInjection; using Furion.DynamicApiController; using GDZZ.Application.Service.WXPay.Dto; using TenPayOldV3 = Senparc.Weixin.TenPay.V3.TenPayV3; using Senparc.Weixin; using Senparc.Weixin.TenPay.V3; using Senparc.Weixin.TenPay; namespace GDZZ.Application.Service.WXPay { [ApiDescriptionSettings("Application", Name = "WXPay", Order = 1)] public class WXPayService: IWXPayService, IDynamicApiController, ITransient { private readonly SqlSugarRepository Baseuser; // wx用户仓储 private readonly SqlSugarRepository _sysUserRep; // 用户表仓储 private readonly SqlSugarRepository _sysTenantRep; //租户仓储 private readonly SqlSugarRepository self; //职业仓储 private readonly SqlSugarRepository balance; //职业仓储 private readonly SqlSugarRepository rechargeRep; //充值仓储 private readonly SqlSugarRepository payTakeRep; //支付仓储 private readonly WechatOAuth _wechatOAuth; //微信权限服务 private readonly IEventPublisher _eventPublisher; //事件写入服务 private readonly IHttpContextAccessor _httpContextAccessor; //http服务 /// /// 获取配置文件 /// private readonly SenparcWeixinSetting _oauthConfig; public WXPayService( IOptions options, SqlSugarRepository Baseuser, SqlSugarRepository sysTenantRep, SqlSugarRepository sysUserRep, SqlSugarRepository balance, SqlSugarRepository Self, SqlSugarRepository rechargeRep, SqlSugarRepository payTakeRep, WechatOAuth wechatOAuth, IHttpContextAccessor _httpContextAccessor, IEventPublisher eventPublisher) { this.balance = balance; this.Baseuser= Baseuser; this._sysUserRep= sysUserRep; this._sysTenantRep= sysTenantRep; this.self= Self; this.rechargeRep = rechargeRep; this.payTakeRep= payTakeRep; this._wechatOAuth= wechatOAuth; this._oauthConfig =options.Value.SenparcWeixin; this._httpContextAccessor = _httpContextAccessor; } /// /// 查询余额 /// /// [HttpGet] [Route("Mini/v1/GetUserBalance")] public async Task GetUserBalance() { var balan = await this.balance.SingleAsync(x => x.UserID == UserManager.UserId); if (balan == null) return null; return new BalanceOut() { Amount= balan.Amount, UserID= UserManager.UserId, }; } /// ///微信小程序支付 /// /// 商品Id /// [HttpPost] [Route("Mini/v1/wxpay")] public async Task WxPay(AuthUserInput authUserInput) { var recharge = await this.rechargeRep.InsertReturnEntityAsync(new MiniRecharge() { PaymentMoney = authUserInput.Money, Status = (int)RechargeEnum.NoFinis, TotalPrice = authUserInput.Money }); var payTake = await this.payTakeRep.InsertReturnEntityAsync(new MiniPayTake() { PaymentMoney = authUserInput.Money, OrderId = recharge.Id, PaySn = "", PayStatus = (int)PayStatusEnum.NotPaying }); string sp_billno = $"{Config.SenparcWeixinSetting.TenPayV3_MchId}{SystemTime.Now:yyyyMMddHHmmss}{TenPayV3Util.BuildRandomStr(6)}"; string timeStamp = TenPayV3Util.GetTimestamp(); string nonceStr = TenPayV3Util.GetNoncestr(); TenPayV3UnifiedorderRequestData xmlDataInfo = new TenPayV3UnifiedorderRequestData(Config.SenparcWeixinSetting.WxOpenAppId, Config.SenparcWeixinSetting.TenPayV3_MchId, null, sp_billno, (int)authUserInput.Money * 100, "127.0.0.1", Config.SenparcWeixinSetting.TenPayV3_WxOpenTenpayNotify, TenPayV3Type.JSAPI, authUserInput.OpenID, Config.SenparcWeixinSetting.TenPayV3_Key, nonceStr, null, null, null, null, payTake.OrderId.ToString()); Console.WriteLine(xmlDataInfo.PackageRequestHandler.ParseXML()); var result = TenPayOldV3.Unifiedorder(xmlDataInfo);//调用统一订单接口 string packageStr = "prepay_id=" + result.prepay_id; return new { success = true, result.prepay_id, appId = Config.SenparcWeixinSetting.WxOpenAppId, timeStamp, nonceStr, package = packageStr, signType = "MD5", paySign = TenPayV3.GetJsPaySign(Config.SenparcWeixinSetting.WxOpenAppId, timeStamp, nonceStr, packageStr, Config.SenparcWeixinSetting.TenPayV3_Key) }; } /// /// 微信小程序支付回调 /// /// [HttpPost] [Route("Mini/v1/NotifyUrl")] public async Task NotifyUrl() { return null; //try //{ // Console.WriteLine(""); // ResponseHandler resHandler = new ResponseHandler(_httpContextAccessor.HttpContext); // string returnCode = resHandler.GetParameter("return_code"); // resHandler.SetKey(TenPayKey); // if (resHandler.IsTenpaySign() && returnCode.ToUpper() == "SUCCESS") // { // //attach // var paymentId =long.Parse(resHandler.GetParameter("attach")); // //业务处理 // var paytake = await this.payTakeRep.Where(x => x.OrderId == paymentId).SingleAsync(); // if (!paytake.IsEmpty()) // { // paytake.PayStatus =(int)PayStatusEnum.Paying; // this.payTakeRep.Update(paytake); // } // var recharge = await this.rechargeRep.Where(x=>x.Id == paymentId).SingleAsync(); // if(!recharge.IsEmpty()) // { // recharge.Status = (int)RechargeEnum.Finish; // this.rechargeRep.Update(recharge); // var balan = await this.balance.Where(x => x.UserID == paytake.CreatedUserId).SingleAsync(); // if (!balan.IsEmpty()) // { // balan.Amount += recharge.PaymentMoney; // this.balance.Update(balan); // } // } // } // string xml = "" // + "SUCCESS" // + "OK" // + ""; // return xml; //} //catch (Exception ex) //{ // string xml = "" // + "FAIL" // + "支付通知失败" // + ""; // return xml; //} } } }