99 Environment ,
1010 Paddle ,
1111 SubscriptionActivatedEvent ,
12+ SubscriptionCanceledEvent ,
1213} from '@paddle/paddle-node-sdk'
14+ import { ok } from 'node:assert/strict'
1315
1416const stripe = new Stripe ( process . env . STRIPE_API_KEY , {
1517 apiVersion : '2023-08-16' ,
@@ -91,11 +93,27 @@ export async function handlePaddleSubscriptionActivated(
9193 } )
9294}
9395
96+ export async function handlePaddleSubscriptionCanceled (
97+ event : SubscriptionCanceledEvent
98+ ) {
99+ const userId = ( event . data . customData as { userId : string } ) . userId
100+
101+ ok ( userId , 'userId is missing in customData' )
102+
103+ const hasSubscription = await hasActivePaddleSubscription (
104+ event . data . customerId
105+ )
106+
107+ if ( ! hasSubscription ) {
108+ await switchToPlan ( userId , PlanName . FREE )
109+ }
110+ }
111+
94112export async function handleStripeCustomerSubscriptionDeleted ( {
95113 metadata,
96114 customer : stripeCustomerId ,
97115} : Stripe . Subscription ) {
98- const hasSubscription = await hasActiveSubscription (
116+ const hasSubscription = await hasActiveStripeSubscription (
99117 stripeCustomerId as string
100118 )
101119
@@ -135,7 +153,7 @@ export async function handleStripeInvoicePaymentFailed({
135153 return
136154 }
137155
138- const hasSubscription = await hasActiveSubscription (
156+ const hasSubscription = await hasActiveStripeSubscription (
139157 stripeCustomerId as string
140158 )
141159
@@ -144,7 +162,7 @@ export async function handleStripeInvoicePaymentFailed({
144162 }
145163}
146164
147- async function hasActiveSubscription ( stripeCustomerId : string ) {
165+ async function hasActiveStripeSubscription ( stripeCustomerId : string ) {
148166 const { subscriptions } = ( await stripe . customers . retrieve ( stripeCustomerId , {
149167 expand : [ 'subscriptions' ] ,
150168 } ) ) as Stripe . Customer & {
@@ -153,3 +171,12 @@ async function hasActiveSubscription(stripeCustomerId: string) {
153171
154172 return subscriptions . data . some ( ( sub ) => sub . status === 'active' )
155173}
174+
175+ async function hasActivePaddleSubscription ( paddleCustomerId : string ) {
176+ const subscriptions = await paddle . subscriptions . list ( {
177+ customerId : [ paddleCustomerId ] ,
178+ status : [ 'active' ] ,
179+ } )
180+
181+ return subscriptions . hasMore
182+ }
0 commit comments