@stigg/react-sdk 4.13.1 → 4.14.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.
Files changed (34) hide show
  1. package/dist/components/common/TiersSelectContainer.d.ts +8 -3
  2. package/dist/components/common/VolumeBulkSelect.d.ts +5 -0
  3. package/dist/components/common/VolumePerUnitInput.d.ts +5 -0
  4. package/dist/components/paywall/PlanOfferingButton.d.ts +2 -1
  5. package/dist/components/paywall/PlanPrice.d.ts +3 -1
  6. package/dist/components/paywall/index.d.ts +1 -1
  7. package/dist/components/paywall/paywallTextOverrides.d.ts +9 -5
  8. package/dist/components/utils/getPaidPriceText.d.ts +2 -1
  9. package/dist/components/utils/getPlanPrice.d.ts +1 -1
  10. package/dist/components/utils/priceTierUtils.d.ts +3 -2
  11. package/dist/index.d.ts +1 -1
  12. package/dist/react-sdk.cjs.development.js +558 -365
  13. package/dist/react-sdk.cjs.development.js.map +1 -1
  14. package/dist/react-sdk.cjs.production.min.js +1 -1
  15. package/dist/react-sdk.cjs.production.min.js.map +1 -1
  16. package/dist/react-sdk.esm.js +628 -430
  17. package/dist/react-sdk.esm.js.map +1 -1
  18. package/package.json +2 -2
  19. package/src/components/checkout/hooks/usePlanStepModel.ts +3 -3
  20. package/src/components/checkout/steps/payment/stripe/stripe.utils.ts +10 -7
  21. package/src/components/checkout/steps/plan/CheckoutChargeList.tsx +10 -2
  22. package/src/components/common/TiersSelectContainer.tsx +33 -62
  23. package/src/components/common/VolumeBulkSelect.tsx +68 -0
  24. package/src/components/common/VolumePerUnitInput.tsx +40 -0
  25. package/src/components/paywall/PlanOffering.tsx +15 -6
  26. package/src/components/paywall/PlanOfferingButton.tsx +8 -2
  27. package/src/components/paywall/PlanPrice.tsx +20 -0
  28. package/src/components/paywall/index.ts +1 -1
  29. package/src/components/paywall/paywallTextOverrides.ts +7 -2
  30. package/src/components/utils/getPaidPriceText.ts +9 -1
  31. package/src/components/utils/getPlanPrice.ts +6 -1
  32. package/src/components/utils/priceTierUtils.ts +60 -14
  33. package/src/index.ts +1 -0
  34. package/src/stories/mocks/checkout/mockCheckoutPreview.ts +1 -1
@@ -14,10 +14,12 @@ export function getTierByQuantity(tiers: PriceTierFragment[], quantity: number)
14
14
  if (!tiers) return undefined;
15
15
 
16
16
  const ascendingTiers = [...tiers];
17
- ascendingTiers.sort((a, b) => a.upTo - b.upTo);
17
+ // Sort tiers by upTo value, ascending, if upTo is not set,
18
+ // put it at the end as it represent the last infinity tier
19
+ ascendingTiers.sort((a, b) => (!a.upTo ? 1 : !b.upTo ? -1 : a.upTo - b.upTo));
18
20
 
19
21
  for (const tier of ascendingTiers) {
20
- if (quantity <= tier.upTo) {
22
+ if (tier.upTo && quantity <= tier.upTo) {
21
23
  return tier;
22
24
  }
23
25
  }
@@ -30,21 +32,52 @@ export function calculateTierPrice(
30
32
  currentTier: PriceTierFragment,
31
33
  selectedBillingPeriod: BillingPeriod,
32
34
  shouldShowMonthlyPriceAmount: boolean,
35
+ perUnitQuantity?: number,
33
36
  ): number {
37
+ const unitPrice =
38
+ selectedBillingPeriod === BillingPeriod.Annually && shouldShowMonthlyPriceAmount
39
+ ? currentTier.unitPrice.amount / 12
40
+ : currentTier.unitPrice.amount;
34
41
  switch (price.tiersMode) {
35
42
  case TiersMode.Volume: {
36
- const unitPrice =
37
- selectedBillingPeriod === BillingPeriod.Annually && shouldShowMonthlyPriceAmount
38
- ? currentTier.unitPrice.amount / 12
39
- : currentTier.unitPrice.amount;
40
-
41
- return unitPrice * currentTier.upTo;
43
+ return unitPrice * (currentTier.upTo || 1);
44
+ }
45
+ case TiersMode.VolumePerUnit: {
46
+ return unitPrice * (perUnitQuantity || 1);
42
47
  }
43
48
  default:
44
49
  return 0;
45
50
  }
46
51
  }
47
52
 
53
+ export function getSelectedTierQuantityBeFeature(
54
+ plan: PaywallPlan,
55
+ billingPeriod: BillingPeriod,
56
+ currentSubscription: Subscription | null,
57
+ ) {
58
+ const result: Record<string, number> = {};
59
+ const planTierPrices = plan.pricePoints.filter(
60
+ (price) => price.billingPeriod === billingPeriod && price.isTieredPrice,
61
+ );
62
+ if (planTierPrices.length === 1) {
63
+ const [price] = planTierPrices;
64
+ const featureId = price?.feature!.featureId;
65
+ if (currentSubscription && currentSubscription.plan.id === plan.id) {
66
+ const tieredPrice = currentSubscription?.prices.find(
67
+ (subscriptionPrice) =>
68
+ subscriptionPrice.pricingModel === BillingModel.PerUnit &&
69
+ subscriptionPrice.tiersMode &&
70
+ subscriptionPrice.feature?.featureId === featureId,
71
+ );
72
+ if (tieredPrice) {
73
+ result[featureId] = tieredPrice.feature?.unitQuantity || 1;
74
+ }
75
+ }
76
+ }
77
+
78
+ return result;
79
+ }
80
+
48
81
  export function getSelectedTier(
49
82
  plan: PaywallPlan,
50
83
  billingPeriod: BillingPeriod,
@@ -64,7 +97,7 @@ export function getSelectedTier(
64
97
 
65
98
  if (selectedTierByFeature[featureId]) {
66
99
  currentTier = price.tiers?.find((tier) => tier.upTo === selectedTierByFeature[featureId].upTo) || currentTier;
67
- } else if (currentSubscription) {
100
+ } else if (currentSubscription && currentSubscription.plan.id === plan.id) {
68
101
  const tieredPrice = currentSubscription.prices.find(
69
102
  (subscriptionPrice) =>
70
103
  subscriptionPrice.pricingModel === BillingModel.PerUnit &&
@@ -94,6 +127,7 @@ export enum PriceTierComparison {
94
127
 
95
128
  export function compareSelectedTierToCurrentTier(
96
129
  selectedTierByFeature: Record<string, PriceTierFragment>,
130
+ perUnitQuantityByFeature: Record<string, number>,
97
131
  currentSubscription: Subscription | null,
98
132
  ): PriceTierComparison {
99
133
  if (!currentSubscription || !selectedTierByFeature) {
@@ -108,22 +142,34 @@ export function compareSelectedTierToCurrentTier(
108
142
  }
109
143
 
110
144
  const { featureId, unitQuantity } = currentTierPrice.feature!;
145
+ const { tiersMode } = currentTierPrice;
111
146
 
112
147
  if (!unitQuantity) {
113
148
  return PriceTierComparison.Equal;
114
149
  }
115
150
 
116
151
  const selectedTier = selectedTierByFeature[featureId];
152
+ const selectedQuantity = perUnitQuantityByFeature[featureId];
117
153
  if (!selectedTier) {
118
154
  return PriceTierComparison.Equal;
119
155
  }
120
156
 
121
- if (selectedTier.upTo < unitQuantity) {
122
- return PriceTierComparison.Lower;
123
- }
157
+ if (tiersMode === TiersMode.Volume) {
158
+ if (selectedTier.upTo && selectedTier.upTo < unitQuantity) {
159
+ return PriceTierComparison.Lower;
160
+ }
124
161
 
125
- if (selectedTier.upTo > unitQuantity) {
126
- return PriceTierComparison.Higher;
162
+ if (selectedTier.upTo && selectedTier.upTo > unitQuantity) {
163
+ return PriceTierComparison.Higher;
164
+ }
165
+ } else if (tiersMode === TiersMode.VolumePerUnit) {
166
+ if (selectedQuantity && selectedQuantity < unitQuantity) {
167
+ return PriceTierComparison.Lower;
168
+ }
169
+
170
+ if (selectedQuantity && selectedQuantity > unitQuantity) {
171
+ return PriceTierComparison.Higher;
172
+ }
127
173
  }
128
174
 
129
175
  return PriceTierComparison.Equal;
package/src/index.ts CHANGED
@@ -9,6 +9,7 @@ export {
9
9
  SubscribeIntentionType,
10
10
  PaywallLocalization,
11
11
  PlanPriceText,
12
+ CurrentPlanParams,
12
13
  } from './components/paywall';
13
14
  export {
14
15
  CustomerPortalProvider,
@@ -69,7 +69,7 @@ const billableFeatureCost = (plan: Plan, billingPeriod: BillingPeriod, featureId
69
69
  const { tiers } = price;
70
70
 
71
71
  if (tiers) {
72
- const quantityTier = tiers.find((tier) => tier.upTo >= quantity);
72
+ const quantityTier = tiers.find((tier) => tier.upTo && tier.upTo >= quantity);
73
73
  const priceTier = quantityTier || tiers[tiers.length - 1];
74
74
  return quantity * priceTier.unitPrice.amount;
75
75
  }