2
0

🐛 (billing) Fix currency possible mismatch on sub update

This commit is contained in:
Baptiste Arnaud
2022-11-29 13:17:46 +01:00
parent 43a85b5529
commit f9ffdbc4c5
7 changed files with 18 additions and 5 deletions

View File

@@ -42,6 +42,7 @@ export const ChangePlanForm = () => {
workspaceId: workspace.id, workspaceId: workspace.id,
additionalChats: selectedChatsLimitIndex, additionalChats: selectedChatsLimitIndex,
additionalStorage: selectedStorageLimitIndex, additionalStorage: selectedStorageLimitIndex,
currency: data?.currency,
}) })
if (typeof response === 'object' && response?.error) { if (typeof response === 'object' && response?.error) {
showToast({ description: response.error.message }) showToast({ description: response.error.message })
@@ -75,6 +76,7 @@ export const ChangePlanForm = () => {
onPayClick={(props) => onPayClick={(props) =>
handlePayClick({ ...props, plan: Plan.STARTER }) handlePayClick({ ...props, plan: Plan.STARTER })
} }
currency={data?.currency}
/> />
<ProPlanContent <ProPlanContent
@@ -85,6 +87,7 @@ export const ChangePlanForm = () => {
workspace?.plan === Plan.PRO ? data?.additionalStorageIndex : 0 workspace?.plan === Plan.PRO ? data?.additionalStorageIndex : 0
} }
onPayClick={(props) => handlePayClick({ ...props, plan: Plan.PRO })} onPayClick={(props) => handlePayClick({ ...props, plan: Plan.PRO })}
currency={data?.currency}
/> />
</HStack> </HStack>
<Text color="gray.500"> <Text color="gray.500">

View File

@@ -32,6 +32,7 @@ import { MoreInfoTooltip } from '@/components/MoreInfoTooltip'
type ProPlanContentProps = { type ProPlanContentProps = {
initialChatsLimitIndex?: number initialChatsLimitIndex?: number
initialStorageLimitIndex?: number initialStorageLimitIndex?: number
currency?: 'usd' | 'eur'
onPayClick: (props: { onPayClick: (props: {
selectedChatsLimitIndex: number selectedChatsLimitIndex: number
selectedStorageLimitIndex: number selectedStorageLimitIndex: number
@@ -41,6 +42,7 @@ type ProPlanContentProps = {
export const ProPlanContent = ({ export const ProPlanContent = ({
initialChatsLimitIndex, initialChatsLimitIndex,
initialStorageLimitIndex, initialStorageLimitIndex,
currency,
onPayClick, onPayClick,
}: ProPlanContentProps) => { }: ProPlanContentProps) => {
const { workspace } = useWorkspace() const { workspace } = useWorkspace()
@@ -153,7 +155,8 @@ export const ProPlanContent = ({
Plan.PRO, Plan.PRO,
selectedChatsLimitIndex ?? 0, selectedChatsLimitIndex ?? 0,
selectedStorageLimitIndex ?? 0 selectedStorageLimitIndex ?? 0
) ?? NaN ) ?? NaN,
currency
)} )}
<chakra.span fontSize="md">/ month</chakra.span> <chakra.span fontSize="md">/ month</chakra.span>
</Heading> </Heading>

View File

@@ -29,6 +29,7 @@ import { MoreInfoTooltip } from '@/components/MoreInfoTooltip'
type StarterPlanContentProps = { type StarterPlanContentProps = {
initialChatsLimitIndex?: number initialChatsLimitIndex?: number
initialStorageLimitIndex?: number initialStorageLimitIndex?: number
currency?: 'eur' | 'usd'
onPayClick: (props: { onPayClick: (props: {
selectedChatsLimitIndex: number selectedChatsLimitIndex: number
selectedStorageLimitIndex: number selectedStorageLimitIndex: number
@@ -38,6 +39,7 @@ type StarterPlanContentProps = {
export const StarterPlanContent = ({ export const StarterPlanContent = ({
initialChatsLimitIndex, initialChatsLimitIndex,
initialStorageLimitIndex, initialStorageLimitIndex,
currency,
onPayClick, onPayClick,
}: StarterPlanContentProps) => { }: StarterPlanContentProps) => {
const { workspace } = useWorkspace() const { workspace } = useWorkspace()
@@ -126,7 +128,8 @@ export const StarterPlanContent = ({
Plan.STARTER, Plan.STARTER,
selectedChatsLimitIndex ?? 0, selectedChatsLimitIndex ?? 0,
selectedStorageLimitIndex ?? 0 selectedStorageLimitIndex ?? 0
) ?? NaN ) ?? NaN,
currency
)} )}
<chakra.span fontSize="md">/ month</chakra.span> <chakra.span fontSize="md">/ month</chakra.span>
</Heading> </Heading>

View File

@@ -13,6 +13,7 @@ export const useCurrentSubscriptionInfo = ({
{ {
additionalChatsIndex: number additionalChatsIndex: number
additionalStorageIndex: number additionalStorageIndex: number
currency?: 'eur' | 'usd'
}, },
Error Error
>( >(

View File

@@ -15,6 +15,7 @@ type UpgradeProps = {
workspaceId: string workspaceId: string
additionalChats: number additionalChats: number
additionalStorage: number additionalStorage: number
currency?: 'eur' | 'usd'
} }
export const upgradePlanQuery = async ({ export const upgradePlanQuery = async ({
@@ -31,6 +32,7 @@ const updatePlan = async ({
workspaceId, workspaceId,
additionalChats, additionalChats,
additionalStorage, additionalStorage,
currency,
}: Omit<UpgradeProps, 'user'>): Promise<{ newPlan?: Plan; error?: Error }> => { }: Omit<UpgradeProps, 'user'>): Promise<{ newPlan?: Plan; error?: Error }> => {
const { data, error } = await sendRequest<{ message: string }>({ const { data, error } = await sendRequest<{ message: string }>({
method: 'PUT', method: 'PUT',
@@ -41,7 +43,7 @@ const updatePlan = async ({
stripeId, stripeId,
additionalChats, additionalChats,
additionalStorage, additionalStorage,
currency: guessIfUserIsEuropean() ? 'eur' : 'usd', currency: currency ?? (guessIfUserIsEuropean() ? 'eur' : 'usd'),
}, },
}) })
if (error || !data) return { error } if (error || !data) return { error }

View File

@@ -63,6 +63,7 @@ const getSubscriptionDetails =
(item) => (item) =>
item.price.id === process.env.STRIPE_ADDITIONAL_STORAGE_PRICE_ID item.price.id === process.env.STRIPE_ADDITIONAL_STORAGE_PRICE_ID
)?.quantity ?? 0, )?.quantity ?? 0,
currency: subscriptions.data[0]?.currency,
} }
} }

View File

@@ -176,11 +176,11 @@ export const guessIfUserIsEuropean = () =>
: europeanUnionExclusiveLanguageCodes.includes(languageCode) : europeanUnionExclusiveLanguageCodes.includes(languageCode)
}) })
export const formatPrice = (price: number) => { export const formatPrice = (price: number, currency?: 'eur' | 'usd') => {
const isEuropean = guessIfUserIsEuropean() const isEuropean = guessIfUserIsEuropean()
const formatter = new Intl.NumberFormat(isEuropean ? 'fr-FR' : 'en-US', { const formatter = new Intl.NumberFormat(isEuropean ? 'fr-FR' : 'en-US', {
style: 'currency', style: 'currency',
currency: isEuropean ? 'EUR' : 'USD', currency: currency?.toUpperCase() ?? (isEuropean ? 'EUR' : 'USD'),
maximumFractionDigits: 0, // (causes 2500.99 to be printed as $2,501) maximumFractionDigits: 0, // (causes 2500.99 to be printed as $2,501)
}) })
return formatter.format(price) return formatter.format(price)