简体   繁体   中英

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.

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. Seems like that is not the case. The subscription has a new 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.

You need to define the webhook address in your Stripe Dashboard. Once the checkout completes, the webhook is invoked, so you can carry out all the steps you need after the payment is successful.

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 . 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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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