-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support stripe checkout and portal sessions
- Loading branch information
Showing
17 changed files
with
130 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Stripe { | ||
apiKey = ''; | ||
prices = { | ||
list: jest.fn(() => ({ data: [{ id: 'price_id' }] })), | ||
}; | ||
checkout = { | ||
sessions: { | ||
create: jest.fn(() => ({ url: 'checkout_session_url' })), | ||
retrieve: jest.fn(() => ({ customer: 'checkout_session_customer' })), | ||
}, | ||
}; | ||
billingPortal = { | ||
sessions: { | ||
create: jest.fn(() => ({ url: 'portal_session_url' })), | ||
}, | ||
}; | ||
|
||
constructor(apiKey: string) { | ||
this.apiKey = apiKey; | ||
} | ||
} | ||
|
||
export default Stripe; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
jest.mock('stripe'); | ||
import { requestFixture, responseFixture } from '../../../__tests__/shared/fixtures'; | ||
import { postCheckoutSession, postPortalSession } from '../stripe'; | ||
|
||
describe('Credentials', () => { | ||
it('creates a new checkout session', async () => { | ||
const res = responseFixture(); | ||
// @ts-expect-error Request fixture | ||
await postCheckoutSession(requestFixture(), res); | ||
|
||
expect(res.redirect).toHaveBeenCalledWith(303, 'checkout_session_url'); | ||
}); | ||
|
||
it('creates a new portal session', async () => { | ||
const res = responseFixture(); | ||
// @ts-expect-error Request fixture | ||
await postPortalSession(requestFixture(), res); | ||
|
||
expect(res.redirect).toHaveBeenCalledWith(303, 'portal_session_url'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Request, Response } from 'express'; | ||
import Stripe from 'stripe'; | ||
import { API_ROUTE } from '../config'; | ||
|
||
const STRIPE_SECRET_KEY = 'sk_test_hpwuITjteocLizB8Afq7H3cV00FEEViC1s'; | ||
const stripe = new Stripe(STRIPE_SECRET_KEY); | ||
|
||
export const postCheckoutSession = async (req: Request, res: Response) => { | ||
const prices = await stripe.prices.list({ | ||
lookup_keys: [req.body.lookup_key], | ||
expand: ['data.product'], | ||
}); | ||
|
||
const session = await stripe.checkout.sessions.create({ | ||
billing_address_collection: 'auto', | ||
line_items: [ | ||
{ | ||
price: prices.data[0].id, | ||
quantity: 1, | ||
}, | ||
], | ||
mode: 'subscription', | ||
success_url: `${API_ROUTE}/?success=true&session_id={CHECKOUT_SESSION_ID}`, | ||
cancel_url: `${API_ROUTE}/?canceled=true`, | ||
}); | ||
|
||
return res.redirect(303, session.url || '/'); | ||
}; | ||
|
||
export const postPortalSession = async (req: Request, res: Response) => { | ||
const { sessionId } = req.body; | ||
const checkoutSession = await stripe.checkout.sessions.retrieve(sessionId); | ||
|
||
if (!checkoutSession.customer) { | ||
throw new Error('No associated customer with checkout session'); | ||
} | ||
|
||
const returnUrl = API_ROUTE; | ||
|
||
const portalSession = await stripe.billingPortal.sessions.create({ | ||
customer: `${checkoutSession.customer}`, | ||
return_url: returnUrl, | ||
}); | ||
|
||
return res.redirect(303, portalSession.url); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import router from './router'; | ||
import routerV2 from './routerV2'; | ||
import siteRouter from './siteRouter'; | ||
import stripeRouter from './stripeRouter'; | ||
import testRouter from './testRouter'; | ||
|
||
export { router, routerV2, siteRouter, testRouter }; | ||
export { router, routerV2, siteRouter, stripeRouter, testRouter }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Router } from 'express'; | ||
import { postCheckoutSession, postPortalSession } from '../controllers/stripe'; | ||
|
||
const router = Router(); | ||
|
||
router.post('/checkout', postCheckoutSession); | ||
router.post('/portal', postPortalSession); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters