示例#1
0
        public string CreateOrder(Guid merchantAccountId, string fiatCurrency, int cryptoId, decimal amount, PaymentType paymentType, string userToken, string clientIP)
        {
            var accountDAC = new MerchantAccountDAC();

            var account = accountDAC.GetById(merchantAccountId);

            if (!account.IsAllowAcceptPayment)
            {
                throw new CommonException(ReasonCode.Not_Allow_Withdrawal, Resources.禁止收款);
            }

            var coin = new CryptocurrencyDAC().GetById(cryptoId);

            if (!coin.Status.HasFlag(CryptoStatus.Pay))
            {
                throw new CommonException(ReasonCode.CURRENCY_FORBIDDEN, Resources.CurrencyForbidden);
            }

            if (coin.Enable == (byte)CurrencyStatus.Forbidden)
            {
                throw new CommonException(ReasonCode.CURRENCY_FORBIDDEN, Resources.CurrencyForbidden);
            }

            var order = new RedisOrderDTO()
            {
                FiatAmount   = amount,
                CryptoId     = cryptoId,
                FiatCurrency = fiatCurrency,
                MerchantGuid = account.Id,
                OrderNo      = NumberGenerator.GenerateUnixOrderNo(),
                CountryId    = account.CountryId,
                Markup       = account.Markup,
                Type         = paymentType,
                UserId       = Guid.Empty
            };

            Guid?userAccountId = null;

            if (!string.IsNullOrEmpty(userToken))
            {
                if (!userToken.StartsWith(Constant.PAYMENT_CODE_PREFIX))
                {
                    throw new CommonException(ReasonCode.GENERAL_ERROR, Resources.无效的用户Token);
                }

                var paymentInfo = RedisHelper.Get <PaymentCodeDTO>(
                    Constant.REDIS_PAYMENT_CODE_DBINDEX,
                    $"{Constant.REDIS_PAYMENT_CODE_PREFIX}{userToken}");
                if (paymentInfo == null)
                {
                    throw new CommonException(ReasonCode.GENERAL_ERROR, Resources.无效的用户Token);
                }
                userAccountId = paymentInfo.UserId;
            }
            if (userAccountId.HasValue)
            {
                order.UserId = userAccountId.Value;

                SendPayOrderModel model = new SendPayOrderModel();
                model.OrderNo       = order.OrderNo;
                model.UserAccountId = userAccountId.Value.ToString();

                MerchantMSMQ.PubPayOrder(model, 0);
            }
            RedisHelper.StringSet($"fiiipos:order:{ order.OrderNo}", JsonConvert.SerializeObject(order), TimeSpan.FromMinutes(30));
            return(order.OrderNo);
        }
示例#2
0
        private OrderWithdrawalFee CalculateOrderAmount(ref Order order, RedisOrderDTO orderDto, MerchantAccount account, Cryptocurrency coin)
        {
            var orderWithdrawalFee = new OrderWithdrawalFee()
            {
                Timestamp = DateTime.UtcNow
            };

            var wallet = new MerchantWalletDAC().GetByAccountId(account.Id, new CryptocurrencyDAC().GetByCode("FIII").Id);

            if (wallet == null || !wallet.SupportReceipt)
            {
                Excute(ref order);
            }
            else
            {
                var exchangeFiiiRate = GetExchangeRate(orderDto.CountryId, order.FiatCurrency, new CryptocurrencyDAC().GetById(wallet.CryptoId));
                var fiiiCoin         = new CryptocurrencyDAC().GetById(wallet.CryptoId);

                //var tempFiatActualAmount = orderDto.FiatAmount * (1 + account.Markup);
                var tempFiatActualAmount = orderDto.FiatAmount + (orderDto.FiatAmount * account.Markup).ToSpecificDecimal(4);

                var fiiiTransactionFee = ((orderDto.FiatAmount + orderDto.FiatAmount * account.Markup) * account.Receivables_Tier / exchangeFiiiRate)
                                         .ToSpecificDecimal(
                    fiiiCoin.DecimalPlace);
                if (wallet.Balance < fiiiTransactionFee)
                {
                    Excute(ref order);
                }
                else
                {
                    orderWithdrawalFee.CryptoId = fiiiCoin.Id;
                    orderWithdrawalFee.Amount   = fiiiTransactionFee;

                    order.ActualFiatAmount   = tempFiatActualAmount.ToSpecificDecimal(4);
                    order.CryptoAmount       = ((orderDto.FiatAmount + orderDto.FiatAmount * account.Markup) / order.ExchangeRate).ToSpecificDecimal(coin.DecimalPlace);
                    order.TransactionFee     = 0;
                    order.ActualCryptoAmount = order.CryptoAmount;

                    var model = CalculateUnifiedAmount(order.CryptoAmount, order.ActualCryptoAmount, order.UnifiedExchangeRate);
                    order.UnifiedFiatAmount       = model.UnifiedFiatAmount;
                    order.UnifiedActualFiatAmount = model.UnifiedActualFiatAmount;
                }
            }
            void Excute(ref Order inOrder)
            {
                var calcModel =
                    CalculateAmount(orderDto.FiatAmount, account.Markup, account.Receivables_Tier, inOrder.ExchangeRate, coin);

                inOrder.ActualFiatAmount   = calcModel.FiatTotalAmount;
                inOrder.CryptoAmount       = calcModel.CryptoAmount;
                inOrder.TransactionFee     = calcModel.TransactionFee;
                inOrder.ActualCryptoAmount = calcModel.ActualCryptoAmount;

                var model = CalculateUnifiedAmount(inOrder.CryptoAmount, inOrder.ActualCryptoAmount, inOrder.UnifiedExchangeRate);

                inOrder.UnifiedFiatAmount       = model.UnifiedFiatAmount;
                inOrder.UnifiedActualFiatAmount = model.UnifiedActualFiatAmount;

                orderWithdrawalFee.CryptoId = inOrder.CryptoId;
                orderWithdrawalFee.Amount   = 0;
            }

            return(orderWithdrawalFee);
        }