2
0

🐛 (billing) Set invoicing behavior to "always invoice" to fix double payment issue

This commit is contained in:
Baptiste Arnaud
2023-11-14 16:00:10 +01:00
parent 6c0f28b3e4
commit a1d7415227
4 changed files with 73 additions and 2 deletions

View File

@ -125,10 +125,10 @@ export const updateSubscription = authenticatedProcedure
})
}
}
await stripe.subscriptions.update(subscription.id, {
items,
proration_behavior:
plan === 'PRO' ? 'always_invoice' : 'create_prorations',
proration_behavior: 'always_invoice',
})
} else {
const checkoutUrl = await createCheckoutSessionUrl(stripe)({

View File

@ -370,6 +370,51 @@
"data"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"userId": {
"type": "string"
},
"workspaceId": {
"type": "string"
},
"name": {
"type": "string",
"enum": [
"Subscription automatically updated"
]
},
"data": {
"type": "object",
"properties": {
"plan": {
"type": "string",
"enum": [
"FREE",
"STARTER",
"PRO",
"LIFETIME",
"OFFERED",
"CUSTOM",
"UNLIMITED"
]
}
},
"required": [
"plan"
],
"additionalProperties": false
}
},
"required": [
"userId",
"workspaceId",
"name",
"data"
],
"additionalProperties": false
}
]
}

View File

@ -66,6 +66,15 @@ const subscriptionUpdatedEventSchema = workspaceEvent.merge(
})
)
const subscriptionAutoUpdatedEventSchema = workspaceEvent.merge(
z.object({
name: z.literal('Subscription automatically updated'),
data: z.object({
plan: z.nativeEnum(Plan),
}),
})
)
const newResultsCollectedEventSchema = typebotEvent.merge(
z.object({
name: z.literal('New results collected'),
@ -105,6 +114,7 @@ export const eventSchema = z.discriminatedUnion('name', [
newResultsCollectedEventSchema,
workspaceLimitReachedEventSchema,
workspaceAutoQuarantinedEventSchema,
subscriptionAutoUpdatedEventSchema,
])
export type TelemetryEvent = z.infer<typeof eventSchema>

View File

@ -76,6 +76,7 @@ export const checkAndReportChatsUsage = async () => {
})
const quarantineEvents: TelemetryEvent[] = []
const autoUpgradeEvents: TelemetryEvent[] = []
for (const workspace of workspaces) {
if (workspace.isQuarantined) continue
@ -136,6 +137,21 @@ export const checkAndReportChatsUsage = async () => {
stripe,
workspaceId: workspace.id,
})
autoUpgradeEvents.push(
...workspace.members
.filter((member) => member.role === WorkspaceRole.ADMIN)
.map(
(member) =>
({
name: 'Subscription automatically updated',
userId: member.user.id,
workspaceId: workspace.id,
data: {
plan: 'PRO',
},
} satisfies TelemetryEvent)
)
)
await reportUsageToStripe(totalChatsUsed, {
stripe,
subscription: newSubscription,