2
0

🐛 (billing) Upgrade again after cancelling

This commit is contained in:
Baptiste Arnaud
2022-10-31 19:08:04 +01:00
parent 385853ca3c
commit d132cb118a
4 changed files with 27 additions and 7 deletions

View File

@ -35,7 +35,14 @@ export const updatePlan = async ({
const { data, error } = await sendRequest<{ message: string }>({ const { data, error } = await sendRequest<{ message: string }>({
method: 'PUT', method: 'PUT',
url: '/api/stripe/subscription', 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 } if (error || !data) return { error }
return { newPlan: plan } return { newPlan: plan }

View File

@ -100,14 +100,20 @@ const createCheckoutSession = (req: NextApiRequest) => {
} }
const updateSubscription = async (req: NextApiRequest) => { const updateSubscription = async (req: NextApiRequest) => {
const { stripeId, plan, workspaceId, additionalChats, additionalStorage } = ( const {
typeof req.body === 'string' ? JSON.parse(req.body) : req.body stripeId,
) as { plan,
workspaceId,
additionalChats,
additionalStorage,
currency,
} = (typeof req.body === 'string' ? JSON.parse(req.body) : req.body) as {
stripeId: string stripeId: string
workspaceId: string workspaceId: string
additionalChats: number additionalChats: number
additionalStorage: number additionalStorage: number
plan: 'STARTER' | 'PRO' plan: 'STARTER' | 'PRO'
currency: 'eur' | 'usd'
} }
if (!process.env.STRIPE_SECRET_KEY) if (!process.env.STRIPE_SECRET_KEY)
throw Error('STRIPE_SECRET_KEY var is missing') throw Error('STRIPE_SECRET_KEY var is missing')
@ -145,7 +151,7 @@ const updateSubscription = async (req: NextApiRequest) => {
id: currentAdditionalChatsItemId, id: currentAdditionalChatsItemId,
price: process.env.STRIPE_ADDITIONAL_CHATS_PRICE_ID, price: process.env.STRIPE_ADDITIONAL_CHATS_PRICE_ID,
quantity: additionalChats, quantity: additionalChats,
deleted: additionalChats === 0, deleted: subscription ? additionalChats === 0 : undefined,
}, },
additionalStorage === 0 && !currentAdditionalStorageItemId additionalStorage === 0 && !currentAdditionalStorageItemId
? undefined ? undefined
@ -153,7 +159,7 @@ const updateSubscription = async (req: NextApiRequest) => {
id: currentAdditionalStorageItemId, id: currentAdditionalStorageItemId,
price: process.env.STRIPE_ADDITIONAL_STORAGE_PRICE_ID, price: process.env.STRIPE_ADDITIONAL_STORAGE_PRICE_ID,
quantity: additionalStorage, quantity: additionalStorage,
deleted: additionalStorage === 0, deleted: subscription ? additionalStorage === 0 : undefined,
}, },
].filter(isDefined) ].filter(isDefined)
@ -165,6 +171,7 @@ const updateSubscription = async (req: NextApiRequest) => {
await stripe.subscriptions.create({ await stripe.subscriptions.create({
customer: stripeId, customer: stripeId,
items, items,
currency,
}) })
} }

View File

@ -40,7 +40,7 @@ export const addSubscriptionToWorkspace = async (
customer: stripeId, customer: stripeId,
items, items,
default_payment_method: paymentId, default_payment_method: paymentId,
currency: 'eur', currency: 'usd',
}) })
await stripe.customers.update(stripeId, { await stripe.customers.update(stripeId, {
invoice_settings: { default_payment_method: paymentId }, invoice_settings: { default_payment_method: paymentId },

View File

@ -207,6 +207,12 @@ test('plan changes should work', async ({ page }) => {
await expect(page.locator('[data-testid="current-subscription"]')).toHaveText( await expect(page.locator('[data-testid="current-subscription"]')).toHaveText(
'Current workspace subscription: Free' '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 }) => { test('should display invoices', async ({ page }) => {