@strimz/shared-config 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/tiers.ts CHANGED
@@ -6,84 +6,106 @@
6
6
  * payment is recurring (i.e. originated from a SubscriptionPlan).
7
7
  */
8
8
 
9
- export type MerchantTier = 'free' | 'growth' | 'business' | 'enterprise'
9
+ import { PAYMENT_TOKENS, TOKENS, type PaymentToken } from './tokens.js'
10
10
 
11
- export interface TierDefinition {
11
+ export type MerchantTier = 'free' | 'starter' | 'growth' | 'enterprise'
12
+
13
+ export type VolumePlan = Exclude<MerchantTier, 'enterprise'>
14
+
15
+ export type PlanFeature = 'storefront' | 'anomaly_alerts' | 'yield_recommendations'
16
+
17
+ export interface PlanDefinition {
12
18
  id: MerchantTier
13
19
  name: string
14
- /** Monthly fee in USD cents (0 for free, null for custom enterprise). */
15
- monthlyFeeUsdCents: number | null
16
- /** Per-transaction fee on one-shot payments, in basis points. */
17
- oneShotFeeBps: number | null
18
- /** Per-transaction premium added on subscription payments, in basis points. */
19
- subscriptionPremiumBps: number | null
20
- /** Monthly transaction volume cap in USD cents. `null` for unlimited. */
21
- monthlyVolumeCapUsdCents: number | null
22
- /** Whether the "Powered by Strimz" badge can be removed from checkout. */
23
- whiteLabelEligible: boolean
24
- /** Whether the tier includes priority support. */
20
+ feeBps: number | null
21
+ entersAtVolume: number | null
22
+ features: readonly PlanFeature[]
25
23
  prioritySupport: boolean
26
24
  }
27
25
 
28
- export const TIERS: Record<MerchantTier, TierDefinition> = {
26
+ const PAID_FEATURES: readonly PlanFeature[] = [
27
+ 'storefront',
28
+ 'anomaly_alerts',
29
+ 'yield_recommendations',
30
+ ]
31
+
32
+ export const PLANS: Record<MerchantTier, PlanDefinition> = {
29
33
  free: {
30
34
  id: 'free',
31
35
  name: 'Free',
32
- monthlyFeeUsdCents: 0,
33
- oneShotFeeBps: 150, // 1.5%
34
- subscriptionPremiumBps: 20, // +0.2% on recurring => 1.7% effective
35
- monthlyVolumeCapUsdCents: 1_000_000, // $10,000
36
- whiteLabelEligible: false,
36
+ feeBps: 0,
37
+ entersAtVolume: 0,
38
+ features: [],
37
39
  prioritySupport: false,
38
40
  },
41
+ starter: {
42
+ id: 'starter',
43
+ name: 'Starter',
44
+ feeBps: 50,
45
+ entersAtVolume: 1_000,
46
+ features: PAID_FEATURES,
47
+ prioritySupport: true,
48
+ },
39
49
  growth: {
40
50
  id: 'growth',
41
51
  name: 'Growth',
42
- monthlyFeeUsdCents: 4_900, // $49
43
- oneShotFeeBps: 80, // 0.8%
44
- subscriptionPremiumBps: 20, // 1.0% effective
45
- monthlyVolumeCapUsdCents: 10_000_000, // $100,000
46
- whiteLabelEligible: true,
47
- prioritySupport: false,
48
- },
49
- business: {
50
- id: 'business',
51
- name: 'Business',
52
- monthlyFeeUsdCents: 19_900, // $199
53
- oneShotFeeBps: 50, // 0.5%
54
- subscriptionPremiumBps: 20, // 0.7% effective
55
- monthlyVolumeCapUsdCents: 100_000_000, // $1,000,000
56
- whiteLabelEligible: true,
52
+ feeBps: 40,
53
+ entersAtVolume: 50_000,
54
+ features: PAID_FEATURES,
57
55
  prioritySupport: true,
58
56
  },
59
57
  enterprise: {
60
58
  id: 'enterprise',
61
59
  name: 'Enterprise',
62
- monthlyFeeUsdCents: null,
63
- oneShotFeeBps: null,
64
- subscriptionPremiumBps: null,
65
- monthlyVolumeCapUsdCents: null,
66
- whiteLabelEligible: true,
60
+ feeBps: null,
61
+ entersAtVolume: null,
62
+ features: PAID_FEATURES,
67
63
  prioritySupport: true,
68
64
  },
69
65
  }
70
66
 
71
67
  export const DEFAULT_TIER: MerchantTier = 'free'
72
68
 
73
- export function getTier(id: MerchantTier): TierDefinition {
74
- return TIERS[id]
69
+ const PLAN_ORDER: readonly MerchantTier[] = ['free', 'starter', 'growth', 'enterprise']
70
+
71
+ const VOLUME_PLANS: readonly VolumePlan[] = ['free', 'starter', 'growth']
72
+
73
+ export const PLAN_FEE_CEILING_BPS: number = Math.max(
74
+ ...VOLUME_PLANS.map((plan) => PLANS[plan].feeBps ?? 0),
75
+ )
76
+
77
+ export function planFeeBps(plan: MerchantTier): number | null {
78
+ return PLANS[plan].feeBps
75
79
  }
76
80
 
77
- /**
78
- * Effective fee in bps for a given tier and payment kind.
79
- * Returns `null` for the enterprise tier (custom-priced).
80
- */
81
- export function effectiveFeeBps(
82
- tier: MerchantTier,
83
- kind: 'one_shot' | 'subscription',
84
- ): number | null {
85
- const def = TIERS[tier]
86
- if (def.oneShotFeeBps === null) return null
87
- if (kind === 'one_shot') return def.oneShotFeeBps
88
- return def.oneShotFeeBps + (def.subscriptionPremiumBps ?? 0)
81
+ export function planIncludes(plan: MerchantTier, feature: PlanFeature): boolean {
82
+ return PLANS[plan].features.includes(feature)
83
+ }
84
+
85
+ export function plansWith(feature: PlanFeature): MerchantTier[] {
86
+ return PLAN_ORDER.filter((plan) => planIncludes(plan, feature))
87
+ }
88
+
89
+ export function lowestPlanWith(feature: PlanFeature): MerchantTier {
90
+ const plan = PLAN_ORDER.find((candidate) => planIncludes(candidate, feature))
91
+ if (!plan) throw new Error(`no plan includes ${feature}`)
92
+ return plan
93
+ }
94
+
95
+ export type PaymentVolumes = Readonly<Record<PaymentToken, bigint>>
96
+
97
+ function thresholdInBaseUnits(plan: VolumePlan, token: PaymentToken): bigint {
98
+ const wholeUnits = PLANS[plan].entersAtVolume
99
+ if (wholeUnits === null) throw new Error(`${plan} has no volume threshold`)
100
+ return BigInt(wholeUnits) * 10n ** BigInt(TOKENS[token].decimals)
101
+ }
102
+
103
+ export function planForVolume(volumes: PaymentVolumes): VolumePlan {
104
+ let reached: VolumePlan = 'free'
105
+ for (const plan of VOLUME_PLANS) {
106
+ if (PAYMENT_TOKENS.some((token) => volumes[token] >= thresholdInBaseUnits(plan, token))) {
107
+ reached = plan
108
+ }
109
+ }
110
+ return reached
89
111
  }