diff --git a/apps/builder/components/shared/ChangePlanForm/queries/updatePlan.tsx b/apps/builder/components/shared/ChangePlanForm/queries/updatePlan.tsx index 91f225ba4..2f8cdd46b 100644 --- a/apps/builder/components/shared/ChangePlanForm/queries/updatePlan.tsx +++ b/apps/builder/components/shared/ChangePlanForm/queries/updatePlan.tsx @@ -35,7 +35,14 @@ export const updatePlan = async ({ const { data, error } = await sendRequest<{ message: string }>({ method: 'PUT', url: '/api/stripe/subscription', - body: { workspaceId, plan, stripeId, additionalChats, additionalStorage }, + body: { + workspaceId, + plan, + stripeId, + additionalChats, + additionalStorage, + currency: guessIfUserIsEuropean() ? 'eur' : 'usd', + }, }) if (error || !data) return { error } return { newPlan: plan } diff --git a/apps/builder/pages/api/stripe/subscription.ts b/apps/builder/pages/api/stripe/subscription.ts index 656a0aa85..bb13670e0 100644 --- a/apps/builder/pages/api/stripe/subscription.ts +++ b/apps/builder/pages/api/stripe/subscription.ts @@ -100,14 +100,20 @@ const createCheckoutSession = (req: NextApiRequest) => { } const updateSubscription = async (req: NextApiRequest) => { - const { stripeId, plan, workspaceId, additionalChats, additionalStorage } = ( - typeof req.body === 'string' ? JSON.parse(req.body) : req.body - ) as { + const { + stripeId, + plan, + workspaceId, + additionalChats, + additionalStorage, + currency, + } = (typeof req.body === 'string' ? JSON.parse(req.body) : req.body) as { stripeId: string workspaceId: string additionalChats: number additionalStorage: number plan: 'STARTER' | 'PRO' + currency: 'eur' | 'usd' } if (!process.env.STRIPE_SECRET_KEY) throw Error('STRIPE_SECRET_KEY var is missing') @@ -145,7 +151,7 @@ const updateSubscription = async (req: NextApiRequest) => { id: currentAdditionalChatsItemId, price: process.env.STRIPE_ADDITIONAL_CHATS_PRICE_ID, quantity: additionalChats, - deleted: additionalChats === 0, + deleted: subscription ? additionalChats === 0 : undefined, }, additionalStorage === 0 && !currentAdditionalStorageItemId ? undefined @@ -153,7 +159,7 @@ const updateSubscription = async (req: NextApiRequest) => { id: currentAdditionalStorageItemId, price: process.env.STRIPE_ADDITIONAL_STORAGE_PRICE_ID, quantity: additionalStorage, - deleted: additionalStorage === 0, + deleted: subscription ? additionalStorage === 0 : undefined, }, ].filter(isDefined) @@ -165,6 +171,7 @@ const updateSubscription = async (req: NextApiRequest) => { await stripe.subscriptions.create({ customer: stripeId, items, + currency, }) } diff --git a/apps/builder/playwright/services/databaseActions.ts b/apps/builder/playwright/services/databaseActions.ts index 14cf1e413..578087134 100644 --- a/apps/builder/playwright/services/databaseActions.ts +++ b/apps/builder/playwright/services/databaseActions.ts @@ -40,7 +40,7 @@ export const addSubscriptionToWorkspace = async ( customer: stripeId, items, default_payment_method: paymentId, - currency: 'eur', + currency: 'usd', }) await stripe.customers.update(stripeId, { invoice_settings: { default_payment_method: paymentId }, diff --git a/apps/builder/playwright/tests/billing.spec.ts b/apps/builder/playwright/tests/billing.spec.ts index 0d1ac902a..64450fda0 100644 --- a/apps/builder/playwright/tests/billing.spec.ts +++ b/apps/builder/playwright/tests/billing.spec.ts @@ -207,6 +207,12 @@ test('plan changes should work', async ({ page }) => { await expect(page.locator('[data-testid="current-subscription"]')).toHaveText( 'Current workspace subscription: Free' ) + + // Upgrade again to PRO + await page.getByRole('button', { name: 'Upgrade' }).nth(1).click() + await expect( + page.locator('text="Workspace PRO plan successfully updated 🎉" >> nth=0') + ).toBeVisible({ timeout: 20 * 1000 }) }) test('should display invoices', async ({ page }) => {