Skip to content

Commit

Permalink
Handle plan manual cancelation
Browse files Browse the repository at this point in the history
  • Loading branch information
gaspergrom committed Dec 28, 2023
1 parent cb3be7f commit f4131b5
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions backend/src/serverless/integrations/workers/stripeWebhookWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export const processStripeWebhook = async (message: any) => {
where: { stripeSubscriptionId: stripeWebhookMessage.data.object.id },
})

if (!tenant) {
throw new Error404()
}

const subscription = await StripeService.retreiveSubscription(
stripeWebhookMessage.data.object.id,
)
Expand Down Expand Up @@ -87,6 +91,25 @@ export const processStripeWebhook = async (message: any) => {

break
}
case 'customer.subscription.deleted': {
const tenant = await options.database.tenant.findOne({
where: { stripeSubscriptionId: stripeWebhookMessage.data.object.id },
})

if (!tenant) {
throw new Error404()
}

await tenant.update({
plan: null,
isTrialPlan: false,
trialEndsAt: null,
stripeSubscriptionId: stripeWebhookMessage.data.object.id,
planSubscriptionEndsAt: moment().toISOString(),
})

break
}
case 'checkout.session.completed': {
log.info(
{ tenant: stripeWebhookMessage.data.object.client_reference_id },
Expand Down

0 comments on commit f4131b5

Please sign in to comment.