@voltro/plugin-billing 0.85.0 → 0.87.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.
@@ -0,0 +1,471 @@
1
+ import { Stripe as e } from "./stripeClient.js";
2
+ import { STRIPE_WEBHOOK_EVENTS as t } from "./stripeProvider.js";
3
+ //#region src/catalog.ts
4
+ var n = e.API_VERSION, r = [
5
+ "Checkout → \"Limit customers to one subscription\" (with the customer portal login link enabled) — Stripe's own refusal of a second subscription",
6
+ "Smart Retries + failed-payment emails (Billing → Revenue recovery)",
7
+ "invoice branding and email settings",
8
+ "a restricted key (rk_) for the app, separate from the provisioning key"
9
+ ], i = (e) => typeof e.tax_code == "string" ? e.tax_code : e.tax_code?.id ?? null, a = (e) => ({
10
+ name: e.name,
11
+ description: e.description,
12
+ unit_label: e.unitLabel,
13
+ tax_code: e.taxCode,
14
+ metadata: {
15
+ ...e.metadata,
16
+ voltro_key: e.key
17
+ }
18
+ }), o = (e, t) => {
19
+ let n = [];
20
+ t.active || n.push("active"), t.name !== e.name && n.push("name"), (t.description ?? "") !== e.description && n.push("description"), (t.unit_label ?? "") !== e.unitLabel && n.push("unit_label"), i(t) !== e.taxCode && n.push("tax_code");
21
+ for (let [r, i] of Object.entries({
22
+ ...e.metadata,
23
+ voltro_key: e.key
24
+ })) t.metadata[r] !== i && n.push(`metadata.${r}`);
25
+ return n;
26
+ }, s = (e, t) => {
27
+ let n = `product ${e.key}`, r = t.filter((t) => t.metadata.voltro_key === e.key);
28
+ if (r.length > 1) return {
29
+ finding: {
30
+ kind: "manual",
31
+ object: n,
32
+ detail: `${r.length} products carry this key — archive the extra ones`
33
+ },
34
+ id: null
35
+ };
36
+ let i = r[0];
37
+ if (i !== void 0) {
38
+ let t = o(e, i);
39
+ return t.length === 0 ? {
40
+ finding: {
41
+ kind: "ok",
42
+ object: n
43
+ },
44
+ id: i.id
45
+ } : {
46
+ finding: {
47
+ kind: "update",
48
+ object: n,
49
+ detail: t.join(", "),
50
+ apply: {
51
+ method: "POST",
52
+ path: `/v1/products/${i.id}`,
53
+ params: {
54
+ ...a(e),
55
+ active: !0
56
+ }
57
+ }
58
+ },
59
+ id: i.id
60
+ };
61
+ }
62
+ let s = t.filter((t) => t.active && t.metadata.voltro_key === void 0 && Object.keys(e.metadata).length > 0 && Object.entries(e.metadata).every(([e, n]) => t.metadata[e] === n));
63
+ if (s.length === 1) {
64
+ let t = s[0];
65
+ return {
66
+ finding: {
67
+ kind: "adopt",
68
+ object: n,
69
+ detail: "existing product gets the catalog key",
70
+ apply: {
71
+ method: "POST",
72
+ path: `/v1/products/${t.id}`,
73
+ params: a(e)
74
+ }
75
+ },
76
+ id: t.id
77
+ };
78
+ }
79
+ return s.length > 1 ? {
80
+ finding: {
81
+ kind: "manual",
82
+ object: n,
83
+ detail: `${s.length} products could be this one — archive all but one`
84
+ },
85
+ id: null
86
+ } : {
87
+ finding: {
88
+ kind: "create",
89
+ object: n,
90
+ detail: e.name,
91
+ apply: {
92
+ method: "POST",
93
+ path: "/v1/products",
94
+ params: a(e)
95
+ }
96
+ },
97
+ id: null
98
+ };
99
+ }, c = (e, t, n, r) => {
100
+ let i = [];
101
+ return r.product !== t && i.push("product"), r.currency !== e.currency && i.push("currency"), r.unit_amount !== e.unitAmount && i.push("unit_amount"), r.recurring?.interval !== e.interval && i.push("interval"), r.recurring?.usage_type !== e.usageType && i.push("usage_type"), e.usageType === "metered" && r.recurring?.meter !== n && i.push("meter"), r.tax_behavior !== e.taxBehavior && i.push("tax_behavior"), i;
102
+ }, l = (e, t, n, r) => ({
103
+ product: t,
104
+ currency: e.currency,
105
+ unit_amount: e.unitAmount,
106
+ recurring: {
107
+ interval: e.interval,
108
+ usage_type: e.usageType,
109
+ ...e.usageType === "metered" && n !== null ? { meter: n } : {}
110
+ },
111
+ tax_behavior: e.taxBehavior,
112
+ lookup_key: e.lookupKey,
113
+ ...r ? { transfer_lookup_key: !0 } : {},
114
+ metadata: { voltro_key: e.lookupKey }
115
+ }), u = (e, t, n, r, i, a) => {
116
+ let o = `price ${e.lookupKey}`;
117
+ if (t === null) return {
118
+ finding: {
119
+ kind: "manual",
120
+ object: o,
121
+ detail: "its product is unresolved"
122
+ },
123
+ id: null
124
+ };
125
+ if (e.usageType === "metered" && n === null) return {
126
+ finding: {
127
+ kind: "manual",
128
+ object: o,
129
+ detail: "its meter is unresolved"
130
+ },
131
+ id: null
132
+ };
133
+ if (r !== null) {
134
+ let i = c(e, t, n, r);
135
+ if (i.length > 0) {
136
+ let s = {
137
+ method: "POST",
138
+ path: "/v1/prices",
139
+ params: l(e, t, n, !0)
140
+ };
141
+ return a ? {
142
+ finding: {
143
+ kind: "drift",
144
+ object: o,
145
+ detail: `replaced (${i.join(", ")}); existing subscriptions keep the old price`,
146
+ apply: s
147
+ },
148
+ id: null
149
+ } : {
150
+ finding: {
151
+ kind: "drift",
152
+ object: o,
153
+ detail: `${i.join(", ")} differ — a price change; rerun with --allow-price-change to replace it`
154
+ },
155
+ id: r.id
156
+ };
157
+ }
158
+ return r.active ? {
159
+ finding: {
160
+ kind: "ok",
161
+ object: o
162
+ },
163
+ id: r.id
164
+ } : {
165
+ finding: {
166
+ kind: "update",
167
+ object: o,
168
+ detail: "inactive",
169
+ apply: {
170
+ method: "POST",
171
+ path: `/v1/prices/${r.id}`,
172
+ params: { active: !0 }
173
+ }
174
+ },
175
+ id: r.id
176
+ };
177
+ }
178
+ let s = i.filter((r) => r.active && r.lookup_key === null && c(e, t, n, r).length === 0)[0];
179
+ return s === void 0 ? {
180
+ finding: {
181
+ kind: "create",
182
+ object: o,
183
+ detail: `${e.unitAmount} ${e.currency} / ${e.interval}`,
184
+ apply: {
185
+ method: "POST",
186
+ path: "/v1/prices",
187
+ params: l(e, t, n, !1)
188
+ }
189
+ },
190
+ id: null
191
+ } : {
192
+ finding: {
193
+ kind: "adopt",
194
+ object: o,
195
+ detail: "existing price gets the lookup key",
196
+ apply: {
197
+ method: "POST",
198
+ path: `/v1/prices/${s.id}`,
199
+ params: {
200
+ lookup_key: e.lookupKey,
201
+ metadata: { voltro_key: e.lookupKey }
202
+ }
203
+ }
204
+ },
205
+ id: s.id
206
+ };
207
+ }, d = (e, t) => {
208
+ let n = `meter ${e.eventName}`, r = t.find((t) => t.event_name === e.eventName);
209
+ if (r === void 0) return {
210
+ finding: {
211
+ kind: "create",
212
+ object: n,
213
+ detail: e.displayName,
214
+ apply: {
215
+ method: "POST",
216
+ path: "/v1/billing/meters",
217
+ params: {
218
+ display_name: e.displayName,
219
+ event_name: e.eventName,
220
+ default_aggregation: { formula: e.formula },
221
+ customer_mapping: {
222
+ type: "by_id",
223
+ event_payload_key: e.customerPayloadKey
224
+ },
225
+ value_settings: { event_payload_key: e.valuePayloadKey }
226
+ }
227
+ }
228
+ },
229
+ id: null
230
+ };
231
+ let i = [];
232
+ return r.default_aggregation.formula !== e.formula && i.push("aggregation"), (r.customer_mapping.type !== "by_id" || r.customer_mapping.event_payload_key !== e.customerPayloadKey) && i.push("customer mapping"), r.value_settings.event_payload_key !== e.valuePayloadKey && i.push("value key"), i.length > 0 ? {
233
+ finding: {
234
+ kind: "drift",
235
+ object: n,
236
+ detail: `${i.join(", ")} differ and cannot be edited — a new meter needs a new event name`
237
+ },
238
+ id: r.id
239
+ } : r.status === "active" ? r.display_name === e.displayName ? {
240
+ finding: {
241
+ kind: "ok",
242
+ object: n
243
+ },
244
+ id: r.id
245
+ } : {
246
+ finding: {
247
+ kind: "update",
248
+ object: n,
249
+ detail: "display name",
250
+ apply: {
251
+ method: "POST",
252
+ path: `/v1/billing/meters/${r.id}`,
253
+ params: { display_name: e.displayName }
254
+ }
255
+ },
256
+ id: r.id
257
+ } : {
258
+ finding: {
259
+ kind: "update",
260
+ object: n,
261
+ detail: "inactive",
262
+ apply: {
263
+ method: "POST",
264
+ path: `/v1/billing/meters/${r.id}/reactivate`,
265
+ params: {}
266
+ }
267
+ },
268
+ id: r.id
269
+ };
270
+ }, f = (e, t) => {
271
+ let n = "tax settings", r = t.head_office?.address ?? {}, i = [];
272
+ for (let [t, n] of Object.entries(e.headOffice)) r[t] !== n && i.push(`head_office.${t}`);
273
+ return t.defaults.tax_code !== e.defaultTaxCode && i.push("defaults.tax_code"), t.defaults.tax_behavior !== e.defaultTaxBehavior && i.push("defaults.tax_behavior"), i.length === 0 ? t.status === "active" ? {
274
+ kind: "ok",
275
+ object: n
276
+ } : {
277
+ kind: "manual",
278
+ object: n,
279
+ detail: `status ${t.status}`
280
+ } : {
281
+ kind: "update",
282
+ object: n,
283
+ detail: i.join(", "),
284
+ apply: {
285
+ method: "POST",
286
+ path: "/v1/tax/settings",
287
+ params: {
288
+ head_office: { address: e.headOffice },
289
+ defaults: {
290
+ tax_code: e.defaultTaxCode,
291
+ tax_behavior: e.defaultTaxBehavior
292
+ }
293
+ }
294
+ }
295
+ };
296
+ }, p = (e, t, n) => ({
297
+ business_profile: {
298
+ headline: e.headline,
299
+ privacy_policy_url: e.privacyPolicyUrl,
300
+ terms_of_service_url: e.termsOfServiceUrl
301
+ },
302
+ default_return_url: t.portalReturnUrl,
303
+ features: {
304
+ customer_update: {
305
+ enabled: !0,
306
+ allowed_updates: [
307
+ "email",
308
+ "address",
309
+ "tax_id",
310
+ "name"
311
+ ]
312
+ },
313
+ invoice_history: { enabled: !0 },
314
+ payment_method_update: { enabled: !0 },
315
+ subscription_cancel: {
316
+ enabled: !0,
317
+ mode: "at_period_end"
318
+ },
319
+ subscription_update: {
320
+ enabled: !0,
321
+ default_allowed_updates: ["price", "quantity"],
322
+ proration_behavior: "create_prorations",
323
+ products: n.map((e) => ({
324
+ product: e.productId,
325
+ prices: [e.priceId]
326
+ }))
327
+ }
328
+ },
329
+ metadata: { voltro_key: e.key }
330
+ }), m = (e) => JSON.stringify([...e ?? []].sort()), h = (e, t) => {
331
+ let n = [], r = t.features, i = e.features;
332
+ t.default_return_url !== e.default_return_url && n.push("default_return_url");
333
+ let a = e.business_profile;
334
+ for (let e of Object.keys(a)) t.business_profile[e] !== a[e] && n.push(`business_profile.${e}`);
335
+ for (let e of [
336
+ "customer_update",
337
+ "invoice_history",
338
+ "payment_method_update",
339
+ "subscription_cancel"
340
+ ]) r[e]?.enabled !== !0 && n.push(`${e}.enabled`);
341
+ m(r.customer_update?.allowed_updates) !== m(i.customer_update.allowed_updates) && n.push("customer_update.allowed_updates"), r.subscription_cancel?.mode !== "at_period_end" && n.push("subscription_cancel.mode");
342
+ let o = r.subscription_update ?? {}, s = i.subscription_update;
343
+ o.enabled !== !0 && n.push("subscription_update.enabled"), o.proration_behavior !== s.proration_behavior && n.push("subscription_update.proration_behavior");
344
+ let c = (e) => JSON.stringify((e ?? []).map((e) => `${e.product}:${[...e.prices].sort().join("|")}`).sort());
345
+ return c(o.products) !== c(s.products) && n.push("subscription_update.products"), m(o.default_allowed_updates) !== m(s.default_allowed_updates) && n.push("subscription_update.default_allowed_updates"), n;
346
+ }, g = (e, t, n, r) => {
347
+ let i = "customer portal";
348
+ if (n === null) return [{
349
+ kind: "manual",
350
+ object: i,
351
+ detail: "its plan prices are unresolved"
352
+ }];
353
+ let a = p(e, t, n), o = r.find((t) => t.metadata.voltro_key === e.key) ?? r.find((t) => t.active && t.name === e.name);
354
+ if (o === void 0) return [{
355
+ kind: "create",
356
+ object: i,
357
+ detail: "self-service configuration",
358
+ apply: {
359
+ method: "POST",
360
+ path: "/v1/billing_portal/configurations",
361
+ params: {
362
+ ...a,
363
+ name: e.name
364
+ }
365
+ }
366
+ }, ...r.some((e) => e.is_default) ? [{
367
+ kind: "manual",
368
+ object: i,
369
+ detail: "another configuration is the default — make this one the default in the Dashboard"
370
+ }] : []];
371
+ let s = [], c = h(a, o), l = o.metadata.voltro_key !== e.key;
372
+ return c.length > 0 || l || !o.active ? s.push({
373
+ kind: l ? "adopt" : "update",
374
+ object: i,
375
+ detail: [...c, ...o.active ? [] : ["active"]].join(", ") || "catalog key",
376
+ apply: {
377
+ method: "POST",
378
+ path: `/v1/billing_portal/configurations/${o.id}`,
379
+ params: {
380
+ ...a,
381
+ active: !0
382
+ }
383
+ }
384
+ }) : s.push({
385
+ kind: "ok",
386
+ object: i
387
+ }), o.is_default || s.push({
388
+ kind: "manual",
389
+ object: i,
390
+ detail: "not the account default — portal sessions would use another configuration"
391
+ }), s;
392
+ }, _ = (e, r) => {
393
+ if (e.webhookUrl === null) return {
394
+ kind: "ok",
395
+ object: "webhook endpoint (none: stripe listen forwards locally)"
396
+ };
397
+ let i = `webhook endpoint ${e.webhookUrl}`, a = r.filter((t) => t.url === e.webhookUrl);
398
+ if (a.length > 1) return {
399
+ kind: "manual",
400
+ object: i,
401
+ detail: `${a.length} endpoints for this url — delete all but one`
402
+ };
403
+ let o = a[0], s = [...t].sort();
404
+ return o === void 0 ? {
405
+ kind: "create",
406
+ object: i,
407
+ detail: `${s.length} events`,
408
+ apply: {
409
+ method: "POST",
410
+ path: "/v1/webhook_endpoints",
411
+ params: {
412
+ url: e.webhookUrl,
413
+ enabled_events: s,
414
+ api_version: n,
415
+ description: "Voltro billing"
416
+ }
417
+ }
418
+ } : o.api_version !== null && o.api_version !== n ? {
419
+ kind: "drift",
420
+ object: i,
421
+ detail: `api version ${o.api_version} — payloads would not match the SDK; recreate it (and rotate STRIPE_WEBHOOK_SECRET)`
422
+ } : JSON.stringify([...o.enabled_events].sort()) !== JSON.stringify(s) || o.status !== "enabled" ? {
423
+ kind: "update",
424
+ object: i,
425
+ detail: o.status === "enabled" ? "events" : "disabled",
426
+ apply: {
427
+ method: "POST",
428
+ path: `/v1/webhook_endpoints/${o.id}`,
429
+ params: {
430
+ enabled_events: s,
431
+ disabled: !1
432
+ }
433
+ }
434
+ } : {
435
+ kind: "ok",
436
+ object: i
437
+ };
438
+ }, v = (e) => {
439
+ let t = [];
440
+ for (let n of e.catalog.tax.registrationCountries) e.activeRegistrations.includes(n) || t.push({
441
+ kind: "manual",
442
+ object: `tax registration ${n}`,
443
+ detail: "none active — automatic tax collects nothing until it is registered (Dashboard → Tax → Registrations)"
444
+ });
445
+ if (!e.env.requireSellingReadiness) return t;
446
+ e.chargesEnabled === !1 && t.push({
447
+ kind: "manual",
448
+ object: "account",
449
+ detail: "charges not enabled — finish activation (business details, bank account) in the Dashboard"
450
+ }), e.chargesEnabled === null && t.push({
451
+ kind: "manual",
452
+ object: "account",
453
+ detail: "activation not readable with this key — check it in the Dashboard"
454
+ }), e.accountTaxIds === 0 && t.push({
455
+ kind: "manual",
456
+ object: "account tax id",
457
+ detail: "no VAT id on the account — invoices lack the issuer id"
458
+ }), e.accountTaxIds === null && t.push({
459
+ kind: "manual",
460
+ object: "account tax id",
461
+ detail: "not readable with this key — check Dashboard → Settings → Tax ids"
462
+ });
463
+ for (let n of e.catalog.dashboardReminders ?? r) t.push({
464
+ kind: "manual",
465
+ object: "dashboard",
466
+ detail: `${n} — not checkable through the API`
467
+ });
468
+ return t;
469
+ };
470
+ //#endregion
471
+ export { n as STRIPE_API_VERSION, r as STRIPE_DASHBOARD_REMINDERS, d as diffMeter, g as diffPortal, u as diffPrice, s as diffProduct, f as diffTaxSettings, _ as diffWebhook, p as portalParams, v as sellingReadiness };
@@ -0,0 +1,165 @@
1
+ import { BillingError as e } from "./types.js";
2
+ import { Effect as t } from "effect";
3
+ import { and as n, eq as r, id as i, integer as a, table as o, text as s, timestamp as c } from "@voltro/database";
4
+ //#region src/checkoutStore.ts
5
+ var l = "_voltro_billing_checkout_operations", u = o(l, {
6
+ id: i({ prefix: "bcheckout" }),
7
+ tenantId: s().maxLength(191),
8
+ provider: s().maxLength(64),
9
+ requestDigest: s().maxLength(64),
10
+ plan: s(),
11
+ quantity: a(),
12
+ providerCustomerId: s().nullable(),
13
+ providerSessionId: s().nullable(),
14
+ phase: s(),
15
+ revision: a().default(0),
16
+ active: a().nullable(),
17
+ slot: s().maxLength(191),
18
+ createdAt: c(),
19
+ expiresAt: c(),
20
+ updatedAt: c()
21
+ }).unique("byBillingCheckoutActiveTenant", ["tenantId", "slot"]).index("byBillingCheckoutActiveState", ["tenantId", "active"]).index("byBillingCheckoutHistory", ["tenantId", "createdAt"]), d = () => {
22
+ let e = /* @__PURE__ */ new Map(), n = (t) => [...e.values()].find((e) => e.tenantId === t && e.active === 1);
23
+ return {
24
+ getActive: (e) => t.try({
25
+ try: () => {
26
+ let t = n(e);
27
+ return t ? g({ ...t }) : null;
28
+ },
29
+ catch: p
30
+ }),
31
+ reserve: (r) => t.try({
32
+ try: () => {
33
+ let t = n(r.tenantId);
34
+ if (t) return g({ ...t });
35
+ let i = _(r);
36
+ if (e.has(i.id)) throw p();
37
+ return e.set(i.id, i), g({ ...i });
38
+ },
39
+ catch: p
40
+ }),
41
+ patch: (n, r) => t.try({
42
+ try: () => {
43
+ let t = e.get(n);
44
+ if (!t) throw p();
45
+ let i = v(t, r);
46
+ return e.set(t.id, i), g({ ...i });
47
+ },
48
+ catch: p
49
+ })
50
+ };
51
+ }, f = (e) => {
52
+ let t = async (t) => {
53
+ let i = await e.query(b(n(r("tenantId", t), r("active", 1))));
54
+ if (i.length > 1) throw p();
55
+ return i[0] ? g(i[0]) : null;
56
+ };
57
+ return {
58
+ getActive: (e) => y(() => t(e)),
59
+ reserve: (n) => y(async () => {
60
+ for (let r = 0; r < 8; r++) {
61
+ let r = await t(n.tenantId);
62
+ if (r) return r;
63
+ let i = _(n);
64
+ await e.insertIgnore(l, { ...i }, { conflictColumns: ["tenantId", "slot"] });
65
+ let a = await t(n.tenantId);
66
+ if (a) return a;
67
+ }
68
+ throw p();
69
+ }),
70
+ patch: (t, i) => y(async () => {
71
+ for (let a = 0; a < 32; a++) {
72
+ let a = await e.query(b(r("id", t)));
73
+ if (a.length !== 1 || !a[0]) throw p();
74
+ let o = g(a[0]), s = v(o, i);
75
+ if (s === o) return o;
76
+ if (await e.updateMany("_voltro_billing_checkout_operations", {
77
+ phase: s.phase,
78
+ active: s.active,
79
+ slot: s.slot,
80
+ revision: s.revision,
81
+ providerCustomerId: s.providerCustomerId,
82
+ providerSessionId: s.providerSessionId,
83
+ updatedAt: s.updatedAt
84
+ }, { where: n(r("id", t), r("revision", o.revision), r("active", 1)) }) === 1) return s;
85
+ }
86
+ throw p();
87
+ })
88
+ };
89
+ }, p = () => new e({
90
+ source: "checkoutStore",
91
+ message: "Checkout coordination could not be confirmed. Retry its current status before starting another request.",
92
+ transient: !0
93
+ }), m = [
94
+ "preparing",
95
+ "creating",
96
+ "ready",
97
+ "completed",
98
+ "expired",
99
+ "retired"
100
+ ], h = (e) => {
101
+ let t = e instanceof Date ? new Date(e) : new Date(typeof e == "string" || typeof e == "number" ? e : NaN);
102
+ if (!Number.isFinite(t.getTime())) throw p();
103
+ return t;
104
+ }, g = (e) => {
105
+ if ([
106
+ "id",
107
+ "tenantId",
108
+ "provider",
109
+ "requestDigest",
110
+ "plan"
111
+ ].some((t) => typeof e[t] != "string" || e[t] === "") || String(e.tenantId).length > 191 || String(e.id).length > 191 || String(e.provider).length > 64 || !/^[a-f0-9]{64}$/.test(String(e.requestDigest)) || !m.includes(String(e.phase)) || !Number.isSafeInteger(e.quantity) || Number(e.quantity) < 1 || !Number.isSafeInteger(e.revision) || Number(e.revision) < 0 || e.active !== null && e.active !== 1 || (e.active === 1 ? e.slot !== "active" : e.slot !== e.id) || (e.phase === "expired" || e.phase === "retired" ? e.active !== null : e.active !== 1) || e.providerCustomerId !== null && (typeof e.providerCustomerId != "string" || e.providerCustomerId === "") || e.providerSessionId !== null && (typeof e.providerSessionId != "string" || e.providerSessionId === "")) throw p();
112
+ return {
113
+ id: String(e.id),
114
+ tenantId: String(e.tenantId),
115
+ provider: String(e.provider),
116
+ requestDigest: String(e.requestDigest),
117
+ plan: String(e.plan),
118
+ quantity: Number(e.quantity),
119
+ providerCustomerId: e.providerCustomerId,
120
+ providerSessionId: e.providerSessionId,
121
+ phase: e.phase,
122
+ revision: Number(e.revision),
123
+ active: e.active,
124
+ slot: String(e.slot),
125
+ createdAt: h(e.createdAt),
126
+ expiresAt: h(e.expiresAt),
127
+ updatedAt: h(e.updatedAt)
128
+ };
129
+ }, _ = (e) => g({
130
+ ...e,
131
+ id: "bcheckout_" + globalThis.crypto.randomUUID(),
132
+ providerCustomerId: null,
133
+ providerSessionId: null,
134
+ phase: "preparing",
135
+ revision: 0,
136
+ active: 1,
137
+ slot: "active",
138
+ updatedAt: e.createdAt
139
+ }), v = (e, t) => {
140
+ if (t.phase !== void 0 && !m.includes(t.phase) || t.providerCustomerId !== void 0 && (typeof t.providerCustomerId != "string" || t.providerCustomerId.length === 0) || t.providerSessionId !== void 0 && (typeof t.providerSessionId != "string" || t.providerSessionId.length === 0) || t.providerCustomerId !== void 0 && e.providerCustomerId !== null && e.providerCustomerId !== t.providerCustomerId || t.providerSessionId !== void 0 && e.providerSessionId !== null && e.providerSessionId !== t.providerSessionId) throw p();
141
+ let n = (e) => e === "preparing" ? 0 : e === "creating" ? 1 : e === "ready" ? 2 : e === "completed" ? 3 : 4;
142
+ if (e.active === null || t.phase !== void 0 && n(t.phase) < n(e.phase)) return e;
143
+ let r = t.phase ?? e.phase, i = r === "retired" || r === "expired";
144
+ return g({
145
+ ...e,
146
+ ...t,
147
+ phase: r,
148
+ active: i ? null : 1,
149
+ slot: i ? e.id : "active",
150
+ revision: e.revision + 1,
151
+ updatedAt: /* @__PURE__ */ new Date()
152
+ });
153
+ }, y = (e) => t.tryPromise({
154
+ try: e,
155
+ catch: p
156
+ }), b = (e) => ({
157
+ table: l,
158
+ predicate: e,
159
+ order: [],
160
+ take: 2,
161
+ skip: void 0,
162
+ projection: void 0
163
+ });
164
+ //#endregion
165
+ export { l as BILLING_CHECKOUT_OPERATIONS_TABLE, u as billingCheckoutOperationsTable, f as dataStoreCheckoutOperationStore, d as memoryCheckoutOperationStore };