Exemplo n.º 1
0
        public async Task <IActionResult> Start(TransactionModel model)
        {
            if (!System.Enum.IsDefined(typeof(WalletType), model.WalletType))
            {
                throw new ApplicationException($"Wrong wallet type with value '{model.WalletType}'.");
            }

            ShopInfoModel result = null;
            string        hash   = string.Empty;

            if (!await _merchantRepository.ValidateMerchant(model.MerchantId, model.MerchantSecret))
            {
                throw new ApplicationException($"Unable to load merchant with ID '{model.MerchantId}'.");
            }

            var merchant = await _merchantRepository.GetByMerchant(model.MerchantId);

            var subscriptions = await _subscriptionsRepository.GetByUserId(merchant.Id);

            if (subscriptions != null && !subscriptions.Paid && subscriptions.Expired < DateTime.Now)
            {
                return(BadRequest("Subscription has expired."));
            }

            if (!string.Equals(merchant.RedirectUri, model.CallbackUrl))
            {
                return(BadRequest("The redirect URI is not the same as in the merchant settings."));
            }

            try
            {
                result = await _requestProvider
                         .PostAsync <CheckPaymentShop, ShopInfoModel>(model.CallbackUrl, new CheckPaymentShop { TrxId = model.TrxId });

                hash = await _transactionRepository.CreateTransactions(result, merchant.Id, model.WalletType);

                var trx = await _transactionRepository.GetByHash(hash);

                await _logRepository.Add(trx.Id, "Transaction created.");
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
            }

            return(RedirectToAction(nameof(PurchaserController.Index), "Purchaser", new { id = hash }));
        }