@fantastic.dev/better-stripe 0.87.0 → 0.88.1
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/CHANGELOG.md +12 -0
- package/README.md +1 -1
- package/dist/client/billing/renewalAggregate/readMaterial.d.ts.map +1 -1
- package/dist/client/billing/renewalAggregate/readMaterial.js +4 -3
- package/dist/client/billing/renewalAggregate/readMaterial.js.map +1 -1
- package/dist/client/billing/renewalObservation/active-scheduled.d.ts +6 -0
- package/dist/client/billing/renewalObservation/active-scheduled.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/active-scheduled.js +37 -0
- package/dist/client/billing/renewalObservation/active-scheduled.js.map +1 -0
- package/dist/client/billing/renewalObservation/common.d.ts +47 -0
- package/dist/client/billing/renewalObservation/common.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/common.js +39 -0
- package/dist/client/billing/renewalObservation/common.js.map +1 -0
- package/dist/client/billing/renewalObservation/configured-baseline.d.ts +76 -0
- package/dist/client/billing/renewalObservation/configured-baseline.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/configured-baseline.js +26 -0
- package/dist/client/billing/renewalObservation/configured-baseline.js.map +1 -0
- package/dist/client/billing/renewalObservation/coupon-discount-joins.d.ts +53 -0
- package/dist/client/billing/renewalObservation/coupon-discount-joins.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/coupon-discount-joins.js +103 -0
- package/dist/client/billing/renewalObservation/coupon-discount-joins.js.map +1 -0
- package/dist/client/billing/renewalObservation/invoice-tax-identity.d.ts +52 -0
- package/dist/client/billing/renewalObservation/invoice-tax-identity.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/invoice-tax-identity.js +49 -0
- package/dist/client/billing/renewalObservation/invoice-tax-identity.js.map +1 -0
- package/dist/client/billing/renewalObservation/pending-and-stage.d.ts +76 -0
- package/dist/client/billing/renewalObservation/pending-and-stage.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/pending-and-stage.js +36 -0
- package/dist/client/billing/renewalObservation/pending-and-stage.js.map +1 -0
- package/dist/client/billing/renewalObservation/phase-price-normalization.d.ts +57 -0
- package/dist/client/billing/renewalObservation/phase-price-normalization.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/phase-price-normalization.js +70 -0
- package/dist/client/billing/renewalObservation/phase-price-normalization.js.map +1 -0
- package/dist/client/billing/renewalObservation/planProjection.d.ts +8 -0
- package/dist/client/billing/renewalObservation/planProjection.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/planProjection.js +150 -0
- package/dist/client/billing/renewalObservation/planProjection.js.map +1 -0
- package/dist/client/billing/renewalObservation/reference-catalog.d.ts +37 -0
- package/dist/client/billing/renewalObservation/reference-catalog.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/reference-catalog.js +109 -0
- package/dist/client/billing/renewalObservation/reference-catalog.js.map +1 -0
- package/dist/client/billing/renewalObservation/schedule-templates.d.ts +74 -0
- package/dist/client/billing/renewalObservation/schedule-templates.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/schedule-templates.js +72 -0
- package/dist/client/billing/renewalObservation/schedule-templates.js.map +1 -0
- package/dist/client/billing/renewalObservation/scheduled-context.d.ts +44 -0
- package/dist/client/billing/renewalObservation/scheduled-context.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/scheduled-context.js +64 -0
- package/dist/client/billing/renewalObservation/scheduled-context.js.map +1 -0
- package/dist/client/billing/renewalObservation/scheduled-identity.d.ts +53 -0
- package/dist/client/billing/renewalObservation/scheduled-identity.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/scheduled-identity.js +95 -0
- package/dist/client/billing/renewalObservation/scheduled-identity.js.map +1 -0
- package/dist/client/billing/renewalObservation/scheduled-normalization.d.ts +59 -0
- package/dist/client/billing/renewalObservation/scheduled-normalization.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/scheduled-normalization.js +29 -0
- package/dist/client/billing/renewalObservation/scheduled-normalization.js.map +1 -0
- package/dist/client/billing/renewalObservation/settings-normalization.d.ts +54 -0
- package/dist/client/billing/renewalObservation/settings-normalization.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/settings-normalization.js +53 -0
- package/dist/client/billing/renewalObservation/settings-normalization.js.map +1 -0
- package/dist/client/billing/renewalObservation/stage-runner.d.ts +4 -0
- package/dist/client/billing/renewalObservation/stage-runner.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/stage-runner.js +34 -0
- package/dist/client/billing/renewalObservation/stage-runner.js.map +1 -0
- package/dist/client/billing/renewalObservation/weeklyPending.d.ts +13 -0
- package/dist/client/billing/renewalObservation/weeklyPending.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/weeklyPending.js +55 -0
- package/dist/client/billing/renewalObservation/weeklyPending.js.map +1 -0
- package/dist/client/billing/renewalPriceReplacement.d.ts.map +1 -1
- package/dist/client/billing/renewalPriceReplacement.js +7 -3
- package/dist/client/billing/renewalPriceReplacement.js.map +1 -1
- package/dist/client/billing/renewalV2/index.d.ts +383 -0
- package/dist/client/billing/renewalV2/index.d.ts.map +1 -1
- package/dist/client/billing/renewalV2/index.js +8 -3
- package/dist/client/billing/renewalV2/index.js.map +1 -1
- package/dist/client/billing/renewalV2/observation.d.ts +19 -0
- package/dist/client/billing/renewalV2/observation.d.ts.map +1 -0
- package/dist/client/billing/renewalV2/observation.js +95 -0
- package/dist/client/billing/renewalV2/observation.js.map +1 -0
- package/dist/client/billing/renewalV2/observationInput.d.ts +77 -0
- package/dist/client/billing/renewalV2/observationInput.d.ts.map +1 -0
- package/dist/client/billing/renewalV2/observationInput.js +140 -0
- package/dist/client/billing/renewalV2/observationInput.js.map +1 -0
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +16 -16
- package/dist/client/renewal.d.ts +2 -0
- package/dist/client/renewal.d.ts.map +1 -1
- package/dist/client/renewal.js +1 -0
- package/dist/client/renewal.js.map +1 -1
- package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
- package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
- package/dist/component/balanceFunding/workflowValidators.d.ts +16 -16
- package/dist/component/earnings/triggers.d.ts +16 -16
- package/dist/component/usage/allowance.fixtures.d.ts +16 -16
- package/docs/client-api.md +26 -0
- package/docs/renewal-context-reads.md +10 -0
- package/docs/renewal-material-observation.md +124 -0
- package/docs/renewal-phase-assembly.md +7 -0
- package/docs/renewal-price-replacements.md +3 -0
- package/package.json +1 -1
- package/src/client/billing/renewalAggregate/readMaterial.ts +5 -3
- package/src/client/billing/renewalObservation/active-scheduled.ts +67 -0
- package/src/client/billing/renewalObservation/common.ts +83 -0
- package/src/client/billing/renewalObservation/configured-baseline.ts +55 -0
- package/src/client/billing/renewalObservation/coupon-discount-joins.ts +163 -0
- package/src/client/billing/renewalObservation/invoice-tax-identity.ts +65 -0
- package/src/client/billing/renewalObservation/pending-and-stage.ts +89 -0
- package/src/client/billing/renewalObservation/phase-price-normalization.ts +106 -0
- package/src/client/billing/renewalObservation/planProjection.ts +177 -0
- package/src/client/billing/renewalObservation/reference-catalog.ts +150 -0
- package/src/client/billing/renewalObservation/schedule-templates.ts +118 -0
- package/src/client/billing/renewalObservation/scheduled-context.ts +98 -0
- package/src/client/billing/renewalObservation/scheduled-identity.ts +175 -0
- package/src/client/billing/renewalObservation/scheduled-normalization.ts +49 -0
- package/src/client/billing/renewalObservation/settings-normalization.ts +60 -0
- package/src/client/billing/renewalObservation/stage-runner.ts +30 -0
- package/src/client/billing/renewalObservation/weeklyPending.ts +81 -0
- package/src/client/billing/renewalPriceReplacement.ts +7 -3
- package/src/client/billing/renewalV2/index.ts +14 -6
- package/src/client/billing/renewalV2/observation.boundary-review.test.ts +49 -0
- package/src/client/billing/renewalV2/observation.test.ts +334 -0
- package/src/client/billing/renewalV2/observation.ts +149 -0
- package/src/client/billing/renewalV2/observationInput.ts +167 -0
- package/src/client/billing/renewalV2/rawRetention.test.ts +293 -0
- package/src/client/renewal.ts +7 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { object, array, need, clone, get, equal, type O } from "./common.js";
|
|
2
|
+
import type { identities } from "./scheduled-identity.js";
|
|
3
|
+
export function redemptions(
|
|
4
|
+
state: Extract<ReturnType<typeof identities>, { kind: "continue" }>["state"],
|
|
5
|
+
) {
|
|
6
|
+
const {
|
|
7
|
+
expected,
|
|
8
|
+
originalSub,
|
|
9
|
+
originalItem,
|
|
10
|
+
reads,
|
|
11
|
+
sub,
|
|
12
|
+
ref,
|
|
13
|
+
customerId,
|
|
14
|
+
actualItem,
|
|
15
|
+
} = state;
|
|
16
|
+
// BEGIN original 416-559
|
|
17
|
+
const couponRows = array(reads.coupons);
|
|
18
|
+
need(couponRows.length <= 40, "coupon_budget_exceeded", "unknown");
|
|
19
|
+
const couponMap = new Map<string, O>();
|
|
20
|
+
for (const row of couponRows) {
|
|
21
|
+
const c = object(row);
|
|
22
|
+
need(
|
|
23
|
+
typeof c.id === "string" &&
|
|
24
|
+
c.id.trim().length > 0 &&
|
|
25
|
+
c.object === "coupon" &&
|
|
26
|
+
c.deleted !== true &&
|
|
27
|
+
c.livemode === expected.livemode,
|
|
28
|
+
"coupon_join_identity_mismatch",
|
|
29
|
+
);
|
|
30
|
+
need(!couponMap.has(c.id), "duplicate_coupon_read", "unknown");
|
|
31
|
+
couponMap.set(c.id, c);
|
|
32
|
+
}
|
|
33
|
+
const coupon = (v: unknown): O => {
|
|
34
|
+
const id = typeof v === "string" ? v : object(v).id;
|
|
35
|
+
need(
|
|
36
|
+
typeof id === "string" && couponMap.has(id),
|
|
37
|
+
"missing_correlated_coupon",
|
|
38
|
+
"unknown",
|
|
39
|
+
);
|
|
40
|
+
const fetched = couponMap.get(id as string)!;
|
|
41
|
+
if (typeof v !== "string")
|
|
42
|
+
need(equal(v, fetched), "expanded_coupon_read_drift");
|
|
43
|
+
return clone(fetched);
|
|
44
|
+
};
|
|
45
|
+
const discounts = (
|
|
46
|
+
v: unknown,
|
|
47
|
+
level: "subscription" | "item",
|
|
48
|
+
itemId: string,
|
|
49
|
+
): O[] => {
|
|
50
|
+
const rows = array(v);
|
|
51
|
+
need(rows.length <= 20, "discount_budget_exceeded", "unknown");
|
|
52
|
+
return rows.map((row) => {
|
|
53
|
+
const d = clone(object(row));
|
|
54
|
+
need(
|
|
55
|
+
typeof d.id === "string" &&
|
|
56
|
+
/^di_[A-Za-z0-9_]+$/.test(d.id) &&
|
|
57
|
+
d.object === "discount" &&
|
|
58
|
+
d.deleted !== true &&
|
|
59
|
+
d.customer_account === expected.customerAccountId,
|
|
60
|
+
"discount_identity_mismatch",
|
|
61
|
+
);
|
|
62
|
+
if (d.customer !== undefined)
|
|
63
|
+
need(
|
|
64
|
+
ref(d.customer, "cus", "customer") === customerId,
|
|
65
|
+
"discount_customer_mismatch",
|
|
66
|
+
);
|
|
67
|
+
need(
|
|
68
|
+
level === "subscription"
|
|
69
|
+
? d.subscription === expected.subscriptionId &&
|
|
70
|
+
d.subscription_item === null
|
|
71
|
+
: d.subscription_item === itemId &&
|
|
72
|
+
(d.subscription === null ||
|
|
73
|
+
d.subscription === expected.subscriptionId),
|
|
74
|
+
"discount_level_owner_mismatch",
|
|
75
|
+
"unknown",
|
|
76
|
+
);
|
|
77
|
+
const source = get(d, "source");
|
|
78
|
+
need(source.type === "coupon", "unsupported_discount_source", "unknown");
|
|
79
|
+
source.coupon = coupon(source.coupon);
|
|
80
|
+
const c = object(source.coupon);
|
|
81
|
+
need(
|
|
82
|
+
c.duration === "forever" && d.end === null,
|
|
83
|
+
"discount_duration_unqualified",
|
|
84
|
+
"unknown",
|
|
85
|
+
);
|
|
86
|
+
if (c.applies_to !== undefined) {
|
|
87
|
+
const products = array(get(c, "applies_to").products);
|
|
88
|
+
need(
|
|
89
|
+
products.includes(expected.currentProductId) &&
|
|
90
|
+
products.includes(expected.targetProductId),
|
|
91
|
+
"coupon_product_applicability",
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
d.source = source;
|
|
95
|
+
d.customer = d.customer === undefined ? undefined : customerId;
|
|
96
|
+
if (d.customer === undefined) delete d.customer;
|
|
97
|
+
// Validation-view marker only. Both actual ownerships are checked first;
|
|
98
|
+
// raw historical/current IDs remain untouched in rawReads/original records.
|
|
99
|
+
if (level === "item") {
|
|
100
|
+
d.subscription = expected.subscriptionId;
|
|
101
|
+
d.subscription_item = "AUTHORIZED_CURRENT_ITEM";
|
|
102
|
+
}
|
|
103
|
+
return d;
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
const wantedItemId = String(actualItem.id);
|
|
107
|
+
const actualSubDiscounts = discounts(
|
|
108
|
+
sub.discounts,
|
|
109
|
+
"subscription",
|
|
110
|
+
wantedItemId,
|
|
111
|
+
),
|
|
112
|
+
actualItemDiscounts = discounts(actualItem.discounts, "item", wantedItemId);
|
|
113
|
+
const originalSubDiscounts = discounts(
|
|
114
|
+
originalSub.discounts,
|
|
115
|
+
"subscription",
|
|
116
|
+
String(expected.itemId),
|
|
117
|
+
);
|
|
118
|
+
const originalItemDiscounts = discounts(
|
|
119
|
+
originalItem.discounts,
|
|
120
|
+
"item",
|
|
121
|
+
String(expected.itemId),
|
|
122
|
+
);
|
|
123
|
+
need(
|
|
124
|
+
equal(actualSubDiscounts, originalSubDiscounts) &&
|
|
125
|
+
equal(actualItemDiscounts, originalItemDiscounts),
|
|
126
|
+
"redemption_full_read_drift",
|
|
127
|
+
);
|
|
128
|
+
const neededCouponIds = new Set(
|
|
129
|
+
[
|
|
130
|
+
...array(originalSub.discounts),
|
|
131
|
+
...array(originalItem.discounts),
|
|
132
|
+
...array(sub.discounts),
|
|
133
|
+
...array(actualItem.discounts),
|
|
134
|
+
].map((row) => {
|
|
135
|
+
const value = get(object(row), "source").coupon;
|
|
136
|
+
return typeof value === "string" ? value : String(object(value).id);
|
|
137
|
+
}),
|
|
138
|
+
);
|
|
139
|
+
need(
|
|
140
|
+
couponMap.size === neededCouponIds.size &&
|
|
141
|
+
[...couponMap.keys()].every((id) => neededCouponIds.has(id)),
|
|
142
|
+
"unreferenced_coupon_read",
|
|
143
|
+
"unknown",
|
|
144
|
+
);
|
|
145
|
+
const currentRedemptions = new Map<string, O>();
|
|
146
|
+
for (const d of [...array(sub.discounts), ...array(actualItem.discounts)]) {
|
|
147
|
+
const raw = clone(object(d));
|
|
148
|
+
const source = get(raw, "source");
|
|
149
|
+
source.coupon = coupon(source.coupon);
|
|
150
|
+
raw.source = source;
|
|
151
|
+
currentRedemptions.set(String(raw.id), raw);
|
|
152
|
+
}
|
|
153
|
+
// END original 416-559
|
|
154
|
+
return {
|
|
155
|
+
...state,
|
|
156
|
+
coupon,
|
|
157
|
+
actualItemDiscounts,
|
|
158
|
+
actualSubDiscounts,
|
|
159
|
+
originalItemDiscounts,
|
|
160
|
+
originalSubDiscounts,
|
|
161
|
+
currentRedemptions,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { object, array, need, get, equal } from "./common.js";
|
|
2
|
+
import type { redemptions } from "./coupon-discount-joins.js";
|
|
3
|
+
export function taxes(state: ReturnType<typeof redemptions>) {
|
|
4
|
+
const { expected, originalSub, originalSchedule, ref, originalPhase } = state;
|
|
5
|
+
// BEGIN original 560-617
|
|
6
|
+
const originalDefaults = get(originalSchedule, "default_settings");
|
|
7
|
+
const invoiceObjects = [
|
|
8
|
+
originalSub.invoice_settings,
|
|
9
|
+
originalDefaults.invoice_settings,
|
|
10
|
+
originalPhase.invoice_settings,
|
|
11
|
+
]
|
|
12
|
+
.filter((v) => v !== null && v !== undefined)
|
|
13
|
+
.map(object);
|
|
14
|
+
const retainedTaxIds = invoiceObjects
|
|
15
|
+
.flatMap((inv) =>
|
|
16
|
+
inv.account_tax_ids === null || inv.account_tax_ids === undefined
|
|
17
|
+
? []
|
|
18
|
+
: array(inv.account_tax_ids),
|
|
19
|
+
)
|
|
20
|
+
.filter((v) => typeof v !== "string")
|
|
21
|
+
.map(object);
|
|
22
|
+
const retainedPMs = [
|
|
23
|
+
originalSub.default_payment_method,
|
|
24
|
+
originalDefaults.default_payment_method,
|
|
25
|
+
originalPhase.default_payment_method,
|
|
26
|
+
]
|
|
27
|
+
.filter((v) => v !== null && v !== undefined && typeof v !== "string")
|
|
28
|
+
.map(object);
|
|
29
|
+
const invoiceTaxId = (value: unknown) => {
|
|
30
|
+
const id = ref(value, "txi", "tax_id");
|
|
31
|
+
if (typeof value !== "string") {
|
|
32
|
+
const tax = object(value);
|
|
33
|
+
need(
|
|
34
|
+
tax.livemode === expected.livemode &&
|
|
35
|
+
(tax.customer_account === undefined || tax.customer_account === null),
|
|
36
|
+
"invoice_tax_mode_or_scope_mismatch",
|
|
37
|
+
);
|
|
38
|
+
const owner = get(tax, "owner"),
|
|
39
|
+
issuer = get(get(originalSub, "invoice_settings"), "issuer");
|
|
40
|
+
need(
|
|
41
|
+
owner.type === issuer.type && owner.customer_account === null,
|
|
42
|
+
"invoice_tax_owner_mismatch",
|
|
43
|
+
);
|
|
44
|
+
if (owner.type === "account")
|
|
45
|
+
need(
|
|
46
|
+
ref(owner.account, "acct", "account") ===
|
|
47
|
+
ref(issuer.account, "acct", "account"),
|
|
48
|
+
"invoice_tax_issuer_mismatch",
|
|
49
|
+
);
|
|
50
|
+
else
|
|
51
|
+
need(
|
|
52
|
+
owner.type === "self" &&
|
|
53
|
+
(owner.account === undefined || owner.account === null),
|
|
54
|
+
"invoice_tax_owner_unsupported",
|
|
55
|
+
"unknown",
|
|
56
|
+
);
|
|
57
|
+
for (const retained of retainedTaxIds)
|
|
58
|
+
if (retained.id === id)
|
|
59
|
+
need(equal(tax, retained), "invoice_tax_full_read_drift");
|
|
60
|
+
}
|
|
61
|
+
return id;
|
|
62
|
+
};
|
|
63
|
+
// END original 560-617
|
|
64
|
+
return { ...state, invoiceTaxId, retainedPMs };
|
|
65
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { weeklyPending } from "./weeklyPending.js";
|
|
2
|
+
import {
|
|
3
|
+
Stop,
|
|
4
|
+
object,
|
|
5
|
+
array,
|
|
6
|
+
need,
|
|
7
|
+
get,
|
|
8
|
+
equal,
|
|
9
|
+
type ObserverCandidateResult,
|
|
10
|
+
} from "./common.js";
|
|
11
|
+
import type { baseline } from "./configured-baseline.js";
|
|
12
|
+
export function pending(
|
|
13
|
+
state: Extract<ReturnType<typeof baseline>, { kind: "continue" }>["state"],
|
|
14
|
+
) {
|
|
15
|
+
const {
|
|
16
|
+
rawReads,
|
|
17
|
+
base,
|
|
18
|
+
evidence,
|
|
19
|
+
originalItem,
|
|
20
|
+
sub,
|
|
21
|
+
target,
|
|
22
|
+
desired,
|
|
23
|
+
actualItem,
|
|
24
|
+
current,
|
|
25
|
+
currentOriginal,
|
|
26
|
+
activeTarget,
|
|
27
|
+
normalizeSub,
|
|
28
|
+
sourceWithAssociation,
|
|
29
|
+
targetPhase,
|
|
30
|
+
comparableSchedule,
|
|
31
|
+
} = state;
|
|
32
|
+
// BEGIN original 880-926
|
|
33
|
+
if (evidence) {
|
|
34
|
+
need(
|
|
35
|
+
targetPhase.end_date === evidence.targetEnd &&
|
|
36
|
+
equal(comparableSchedule, evidence.comparable),
|
|
37
|
+
"configured_ack_full_baseline_drift",
|
|
38
|
+
);
|
|
39
|
+
need(
|
|
40
|
+
!(evidence.stage === "target" && currentOriginal),
|
|
41
|
+
"phase_regression_from_configured_ack",
|
|
42
|
+
"unknown",
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
if (currentOriginal) {
|
|
46
|
+
need(
|
|
47
|
+
equal(
|
|
48
|
+
normalizeSub(sub, "original", get(originalItem, "price")),
|
|
49
|
+
normalizeSub(
|
|
50
|
+
sourceWithAssociation,
|
|
51
|
+
"original",
|
|
52
|
+
get(originalItem, "price"),
|
|
53
|
+
),
|
|
54
|
+
),
|
|
55
|
+
"current_paid_material_drift",
|
|
56
|
+
);
|
|
57
|
+
if (evidence || weeklyPending(state))
|
|
58
|
+
return {
|
|
59
|
+
kind: "result" as const,
|
|
60
|
+
result: {
|
|
61
|
+
...base,
|
|
62
|
+
predicate: "confirmed_pending",
|
|
63
|
+
rawReads,
|
|
64
|
+
} as ObserverCandidateResult,
|
|
65
|
+
};
|
|
66
|
+
throw new Stop("future_duration_not_bound", "unknown");
|
|
67
|
+
}
|
|
68
|
+
need(
|
|
69
|
+
activeTarget &&
|
|
70
|
+
current.start_date === targetPhase.start_date &&
|
|
71
|
+
current.end_date === targetPhase.end_date,
|
|
72
|
+
"current_phase_target_relation_mismatch",
|
|
73
|
+
);
|
|
74
|
+
need(
|
|
75
|
+
actualItem.current_period_start === current.start_date &&
|
|
76
|
+
actualItem.current_period_end === current.end_date,
|
|
77
|
+
"native_target_period_not_exact",
|
|
78
|
+
"unknown",
|
|
79
|
+
);
|
|
80
|
+
need(
|
|
81
|
+
equal(object(array(get(desired, "payload").phases)[1]).duration, {
|
|
82
|
+
interval: get(target, "recurring").interval,
|
|
83
|
+
interval_count: get(target, "recurring").interval_count,
|
|
84
|
+
}),
|
|
85
|
+
"target_native_interval_mismatch",
|
|
86
|
+
);
|
|
87
|
+
// END original 880-926
|
|
88
|
+
return { kind: "continue" as const, state: { ...state } };
|
|
89
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import {
|
|
2
|
+
object,
|
|
3
|
+
array,
|
|
4
|
+
need,
|
|
5
|
+
clone,
|
|
6
|
+
get,
|
|
7
|
+
equal,
|
|
8
|
+
productId,
|
|
9
|
+
planMatchesPrice,
|
|
10
|
+
type O,
|
|
11
|
+
} from "./common.js";
|
|
12
|
+
import type { settingsStage } from "./settings-normalization.js";
|
|
13
|
+
export function phasesStage(state: ReturnType<typeof settingsStage>) {
|
|
14
|
+
const { context, ref, coupon, currentRedemptions, settings } = state;
|
|
15
|
+
// BEGIN original 670-758
|
|
16
|
+
const phaseDiscounts = (v: unknown) =>
|
|
17
|
+
array(v).map((row) => {
|
|
18
|
+
const d = clone(object(row));
|
|
19
|
+
need(
|
|
20
|
+
d.promotion_code === null,
|
|
21
|
+
"promotion_code_not_supported",
|
|
22
|
+
"unknown",
|
|
23
|
+
);
|
|
24
|
+
const id =
|
|
25
|
+
typeof d.discount === "string"
|
|
26
|
+
? d.discount
|
|
27
|
+
: String(object(d.discount).id);
|
|
28
|
+
const known = currentRedemptions.get(id);
|
|
29
|
+
need(known, "phase_redemption_not_correlated", "unknown");
|
|
30
|
+
if (typeof d.discount !== "string") {
|
|
31
|
+
const expanded = object(d.discount);
|
|
32
|
+
need(
|
|
33
|
+
expanded.object === "discount" && expanded.deleted !== true,
|
|
34
|
+
"bad_expanded_discount",
|
|
35
|
+
);
|
|
36
|
+
if (
|
|
37
|
+
!Object.keys(expanded).every((k) =>
|
|
38
|
+
["id", "object", "deleted"].includes(k),
|
|
39
|
+
)
|
|
40
|
+
) {
|
|
41
|
+
const joined = clone(expanded);
|
|
42
|
+
const source = get(joined, "source");
|
|
43
|
+
source.coupon = coupon(source.coupon);
|
|
44
|
+
joined.source = source;
|
|
45
|
+
need(equal(joined, known), "expanded_discount_read_drift");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (d.coupon !== null) {
|
|
49
|
+
const cid =
|
|
50
|
+
typeof d.coupon === "string" ? d.coupon : object(d.coupon).id;
|
|
51
|
+
need(
|
|
52
|
+
cid === get(get(known!, "source"), "coupon").id,
|
|
53
|
+
"phase_coupon_identity_mismatch",
|
|
54
|
+
);
|
|
55
|
+
if (typeof d.coupon !== "string") coupon(d.coupon);
|
|
56
|
+
}
|
|
57
|
+
d.discount = id;
|
|
58
|
+
d.coupon = get(get(known!, "source"), "coupon").id;
|
|
59
|
+
return d;
|
|
60
|
+
});
|
|
61
|
+
const priceRef = (v: unknown, known: O, kind = "price") => {
|
|
62
|
+
const id = ref(v, "price", kind);
|
|
63
|
+
need(id === known.id, "phase_price_mismatch");
|
|
64
|
+
if (typeof v !== "string") {
|
|
65
|
+
const e = object(v);
|
|
66
|
+
if (
|
|
67
|
+
kind === "plan" &&
|
|
68
|
+
!Object.keys(e).every((k) => ["id", "object", "deleted"].includes(k))
|
|
69
|
+
) {
|
|
70
|
+
need(
|
|
71
|
+
planMatchesPrice(e, known, context),
|
|
72
|
+
"unproven_full_plan_price_mapping",
|
|
73
|
+
"unknown",
|
|
74
|
+
);
|
|
75
|
+
return id;
|
|
76
|
+
}
|
|
77
|
+
if (e.product !== undefined)
|
|
78
|
+
need(
|
|
79
|
+
productId(e.product, context) === productId(known.product, context),
|
|
80
|
+
"phase_product_mismatch",
|
|
81
|
+
);
|
|
82
|
+
if (e.currency !== undefined)
|
|
83
|
+
need(e.currency === known.currency, "phase_price_currency_mismatch");
|
|
84
|
+
if (
|
|
85
|
+
kind === "price" &&
|
|
86
|
+
!Object.keys(e).every((k) => ["id", "object", "deleted"].includes(k))
|
|
87
|
+
)
|
|
88
|
+
need(equal(e, known), "expanded_price_read_drift");
|
|
89
|
+
}
|
|
90
|
+
return id;
|
|
91
|
+
};
|
|
92
|
+
const normalizePhase = (v: O, known: O) => {
|
|
93
|
+
const phase = settings(v);
|
|
94
|
+
phase.discounts = phaseDiscounts(phase.discounts);
|
|
95
|
+
const ps = array(phase.items);
|
|
96
|
+
need(ps.length === 1, "ambiguous_phase_items", "unknown");
|
|
97
|
+
const item = clone(object(ps[0]));
|
|
98
|
+
item.price = priceRef(item.price, known);
|
|
99
|
+
if (item.plan !== undefined) item.plan = priceRef(item.plan, known, "plan");
|
|
100
|
+
item.discounts = phaseDiscounts(item.discounts);
|
|
101
|
+
phase.items = [item];
|
|
102
|
+
return phase;
|
|
103
|
+
};
|
|
104
|
+
// END original 670-758
|
|
105
|
+
return { ...state, phaseDiscounts, priceRef, normalizePhase };
|
|
106
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { renewalSnapshot } from "../renewalReplacementSnapshot.js";
|
|
2
|
+
import {
|
|
3
|
+
equal,
|
|
4
|
+
productId,
|
|
5
|
+
type ReferenceContext,
|
|
6
|
+
} from "../renewalAggregate/shared.js";
|
|
7
|
+
type O = Record<string, unknown>;
|
|
8
|
+
const record = (x: unknown): x is O =>
|
|
9
|
+
!!x && typeof x === "object" && !Array.isArray(x);
|
|
10
|
+
const owns = (x: O, k: string) => Object.prototype.hasOwnProperty.call(x, k);
|
|
11
|
+
const minimalProduct = (x: O) =>
|
|
12
|
+
Object.keys(x).every((k) =>
|
|
13
|
+
["id", "object", "deleted", "livemode", "customer_account"].includes(k),
|
|
14
|
+
) &&
|
|
15
|
+
(x.deleted === undefined || x.deleted === false);
|
|
16
|
+
const PLAN_KEYS = [
|
|
17
|
+
"id",
|
|
18
|
+
"object",
|
|
19
|
+
"active",
|
|
20
|
+
"amount",
|
|
21
|
+
"amount_decimal",
|
|
22
|
+
"billing_scheme",
|
|
23
|
+
"created",
|
|
24
|
+
"currency",
|
|
25
|
+
"interval",
|
|
26
|
+
"interval_count",
|
|
27
|
+
"livemode",
|
|
28
|
+
"metadata",
|
|
29
|
+
"meter",
|
|
30
|
+
"nickname",
|
|
31
|
+
"product",
|
|
32
|
+
"tiers_mode",
|
|
33
|
+
"transform_usage",
|
|
34
|
+
"trial_period_days",
|
|
35
|
+
"usage_type",
|
|
36
|
+
];
|
|
37
|
+
const PRICE_KEYS = [
|
|
38
|
+
"id",
|
|
39
|
+
"object",
|
|
40
|
+
"active",
|
|
41
|
+
"unit_amount",
|
|
42
|
+
"unit_amount_decimal",
|
|
43
|
+
"billing_scheme",
|
|
44
|
+
"created",
|
|
45
|
+
"currency",
|
|
46
|
+
"livemode",
|
|
47
|
+
"metadata",
|
|
48
|
+
"nickname",
|
|
49
|
+
"product",
|
|
50
|
+
"tiers_mode",
|
|
51
|
+
"transform_quantity",
|
|
52
|
+
"recurring",
|
|
53
|
+
];
|
|
54
|
+
const REC_KEYS = [
|
|
55
|
+
"interval",
|
|
56
|
+
"interval_count",
|
|
57
|
+
"usage_type",
|
|
58
|
+
"meter",
|
|
59
|
+
"trial_period_days",
|
|
60
|
+
];
|
|
61
|
+
/** Finite validation-view template only. No provider Plan or financial calculation is invented. */
|
|
62
|
+
export function planTemplateForPrice(
|
|
63
|
+
value: unknown,
|
|
64
|
+
context: ReferenceContext,
|
|
65
|
+
): O | null {
|
|
66
|
+
try {
|
|
67
|
+
if (
|
|
68
|
+
!record(value) ||
|
|
69
|
+
!PRICE_KEYS.every((k) => owns(value, k)) ||
|
|
70
|
+
!record(value.recurring) ||
|
|
71
|
+
!REC_KEYS.every((k) => owns(value.recurring as O, k))
|
|
72
|
+
)
|
|
73
|
+
return null;
|
|
74
|
+
const p = value,
|
|
75
|
+
r = value.recurring;
|
|
76
|
+
if (
|
|
77
|
+
p.object !== "price" ||
|
|
78
|
+
typeof p.id !== "string" ||
|
|
79
|
+
!/^price_[A-Za-z0-9_]+$/.test(p.id) ||
|
|
80
|
+
typeof p.active !== "boolean" ||
|
|
81
|
+
p.livemode !== context.livemode ||
|
|
82
|
+
p.billing_scheme !== "per_unit" ||
|
|
83
|
+
!["day", "week", "month", "year"].includes(String(r.interval)) ||
|
|
84
|
+
!Number.isSafeInteger(r.interval_count) ||
|
|
85
|
+
Number(r.interval_count) <= 0 ||
|
|
86
|
+
r.usage_type !== "licensed" ||
|
|
87
|
+
r.meter !== null ||
|
|
88
|
+
r.trial_period_days !== null ||
|
|
89
|
+
p.tiers_mode !== null ||
|
|
90
|
+
p.transform_quantity !== null ||
|
|
91
|
+
owns(p, "tiers")
|
|
92
|
+
)
|
|
93
|
+
return null;
|
|
94
|
+
if (
|
|
95
|
+
!Number.isSafeInteger(p.created) ||
|
|
96
|
+
Number(p.created) < 0 ||
|
|
97
|
+
typeof p.currency !== "string" ||
|
|
98
|
+
!/^[a-z]{3}$/.test(p.currency) ||
|
|
99
|
+
(p.nickname !== null && typeof p.nickname !== "string") ||
|
|
100
|
+
!record(p.metadata) ||
|
|
101
|
+
!Object.values(p.metadata).every((x) => typeof x === "string")
|
|
102
|
+
)
|
|
103
|
+
return null;
|
|
104
|
+
if (
|
|
105
|
+
p.unit_amount !== null &&
|
|
106
|
+
(!Number.isSafeInteger(p.unit_amount) || Number(p.unit_amount) < 0)
|
|
107
|
+
)
|
|
108
|
+
return null;
|
|
109
|
+
if (
|
|
110
|
+
p.unit_amount_decimal !== null &&
|
|
111
|
+
(typeof p.unit_amount_decimal !== "string" ||
|
|
112
|
+
!/^\d+(?:\.\d{1,12})?$/.test(p.unit_amount_decimal))
|
|
113
|
+
)
|
|
114
|
+
return null;
|
|
115
|
+
if (p.unit_amount === null && p.unit_amount_decimal === null) return null;
|
|
116
|
+
productId(p.product, context);
|
|
117
|
+
return renewalSnapshot({
|
|
118
|
+
id: p.id,
|
|
119
|
+
object: "plan",
|
|
120
|
+
active: p.active,
|
|
121
|
+
amount: p.unit_amount,
|
|
122
|
+
amount_decimal: p.unit_amount_decimal,
|
|
123
|
+
billing_scheme: p.billing_scheme,
|
|
124
|
+
created: p.created,
|
|
125
|
+
currency: p.currency,
|
|
126
|
+
interval: r.interval,
|
|
127
|
+
interval_count: r.interval_count,
|
|
128
|
+
livemode: p.livemode,
|
|
129
|
+
metadata: p.metadata,
|
|
130
|
+
meter: r.meter,
|
|
131
|
+
nickname: p.nickname,
|
|
132
|
+
product: p.product,
|
|
133
|
+
tiers_mode: p.tiers_mode,
|
|
134
|
+
transform_usage: p.transform_quantity,
|
|
135
|
+
trial_period_days: r.trial_period_days,
|
|
136
|
+
usage_type: r.usage_type,
|
|
137
|
+
});
|
|
138
|
+
} catch {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/** Every native Plan field is required and mapped exactly; unknown/unmapped facts reject. */
|
|
143
|
+
export function planMatchesPrice(
|
|
144
|
+
value: unknown,
|
|
145
|
+
price: unknown,
|
|
146
|
+
context: ReferenceContext,
|
|
147
|
+
): boolean {
|
|
148
|
+
try {
|
|
149
|
+
const expected = planTemplateForPrice(price, context);
|
|
150
|
+
if (
|
|
151
|
+
!expected ||
|
|
152
|
+
!record(value) ||
|
|
153
|
+
Object.keys(value).length !== PLAN_KEYS.length ||
|
|
154
|
+
!PLAN_KEYS.every((k) => owns(value, k))
|
|
155
|
+
)
|
|
156
|
+
return false;
|
|
157
|
+
if (value.object !== "plan") return false;
|
|
158
|
+
const actual = renewalSnapshot(value),
|
|
159
|
+
template = renewalSnapshot(expected);
|
|
160
|
+
const id = productId(actual.product, context),
|
|
161
|
+
wanted = productId(template.product, context);
|
|
162
|
+
if (id !== wanted) return false;
|
|
163
|
+
if (record(actual.product) && !minimalProduct(actual.product)) {
|
|
164
|
+
if (
|
|
165
|
+
!record(template.product) ||
|
|
166
|
+
minimalProduct(template.product) ||
|
|
167
|
+
!equal(actual.product, template.product)
|
|
168
|
+
)
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
actual.product = id;
|
|
172
|
+
template.product = wanted;
|
|
173
|
+
return equal(actual, template);
|
|
174
|
+
} catch {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
}
|