Esempio n. 1
0
        /// <inheritdoc />
        /// <exception cref="UserNotFoundException">Thrown when the user is not found</exception>
        public async Task <BankAccountViewModel> AddBankAccount(AddBankAccountBody addBankAccountBody)
        {
            var user = await ApplicationContext.Users.Include(x => x.BankAccounts)
                       .FirstOrDefaultAsync(x => x.Id == addBankAccountBody.UserId);

            if (user == null)
            {
                throw new UserNotFoundException();
            }

            if (user.StripeAccountId == null)
            {
                var account = CreateAccount(user);
                user.StripeAccountId = account.Id;
                await ApplicationContext.SaveChangesAsync();
            }

            var options = new ExternalAccountCreateOptions()
            {
                ExternalAccount = addBankAccountBody.BankToken
            };

            var service = new ExternalAccountService();

            try
            {
                // request create bank account from the Stripe api
                var bank = (Stripe.BankAccount)service.Create(user.StripeAccountId, options);

                // create bank account using Stripe response
                var bankAccount = new BankAccount
                {
                    StripeBankAccountId = bank.Id,
                    Bank     = bank.BankName,
                    Country  = bank.Country,
                    Currency = bank.Currency,
                    Name     = bank.AccountHolderName,
                    LastFour = bank.Last4
                };

                user.BankAccounts = new List <BankAccount> {
                    bankAccount
                };

                await ApplicationContext.SaveChangesAsync();

                return(Mapper.Map <BankAccountViewModel>(user.BankAccounts[0]));
            }
            catch
            {
                Logger.LogWarning("Failed to save bank account");
                throw;
            }
        }
        public async Task <ActionResult <BankAccountViewModel> > AddBankAccount(AddBankAccountBody addBankAccountBody)
        {
            try
            {
                Logger.LogInformation("{Id} is adding a bank", addBankAccountBody.UserId);
                var result = await BankingService.AddBankAccount(addBankAccountBody);

                return(result);
            }
            catch (Exception e)
            {
                Logger.LogWarning(e.ToString());
                return(BadRequest(e));
            }
        }