Chandan Kumar
Chandan Kumar

Reputation: 221

Stripe is Creating Duplicate Customers when checkout using Different Cards

I am working with stripe using .NET Core. and I am having an issue with if Let's say I have a customer in the Stripe with [email protected] and he has used a visa card for the first checkout.

And then he is trying to purchase another Item using a different card (let's say Master Card) then Stripe is creating a duplicate customer(With same Email id) for that checkout with the same email. While I want to add this new purchase Item to the existing customer even he has used different cards.

NOTE:- If I use same card for each checkout for that customer then It works as expected.

I am using Stripe Checkout Session Service for the Checkout process. And creating Customer only If there is no customer exist in stripe for the given Email. But in case I used different Card for the checkout. stripe is creating duplicate Customer implicitly.

This is my create customer to Stripe code

           // Create customer object   
            var customerOptions = new CustomerListOptions()
            {
                Email = user.UserName
            };
            // get list of customers with matching options (should be one since email is unique)
            var customers = await customerService.ListAsync(customerOptions);
            // get first matching customer
            var customer = customers.FirstOrDefault();

            // if we didn't find the customer, we create one in stripe
            if (customer == null)
            {
                customer = await customerService.CreateAsync(new CustomerCreateOptions
                {
                    Email = model.StripeEmail
                });
                newCustomer = true;
            }

And This is Checkout session creation logic

        var sessionOptions = new SessionCreateOptions
        {
            BillingAddressCollection = "auto",
            CustomerEmail = customer.Email,
            PaymentMethodTypes = new List<string> {
                "card",
            },
            SubscriptionData = new SessionSubscriptionDataOptions
            {
                Items = subscriptions
            },
            SuccessUrl = "https://localhost:44xx/Shop/CheckoutConfirmation?sessionId={CHECKOUT_SESSION_ID}",
            CancelUrl = "https://localhost:44xx/Shop/Cart",
        };

        try
        {

            var sessionService = new SessionService();
            Session session = sessionService.Create(sessionOptions);

            response.IsValid = true;
            response.SessionId = session.Id;
            return new JsonResult(response);
        }
        catch (Exception ex)
        {
            response.IsValid = false;
            return new JsonResult(response);
        }

Upvotes: 1

Views: 5807

Answers (1)

epicrato
epicrato

Reputation: 8418

You need to pass 'customer' when creating the Session, instead of 'customer_email'. You can use 'customer_email' for the first time a user will create a Session (meaning when you don't know already what is the customer's stripe id. Then pass 'customer' instead of 'customer_email', once you already have a customer stripe id. This will make sure your user is not duplicated on the Stripe dashboard.

Upvotes: 2

Related Questions