Ejemplo n.º 1
0
        public ActionResult Status(bool status)
        {
            if (TempData["orderId"] == null ||
                TempData["cardNumber"] == null ||
                TempData["price"] == null ||
                TempData["currency"] == null ||
                TempData["date"] == null ||
                TempData["acquirer"] == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            int      orderId    = (int)TempData["orderId"];
            string   cardNumber = TempData["cardNumber"].ToString();
            decimal  price      = (decimal)TempData["price"];
            string   currency   = TempData["currency"].ToString();
            DateTime date       = DateTime.Parse(TempData["date"].ToString());
            Acquirer acquirer   = (Acquirer)TempData["acquirer"];

            TempData.Remove("orderId");
            TempData.Remove("cardNumber");
            TempData.Remove("price");
            TempData.Remove("currency");
            TempData.Remove("date");
            TempData.Remove("acquirer");

            if (status)
            {
                var bankAccount = db.PaymentCards
                                  .Include(p => p.BankAccount)
                                  .Single(b => b.PaymentCardNumber == cardNumber).BankAccount;

                var currencyTo = db.Currencies.SingleOrDefault(c => c.Code == currency);

                if (currencyTo != null)
                {
                    var     commision = bankAccount.BankAccountType.Commission / 100;
                    decimal value;

                    if (bankAccount.Currency.Code == currencyTo.Code)
                    {
                        value = price + (price * commision);
                    }
                    else
                    {
                        var exchangedCurrency = CurrenciesController.ExchangeCurrencyBid(bankAccount.Currency.Code, currencyTo.Code, price);
                        value = exchangedCurrency + (exchangedCurrency * commision);
                    }

                    if (bankAccount.AvailableFounds >= value)
                    {
                        bankAccount.AvailableFounds -= value;
                        bankAccount.Balance         -= value;

                        Transaction transaction = new Transaction
                        {
                            ValueTo = value,
                            BalanceAfterTransactionUserTo = bankAccount.Balance,

                            FromBankAccountNumber = bankAccount.BankAccountNumber,
                            ToBankAccountNumber   = acquirer.BankAccountNumebr,
                            ReceiverName          = acquirer.Name,
                            Description           = "Płatność kartą",
                            CurrencyTo            = bankAccount.Currency,

                            TransactionTypeID = db.TransactionTypes.Single(t => t.Type == "CARD_PAYMENT").ID,

                            Date = DateTime.Now
                        };

                        db.Transactions.Add(transaction);
                        db.SaveChanges();
                    }
                    else
                    {
                        status = false;
                    }
                }
                else
                {
                    status = false;
                }
            }

            client.PostAsJsonAsync(acquirer.URL + acquirer.UpdateOrderStatusPath, new { id = orderId, status = status, apiKey = acquirer.ApiKey });
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

            return(Redirect(acquirer.URL + acquirer.OrderSummaryPath + orderId));
        }