Example #1
0
    public static string TryInvokeTransfer(string transferFrom, string transferTo, Money amount, Member user, ref bool htmlResponse)
    {
        var TokenCryptocurrency = CryptocurrencyFactory.Get(CryptocurrencyType.ERC20Token);

        //Anti-Injection Fix
        if (amount <= new Money(0))
        {
            throw new MsgException(L1.ERROR);
        }

        if (!TransferHelper.IsAllowed(transferFrom, transferTo))
        {
            throw new MsgException("This transfer is not allowed.");
        }

        if (transferFrom == "MPesa" && String.IsNullOrWhiteSpace(user.GetPaymentAddress(PaymentProcessor.MPesa)))
        {
            throw new MsgException(U6010.ACCOUNTIDREQUIRED);
        }

        AppSettings.DemoCheck();
        htmlResponse = false;

        if (transferFrom == "Main balance")
        {
            // Option 1: From Main Balance (always available)

            if (amount > user.MainBalance)
            {
                throw new MsgException(L1.NOTENOUGHFUNDS);
            }

            user.SubtractFromMainBalance(amount, "Transfer from Main Balance");
            user.MoneyTransferredFromMainBalance += amount;

            if (transferTo == "Traffic balance")
            {
                user.AddToTrafficBalance(amount, "Transfer from Main Balance");
            }

            else if (transferTo == "Purchase balance" && AppSettings.Payments.TransferMainInAdBalanceEnabled)
            {
                user.AddToPurchaseBalance(amount, "Transfer from Main Balance");
            }

            else if (transferTo == "Cash balance")
            {
                user.AddToCashBalance(amount, "Transfer from Main Balance");
            }

            else if (transferTo == TokenCryptocurrency.Code + " Wallet" && AppSettings.Payments.TransferFromMainBalanceToTokenWalletEnabled)
            {
                //Let's calculate transfer amount using current token rate
                decimal tokenValue = CryptocurrencyFactory.Get(CryptocurrencyType.ERC20Token).ConvertFromMoney(amount);
                user.AddToCryptocurrencyBalance(CryptocurrencyType.ERC20Token, tokenValue, "Transfer from Main Balance");
            }

            else
            {
                throw new MsgException("You must select your target account / this transfer is not available.");
            }

            user.Save();
            AddHistoryAndTryAchievement(amount, user, "Main balance", transferTo);
            return(U3501.TRANSFEROK);
        }
        else if (transferFrom == "Cash balance")
        {
            if (AppSettings.Payments.CashBalanceEnabled && AppSettings.Payments.CashToAdBalanceEnabled)
            {
                if (amount > user.CashBalance)
                {
                    throw new MsgException(L1.NOTENOUGHFUNDS);
                }

                user.SubtractFromCashBalance(amount, "Transfer from Cash Balance");

                if (transferTo == "Purchase balance")
                {
                    user.AddToPurchaseBalance(amount, "Transfer from Cash Balance");
                }

                user.SaveBalances();
                return(U3501.TRANSFEROK);
            }
            else
            {
                throw new MsgException("You cannot transfer from Cash Balance");
            }
        }
        else if (transferFrom == "Commission Balance")
        {
            if (((AppSettings.Payments.CommissionToMainBalanceEnabled) ||
                 (TitanFeatures.UserCommissionToMainBalanceEnabled && user.CommissionToMainBalanceEnabled) ||
                 (AppSettings.Payments.CommissionToAdBalanceEnabled) || TitanFeatures.isAri) && user.CheckAccessCustomizeTradeOwnSystem)
            {
                if (amount > user.CommissionBalance)
                {
                    throw new MsgException(L1.NOTENOUGHFUNDS);
                }

                user.SubtractFromCommissionBalance(amount, "Transfer from Commission Balance");

                if (transferTo == "Main balance")
                {
                    Money amountWithFee = TransferManager.GetAmountWithFee(amount, TransferFeeType.SameUserCommissionToMain, user);
                    user.AddToMainBalance(amountWithFee, "Transfer from Commission Balance");

                    //Pools
                    Money moneyLeft = amount - amountWithFee;
                    PoolDistributionManager.AddProfit(ProfitSource.TransferFees, moneyLeft);
                }
                else if (transferTo == "Purchase balance")
                {
                    user.AddToPurchaseBalance(amount, "Transfer from Commission Balance");
                }
                else if (transferTo == "Cash balance")
                {
                    user.AddToCashBalance(amount, "Transfer from Purchase Balance");
                }

                user.SaveBalances();
                return(U3501.TRANSFEROK);
            }
            else
            {
                throw new MsgException("You cannot transfer from Commission Balance");
            }
        }
        else if (transferFrom == "Purchase balance")
        {
            if (!TitanFeatures.IsRevolca)
            {
                throw new MsgException("You cannot transfer from Purchase Balance");
            }

            if (amount > user.MainBalance)
            {
                throw new MsgException(L1.NOTENOUGHFUNDS);
            }

            user.SubtractFromPurchaseBalance(amount, "Transfer from Purchase Balance");
            user.AddToMainBalance(amount, "Transfer from Purchase Balance");
            user.SaveBalances();
            return(U3501.TRANSFEROK);
        }
        else
        {
            htmlResponse = true;
            return(TryInvokeProcessorTransfer(transferFrom, transferTo, amount, user));
        }
    }