使用不同的卡结账时,Stripe 正在创建重复的客户

Cha*_*mar 4 .net c# stripe-payments .net-core

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

然后他尝试使用不同的卡(假设Master Card)购买另一件商品,然后 Stripe使用相同的电子邮件为该结帐创建重复的客户(使用相同的电子邮件 ID)。虽然我想将这个新购买的物品添加到现有客户中,即使他使用了不同的卡。

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

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

这是我创建的 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;
            }
Run Code Online (Sandbox Code Playgroud)

这是结帐会话创建逻辑

        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);
        }
Run Code Online (Sandbox Code Playgroud)

小智 7

是的,我还研究了在 Stripe 中创建新客户对象的 Stripe,并发现您可以通过在会话中传递客户 ID 来阻止 Stripe 创建另一个客户。只需SessionCreateOptions用下面的代码替换你的。

        var sessionOptions = new SessionCreateOptions
            {
                BillingAddressCollection = "auto",
                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",
                ClientReferenceId = customer.Id,
                CustomerId = customer.Id
            };
Run Code Online (Sandbox Code Playgroud)

因此,它将创建对我关联到条带结帐会话对象的客户 ID 的订阅。