Beispiel #1
0
        public BillerDetailOM Detail(UserAccount account, Guid id)
        {
            var order       = new BillerOrderDAC().GetById(id);
            var currentRate = new CryptocurrencyAgent()
                              .GetMarketPrice(account.CountryId, order.FiatCurrency, order.CryptoCode).Price;

            return(new BillerDetailOM()
            {
                CryptoCode = order.CryptoCode,
                Id = order.Id,
                CurrentExchangeRate = $"1 {order.CryptoCode} = {currentRate.ToString(4)} {order.FiatCurrency}",
                BillerCode = order.BillerCode,
                Status = order.Status,
                Tag = order.Tag,
                ReferenceNumber = order.ReferenceNumber,
                FiatAmount = order.FiatAmount.ToString(2),
                FiatCurrency = order.FiatCurrency,
                ExchangeRate = $"1 {order.CryptoCode} = {order.ExchangeRate.ToString(4)} {order.FiatCurrency}",
                IncreaseRate = ((currentRate - order.ExchangeRate) / order.ExchangeRate) > 0 ? $"+{((currentRate - order.ExchangeRate) / order.ExchangeRate*100).ToString(2)}" : ((currentRate - order.ExchangeRate) / order.ExchangeRate * 100).ToString(2),
                CryptoAmount = order.CryptoAmount.ToString(CultureInfo.InvariantCulture),
                Timestamp = order.PayTime.ToUnixTime().ToString(),
                OrderNo = order.OrderNo,
                Remark = order.Remark,
                StatusStr = order.Status == BillerOrderStatus.Complete ? Resources.BillerOrderComplete : order.Status == BillerOrderStatus.Fail ? Resources.BillerOrderFail : Resources.BillerOrderPending,
                TypeStr = Resources.BillerDetailType
            });
        }
        public void PushBiller(Guid id)
        {
            var billerOrder = new BillerOrderDAC().GetById(id);
            var regId       = RedisHelper.StringGet($"FiiiPay:Notice:UserId:{billerOrder.AccountId}");

            var lang        = RedisHelper.StringGet(REDIS_LANGUAGE_DBINDEX, $"FiiiPay:Language:{billerOrder.AccountId}") ?? "en";
            var titleKey    = billerOrder.Status == BillerOrderStatus.Complete ? "BillerTitle" : "BillerFailTitle";
            var subTitleKey = billerOrder.Status == BillerOrderStatus.Complete ? "BillerCompleteSubTitle" : "BillerFailSubTitle";

            if (!(_resourcePropertyNames.Contains(titleKey) && _resourcePropertyNames.Contains(subTitleKey)))
            {
                throw new Exception("没有找到资源");
            }
            var content  = ResourceHelper.FiiiPay.GetFormatResource(titleKey, lang, billerOrder.CryptoCode);
            var subTitle = ResourceHelper.FiiiPay.GetFormatResource(subTitleKey, lang);

            string noticeId = "";

            //写MongoDB [消费(支付成功)]
            MessagesComponent.AddMessage(billerOrder.AccountId, UserType.User, billerOrder.Id.ToString(), billerOrder.Status == BillerOrderStatus.Complete ? FiiiPayPushType.TYPE_BILLER_COMPLETE : FiiiPayPushType.TYPE_BILLER_FAIL, titleKey, subTitleKey, billerOrder.CryptoCode, content, subTitle, out noticeId);

            RegPush(billerOrder.Status == BillerOrderStatus.Complete ? FiiiPayPushType.TYPE_BILLER_COMPLETE : FiiiPayPushType.TYPE_BILLER_FAIL, new List <string> {
                regId
            }, billerOrder.Id, content, subTitle, noticeId);
            LogHelper.Info($"--------{lang}------{content}----------{subTitle}");
        }
Beispiel #3
0
        public BillerMessageFailOM MessageFail(Guid id)
        {
            var order = new BillerOrderDAC().GetById(id);

            return(new BillerMessageFailOM()
            {
                Content = order.Remark,
                Timestamp = order.FinishTime.ToUnixTime().ToString(),
                Title = Resources.BillerMessageFailTitle
            });
        }
Beispiel #4
0
        public BillerPrePayOM PrePay(BillerPrePayIM im)
        {
            var exchangeRate = new PriceInfoDAC()
                               .GetPriceInfo(new CurrenciesDAC().GetByCode(im.FiatCurrency).ID, im.CryptoId).Price;
            var discount = im.CryptoCode.Equals("Fiii", StringComparison.InvariantCultureIgnoreCase)
                ? 1 - decimal.Parse(new MasterSettingDAC().SelectByGroup("BillerMaxAmount").First(item =>
                                                                                                  item.Name.Equals("DiscountRate", StringComparison.CurrentCultureIgnoreCase)).Value)
                : 1;
            var errorTolerantRate =
                decimal.Parse(new MasterSettingDAC().Single("BillerMaxAmount", "Error_Tolerant_Rate").Value);

            if (!(im.CryptoAmount <= ((im.FiatAmount / exchangeRate) * discount) * (1 + errorTolerantRate) && im.CryptoAmount >= ((im.FiatAmount / exchangeRate) * discount) * (1 - errorTolerantRate)))
            {
                throw new CommonException(ReasonCode.BillerInvalidValues, Resources.BillerExchangeRateError);
            }

            var dac      = new BillerOrderDAC();
            var maxValue = decimal.Parse(new MasterSettingDAC().Single("BillerMaxAmount", "Biller_MaxAmount ").Value);

            if (im.FiatAmount > maxValue)
            {
                throw new CommonException(ReasonCode.BillerOverMaxAmount, string.Format(Resources.BillerOverMaxAmount, $"{maxValue} {im.FiatCurrency}"));
            }

            var totalMonthAmount = dac.GetMonthAmount(DateTime.UtcNow);

            if (totalMonthAmount + im.FiatAmount >
                decimal.Parse(new MasterSettingDAC().Single("BillerMaxAmount", "Biller_Month_MaxAmount").Value))
            {
                throw new CommonException(ReasonCode.BillerOverMonthMaxAmount, Resources.BillerOverMonthMaxAmount);
            }
            var totalDayAmount = dac.GetDayAmount(DateTime.UtcNow);

            if (totalDayAmount + im.FiatAmount >
                decimal.Parse(new MasterSettingDAC().Single("BillerMaxAmount", "Biller_Day_MaxAmount").Value))
            {
                throw new CommonException(ReasonCode.BillerOverDayMaxAmount, Resources.BillerOverDayMaxAmount);
            }

            if (new CountryComponent().GetById(im.CountryId) == null)
            {
                return(new BillerPrePayOM()
                {
                    Status = 5
                });
            }
            return(new BillerPrePayOM()
            {
                Status = 0
            });
        }
Beispiel #5
0
        public bool BillerRefund(string orderNo)
        {
            var orderDac = new BillerOrderDAC();
            var order    = FiiiPayDB.DB.Queryable <BillerOrders>().Where(t => t.OrderNo.Equals(orderNo)).First();

            if (order == null)
            {
                throw new CommonException(10000, "Order does not exist!");
            }

            var merchantWalletDAC = new MerchantWalletDAC();
            var userWalletDAC     = new UserWalletDAC();

            var userWallet = userWalletDAC.GetByAccountId(order.AccountId, order.CryptoId);

            if (userWallet == null)
            {
                throw new CommonException(10001, "A currency that is not supported by the user");
            }
            using (var scope = new TransactionScope())
            {
                userWalletDAC.Increase(order.AccountId, order.CryptoId, order.CryptoAmount);
                new UserWalletStatementDAC().Insert(new UserWalletStatement
                {
                    WalletId  = userWallet.Id,
                    Action    = "REFUND",
                    Amount    = order.CryptoAmount,
                    Balance   = userWallet.Balance + order.CryptoAmount,
                    Timestamp = DateTime.UtcNow,
                    Remark    = $"Refund from billerorder({order.OrderNo})"
                });

                order.Status = BillerOrderStatus.Fail;
                orderDac.UpdateStatus(order);

                new RefundDAC().Insert(new Refund
                {
                    OrderId   = order.Id,
                    Status    = RefundStatus.Completed,
                    Timestamp = DateTime.UtcNow
                });
                scope.Complete();
            }
            return(true);
        }
Beispiel #6
0
        public BillerPayOM Pay(UserAccount account, BillerPayIM im, ref int errorCode)
        {
            var coin = new CryptocurrencyDAC().GetById(im.CryptoId);

            if (!coin.Status.HasFlag(CryptoStatus.Biller) || coin.Enable == 0)
            {
                throw new CommonException(ReasonCode.CURRENCY_FORBIDDEN, MessageResources.CurrencyForbidden);
            }
            var result = PrePay(new BillerPrePayIM()
            {
                CountryId = im.CountryId, CryptoAmount = im.CryptoAmount, CryptoCode = im.CryptoCode,
                CryptoId  = im.CryptoId, FiatAmount = im.FiatAmount, FiatCurrency = im.FiatCurrency
            }).Status;

            if (result != 0)
            {
                errorCode = 10400 + result;
                return(null);
            }

            new SecurityComponent().VerifyPin(account, im.Pin);
            var cryptoCurrency = new CryptocurrencyDAC().GetById(im.CryptoId);

            if (cryptoCurrency?.Id == null)
            {
                throw new CommonException(ReasonCode.CRYPTO_NOT_EXISTS, "Error: Invalid Cryptocurrency");
            }
            var uwComponent = new UserWalletComponent();
            var userWallet  = uwComponent.GetUserWallet(account.Id, im.CryptoId);

            if (userWallet == null)
            {
                throw new CommonException(ReasonCode.INSUFFICIENT_BALANCE, MessageResources.InsufficientBalance);
            }
            if (userWallet.Balance < im.CryptoAmount)
            {
                throw new CommonException(ReasonCode.INSUFFICIENT_BALANCE, MessageResources.InsufficientBalance);
            }
            var uwDAC       = new UserWalletDAC();
            var uwsDAC      = new UserWalletStatementDAC();
            var boDAC       = new BillerOrderDAC();
            var billerOrder = new BillerOrder()
            {
                Id           = Guid.NewGuid(),
                BillerCode   = im.BillerCode,
                CryptoAmount = im.CryptoAmount,
                CryptoCode   = im.CryptoCode,
                CryptoId     = im.CryptoId,
                Discount     = im.CryptoCode.Equals("Fiii", StringComparison.InvariantCultureIgnoreCase)
                    ? decimal.Parse(new MasterSettingDAC().SelectByGroup("BillerMaxAmount").First(item =>
                                                                                                  item.Name.Equals("DiscountRate", StringComparison.CurrentCultureIgnoreCase)).Value)
                    : 0,
                ExchangeRate    = im.ExchangeRate,
                FiatAmount      = im.FiatAmount,
                FiatCurrency    = im.FiatCurrency,
                ReferenceNumber = im.ReferenceNumber,
                Tag             = im.Tag,
                Status          = BillerOrderStatus.Pending,
                Timestamp       = DateTime.UtcNow,
                AccountId       = account.Id,
                OrderNo         = IdentityHelper.OrderNo(),
                PayTime         = DateTime.UtcNow,
                CountryId       = im.CountryId
            };
            var address = new BillerAddressDAC().GetAllAddresses(account.Id).FirstOrDefault(item =>
                                                                                            item.BillerCode == im.BillerCode && im.ReferenceNumber == item.ReferenceNumber);

            billerOrder.Tag = address?.Tag;
            using (var scope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, new TimeSpan(0, 0, 1, 30)))
            {
                uwDAC.Decrease(userWallet.Id, billerOrder.CryptoAmount);
                uwsDAC.Insert(new UserWalletStatement
                {
                    WalletId      = userWallet.Id,
                    Action        = UserWalletStatementAction.Consume,
                    Amount        = -billerOrder.CryptoAmount,
                    Balance       = userWallet.Balance - billerOrder.CryptoAmount,
                    FrozenAmount  = 0,
                    FrozenBalance = userWallet.FrozenBalance,
                    Timestamp     = DateTime.UtcNow
                });

                boDAC.Insert(billerOrder);
                new UserTransactionDAC().Insert(new UserTransaction
                {
                    Id         = Guid.NewGuid(),
                    AccountId  = billerOrder.AccountId,
                    CryptoId   = cryptoCurrency.Id,
                    CryptoCode = cryptoCurrency.Code,
                    Type       = UserTransactionType.BillOrder,
                    DetailId   = billerOrder.Id.ToString(),
                    Status     = (byte)billerOrder.Status,
                    Timestamp  = billerOrder.Timestamp,
                    Amount     = billerOrder.CryptoAmount,
                    OrderNo    = billerOrder.OrderNo
                });
                scope.Complete();
            }

            return(new BillerPayOM()
            {
                CryptoAmount = billerOrder.CryptoAmount.ToString(), OrderNo = billerOrder.OrderNo,
                CryptoCode = billerOrder.CryptoCode, OrderId = billerOrder.Id,
                Timestamp = billerOrder.PayTime.ToUnixTime().ToString(),
                SaveAddress = address != null
            });
        }