public async Task Should_Add_And_Remove_Payment_Method()
        {
            var loggedUser = await TestData.LogUserIn(Client);

            AddPaymentMethodBody body = new AddPaymentMethodBody()
            {
                UserId    = loggedUser.Id,
                NickName  = "Debit",
                CardToken = "tok_gb_debit"
            };

            var response = await Client.PostAsJsonAsync(TestData.BaseUrl + "/api/payment/AddPaymentMethod", body);

            response.StatusCode.Should().Be(200);

            var methodString = await response.Content.ReadAsStringAsync();

            var method = JsonConvert.DeserializeObject <PaymentMethodViewModel>(methodString);

            RemovePaymentMethodBody removeBody = new()
            {
                PaymentId = method.StripeCardId,
                UserId    = loggedUser.Id
            };

            response = await Client.PostAsJsonAsync(TestData.BaseUrl + "/api/payment/RemovePaymentMethod", removeBody);

            response.StatusCode.Should().Be(200);
        }
    }
        /// <inheritdoc />
        /// <exception cref="UserNotFoundException">Thrown when the user is not found</exception>
        public async Task <PaymentMethodViewModel> AddPaymentMethod(AddPaymentMethodBody addPaymentMethodBody)
        {
            var user = await ApplicationContext.Users.Include(x => x.PaymentMethods)
                       .FirstOrDefaultAsync(x => x.Id == addPaymentMethodBody.UserId);

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

            // if the user doesn't have a stripe customer create one
            if (user.StripeCustomerId == null)
            {
                var customer = CreateCustomer(user);
                user.StripeCustomerId = customer.Id;
                await ApplicationContext.SaveChangesAsync();
            }

            var options = new CardCreateOptions()
            {
                Source = addPaymentMethodBody.CardToken
            };

            var service = new CardService();

            try
            {
                // request create card to Stripe api
                var card = service.Create(user.StripeCustomerId, options);

                // create payment method using Stripe response data
                var paymentMethod = new PaymentMethod()
                {
                    StripeCardId = card.Id,
                    Brand        = card.Brand,
                    Country      = card.Country,
                    Funding      = card.Funding,
                    Name         = card.Name,
                    ExpiryMonth  = card.ExpMonth,
                    ExpiryYear   = card.ExpYear,
                    LastFour     = card.Last4,
                    NickName     = addPaymentMethodBody.NickName,
                    IsDefault    = !user.PaymentMethods.Any()
                };

                user.PaymentMethods.Add(paymentMethod);
                await ApplicationContext.SaveChangesAsync();

                return(Mapper.Map <PaymentMethodViewModel>(paymentMethod));
            }
            catch (Exception e)
            {
                Logger.LogInformation(e.Message);
                throw;
            }
        }
        public async Task <ActionResult <PaymentMethodViewModel> > AddPaymentMethod(AddPaymentMethodBody addPaymentMethodBody)
        {
            try
            {
                Logger.LogInformation("{Id} is adding a payment method", addPaymentMethodBody.UserId);
                var result = await PaymentService.AddPaymentMethod(addPaymentMethodBody);

                return(result);
            }
            catch (Exception e)
            {
                Logger.LogError(e.ToString());
                return(BadRequest(e));
            }
        }
Example #4
0
        public async Task Should_Add_Method()
        {
            var loggedUser = await TestData.LogUserIn(Client);

            AddPaymentMethodBody body = new AddPaymentMethodBody()
            {
                UserId    = loggedUser.Id,
                NickName  = "Debit",
                CardToken = "tok_gb_debit"
            };

            var response = await Client.PostAsJsonAsync(TestData.BaseUrl + "/api/payment/AddPaymentMethod", body);

            response.StatusCode.Should().Be(200);

            var methodString = await response.Content.ReadAsStringAsync();

            var method = JsonConvert.DeserializeObject <PaymentMethodViewModel>(methodString);

            method.NickName.Should().BeEquivalentTo(body.NickName);
            method.Brand.Should().BeEquivalentTo("Visa");
        }