| | |
| | | /// </summary> |
| | | public class EnterpriseWalletCommandHandler( |
| | | IRepository<EnterpriseWallet> rep, |
| | | IRepository<EnterpriseWalletTransaction> repEnterpriseWalletTransaction, |
| | | IRepository<Enterprise> repEnterprise, |
| | | AlipayUtils alipayUtils |
| | | ) : |
| | | IRequestHandler<OpenEnterpriseWalletCommand, OpenEnterpriseWalletCommandResult>, |
| | | IRequestHandler<CloseEnterpriseWalletCommand, Guid>, |
| | | IRequestHandler<RechargeEnterpriseWalletCommand, Guid>, |
| | | IRequestHandler<AlipayTransferCommand, Guid> |
| | | IRequestHandler<RechargeEnterpriseWalletCommand, RechargeEnterpriseWalletCommandResult>, |
| | | IRequestHandler<TransferEnterpriseWalletCommand, Guid> |
| | | { |
| | | private readonly IRepository<EnterpriseWallet> rep = rep; |
| | | private readonly IRepository<Enterprise> repEnterprise = repEnterprise; |
| | |
| | | public async Task<OpenEnterpriseWalletCommandResult> Handle(OpenEnterpriseWalletCommand request, CancellationToken cancellationToken) |
| | | { |
| | | var logier = JwtUtils.GetCurrentLogier(); |
| | | if (logier.Type == EnumUserType.Enterprise) |
| | | { |
| | | request.EnterpriseId = logier.EnterpriseId; |
| | | } |
| | | if (request.EnterpriseId == null) throw Oops.Oh(EnumErrorCodeType.s400, "请填写企业Id"); |
| | | var enterprise = await repEnterprise.AsQueryable().AsNoTracking() |
| | | .Where(it => it.Id == logier.EnterpriseId) |
| | | .Where(it => it.Id == request.EnterpriseId) |
| | | .FirstOrDefaultAsync(); |
| | | if (enterprise == null) throw Oops.Oh(EnumErrorCodeType.s404, "企业"); |
| | | if (!enterprise.IsReal) throw Oops.Oh(EnumErrorCodeType.s510, "请先实名"); |
| | | var entity = await rep.AsQueryable() |
| | | .Where(it => it.EnterpriseId == logier.EnterpriseId && it.Access == request.Access) |
| | | .Where(it => it.EnterpriseId == request.EnterpriseId && it.Access == request.Access) |
| | | .FirstOrDefaultAsync(); |
| | | if (entity == null) |
| | | { |
| | | entity = new EnterpriseWallet(); |
| | | entity.EnterpriseId = logier.EnterpriseId!.Value; |
| | | entity.EnterpriseId = request.EnterpriseId!.Value; |
| | | entity.Access = EnumEnterpriseWalletAccess.Alipay; |
| | | entity.Account = request.Account; |
| | | entity.MerchantId = request.MerchantId; |
| | | entity.PersonalProductCode = "FUND_SAFT_SIGN_WITHHOLDING_P"; |
| | | entity.SignScene = "INDUSTRY|SATF_ACC"; |
| | | entity.ThirdPartyType = "PARTNER"; |
| | |
| | | else |
| | | { |
| | | if (entity.SignStatus == EnumEnterpriseWalletSignStatus.Normal) throw Oops.Oh(EnumErrorCodeType.s510, "已签约"); |
| | | entity.Account = request.Account; |
| | | entity.MerchantId = request.MerchantId; |
| | | entity.SignStatus = EnumEnterpriseWalletSignStatus.Apply; |
| | | await rep.UpdateAsync(entity); |
| | | } |
| | |
| | | public async Task<Guid> Handle(CloseEnterpriseWalletCommand request, CancellationToken cancellationToken) |
| | | { |
| | | var logier = JwtUtils.GetCurrentLogier(); |
| | | if (logier.Type == EnumUserType.Enterprise) |
| | | { |
| | | request.EnterpriseId = logier.EnterpriseId; |
| | | } |
| | | if (request.EnterpriseId == null) throw Oops.Oh(EnumErrorCodeType.s400, "请填写企业Id"); |
| | | var entity = await rep.AsQueryable() |
| | | .Where(it => it.EnterpriseId == logier.EnterpriseId && it.Access == request.Access) |
| | | .Where(it => it.EnterpriseId == request.EnterpriseId && it.Access == request.Access) |
| | | .FirstOrDefaultAsync(); |
| | | if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "企业钱包"); |
| | | if (entity.SignStatus != EnumEnterpriseWalletSignStatus.Normal) throw Oops.Oh(EnumErrorCodeType.s510, "未签约"); |
| | |
| | | /// <param name="cancellationToken"></param> |
| | | /// <returns></returns> |
| | | /// <exception cref="NotImplementedException"></exception> |
| | | public async Task<Guid> Handle(RechargeEnterpriseWalletCommand request, CancellationToken cancellationToken) |
| | | [UnitOfWork(false)] |
| | | public async Task<RechargeEnterpriseWalletCommandResult> Handle(RechargeEnterpriseWalletCommand request, CancellationToken cancellationToken) |
| | | { |
| | | var logier = JwtUtils.GetCurrentLogier(); |
| | | if (logier.Type == EnumUserType.Enterprise) |
| | | { |
| | | request.EnterpriseId = logier.EnterpriseId; |
| | | } |
| | | if (request.EnterpriseId == null) throw Oops.Oh(EnumErrorCodeType.s400, "请填写企业Id"); |
| | | var entity = await rep.AsQueryable() |
| | | .Where(it => it.EnterpriseId == logier.EnterpriseId && it.Access == request.Access) |
| | | .Where(it => it.EnterpriseId == request.EnterpriseId && it.Access == request.Access) |
| | | .FirstOrDefaultAsync(); |
| | | if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "企业钱包"); |
| | | if (entity.SignStatus != EnumEnterpriseWalletSignStatus.Normal) throw Oops.Oh(EnumErrorCodeType.s510, "未签约"); |
| | | if (entity.AccountBookStatus != EnumEnterpriseWalletAccountBookStatus.Normal) throw Oops.Oh(EnumErrorCodeType.s510, "未开通记账本"); |
| | | await GetBalance(entity); |
| | | var order = new EnterpriseWalletTransaction(); |
| | | order.Code = $"{DateTime.Now:yyyyMMddHHmmss}{new Random(IDGen.NextID().GetHashCode()).Next(1000, 9999)}"; |
| | | order.Type = EnumEnterpriseWalletTransactionType.Recharge; |
| | | order.WalletId = entity.Id; |
| | | order.Amount = request.Amount; |
| | | order.Remark = request.Remark ?? "充值"; |
| | | order.ProductCode = "FUND_ACCOUNT_BOOK"; |
| | | order.BizScene = "SATF_DEPOSIT"; |
| | | order.TransactionStatus = EnumEnterpriseWalletTransactionStatus.WaitSubmit; |
| | | order.Balance = entity.Balance; |
| | | await SetCode(order); |
| | | await repEnterpriseWalletTransaction.InsertAsync(order); |
| | | |
| | | var response = alipayUtils.FundTransPagePay(new AlipayFundTransPagePayModel |
| | | { |
| | | OutBizNo = order.Code, |
| | | TransAmount = order.Amount.ToString(), |
| | | ProductCode = "FUND_ACCOUNT_BOOK", |
| | | BizScene = "SATF_DEPOSIT", |
| | | Remark = "记账本充值", |
| | | OrderTitle = "记账本充值", |
| | | TimeExpire = DateTime.Now.AddHours(1).ToString("yyyy-MM-dd HH:mm:ss"), |
| | | ProductCode = order.ProductCode, |
| | | BizScene = order.BizScene, |
| | | Remark = order.Remark, |
| | | OrderTitle = order.Remark, |
| | | TimeExpire = DateTime.Now.AddMinutes(15).ToString("yyyy-MM-dd HH:mm"), |
| | | PayeeInfo = new Participant |
| | | { |
| | | IdentityType = "ACCOUNT_BOOK_ID", |
| | |
| | | agreement_no = entity.AgreementNo, |
| | | }.ToJson(), |
| | | }, |
| | | }); |
| | | }, "/api/user/enterpriseWallet/alipayFundTransOrderChangedNotify"); |
| | | if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg); |
| | | order.OrderId = response.OrderId; |
| | | return order.Id; |
| | | order.TransactionStatus = EnumEnterpriseWalletTransactionStatus.WaitPay; |
| | | await repEnterpriseWalletTransaction.UpdateAsync(order); |
| | | return new RechargeEnterpriseWalletCommandResult |
| | | { |
| | | PayUrl = response.Body |
| | | }; |
| | | } |
| | | |
| | | public async Task<Guid> Handle(AlipayTransferCommand request, CancellationToken cancellationToken) |
| | | /// <summary> |
| | | /// 企业钱包转账 |
| | | /// </summary> |
| | | /// <param name="request"></param> |
| | | /// <param name="cancellationToken"></param> |
| | | /// <returns></returns> |
| | | [UnitOfWork(false)] |
| | | public async Task<Guid> Handle(TransferEnterpriseWalletCommand request, CancellationToken cancellationToken) |
| | | { |
| | | var logier = JwtUtils.GetCurrentLogier(); |
| | | var entity = await rep.AsQueryable() |
| | | .Where(it => it.EnterpriseId == logier.EnterpriseId && it.Access == EnumEnterpriseWalletAccess.Alipay) |
| | | .FirstOrDefaultAsync(); |
| | | if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, "企业钱包"); |
| | | if (entity.SignStatus != EnumEnterpriseWalletSignStatus.Normal) throw Oops.Oh(EnumErrorCodeType.s510, "未签约"); |
| | | if (entity.AccountBookStatus != EnumEnterpriseWalletAccountBookStatus.Normal) throw Oops.Oh(EnumErrorCodeType.s510, "未开通记账本"); |
| | | await GetBalance(entity); |
| | | |
| | | var order = new EnterpriseWalletTransaction(); |
| | | order.Type = EnumEnterpriseWalletTransactionType.Recharge; |
| | | order.WalletId = entity.Id; |
| | | order.Amount = request.Amount; |
| | | order.Remark = request.Remark; |
| | | order.ProductCode = "SINGLE_TRANSFER_NO_PWD"; |
| | | order.BizScene = "ENTRUST_TRANSFER"; |
| | | order.TransactionStatus = EnumEnterpriseWalletTransactionStatus.WaitSubmit; |
| | | order.Balance = entity.Balance; |
| | | await SetCode(order); |
| | | await repEnterpriseWalletTransaction.InsertAsync(order); |
| | | |
| | | var response = alipayUtils.FundTransUniTransfer(new AlipayFundTransUniTransferModel |
| | | { |
| | | OutBizNo = $"Test{DateTime.Now:yyyyMMddHHmmss}{new Random(IDGen.NextID().GetHashCode()).Next(1000, 9999)}", |
| | | TransAmount = "0.01", |
| | | ProductCode = "TRANS_ACCOUNT_NO_PWD", |
| | | OutBizNo = order.Code, |
| | | TransAmount = order.Amount.ToString(), |
| | | ProductCode = order.ProductCode, |
| | | BizScene = order.BizScene, |
| | | PayeeInfo = new Participant |
| | | { |
| | | IdentityType = "BANKCARD_ACCOUNT", |
| | | Identity = "6214180000016351382", |
| | | Name = "孙鹏飞", |
| | | BankcardExtInfo = new BankcardExtInfo |
| | | { |
| | | AccountType = "2", |
| | | InstName = "宁波银行" |
| | | } |
| | | IdentityType = "ALIPAY_LOGON_ID", |
| | | Identity = "13616515310", |
| | | Name = "杨振宇", |
| | | }, |
| | | PayerInfo = new Participant |
| | | { |
| | | IdentityType = "ALIPAY_OPEN_ID", |
| | | Identity = entity.PrincipalOpenId, |
| | | Name = "孙鹏飞" |
| | | IdentityType = "ACCOUNT_BOOK_ID", |
| | | Identity = entity.AccountBookId, |
| | | ExtInfo = new |
| | | { |
| | | agreement_no = entity.AgreementNo, |
| | | }.ToJson(), |
| | | }, |
| | | BizScene = "CAE_TRANSFER", |
| | | OrderTitle = "测试代扣", |
| | | Remark = "测试代扣备注" |
| | | OrderTitle = order.Remark, |
| | | Remark = order.Remark, |
| | | BusinessParams = new |
| | | { |
| | | withdraw_timeliness = "T0" |
| | | }.ToJson() |
| | | }, "/api/user/enterpriseWallet/alipayFundTransOrderChangedNotify"); |
| | | if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg); |
| | | order.OrderId = response.OrderId; |
| | | order.PayFundOrderId = response.PayFundOrderId; |
| | | order.SettleSerialNo = response.SettleSerialNo; |
| | | order.TransDate = response.TransDate.ToDateTime(); |
| | | order.Link = response.Link; |
| | | order.Status = response.Status; |
| | | order.SubStatus = response.SubStatus; |
| | | order.TransactionStatus = response.Status == "SUCCESS" |
| | | ? EnumEnterpriseWalletTransactionStatus.Success |
| | | : response.Status == "DEALING" |
| | | ? EnumEnterpriseWalletTransactionStatus.Dealing |
| | | : response.Status == "REFUND" |
| | | ? EnumEnterpriseWalletTransactionStatus.Refund |
| | | : response.Status == "FAIL" |
| | | ? EnumEnterpriseWalletTransactionStatus.Fail |
| | | : throw Oops.Oh(EnumErrorCodeType.s510, $"未识别的状态:{response.Status}"); |
| | | await repEnterpriseWalletTransaction.UpdateAsync(order); |
| | | if (order.TransactionStatus == EnumEnterpriseWalletTransactionStatus.Success) |
| | | { |
| | | await GetBalance(entity); |
| | | } |
| | | return entity.Id; |
| | | } |
| | | |
| | | private async Task GetBalance(EnterpriseWallet entity) |
| | | { |
| | | var response = alipayUtils.FundAccountbookQuery(new AlipayFundAccountbookQueryModel |
| | | { |
| | | AccountBookId = entity.AccountBookId, |
| | | SceneCode = "SATF_FUND_BOOK", |
| | | MerchantUserId = entity.Code, |
| | | }); |
| | | if (response.IsError) throw Oops.Oh(EnumErrorCodeType.s510, response.SubMsg ?? response.Msg); |
| | | return entity.Id; |
| | | entity.Balance = response.AvailableAmount.ToDecimal() ?? 0; |
| | | await rep.UpdateNowAsync(entity); |
| | | } |
| | | |
| | | private async Task SetCode(EnterpriseWalletTransaction entity) |
| | | { |
| | | entity.Code = $"{DateTime.Now:yyyyMMddHHmmss}{new Random(IDGen.NextID().GetHashCode()).Next(1000, 9999)}"; |
| | | var exist = await repEnterpriseWalletTransaction.AsQueryable().AsNoTracking() |
| | | .AnyAsync(it => it.Code == entity.Code); |
| | | if (exist) |
| | | { |
| | | await SetCode(entity); |
| | | } |
| | | } |
| | | |
| | | private async Task SetCode(EnterpriseWallet entity) |