Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Add missing plan IDs

Blue Mouseff4b46c9e8f3
diff --git a/backend/apps/cloud/src/user/entities/user.entity.ts b/backend/apps/cloud/src/user/entities/user.entity.ts
index d2e1023e7..603357d4c 100644
--- a/backend/apps/cloud/src/user/entities/user.entity.ts
+++ b/backend/apps/cloud/src/user/entities/user.entity.ts
@@ -57,8 +57,8 @@ interface PlanSignature {
monthlyUsageLimit: number
maxAlerts: number
legacy?: boolean
- pid?: string
- ypid?: string
+ pid?: string | null
+ ypid?: string | null
}
const DEFAULT_MAX_PROJECTS = 10
@@ -117,6 +117,30 @@ const PLAN_FEATURE_REQUIRED_PLAN = {
[PlanFeatureCode.replays]: PlanType.plus,
} as const
+interface PaddleProductIds {
+ pid: string
+ ypid: string
+}
+
+const PLAN_TYPE_PADDLE_PRODUCTS: Partial<
+ Record<PlanType, Partial<Record<PlanCode, PaddleProductIds>>>
+> = {
+ [PlanType.plus]: {
+ [PlanCode['100k']]: { pid: '925536', ypid: '925537' },
+ [PlanCode['200k']]: { pid: '925538', ypid: '925539' },
+ [PlanCode['500k']]: { pid: '925540', ypid: '925541' },
+ [PlanCode['1m']]: { pid: '925542', ypid: '925543' },
+ [PlanCode['2m']]: { pid: '925544', ypid: '925545' },
+ [PlanCode['5m']]: { pid: '925546', ypid: '925547' },
+ [PlanCode['10m']]: { pid: '925548', ypid: '925549' },
+ [PlanCode['15m']]: { pid: '925550', ypid: '925551' },
+ [PlanCode['20m']]: { pid: '925552', ypid: '925553' },
+ [PlanCode['30m']]: { pid: '925554', ypid: '925555' },
+ [PlanCode['40m']]: { pid: '925556', ypid: '925557' },
+ [PlanCode['50m']]: { pid: '925558', ypid: '925559' },
+ },
+}
+
export const getEffectivePlanType = (
user?: {
planCode?: PlanCode | null
@@ -451,25 +475,25 @@ export const ACCOUNT_PLANS = {
[PlanCode['30m']]: {
id: PlanCode['30m'],
monthlyUsageLimit: 30000000,
- pid: null,
- ypid: null,
+ pid: '925498',
+ ypid: '925499',
maxAlerts: 50,
},
[PlanCode['40m']]: {
id: PlanCode['40m'],
monthlyUsageLimit: 40000000,
- pid: null,
- ypid: null,
+ pid: '925500',
+ ypid: '925501',
maxAlerts: 50,
},
[PlanCode['50m']]: {
id: PlanCode['50m'],
monthlyUsageLimit: 50000000,
- pid: null,
- ypid: null,
+ pid: '925503',
+ ypid: '925505',
maxAlerts: 50,
},
-}
+} satisfies Record<PlanCode, PlanSignature>
export const getNextPlan = (planCode: PlanCode): PlanSignature | undefined => {
const currentLimit = ACCOUNT_PLANS[planCode].monthlyUsageLimit
@@ -515,6 +539,58 @@ export enum BillingFrequency {
Yearly = 'yearly',
}
+export const getAccountPlanByPaddleProductId = (
+ productId?: string | number | null,
+) => {
+ if (!productId) {
+ return null
+ }
+
+ const stringifiedProductId = String(productId)
+
+ for (const plan of Object.values(ACCOUNT_PLANS)) {
+ if (plan.pid === stringifiedProductId) {
+ return {
+ plan,
+ planType: PlanType.standard,
+ billingFrequency: BillingFrequency.Monthly,
+ }
+ }
+
+ if (plan.ypid === stringifiedProductId) {
+ return {
+ plan,
+ planType: PlanType.standard,
+ billingFrequency: BillingFrequency.Yearly,
+ }
+ }
+
+ for (const [planType, productsByPlanCode] of Object.entries(
+ PLAN_TYPE_PADDLE_PRODUCTS,
+ )) {
+ const products = productsByPlanCode?.[plan.id]
+
+ if (products?.pid === stringifiedProductId) {
+ return {
+ plan,
+ planType: planType as PlanType,
+ billingFrequency: BillingFrequency.Monthly,
+ }
+ }
+
+ if (products?.ypid === stringifiedProductId) {
+ return {
+ plan,
+ planType: planType as PlanType,
+ billingFrequency: BillingFrequency.Yearly,
+ }
+ }
+ }
+ }
+
+ return null
+}
+
export enum TimeFormat {
'12-hour' = '12-hour',
'24-hour' = '24-hour',
diff --git a/backend/apps/cloud/src/user/user.service.ts b/backend/apps/cloud/src/user/user.service.ts
index 5b187096e..f28f9c46f 100644
--- a/backend/apps/cloud/src/user/user.service.ts
+++ b/backend/apps/cloud/src/user/user.service.ts
@@ -39,6 +39,7 @@ import {
getPurchasedWebsiteAddons,
getPurchasedSessionReplayAddons,
getSessionReplayQuota,
+ getAccountPlanByPaddleProductId,
} from './entities/user.entity'
import { UserProfileDTO } from './dto/user.dto'
import { RefreshToken } from './entities/refresh-token.entity'
@@ -2486,40 +2487,25 @@ export class UserService {
}
getPlanById(planId: number, requestedPlanType?: string | null) {
- if (!planId) {
+ const planMatch = getAccountPlanByPaddleProductId(planId)
+
+ if (!planMatch) {
return null
}
- const stringifiedPlanId = String(planId)
-
- const plan = Object.values(ACCOUNT_PLANS).find(
- (tier) =>
- // @ts-ignore
- tier.pid === stringifiedPlanId ||
- // @ts-ignore
- tier.ypid === stringifiedPlanId,
- )
-
- // @ts-ignore
- if (plan && plan.pid) {
- const planCode = plan.id
- // @ts-ignore
- const isMonthly =
- // @ts-ignore
- plan.pid === stringifiedPlanId
-
- const billingFrequency = isMonthly
- ? BillingFrequency.Monthly
- : BillingFrequency.Yearly
+ const requestedType = requestedPlanType
+ ? this.normalizePlanType(requestedPlanType)
+ : null
- return {
- planCode,
- billingFrequency,
- planType: this.normalizePlanType(requestedPlanType),
- }
+ if (requestedType && requestedType !== planMatch.planType) {
+ return null
}
- return null
+ return {
+ planCode: planMatch.plan.id,
+ billingFrequency: planMatch.billingFrequency,
+ planType: planMatch.planType,
+ }
}
async previewSubscription(
diff --git a/backend/apps/cloud/src/webhook/webhook.controller.ts b/backend/apps/cloud/src/webhook/webhook.controller.ts
index 9914bcbca..8b71f5691 100644
--- a/backend/apps/cloud/src/webhook/webhook.controller.ts
+++ b/backend/apps/cloud/src/webhook/webhook.controller.ts
@@ -9,15 +9,13 @@ import {
Ip,
} from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
-import _find from 'lodash/find'
import { ProjectService } from '../project/project.service'
import { getIPFromHeaders } from '../common/utils'
import {
- ACCOUNT_PLANS,
- BillingFrequency,
DashboardBlockReason,
PlanType,
+ getAccountPlanByPaddleProductId,
isNextPlan,
} from '../user/entities/user.entity'
import { UserService } from '../user/user.service'
@@ -119,19 +117,14 @@ export class WebhookController {
)
}
- let monthlyBilling = true
- let plan = _find(ACCOUNT_PLANS, ({ pid }) => pid === subscriptionPlanId)
+ const planMatch = getAccountPlanByPaddleProductId(subscriptionPlanId)
- if (!plan) {
- monthlyBilling = false
- plan = _find(ACCOUNT_PLANS, ({ ypid }) => ypid === subscriptionPlanId)
- }
-
- if (!plan) {
+ if (!planMatch) {
throw new NotFoundException(
`The selected account plan (${subscriptionPlanId}) is not available`,
)
}
+ const { plan, billingFrequency, planType: resolvedPlanType } = planMatch
let currentUser = uid
? await this.userService.findOne({ where: { id: uid } })
@@ -170,7 +163,9 @@ export class WebhookController {
: {}
const planType =
- requestedPlanType || currentUser.planType || PlanType.standard
+ requestedPlanType === resolvedPlanType
+ ? requestedPlanType
+ : resolvedPlanType
const updateParams: Record<string, any> = {
planCode: plan.id,
planType,
@@ -178,9 +173,7 @@ export class WebhookController {
subUpdateURL,
subCancelURL,
nextBillDate,
- billingFrequency: monthlyBilling
- ? BillingFrequency.Monthly
- : BillingFrequency.Yearly,
+ billingFrequency,
tierCurrency: currency,
cancellationEffectiveDate: null,
...statusParams,