Skip to content

Commit

Permalink
🐛 (billing) Upgrade again after cancelling
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Oct 31, 2022
1 parent 385853c commit d132cb1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
17 changes: 12 additions & 5 deletions apps/builder/pages/api/stripe/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -145,15 +151,15 @@ 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
: {
id: currentAdditionalStorageItemId,
price: process.env.STRIPE_ADDITIONAL_STORAGE_PRICE_ID,
quantity: additionalStorage,
deleted: additionalStorage === 0,
deleted: subscription ? additionalStorage === 0 : undefined,
},
].filter(isDefined)

Expand All @@ -165,6 +171,7 @@ const updateSubscription = async (req: NextApiRequest) => {
await stripe.subscriptions.create({
customer: stripeId,
items,
currency,
})
}

Expand Down
2 changes: 1 addition & 1 deletion apps/builder/playwright/services/databaseActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
6 changes: 6 additions & 0 deletions apps/builder/playwright/tests/billing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down

0 comments on commit d132cb1

Please sign in to comment.