Exemplo n.º 1
0
        private PaymentOrder BuildPaymentOrder(VerifiedPaymentOrderDto verifiedPaymentOrder, decimal feeRate, string key)
        {
            var input        = verifiedPaymentOrder.PaymentOrder;
            var paymentOrder = new PaymentOrder()
            {
                TenantId        = input.TenantId,
                AppId           = input.AppId,
                Amount          = input.Amount,
                Fee             = Convert.ToInt32(input.Amount * (feeRate / 100)),
                CompanyId       = verifiedPaymentOrder.CompanyId,
                AccountId       = verifiedPaymentOrder.AccountId,
                Id              = idGenerator.NextId(),
                ExternalOrderId = input.ExternalOrderId,
                ChannelId       = verifiedPaymentOrder.ChannelId,
                BankId          = verifiedPaymentOrder.BankId,
                CreateIp        = input.CreateIp,
                AsyncCallback   = input.AsyncCallback,
                SyncCallback    = input.SyncCallback,
                CallbackStatus  = CallbackStatus.Pending,
                Expire          = Clock.Now.AddHours(2),
                ExtData         = input.ExtData,
                DeviceType      = input.DeviceType
            };

            paymentOrder.Md5 = paymentOrder.GetMd5(key);
            paymentOrder.SetNewOrder();
            return(paymentOrder);
        }
Exemplo n.º 2
0
        public async Task <ResultDto <PaymentOrderDto> > PlaceOrderAsync(CreatePaymentOrderDto input)
        {
            var tenant = tenantCache.Get(input.TenantId);

            if (!string.IsNullOrEmpty(input.ExternalOrderId))
            {
                var isDuplicate = await paymentOrderRepository.GetAll().AnyAsync(o =>
                                                                                 o.TenantId == input.TenantId && o.ExternalOrderId == input.ExternalOrderId);

                if (isDuplicate)
                {
                    return(new ResultDto <PaymentOrderDto> {
                        IsSuccess = false, ErrorMessage = "商户订单号重复"
                    });
                }
            }
            //支付通道
            var channel = await channelRepository.FirstOrDefaultAsync(c => c.ChannelCode == input.ChannelCode);

            if (channel == null)
            {
                return(new ResultDto <PaymentOrderDto>()
                {
                    IsSuccess = false, ErrorMessage = "支付通道编码错误"
                });
            }
            int channelId = channel.Id;
            //银行
            int?       bankId = null;
            List <int> excludedBankCompanies = null;

            if (channel.RequiredBank)
            {
                var bank = await bankRepository.FirstOrDefaultAsync(b => b.BankCode == input.BankCode);

                if (bank != null)
                {
                    bankId = bank.Id;
                    excludedBankCompanies = await bankOverrideRepository.GetAll().Where(bo => bo.BankId == bank.Id && bo.OverrideIsActive == false).Select(bo => bo.CompanyId).ToListAsync();
                }
                else
                {
                    return(new ResultDto <PaymentOrderDto>()
                    {
                        IsSuccess = false, ErrorMessage = "银行编码错误"
                    });
                }
            }
            //TODO 支付订单策略

            var policies = await paymentOrderPolicyService.GetPoliciesAsync(input.TenantId, channelId);

            if (policies == null)
            {
                return(new ResultDto <PaymentOrderDto> {
                    IsSuccess = false, ErrorMessage = "没有匹配的支付接口"
                });
            }

            var verifiedPaymentOrder = new VerifiedPaymentOrderDto()
            {
                PaymentOrder = input
            };
            bool isUnsupportedBank = false;

            //订单提交策略验证
            foreach (var item in policies)
            {
                if (await paymentOrderPolicyService.VerifyPolicyAsync(item, input))
                {
                    if (excludedBankCompanies != null && excludedBankCompanies.Contains(item.CompanyId))
                    {
                        isUnsupportedBank = true;
                        continue;
                    }
                    verifiedPaymentOrder.Success   = true;
                    verifiedPaymentOrder.CompanyId = item.CompanyId;
                    verifiedPaymentOrder.AccountId = item.AccountId;
                    verifiedPaymentOrder.ChannelId = channelId;
                    verifiedPaymentOrder.BankId    = bankId;
                    break;
                }
            }

            if (verifiedPaymentOrder.CompanyId == 0)
            {
                if (isUnsupportedBank)
                {
                    return(new ResultDto <PaymentOrderDto>()
                    {
                        IsSuccess = false, ErrorMessage = "不支持该银行"
                    });
                }
                else
                {
                    return(new ResultDto <PaymentOrderDto>()
                    {
                        IsSuccess = false, ErrorMessage = "没有可用的第三方平台"
                    });
                }
            }

            if (verifiedPaymentOrder.AccountId == 0)
            {
                return(new ResultDto <PaymentOrderDto>()
                {
                    IsSuccess = false, ErrorMessage = "没有可用的第三方账号"
                });
            }
            var company = await companyRepository.GetAsync(verifiedPaymentOrder.CompanyId);

            var account = await accountRepository.GetAsync(verifiedPaymentOrder.AccountId);

            var channelOverride = await channelOverrideRepository.FirstOrDefaultAsync(co => co.CompanyId == company.Id && co.ChannelId == channelId);

            var paymentOrder = BuildPaymentOrder(verifiedPaymentOrder, GetFeeRate(company, channelOverride, account), input.TransparentKey);
            await paymentOrderRepository.InsertAsync(paymentOrder);

            await CurrentUnitOfWork.SaveChangesAsync();

            return(new ResultDto <PaymentOrderDto>
            {
                IsSuccess = true,
                Data = ObjectMapper.Map <PaymentOrderDto>(paymentOrder),
                ErrorMessage = "提交成功"
            });
        }