简体   繁体   English

您如何在 Stripe 结账时将客户链接到订阅?

[英]How do you link customer to subscription on Stripe checkout?

The problem I have is that everytime the customer checks out, they have a new customer id that is linked to the customer's email.我遇到的问题是,每次客户结账时,他们都会有一个新的客户 ID,该 ID 与客户的电子邮件相关联。

var stripe = Stripe('pk_test_xxx', {
    betas: ['checkout_beta_4']
});

var checkoutButton = document.getElementById('checkout-button');
checkoutButton.addEventListener('click', function () {
    stripe.redirectToCheckout({
        items: [{
            plan: 'plan_xxx',
            quantity: 1
        }],
        customerEmail: 'test15@xxx.com',
        clientReferenceId: 'cus_xxx',
        successUrl: window.location.protocol + '//domain.test/en/accounts/billing-success',
        cancelUrl: window.location.protocol + '//domain.test/en/accounts/billing-cancel',
    }).then(function (result) {
        if (result.error) {
            var displayError = document.getElementById('error-message');
            displayError.textContent = result.error.message;
        }
    });
});

I thought the clientReferenceId would persist the Stripe customer id.我认为clientReferenceId会保留 Stripe 客户 ID。 Seems like that is not the case.似乎并非如此。 The subscription has a new customer_id.订阅有一个新的 customer_id。

As described in the documentation , the value you pass to clientReferenceId is included in the data sent via the webhook if the checkout is successful.文档中所述,如果结帐成功,您传递给clientReferenceId的值将包含在通过 webhook 发送的数据中。

You need to define the webhook address in your Stripe Dashboard.您需要在 Stripe Dashboard 中定义 webhook 地址。 Once the checkout completes, the webhook is invoked, so you can carry out all the steps you need after the payment is successful.结帐完成后,将调用 webhook,因此您可以在付款成功后执行所需的所有步骤。

As described in the Stripe API docs, all you have to do is set the customer key to the customer ID you want, not the clientReferenceId .如 Stripe API 文档中所述,您所要做的就是将customer密钥设置为您想要的客户 ID,而不是clientReferenceId https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-customer https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-customer

This was also mentioned in the answer to this other SO post: Stripe Checkout Beta: Avoid creating new customer for every payment这在另一篇 SO 帖子的回答中也提到: Stripe Checkout Beta:避免为每次付款创建新客户

For better security, I also recommend you create a session on your backend and pass the sessionID to your frontend, and call redirectToCheckout with just the sessionID .为了更好的安全性,我还建议您在后端创建一个会话并将sessionID传递给前端,然后仅使用sessionID调用redirectToCheckout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM