Paid subscription during registration (Stripe Checkout)

Hi,

I’m trying to create a customer subscription during registration (first login if I’m not wrong), using Stripe Checkout.

At some point I need to redirect the customer to the Stripe Checkout portal so they can pay and, for that, indicate in the Stripe API what will be the success_url to redirect to when the payment is done.
Auth0 wants me, for good reasons, to redirect to /continue?state=xxxxxx, but this state is dynamically added to the URL by api.redirect.sendUserTo in the action code, so how can I put it in success_url before I call the redirection?

I’ve tried to put event.transaction.state in success_url but it’s not the right one.

I searched everywhere, I can’t find the way to do this. Any help would be highly appreciated!

Here is my code:

exports.onExecutePostLogin = async (event, api) => {
    const stripe = require('stripe')(event.secrets.CHECKOUT_SECRET_KEY);
    const priceId = 'price_xxxxxxxxxx';

    const session = await stripe.checkout.sessions.create({
        mode: 'subscription',
        line_items: [{
            price: priceId,
            quantity: 1,
        }, ],
        // {CHECKOUT_SESSION_ID} is a string literal; do not change it!
        // the actual Session ID is returned in the query parameter when your customer
        // is redirected to the success page.
        success_url: 'https://auth0.example.com/continue?state=??????????&session_id={CHECKOUT_SESSION_ID}',
        cancel_url: 'https://example.com/sub-failure',
    });
    
    // Redirect to the URL returned on the Checkout Session.
    api.redirect.sendUserTo(session.url)
};

Inspired by Stripe Checkout doc: Build a subscriptions integration | Stripe Documentation

1 Like

Camille,

I wrote an article with a potential solution to this problem as we ran into the exact same issue.

Hopefully this helps you.

  • Adam