@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.
Files changed (126) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAggregate/readMaterial.d.ts.map +1 -1
  4. package/dist/client/billing/renewalAggregate/readMaterial.js +4 -3
  5. package/dist/client/billing/renewalAggregate/readMaterial.js.map +1 -1
  6. package/dist/client/billing/renewalObservation/active-scheduled.d.ts +6 -0
  7. package/dist/client/billing/renewalObservation/active-scheduled.d.ts.map +1 -0
  8. package/dist/client/billing/renewalObservation/active-scheduled.js +37 -0
  9. package/dist/client/billing/renewalObservation/active-scheduled.js.map +1 -0
  10. package/dist/client/billing/renewalObservation/common.d.ts +47 -0
  11. package/dist/client/billing/renewalObservation/common.d.ts.map +1 -0
  12. package/dist/client/billing/renewalObservation/common.js +39 -0
  13. package/dist/client/billing/renewalObservation/common.js.map +1 -0
  14. package/dist/client/billing/renewalObservation/configured-baseline.d.ts +76 -0
  15. package/dist/client/billing/renewalObservation/configured-baseline.d.ts.map +1 -0
  16. package/dist/client/billing/renewalObservation/configured-baseline.js +26 -0
  17. package/dist/client/billing/renewalObservation/configured-baseline.js.map +1 -0
  18. package/dist/client/billing/renewalObservation/coupon-discount-joins.d.ts +53 -0
  19. package/dist/client/billing/renewalObservation/coupon-discount-joins.d.ts.map +1 -0
  20. package/dist/client/billing/renewalObservation/coupon-discount-joins.js +103 -0
  21. package/dist/client/billing/renewalObservation/coupon-discount-joins.js.map +1 -0
  22. package/dist/client/billing/renewalObservation/invoice-tax-identity.d.ts +52 -0
  23. package/dist/client/billing/renewalObservation/invoice-tax-identity.d.ts.map +1 -0
  24. package/dist/client/billing/renewalObservation/invoice-tax-identity.js +49 -0
  25. package/dist/client/billing/renewalObservation/invoice-tax-identity.js.map +1 -0
  26. package/dist/client/billing/renewalObservation/pending-and-stage.d.ts +76 -0
  27. package/dist/client/billing/renewalObservation/pending-and-stage.d.ts.map +1 -0
  28. package/dist/client/billing/renewalObservation/pending-and-stage.js +36 -0
  29. package/dist/client/billing/renewalObservation/pending-and-stage.js.map +1 -0
  30. package/dist/client/billing/renewalObservation/phase-price-normalization.d.ts +57 -0
  31. package/dist/client/billing/renewalObservation/phase-price-normalization.d.ts.map +1 -0
  32. package/dist/client/billing/renewalObservation/phase-price-normalization.js +70 -0
  33. package/dist/client/billing/renewalObservation/phase-price-normalization.js.map +1 -0
  34. package/dist/client/billing/renewalObservation/planProjection.d.ts +8 -0
  35. package/dist/client/billing/renewalObservation/planProjection.d.ts.map +1 -0
  36. package/dist/client/billing/renewalObservation/planProjection.js +150 -0
  37. package/dist/client/billing/renewalObservation/planProjection.js.map +1 -0
  38. package/dist/client/billing/renewalObservation/reference-catalog.d.ts +37 -0
  39. package/dist/client/billing/renewalObservation/reference-catalog.d.ts.map +1 -0
  40. package/dist/client/billing/renewalObservation/reference-catalog.js +109 -0
  41. package/dist/client/billing/renewalObservation/reference-catalog.js.map +1 -0
  42. package/dist/client/billing/renewalObservation/schedule-templates.d.ts +74 -0
  43. package/dist/client/billing/renewalObservation/schedule-templates.d.ts.map +1 -0
  44. package/dist/client/billing/renewalObservation/schedule-templates.js +72 -0
  45. package/dist/client/billing/renewalObservation/schedule-templates.js.map +1 -0
  46. package/dist/client/billing/renewalObservation/scheduled-context.d.ts +44 -0
  47. package/dist/client/billing/renewalObservation/scheduled-context.d.ts.map +1 -0
  48. package/dist/client/billing/renewalObservation/scheduled-context.js +64 -0
  49. package/dist/client/billing/renewalObservation/scheduled-context.js.map +1 -0
  50. package/dist/client/billing/renewalObservation/scheduled-identity.d.ts +53 -0
  51. package/dist/client/billing/renewalObservation/scheduled-identity.d.ts.map +1 -0
  52. package/dist/client/billing/renewalObservation/scheduled-identity.js +95 -0
  53. package/dist/client/billing/renewalObservation/scheduled-identity.js.map +1 -0
  54. package/dist/client/billing/renewalObservation/scheduled-normalization.d.ts +59 -0
  55. package/dist/client/billing/renewalObservation/scheduled-normalization.d.ts.map +1 -0
  56. package/dist/client/billing/renewalObservation/scheduled-normalization.js +29 -0
  57. package/dist/client/billing/renewalObservation/scheduled-normalization.js.map +1 -0
  58. package/dist/client/billing/renewalObservation/settings-normalization.d.ts +54 -0
  59. package/dist/client/billing/renewalObservation/settings-normalization.d.ts.map +1 -0
  60. package/dist/client/billing/renewalObservation/settings-normalization.js +53 -0
  61. package/dist/client/billing/renewalObservation/settings-normalization.js.map +1 -0
  62. package/dist/client/billing/renewalObservation/stage-runner.d.ts +4 -0
  63. package/dist/client/billing/renewalObservation/stage-runner.d.ts.map +1 -0
  64. package/dist/client/billing/renewalObservation/stage-runner.js +34 -0
  65. package/dist/client/billing/renewalObservation/stage-runner.js.map +1 -0
  66. package/dist/client/billing/renewalObservation/weeklyPending.d.ts +13 -0
  67. package/dist/client/billing/renewalObservation/weeklyPending.d.ts.map +1 -0
  68. package/dist/client/billing/renewalObservation/weeklyPending.js +55 -0
  69. package/dist/client/billing/renewalObservation/weeklyPending.js.map +1 -0
  70. package/dist/client/billing/renewalPriceReplacement.d.ts.map +1 -1
  71. package/dist/client/billing/renewalPriceReplacement.js +7 -3
  72. package/dist/client/billing/renewalPriceReplacement.js.map +1 -1
  73. package/dist/client/billing/renewalV2/index.d.ts +383 -0
  74. package/dist/client/billing/renewalV2/index.d.ts.map +1 -1
  75. package/dist/client/billing/renewalV2/index.js +8 -3
  76. package/dist/client/billing/renewalV2/index.js.map +1 -1
  77. package/dist/client/billing/renewalV2/observation.d.ts +19 -0
  78. package/dist/client/billing/renewalV2/observation.d.ts.map +1 -0
  79. package/dist/client/billing/renewalV2/observation.js +95 -0
  80. package/dist/client/billing/renewalV2/observation.js.map +1 -0
  81. package/dist/client/billing/renewalV2/observationInput.d.ts +77 -0
  82. package/dist/client/billing/renewalV2/observationInput.d.ts.map +1 -0
  83. package/dist/client/billing/renewalV2/observationInput.js +140 -0
  84. package/dist/client/billing/renewalV2/observationInput.js.map +1 -0
  85. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
  86. package/dist/client/connect/payoutSyncTestHelpers.d.ts +16 -16
  87. package/dist/client/renewal.d.ts +2 -0
  88. package/dist/client/renewal.d.ts.map +1 -1
  89. package/dist/client/renewal.js +1 -0
  90. package/dist/client/renewal.js.map +1 -1
  91. package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
  92. package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
  93. package/dist/component/balanceFunding/workflowValidators.d.ts +16 -16
  94. package/dist/component/earnings/triggers.d.ts +16 -16
  95. package/dist/component/usage/allowance.fixtures.d.ts +16 -16
  96. package/docs/client-api.md +26 -0
  97. package/docs/renewal-context-reads.md +10 -0
  98. package/docs/renewal-material-observation.md +124 -0
  99. package/docs/renewal-phase-assembly.md +7 -0
  100. package/docs/renewal-price-replacements.md +3 -0
  101. package/package.json +1 -1
  102. package/src/client/billing/renewalAggregate/readMaterial.ts +5 -3
  103. package/src/client/billing/renewalObservation/active-scheduled.ts +67 -0
  104. package/src/client/billing/renewalObservation/common.ts +83 -0
  105. package/src/client/billing/renewalObservation/configured-baseline.ts +55 -0
  106. package/src/client/billing/renewalObservation/coupon-discount-joins.ts +163 -0
  107. package/src/client/billing/renewalObservation/invoice-tax-identity.ts +65 -0
  108. package/src/client/billing/renewalObservation/pending-and-stage.ts +89 -0
  109. package/src/client/billing/renewalObservation/phase-price-normalization.ts +106 -0
  110. package/src/client/billing/renewalObservation/planProjection.ts +177 -0
  111. package/src/client/billing/renewalObservation/reference-catalog.ts +150 -0
  112. package/src/client/billing/renewalObservation/schedule-templates.ts +118 -0
  113. package/src/client/billing/renewalObservation/scheduled-context.ts +98 -0
  114. package/src/client/billing/renewalObservation/scheduled-identity.ts +175 -0
  115. package/src/client/billing/renewalObservation/scheduled-normalization.ts +49 -0
  116. package/src/client/billing/renewalObservation/settings-normalization.ts +60 -0
  117. package/src/client/billing/renewalObservation/stage-runner.ts +30 -0
  118. package/src/client/billing/renewalObservation/weeklyPending.ts +81 -0
  119. package/src/client/billing/renewalPriceReplacement.ts +7 -3
  120. package/src/client/billing/renewalV2/index.ts +14 -6
  121. package/src/client/billing/renewalV2/observation.boundary-review.test.ts +49 -0
  122. package/src/client/billing/renewalV2/observation.test.ts +334 -0
  123. package/src/client/billing/renewalV2/observation.ts +149 -0
  124. package/src/client/billing/renewalV2/observationInput.ts +167 -0
  125. package/src/client/billing/renewalV2/rawRetention.test.ts +293 -0
  126. package/src/client/renewal.ts +7 -0
@@ -0,0 +1,150 @@
1
+ import {
2
+ object,
3
+ array,
4
+ need,
5
+ get,
6
+ equal,
7
+ referenceId,
8
+ type O,
9
+ } from "./common.js";
10
+ import type { createContext } from "./scheduled-context.js";
11
+ export function references(state: ReturnType<typeof createContext>) {
12
+ const { originalSub, originalSchedule, schedule, sub, customer, context } =
13
+ state;
14
+ // BEGIN original 145-279
15
+ const retainedSettings = [
16
+ originalSub,
17
+ get(originalSchedule, "default_settings"),
18
+ object(array(originalSchedule.phases)[0]),
19
+ ];
20
+ const fullReferences = new Map<string, O[]>();
21
+ const minimal = (v: O) =>
22
+ Object.keys(v).every((k) =>
23
+ ["id", "object", "deleted", "livemode", "customer_account"].includes(k),
24
+ );
25
+ const retain = (v: unknown, kind: string) => {
26
+ if (v === null || v === undefined || typeof v === "string") return;
27
+ const row = object(v);
28
+ if (row.object === kind && !minimal(row)) {
29
+ const key = kind + ":" + String(row.id);
30
+ fullReferences.set(key, [...(fullReferences.get(key) ?? []), row]);
31
+ }
32
+ };
33
+ for (const setting of retainedSettings) {
34
+ retain(setting.default_payment_method, "payment_method");
35
+ retain(setting.on_behalf_of, "account");
36
+ if (setting.transfer_data)
37
+ retain(get(setting, "transfer_data").destination, "account");
38
+ if (setting.automatic_tax && get(setting, "automatic_tax").liability)
39
+ retain(
40
+ get(get(setting, "automatic_tax"), "liability").account,
41
+ "account",
42
+ );
43
+ if (setting.invoice_settings) {
44
+ const invoice = get(setting, "invoice_settings");
45
+ if (invoice.issuer) retain(get(invoice, "issuer").account, "account");
46
+ if (invoice.account_tax_ids)
47
+ for (const tax of array(invoice.account_tax_ids)) {
48
+ retain(tax, "tax_id");
49
+ if (typeof tax !== "string" && object(tax).owner)
50
+ retain(get(object(tax), "owner").account, "account");
51
+ }
52
+ }
53
+ }
54
+ // Fixed comparison roles: actual fresh data in live mode, immutable
55
+ // historical material in baseline mode. Neither invents acquisition.
56
+ // An arbitrary opaque field cannot act as a counterpart for a retained ref.
57
+ const comparisonFullReferences = new Map<string, O[]>();
58
+ const retainComparison = (v: unknown, kind: string) => {
59
+ if (v === null || v === undefined || typeof v === "string") return;
60
+ const row = object(v);
61
+ if (row.object === kind && !minimal(row)) {
62
+ const key = kind + ":" + String(row.id);
63
+ comparisonFullReferences.set(key, [
64
+ ...(comparisonFullReferences.get(key) ?? []),
65
+ row,
66
+ ]);
67
+ }
68
+ };
69
+ for (const setting of [
70
+ sub,
71
+ get(schedule, "default_settings"),
72
+ ...array(schedule.phases).map(object),
73
+ ]) {
74
+ retainComparison(setting.default_payment_method, "payment_method");
75
+ retainComparison(setting.on_behalf_of, "account");
76
+ if (setting.transfer_data)
77
+ retainComparison(get(setting, "transfer_data").destination, "account");
78
+ if (setting.automatic_tax && get(setting, "automatic_tax").liability)
79
+ retainComparison(
80
+ get(get(setting, "automatic_tax"), "liability").account,
81
+ "account",
82
+ );
83
+ if (setting.invoice_settings) {
84
+ const invoice = get(setting, "invoice_settings");
85
+ if (invoice.issuer)
86
+ retainComparison(get(invoice, "issuer").account, "account");
87
+ if (invoice.account_tax_ids)
88
+ for (const tax of array(invoice.account_tax_ids)) {
89
+ retainComparison(tax, "tax_id");
90
+ if (typeof tax !== "string" && object(tax).owner)
91
+ retainComparison(get(object(tax), "owner").account, "account");
92
+ }
93
+ }
94
+ }
95
+ const ref = (v: unknown, prefix: string, kind: string): string => {
96
+ const id = referenceId(
97
+ v,
98
+ prefix,
99
+ kind,
100
+ kind === "tax_id" ? undefined : context,
101
+ );
102
+ const retained = fullReferences.get(kind + ":" + id) ?? [];
103
+ if (retained.length) {
104
+ const fresh = comparisonFullReferences.get(kind + ":" + id) ?? [];
105
+ need(
106
+ fresh.length > 0 &&
107
+ retained.every((old) => fresh.some((current) => equal(old, current))),
108
+ "missing_fresh_full_" + kind + "_evidence",
109
+ "unknown",
110
+ );
111
+ }
112
+ if (typeof v !== "string") {
113
+ const row = object(v);
114
+ need(
115
+ row.deleted === undefined || row.deleted === false,
116
+ "unproven_deleted_reference",
117
+ "unknown",
118
+ );
119
+ if (
120
+ !minimal(row) &&
121
+ ["customer", "payment_method", "account", "tax_id"].includes(kind)
122
+ ) {
123
+ const correlated =
124
+ kind === "customer"
125
+ ? [customer]
126
+ : (fullReferences.get(kind + ":" + id) ?? []);
127
+ need(
128
+ correlated.length > 0 &&
129
+ correlated.every((known) => equal(row, known)),
130
+ "uncorrelated_full_" + kind + "_reference",
131
+ "unknown",
132
+ );
133
+ }
134
+ if (
135
+ kind === "payment_method" &&
136
+ row.customer !== null &&
137
+ row.customer !== undefined
138
+ )
139
+ need(
140
+ ref(row.customer, "cus", "customer") ===
141
+ referenceId(originalSub.customer, "cus", "customer", context),
142
+ "expanded_payment_method_customer_mismatch",
143
+ "unknown",
144
+ );
145
+ }
146
+ return id;
147
+ };
148
+ // END original 145-279
149
+ return { ...state, ref };
150
+ }
@@ -0,0 +1,118 @@
1
+ import {
2
+ object,
3
+ array,
4
+ need,
5
+ clone,
6
+ get,
7
+ equal,
8
+ timestamp,
9
+ type ObserverCandidateResult,
10
+ } from "./common.js";
11
+ import type { normalize } from "./scheduled-normalization.js";
12
+ export function templates(state: ReturnType<typeof normalize>) {
13
+ const {
14
+ rawReads,
15
+ base,
16
+ expected,
17
+ receipt,
18
+ originalSub,
19
+ originalItem,
20
+ originalSchedule,
21
+ schedule,
22
+ sub,
23
+ target,
24
+ desired,
25
+ current,
26
+ phases,
27
+ originalPhase,
28
+ currentOriginal,
29
+ normalizePhase,
30
+ normalizeSchedule,
31
+ normalizeSub,
32
+ } = state;
33
+ // BEGIN original 788-850
34
+ const sourceWithAssociation = clone(originalSub);
35
+ sourceWithAssociation.schedule = receipt.scheduleId;
36
+ const attachedTemplate = normalizeSchedule(originalSchedule);
37
+ attachedTemplate.phases = [
38
+ normalizePhase(originalPhase, get(originalItem, "price")),
39
+ ];
40
+ const candidateSchedule = normalizeSchedule(schedule);
41
+ if (phases.length === 1 && currentOriginal) {
42
+ candidateSchedule.phases = [
43
+ normalizePhase(object(phases[0]), get(originalItem, "price")),
44
+ ];
45
+ need(
46
+ equal(candidateSchedule, attachedTemplate) &&
47
+ equal(
48
+ normalizeSub(sub, "original", get(originalItem, "price")),
49
+ normalizeSub(
50
+ sourceWithAssociation,
51
+ "original",
52
+ get(originalItem, "price"),
53
+ ),
54
+ ),
55
+ "unconfigured_material_drift",
56
+ );
57
+ return {
58
+ kind: "result" as const,
59
+ result: {
60
+ ...base,
61
+ predicate: "attached_unconfigured_match",
62
+ rawReads,
63
+ } as ObserverCandidateResult,
64
+ };
65
+ }
66
+ need(phases.length === 2, "raw_phase_history_unavailable", "unknown");
67
+ const targetPhase = object(phases[1]);
68
+ need(
69
+ targetPhase.start_date === expected.periodEnd &&
70
+ typeof targetPhase.end_date === "number" &&
71
+ targetPhase.end_date > Number(expected.periodEnd),
72
+ "target_phase_boundary_mismatch",
73
+ );
74
+ const targetTemplate = clone(originalPhase);
75
+ targetTemplate.start_date = expected.periodEnd;
76
+ targetTemplate.end_date = targetPhase.end_date;
77
+ targetTemplate.proration_behavior = "none";
78
+ targetTemplate.billing_cycle_anchor = object(
79
+ array(get(desired, "payload").phases)[1],
80
+ ).billing_cycle_anchor;
81
+ const targetTemplateItem = object(array(targetTemplate.items)[0]);
82
+ targetTemplateItem.price = expected.targetPriceId;
83
+ if (targetTemplateItem.plan !== undefined)
84
+ targetTemplateItem.plan = expected.targetPriceId;
85
+ targetTemplate.items = [targetTemplateItem];
86
+ const completeTemplate = clone(attachedTemplate);
87
+ completeTemplate.phases = [
88
+ normalizePhase(originalPhase, get(originalItem, "price")),
89
+ normalizePhase(targetTemplate, target),
90
+ ];
91
+ completeTemplate.current_phase = clone(current);
92
+ candidateSchedule.phases = [
93
+ normalizePhase(object(phases[0]), get(originalItem, "price")),
94
+ normalizePhase(targetPhase, target),
95
+ ];
96
+ need(
97
+ equal(candidateSchedule, completeTemplate),
98
+ "full_phase_or_default_read_drift",
99
+ );
100
+ // Separate comparison view. This is NOT a provider read or rewritten ACK.
101
+ const comparableSchedule = clone(candidateSchedule);
102
+ comparableSchedule.current_phase = "NAMED_STAGE_PROGRESS_ONLY";
103
+ timestamp.parse(targetPhase.end_date);
104
+ // END original 788-850
105
+ return {
106
+ kind: "continue" as const,
107
+ state: {
108
+ ...state,
109
+ sourceWithAssociation,
110
+ attachedTemplate,
111
+ candidateSchedule,
112
+ targetPhase,
113
+ targetTemplate,
114
+ completeTemplate,
115
+ comparableSchedule,
116
+ },
117
+ };
118
+ }
@@ -0,0 +1,98 @@
1
+ import { object, array, need, get, type O } from "./common.js";
2
+ import type { Purpose, Evidence } from "./common.js";
3
+ type Owned = { args: O; prepared: unknown; receipt: unknown; request: unknown };
4
+ /** Fixed internal V2 entry, called only after the public raw reconstruction succeeds. */
5
+ export function createContext(
6
+ owned: Owned,
7
+ material: unknown,
8
+ purpose: Purpose,
9
+ evidence?: Evidence,
10
+ ) {
11
+ const base = {
12
+ candidateOnly: true,
13
+ providerQualification: "required",
14
+ configureAcknowledged: false,
15
+ configureDispatchOutcome: "unknown",
16
+ providerCallsRequested: false,
17
+ } as const;
18
+ const projection: O = { ...owned.args, prepared: owned.prepared };
19
+ const request = object(owned.request),
20
+ rawReads = material;
21
+ const input: O = {
22
+ retainedProjectionInput: projection,
23
+ desiredRequest: request,
24
+ reads: material,
25
+ };
26
+ const desired = object(request),
27
+ prepared = get(projection, "prepared"),
28
+ expected = get(prepared, "expected"),
29
+ receipt = object(owned.receipt);
30
+ const original = get(prepared, "providerSnapshot"),
31
+ originalSub = get(original, "subscription"),
32
+ originalItem = object(array(get(originalSub, "items").data)[0]);
33
+ const originalSchedule = get(receipt, "providerSnapshot"),
34
+ expectedPrice = get(original, "targetPrice");
35
+ need(
36
+ Object.prototype.hasOwnProperty.call(originalSub, "latest_invoice") &&
37
+ Object.prototype.hasOwnProperty.call(
38
+ object(object(material).subscription),
39
+ "latest_invoice",
40
+ ),
41
+ "missing_latest_invoice",
42
+ "unknown",
43
+ );
44
+ need(
45
+ Object.keys(object(material)).every((k) =>
46
+ [
47
+ "schedule",
48
+ "subscription",
49
+ "targetPrice",
50
+ "billingCustomer",
51
+ "coupons",
52
+ ].includes(k),
53
+ ),
54
+ "unsupported_read_bundle",
55
+ "unknown",
56
+ );
57
+ const reads = object(material),
58
+ schedule = get(reads, "schedule"),
59
+ sub = get(reads, "subscription"),
60
+ target = get(reads, "targetPrice"),
61
+ customer = get(reads, "billingCustomer");
62
+ // New public bare-wire boundary: do not let the inherited terminal matcher coerce values.
63
+ need(
64
+ typeof schedule.status === "string",
65
+ "invalid_schedule_status",
66
+ "unknown",
67
+ );
68
+ const context = {
69
+ customerAccountId: String(expected.customerAccountId),
70
+ livemode: expected.livemode as boolean,
71
+ };
72
+ // Only immutable retained templates or the separately correlated Customer GET
73
+ // can justify discarding expansion structure in a comparison view.
74
+ return {
75
+ input,
76
+ rawReads,
77
+ base,
78
+ purpose,
79
+ evidence,
80
+ projection,
81
+ request,
82
+ prepared,
83
+ expected,
84
+ receipt,
85
+ original,
86
+ originalSub,
87
+ originalItem,
88
+ originalSchedule,
89
+ expectedPrice,
90
+ reads,
91
+ schedule,
92
+ sub,
93
+ target,
94
+ customer,
95
+ context,
96
+ desired,
97
+ };
98
+ }
@@ -0,0 +1,175 @@
1
+ import {
2
+ Stop,
3
+ object,
4
+ array,
5
+ need,
6
+ get,
7
+ equal,
8
+ timestamp,
9
+ planMatchesPrice,
10
+ type ObserverCandidateResult,
11
+ } from "./common.js";
12
+ import type { references } from "./reference-catalog.js";
13
+ export function identities(state: ReturnType<typeof references>) {
14
+ const {
15
+ input,
16
+ rawReads,
17
+ base,
18
+ projection,
19
+ expected,
20
+ receipt,
21
+ originalSub,
22
+ originalItem,
23
+ originalSchedule,
24
+ expectedPrice,
25
+ schedule,
26
+ sub,
27
+ target,
28
+ customer,
29
+ context,
30
+ ref,
31
+ } = state;
32
+ // BEGIN original 280-415
33
+ need(
34
+ planMatchesPrice(originalItem.plan, originalItem.price, context),
35
+ "unproven_original_plan_price_mapping",
36
+ "unknown",
37
+ );
38
+ const customerId = ref(originalSub.customer, "cus", "customer");
39
+ need(
40
+ schedule.id === receipt.scheduleId &&
41
+ schedule.object === "subscription_schedule" &&
42
+ schedule.customer_account === expected.customerAccountId &&
43
+ schedule.livemode === expected.livemode &&
44
+ ref(schedule.customer, "cus", "customer") === customerId,
45
+ "owned_schedule_identity_mismatch",
46
+ );
47
+ need(
48
+ sub.id === expected.subscriptionId &&
49
+ sub.object === "subscription" &&
50
+ sub.customer_account === expected.customerAccountId &&
51
+ sub.livemode === expected.livemode &&
52
+ ref(sub.customer, "cus", "customer") === customerId,
53
+ "original_subscription_identity_mismatch",
54
+ );
55
+ need(
56
+ customer.id === customerId &&
57
+ customer.object === "customer" &&
58
+ customer.customer_account === expected.customerAccountId &&
59
+ customer.livemode === expected.livemode,
60
+ "billing_customer_identity_mismatch",
61
+ );
62
+ for (const value of [schedule.customer, sub.customer])
63
+ if (typeof value !== "string") {
64
+ const expanded = object(value);
65
+ if (
66
+ !Object.keys(expanded).every((k) =>
67
+ ["id", "object", "deleted", "livemode", "customer_account"].includes(
68
+ k,
69
+ ),
70
+ )
71
+ )
72
+ need(equal(expanded, customer), "expanded_customer_read_drift");
73
+ }
74
+ for (const [expanded, correlated, kind] of [
75
+ [sub.schedule, schedule, "subscription_schedule"],
76
+ [schedule.subscription, sub, "subscription"],
77
+ ] as const)
78
+ if (expanded !== null && typeof expanded !== "string") {
79
+ const full = object(expanded);
80
+ if (
81
+ !Object.keys(full).every((k) =>
82
+ ["id", "object", "deleted", "livemode", "customer_account"].includes(
83
+ k,
84
+ ),
85
+ )
86
+ )
87
+ need(equal(full, correlated), "expanded_" + kind + "_read_drift");
88
+ }
89
+ if (input.configureAcknowledgement !== undefined)
90
+ throw new Stop("configure_ack_contract_unavailable", "unknown");
91
+ if (
92
+ ["canceled", "released", "completed", "not_started"].includes(
93
+ String(schedule.status),
94
+ )
95
+ ) {
96
+ if (schedule.subscription !== null)
97
+ need(
98
+ ref(schedule.subscription, "sub", "subscription") ===
99
+ expected.subscriptionId,
100
+ "terminal_original_relation_mismatch",
101
+ );
102
+ if (schedule.released_subscription !== null)
103
+ need(
104
+ schedule.released_subscription === expected.subscriptionId,
105
+ "terminal_released_relation_mismatch",
106
+ );
107
+ return {
108
+ kind: "result" as const,
109
+ result: {
110
+ ...base,
111
+ predicate: "terminal_facts_only",
112
+ providerStatus: schedule.status,
113
+ effectiveness: "unknown",
114
+ rawReads,
115
+ } as ObserverCandidateResult,
116
+ };
117
+ }
118
+ need(schedule.status === "active", "unsupported_schedule_status", "unknown");
119
+ need(
120
+ ref(schedule.subscription, "sub", "subscription") ===
121
+ expected.subscriptionId &&
122
+ ref(sub.schedule, "sub_sched", "subscription_schedule") ===
123
+ receipt.scheduleId,
124
+ "active_owned_association_mismatch",
125
+ );
126
+ need(equal(target, expectedPrice), "target_full_read_drift");
127
+ need(
128
+ equal(customer, projection.billingCustomer),
129
+ "billing_customer_full_read_drift",
130
+ );
131
+ const items = get(sub, "items");
132
+ need(
133
+ items.has_more === false && array(items.data).length === 1,
134
+ "incomplete_or_ambiguous_current_items",
135
+ "unknown",
136
+ );
137
+ const actualItem = object(array(items.data)[0]);
138
+ need(
139
+ typeof actualItem.id === "string" &&
140
+ /^si_[A-Za-z0-9_]+$/.test(actualItem.id) &&
141
+ actualItem.subscription === expected.subscriptionId,
142
+ "target_item_identity_mismatch",
143
+ );
144
+ const current = get(schedule, "current_phase");
145
+ need(
146
+ Object.keys(current).length === 2 &&
147
+ Object.prototype.hasOwnProperty.call(current, "start_date") &&
148
+ Object.prototype.hasOwnProperty.call(current, "end_date"),
149
+ "unqualified_current_phase_shape",
150
+ "unknown",
151
+ );
152
+ timestamp.parse(current.start_date);
153
+ timestamp.parse(current.end_date);
154
+ const phases = array(schedule.phases);
155
+ const originalPhase = object(array(originalSchedule.phases)[0]);
156
+ const currentOriginal =
157
+ current.start_date === expected.periodStart &&
158
+ current.end_date === expected.periodEnd;
159
+ const activeTarget = current.start_date === expected.periodEnd;
160
+ // END original 280-415
161
+ return {
162
+ kind: "continue" as const,
163
+ state: {
164
+ ...state,
165
+ customerId,
166
+ items,
167
+ actualItem,
168
+ current,
169
+ phases,
170
+ originalPhase,
171
+ currentOriginal,
172
+ activeTarget,
173
+ },
174
+ };
175
+ }
@@ -0,0 +1,49 @@
1
+ import {
2
+ object,
3
+ array,
4
+ need,
5
+ clone,
6
+ get,
7
+ planMatchesPrice,
8
+ type O,
9
+ } from "./common.js";
10
+ import type { phasesStage } from "./phase-price-normalization.js";
11
+ export function normalize(state: ReturnType<typeof phasesStage>) {
12
+ const {
13
+ context,
14
+ ref,
15
+ actualItemDiscounts,
16
+ actualSubDiscounts,
17
+ originalItemDiscounts,
18
+ originalSubDiscounts,
19
+ settings,
20
+ } = state;
21
+ // BEGIN original 759-787
22
+ const normalizeSchedule = (v: O) => {
23
+ const s = clone(v);
24
+ s.customer = ref(s.customer, "cus", "customer");
25
+ s.subscription = ref(s.subscription, "sub", "subscription");
26
+ s.default_settings = settings(get(s, "default_settings"));
27
+ return s;
28
+ };
29
+ const normalizeSub = (v: O, stage: "original" | "target", knownPrice: O) => {
30
+ const s = settings(v);
31
+ s.customer = ref(s.customer, "cus", "customer");
32
+ s.schedule = ref(s.schedule, "sub_sched", "subscription_schedule");
33
+ const i = clone(object(array(get(s, "items").data)[0]));
34
+ need(
35
+ planMatchesPrice(i.plan, knownPrice, context),
36
+ "unproven_native_plan_price_mapping",
37
+ "unknown",
38
+ );
39
+ i.plan = knownPrice.id;
40
+ i.discounts =
41
+ stage === "target" ? actualItemDiscounts : originalItemDiscounts;
42
+ s.discounts =
43
+ stage === "target" ? actualSubDiscounts : originalSubDiscounts;
44
+ get(s, "items").data = [i];
45
+ return s;
46
+ };
47
+ // END original 759-787
48
+ return { ...state, normalizeSchedule, normalizeSub };
49
+ }
@@ -0,0 +1,60 @@
1
+ import { object, array, need, clone, get, equal, type O } from "./common.js";
2
+ import type { taxes } from "./invoice-tax-identity.js";
3
+ export function settingsStage(state: ReturnType<typeof taxes>) {
4
+ const { ref, customerId, invoiceTaxId, retainedPMs } = state;
5
+ // BEGIN original 618-669
6
+ const settings = (v: O) => {
7
+ const o = clone(v);
8
+ for (const [field, prefix, kind] of [
9
+ ["default_payment_method", "pm", "payment_method"],
10
+ ["on_behalf_of", "acct", "account"],
11
+ ] as const)
12
+ if (o[field] !== null && o[field] !== undefined) {
13
+ const value = o[field];
14
+ if (field === "default_payment_method" && typeof value !== "string") {
15
+ const pm = object(value);
16
+ if (pm.customer !== null && pm.customer !== undefined)
17
+ need(
18
+ ref(pm.customer, "cus", "customer") === customerId,
19
+ "expanded_payment_method_customer_mismatch",
20
+ );
21
+ for (const retained of retainedPMs)
22
+ if (retained.id === pm.id)
23
+ need(
24
+ equal(pm, retained),
25
+ "payment_method_full_read_drift",
26
+ "unknown",
27
+ );
28
+ }
29
+ o[field] = ref(value, prefix, kind);
30
+ }
31
+ if (o.transfer_data !== null && o.transfer_data !== undefined) {
32
+ const t = get(o, "transfer_data");
33
+ t.destination = ref(t.destination, "acct", "account");
34
+ o.transfer_data = t;
35
+ }
36
+ for (const [outer, inner] of [
37
+ ["automatic_tax", "liability"],
38
+ ["invoice_settings", "issuer"],
39
+ ])
40
+ if (o[outer] !== null && o[outer] !== undefined) {
41
+ const x = get(o, outer);
42
+ if (x[inner] !== null && x[inner] !== undefined) {
43
+ const q = get(x, inner);
44
+ if (q.type === "account")
45
+ q.account = ref(q.account, "acct", "account");
46
+ x[inner] = q;
47
+ }
48
+ o[outer] = x;
49
+ }
50
+ if (o.invoice_settings !== null && o.invoice_settings !== undefined) {
51
+ const inv = get(o, "invoice_settings");
52
+ if (inv.account_tax_ids !== null && inv.account_tax_ids !== undefined)
53
+ inv.account_tax_ids = array(inv.account_tax_ids).map(invoiceTaxId);
54
+ o.invoice_settings = inv;
55
+ }
56
+ return o;
57
+ };
58
+ // END original 618-669
59
+ return { ...state, settings };
60
+ }
@@ -0,0 +1,30 @@
1
+ import type { createContext } from "./scheduled-context.js";
2
+ import { references } from "./reference-catalog.js";
3
+ import { identities } from "./scheduled-identity.js";
4
+ import { redemptions } from "./coupon-discount-joins.js";
5
+ import { taxes } from "./invoice-tax-identity.js";
6
+ import { settingsStage } from "./settings-normalization.js";
7
+ import { phasesStage } from "./phase-price-normalization.js";
8
+ import { normalize } from "./scheduled-normalization.js";
9
+ import { templates } from "./schedule-templates.js";
10
+ import { baseline } from "./configured-baseline.js";
11
+ import { pending } from "./pending-and-stage.js";
12
+ import { active } from "./active-scheduled.js";
13
+ /** Private stages unchanged; fixed callers build their own checked context. */
14
+ export function runStages(c0: ReturnType<typeof createContext>) {
15
+ const c1 = references(c0);
16
+ const c2 = identities(c1);
17
+ if (c2.kind === "result") return c2.result;
18
+ const c3 = redemptions(c2.state);
19
+ const c4 = taxes(c3);
20
+ const c5 = settingsStage(c4);
21
+ const c6 = phasesStage(c5);
22
+ const c7 = normalize(c6);
23
+ const c8 = templates(c7);
24
+ if (c8.kind === "result") return c8.result;
25
+ const c9 = baseline(c8.state);
26
+ if (c9.kind === "result") return c9.result;
27
+ const c10 = pending(c9.state);
28
+ if (c10.kind === "result") return c10.result;
29
+ return active(c10.state);
30
+ }