コード例 #1
0
        public async Task WhenCreatingCustomer_UpdateAppMetadataInAuth0()
        {
            //Arrange
            var stripeCustomerRepositoryMock             = new StripeCustomerRepositoryMock();
            var stripeCheckoutSessionRepositoryMock      = new StripeCheckoutSessionRepositoryMock();
            var stripeBillingPortalSessionRepositoryMock = new StripeBillingPortalSessionRepositoryMock();
            var userManagementServiceMock = new UserManagementServiceMock();
            var priceNameToIdMapper       = new DevPriceNameToIdMapper();

            StripePaymentService stripePaymentService = new StripePaymentService(stripeCustomerRepositoryMock,
                                                                                 stripeCheckoutSessionRepositoryMock, stripeBillingPortalSessionRepositoryMock,
                                                                                 userManagementServiceMock, priceNameToIdMapper);

            var auth0UserId = "google-oauth2|00004";
            var name        = "Patryk Tester";
            var email       = "*****@*****.**";
            var newCustomer = new CreateCustomerDto(auth0UserId, name, email);

            //Act
            var createdCustomer = await stripePaymentService.CreateCustomerAsync(newCustomer);

            var retrievedCustomer = await stripeCustomerRepositoryMock.GetAsync(createdCustomer.Id);

            //Assert
            Assert.NotNull(createdCustomer);
            Assert.NotNull(retrievedCustomer);
            Assert.Equal(auth0UserId, retrievedCustomer.Metadata["auth0UserId"]);

            var auth0User = userManagementServiceMock.Users.First(x => x.UserId == auth0UserId);

            Assert.Equal(retrievedCustomer.Id, auth0User.AppMetadata.StripeCustomerId);
            Assert.Null(auth0User.AppMetadata.SubscriptionPlan);
        }
コード例 #2
0
        public async Task <IActionResult> CreateCheckoutSession([FromBody] CreateCheckoutSessionRequestDto req)
        {
            var user = await _userManagementService.GetAuth0UserAsync(_jwtToken.UserId);

            string stripeCustomerId;

            if (String.IsNullOrEmpty(user.AppMetadata?.StripeCustomerId))
            {
                var newCustomer = new CreateCustomerDto(_jwtToken.UserId, user.Name, user.Email);
                var customer    = await _stripePaymentService.CreateCustomerAsync(newCustomer);

                stripeCustomerId = customer.Id;
            }
            else
            {
                var retrievedCustomer = await _stripePaymentService.GetCustomerAsync(user.AppMetadata.StripeCustomerId);

                if (retrievedCustomer.Subscription == null)
                {
                    stripeCustomerId = user.AppMetadata.StripeCustomerId;
                }
                else
                {
                    return(BadRequest(new ErrorResponseDto
                    {
                        ErrorMessage = new ErrorMessageDto
                        {
                            Message = "This user already has a subscription.",
                        }
                    }));
                }
            }

            try
            {
                CreateCheckoutSessionResponseDto checkoutSessionResponse =
                    await _stripePaymentService.CreateCheckoutSessionAsync(stripeCustomerId, _jwtToken.UserId,
                                                                           req.PriceTier);

                return(Ok(checkoutSessionResponse));
            }
            catch (StripeException e)
            {
                Console.WriteLine(e.StripeError.Message);
                return(BadRequest(new ErrorResponseDto
                {
                    ErrorMessage = new ErrorMessageDto
                    {
                        Message = e.StripeError.Message,
                    }
                }));
            }
        }