JSFiddle - React, Tailwind, and code Playground
JavaScript
const { Stripe } = require('stripe')
const stripe = new Stripe(process.env.STRIPE_TEST_SECRET_KEY, {
apiVersion: '2020-08-27',
})
exports.handler = async (event) => {
try {
// get the stripe customer ID for this Knack account, and the return URL
const stripeCustomerId = event.queryStringParameters.stripe_customer_id;
const returnUrl = event.queryStringParameters.return_url;
// create a portal session
const portalSession = await stripe.billingPortal.sessions.create({
customer: stripeCustomerId,
return_url: returnUrl,
})
// return the portal session to the frontend
return {
statusCode: 200,
body: JSON.stringify({
portalSession
})
};
} catch (error) {
// handle error
}
};