Skip to content

Commit

Permalink
feat: Add createBillingPortalSession (#8)
Browse files Browse the repository at this point in the history
* feat: Add route, function to create a Stripe Billing Portal Session

* docs: Add createBillingPortalSession example
  • Loading branch information
ynnoj authored Jan 27, 2021
1 parent bcaa7b2 commit 7e477dc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
8 changes: 8 additions & 0 deletions src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -33,6 +40,7 @@ async function updatePaymentIntent(id, body) {
}

export default {
createBillingPortalSession,
createCheckoutSession,
createPaymentIntent,
updatePaymentIntent
Expand Down
2 changes: 2 additions & 0 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
13 changes: 13 additions & 0 deletions src/server/routes/create/billing-portal-session.js
Original file line number Diff line number Diff line change
@@ -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 })
}
}
1 change: 1 addition & 0 deletions src/server/routes/index.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 7e477dc

Please sign in to comment.