예제 #1
0
            public async Task <ViewModel> Handle(Request request, CancellationToken cancellationToken)
            {
                var customer = await _dbContext.Customer.FirstOrDefaultAsync(x => x.IdentityKey == _userContext.Id.ToString());

                if (customer == null)
                {
                    var stripeCustomer = await _stripeService.CreateCustomerAsync(_userContext.EmailAddress);

                    customer = new Customer(_userContext.Id.ToString(), stripeCustomer.Id);
                    _dbContext.Customer.Add(customer);
                    await _dbContext.SaveChangesAsync();
                }

                var session = await _stripeService.CreateCheckoutSessionForCustomerAsync(_settings.DefaultPlanKey, customer.ExternalKey);

                var cart = new Cart
                {
                    Id = Guid.NewGuid(),
                    CreatedDateTime      = DateTime.Now,
                    LastModifiedDateTime = DateTime.Now,
                    CartState            = CartState.Created,
                    SessionId            = session.Id,
                    Email = _userContext.EmailAddress
                };

                _dbContext.Cart.Add(cart);

                await _dbContext.SaveChangesAsync();

                _logger.LogInformation("{Entity} was {Action}.  Details: {CartId} {CartState} {CartSession}", "Cart", "Created", cart.Id, cart.CartState, session.Id);

                return(new ViewModel(customer, _userContext.EmailAddress, session.Id));
            }