@@ -13,20 +13,33 @@ export type Session = {
1313
1414export type GetSessionFn = ( ) => Promise < Session >
1515
16- export type CreateBillingPortalSessionFn = ( params : {
16+ export type BillingPortalFlowData = {
17+ type : 'subscription_update'
18+ subscription_update : {
19+ subscription : string
20+ }
21+ }
22+
23+ export type CreateBillingPortalSessionParams = {
1724 customer : string
1825 return_url : string
19- } ) => Promise < { url : string } >
26+ flow_data ?: BillingPortalFlowData
27+ }
28+
29+ export type CreateBillingPortalSessionFn = (
30+ params : CreateBillingPortalSessionParams
31+ ) => Promise < { url : string } >
2032
2133export type PostBillingPortalParams = {
2234 getSession : GetSessionFn
2335 createBillingPortalSession : CreateBillingPortalSessionFn
2436 logger : Logger
2537 returnUrl : string
38+ flowData ?: BillingPortalFlowData
2639}
2740
2841export async function postBillingPortal ( params : PostBillingPortalParams ) {
29- const { getSession, createBillingPortalSession, logger, returnUrl } = params
42+ const { getSession, createBillingPortalSession, logger, returnUrl, flowData } = params
3043
3144 const session = await getSession ( )
3245 if ( ! session ?. user ?. id ) {
@@ -42,10 +55,16 @@ export async function postBillingPortal(params: PostBillingPortalParams) {
4255 }
4356
4457 try {
45- const portalSession = await createBillingPortalSession ( {
58+ const portalParams : CreateBillingPortalSessionParams = {
4659 customer : stripeCustomerId ,
4760 return_url : returnUrl ,
48- } )
61+ }
62+
63+ if ( flowData ) {
64+ portalParams . flow_data = flowData
65+ }
66+
67+ const portalSession = await createBillingPortalSession ( portalParams )
4968
5069 return NextResponse . json ( { url : portalSession . url } )
5170 } catch ( error ) {
0 commit comments