@@ -29,6 +29,7 @@ interface CancelSubscriptionProps {
2929 }
3030 subscriptionData ?: {
3131 periodEnd ?: Date | null
32+ cancelAtPeriodEnd ?: boolean
3233 }
3334}
3435
@@ -126,35 +127,48 @@ export function CancelSubscription({ subscription, subscriptionData }: CancelSub
126127 const subscriptionStatus = getSubscriptionStatus ( )
127128 const activeOrgId = activeOrganization ?. id
128129
129- // For team/enterprise plans, get the subscription ID from organization store
130- if ( ( subscriptionStatus . isTeam || subscriptionStatus . isEnterprise ) && activeOrgId ) {
131- const orgSubscription = useOrganizationStore . getState ( ) . subscriptionData
130+ if ( isCancelAtPeriodEnd ) {
131+ if ( ! betterAuthSubscription . restore ) {
132+ throw new Error ( 'Subscription restore not available' )
133+ }
134+
135+ let referenceId : string
136+ let subscriptionId : string | undefined
137+
138+ if ( ( subscriptionStatus . isTeam || subscriptionStatus . isEnterprise ) && activeOrgId ) {
139+ const orgSubscription = useOrganizationStore . getState ( ) . subscriptionData
140+ referenceId = activeOrgId
141+ subscriptionId = orgSubscription ?. id
142+ } else {
143+ // For personal subscriptions, use user ID and let better-auth find the subscription
144+ referenceId = session . user . id
145+ subscriptionId = undefined
146+ }
147+
148+ logger . info ( 'Restoring subscription' , { referenceId, subscriptionId } )
132149
133- if ( orgSubscription ?. id && orgSubscription ?. cancelAtPeriodEnd ) {
134- // Restore the organization subscription
135- if ( ! betterAuthSubscription . restore ) {
136- throw new Error ( 'Subscription restore not available' )
137- }
138-
139- const result = await betterAuthSubscription . restore ( {
140- referenceId : activeOrgId ,
141- subscriptionId : orgSubscription . id ,
142- } )
143- logger . info ( 'Organization subscription restored successfully' , result )
150+ // Build restore params - only include subscriptionId if we have one (team/enterprise)
151+ const restoreParams : any = { referenceId }
152+ if ( subscriptionId ) {
153+ restoreParams . subscriptionId = subscriptionId
144154 }
155+
156+ const result = await betterAuthSubscription . restore ( restoreParams )
157+
158+ logger . info ( 'Subscription restored successfully' , result )
145159 }
146160
147- // Refresh state and close
148161 await refresh ( )
149162 if ( activeOrgId ) {
150163 await loadOrganizationSubscription ( activeOrgId )
151164 await refreshOrganization ( ) . catch ( ( ) => { } )
152165 }
166+
153167 setIsDialogOpen ( false )
154168 } catch ( error ) {
155- const errorMessage = error instanceof Error ? error . message : 'Failed to keep subscription'
169+ const errorMessage = error instanceof Error ? error . message : 'Failed to restore subscription'
156170 setError ( errorMessage )
157- logger . error ( 'Failed to keep subscription' , { error } )
171+ logger . error ( 'Failed to restore subscription' , { error } )
158172 } finally {
159173 setIsLoading ( false )
160174 }
@@ -190,6 +204,12 @@ export function CancelSubscription({ subscription, subscriptionData }: CancelSub
190204
191205 // Check if subscription is set to cancel at period end
192206 const isCancelAtPeriodEnd = ( ( ) => {
207+ // First check if it's passed via props (works for all subscription types)
208+ if ( subscriptionData ?. cancelAtPeriodEnd === true ) {
209+ return true
210+ }
211+
212+ // Fall back to checking organization store for team subscriptions
193213 const subscriptionStatus = getSubscriptionStatus ( )
194214 if ( subscriptionStatus . isTeam || subscriptionStatus . isEnterprise ) {
195215 return useOrganizationStore . getState ( ) . subscriptionData ?. cancelAtPeriodEnd === true
@@ -271,12 +291,7 @@ export function CancelSubscription({ subscription, subscriptionData }: CancelSub
271291
272292 { ( ( ) => {
273293 const subscriptionStatus = getSubscriptionStatus ( )
274- if (
275- subscriptionStatus . isPaid &&
276- ( activeOrganization ?. id
277- ? useOrganizationStore . getState ( ) . subscriptionData ?. cancelAtPeriodEnd
278- : false )
279- ) {
294+ if ( subscriptionStatus . isPaid && isCancelAtPeriodEnd ) {
280295 return (
281296 < AlertDialogAction
282297 onClick = { handleKeep }
0 commit comments