首页 > 解决方案 > Stripe 在使用不同卡结账时创建重复客户

问题描述

我正在使用 .NET Core 处理条带。如果假设我在 Stripe 有一个客户,email=test@test.com并且他visa在第一次结账时使用了一张卡,我会遇到问题。

然后他尝试使用不同的卡购买另一个项目(比如说Master Card),然后 Stripe 正在使用相同的电子邮件为该结账创建一个重复的客户(具有相同的电子邮件 id )。虽然我想将此新购买项目添加到现有客户,即使他使用了不同的卡。

注意:- 如果我为该客户的每次结账都使用同一张卡,那么它会按预期工作。

我正在使用 Stripe Checkout Session Service 进行结帐流程。如果给定电子邮件的条带中不存在客户,则仅创建客户。但如果我使用不同的卡结帐。条纹正在隐式创建重复的客户。

这是我创建客户到 Stripe 代码

           // 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;
            }

这是 Checkout 会话创建逻辑

        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);
        }

标签: c#.net.net-corestripe-payments

解决方案


推荐阅读