From 7e477dc336f2f9fc5aa862eaf7c2d7a8a6a30a6d Mon Sep 17 00:00:00 2001 From: Jonathan Steele Date: Wed, 27 Jan 2021 22:05:10 +0000 Subject: [PATCH] feat: Add createBillingPortalSession (#8) * feat: Add route, function to create a Stripe Billing Portal Session * docs: Add createBillingPortalSession example --- README.md | 13 +++++++++++++ src/client/index.js | 8 ++++++++ src/server/index.js | 2 ++ src/server/routes/create/billing-portal-session.js | 13 +++++++++++++ src/server/routes/index.js | 1 + 5 files changed, 37 insertions(+) create mode 100644 src/server/routes/create/billing-portal-session.js diff --git a/README.md b/README.md index 542d944..6b3ab44 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,19 @@ const paymentIntent = await updatePaymentIntent('pi_id', { }) ``` +### Billing Portal Sessions + +#### Create + +```js +import { createBillingPortalSession } from 'next-stripe/client' + +const session = await createBillingPortalSession({ + customer: 'cus_id', + return_url: window.location.href +}) +``` + ## Acknowledgements - A lot of the patterns in this library were inspred by [NextAuth](https://github.com/nextauthjs/next-auth). diff --git a/src/client/index.js b/src/client/index.js index 84a3b5e..bb2faf5 100644 --- a/src/client/index.js +++ b/src/client/index.js @@ -8,6 +8,13 @@ async function confirmPaymentIntent(id, body) { }) } +async function createBillingPortalSession(body) { + return await fetcher({ + body, + method: 'POST', + url: `/api/stripe/create/billing-portal-session` + }) +} async function createCheckoutSession(body) { return await fetcher({ body, @@ -33,6 +40,7 @@ async function updatePaymentIntent(id, body) { } export default { + createBillingPortalSession, createCheckoutSession, createPaymentIntent, updatePaymentIntent diff --git a/src/server/index.js b/src/server/index.js index 563c61d..a22059b 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -15,6 +15,8 @@ async function NextStripeHandler(req, res, options) { } } else if (method === 'create') { switch (type) { + case 'billing-portal-session': + return routes.createBillingPortalSession(req, res, options) case 'checkout-session': return routes.createCheckoutSession(req, res, options) case 'payment-intent': diff --git a/src/server/routes/create/billing-portal-session.js b/src/server/routes/create/billing-portal-session.js new file mode 100644 index 0000000..d0adafb --- /dev/null +++ b/src/server/routes/create/billing-portal-session.js @@ -0,0 +1,13 @@ +import Stripe from 'stripe' + +export default async function createBillingPortalSession(req, res, options) { + try { + const stripe = new Stripe(options.secret_key) + + const session = await stripe.billingPortal.sessions.create(req.body) + + res.status(201).json(session) + } catch ({ statusCode, raw: { message } }) { + res.status(statusCode).json({ message, status: statusCode }) + } +} diff --git a/src/server/routes/index.js b/src/server/routes/index.js index eda17c1..1967fc3 100644 --- a/src/server/routes/index.js +++ b/src/server/routes/index.js @@ -1,6 +1,7 @@ // Confirm export { default as confirmPaymentIntent } from './confirm/payment-intent' // Create +export { default as createBillingPortalSession } from './create/billing-portal-session' export { default as createCheckoutSession } from './create/checkout-session' export { default as createPaymentIntent } from './create/payment-intent' // Update