@voltro/plugin-billing 0.89.1 → 0.90.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/catalog.js CHANGED
@@ -1,12 +1,23 @@
1
1
  import { Stripe as e } from "./stripeClient.js";
2
2
  import { STRIPE_WEBHOOK_EVENTS as t } from "./stripeProvider.js";
3
3
  // src/catalog.ts
4
- var n = e.API_VERSION, r = [
4
+ var n = e.API_VERSION, r = (e) => {
5
+ let t = e.retired ?? {}, n = {
6
+ prices: new Set(e.products.map((e) => e.price.lookupKey)),
7
+ products: new Set(e.products.map((e) => e.key)),
8
+ meters: new Set(e.meters.map((e) => e.eventName))
9
+ };
10
+ return [
11
+ "prices",
12
+ "products",
13
+ "meters"
14
+ ].flatMap((e) => (t[e] ?? []).filter((t) => n[e].has(t)).map((t) => `${e.slice(0, -1)} ${t}`));
15
+ }, i = [
5
16
  "Checkout → \"Limit customers to one subscription\" (with the customer portal login link enabled) — Stripe's own refusal of a second subscription",
6
17
  "Smart Retries + failed-payment emails (Billing → Revenue recovery)",
7
18
  "invoice branding and email settings",
8
19
  "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) => ({
20
+ ], a = (e) => typeof e.tax_code == "string" ? e.tax_code : e.tax_code?.id ?? null, o = (e) => ({
10
21
  name: e.name,
11
22
  description: e.description,
12
23
  unit_label: e.unitLabel,
@@ -15,15 +26,15 @@ var n = e.API_VERSION, r = [
15
26
  ...e.metadata,
16
27
  voltro_key: e.key
17
28
  }
18
- }), o = (e, t) => {
29
+ }), s = (e, t) => {
19
30
  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");
31
+ 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"), a(t) !== e.taxCode && n.push("tax_code");
21
32
  for (let [r, i] of Object.entries({
22
33
  ...e.metadata,
23
34
  voltro_key: e.key
24
35
  })) t.metadata[r] !== i && n.push(`metadata.${r}`);
25
36
  return n;
26
- }, s = (e, t) => {
37
+ }, c = (e, t) => {
27
38
  let n = `product ${e.key}`, r = t.filter((t) => t.metadata.voltro_key === e.key);
28
39
  if (r.length > 1) return {
29
40
  finding: {
@@ -35,7 +46,7 @@ var n = e.API_VERSION, r = [
35
46
  };
36
47
  let i = r[0];
37
48
  if (i !== void 0) {
38
- let t = o(e, i);
49
+ let t = s(e, i);
39
50
  return t.length === 0 ? {
40
51
  finding: {
41
52
  kind: "ok",
@@ -51,7 +62,7 @@ var n = e.API_VERSION, r = [
51
62
  method: "POST",
52
63
  path: `/v1/products/${i.id}`,
53
64
  params: {
54
- ...a(e),
65
+ ...o(e),
55
66
  active: !0
56
67
  }
57
68
  }
@@ -59,9 +70,9 @@ var n = e.API_VERSION, r = [
59
70
  id: i.id
60
71
  };
61
72
  }
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];
73
+ let a = 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));
74
+ if (a.length === 1) {
75
+ let t = a[0];
65
76
  return {
66
77
  finding: {
67
78
  kind: "adopt",
@@ -70,17 +81,17 @@ var n = e.API_VERSION, r = [
70
81
  apply: {
71
82
  method: "POST",
72
83
  path: `/v1/products/${t.id}`,
73
- params: a(e)
84
+ params: o(e)
74
85
  }
75
86
  },
76
87
  id: t.id
77
88
  };
78
89
  }
79
- return s.length > 1 ? {
90
+ return a.length > 1 ? {
80
91
  finding: {
81
92
  kind: "manual",
82
93
  object: n,
83
- detail: `${s.length} products could be this one — archive all but one`
94
+ detail: `${a.length} products could be this one — archive all but one`
84
95
  },
85
96
  id: null
86
97
  } : {
@@ -91,18 +102,38 @@ var n = e.API_VERSION, r = [
91
102
  apply: {
92
103
  method: "POST",
93
104
  path: "/v1/products",
94
- params: a(e)
105
+ params: o(e)
95
106
  }
96
107
  },
97
108
  id: null
98
109
  };
99
- }, c = (e, t, n, r) => {
110
+ }, l = (e) => e.upTo === "inf" ? null : e.upTo, u = /^(?:0|[1-9][0-9]*)(?:\.[0-9]{1,12})?$/, d = (e, t) => e !== null && u.test(e) && u.test(t) && f(e) === f(t), f = (e) => {
111
+ let [t = "0", n = ""] = e.split("."), r = n.replace(/0+$/, "");
112
+ return r.length === 0 ? t : `${t}.${r}`;
113
+ }, p = (e) => e.tiers.some((e) => e.unitAmount !== void 0 && e.unitAmountDecimal !== void 0 || e.unitAmountDecimal !== void 0 && !u.test(e.unitAmountDecimal)), m = (e, t) => {
114
+ let n = [];
115
+ (t.billing_scheme ?? "per_unit") !== "tiered" && n.push("billing_scheme"), (t.tiers_mode ?? null) !== e.mode && n.push("tiers_mode");
116
+ let r = t.tiers ?? [];
117
+ return r.length === e.tiers.length ? (e.tiers.forEach((e, t) => {
118
+ let i = r[t];
119
+ i.up_to !== l(e) && n.push(`tiers[${t}].up_to`), e.unitAmount !== void 0 && i.unit_amount !== e.unitAmount && n.push(`tiers[${t}].unit_amount`), e.unitAmountDecimal !== void 0 && !d(i.unit_amount_decimal ?? (i.unit_amount === null ? null : String(i.unit_amount)), e.unitAmountDecimal) && n.push(`tiers[${t}].unit_amount_decimal`), e.flatAmount !== void 0 && i.flat_amount !== e.flatAmount && n.push(`tiers[${t}].flat_amount`);
120
+ }), n) : (n.push("tiers"), n);
121
+ }, h = (e) => e.tiers.length > 0 && e.tiers.at(-1).upTo === "inf" && e.tiers.every((e) => e.unitAmount !== void 0 || e.unitAmountDecimal !== void 0 || e.flatAmount !== void 0), g = (e, t, n, r) => {
100
122
  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) => ({
123
+ return r.product !== t && i.push("product"), r.currency !== e.currency && i.push("currency"), e.tiers === void 0 ? (r.billing_scheme ?? "per_unit") !== "per_unit" && i.push("billing_scheme") : i.push(...m(e.tiers, r)), e.unitAmount !== void 0 && 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;
124
+ }, _ = (e) => ({
125
+ billing_scheme: "tiered",
126
+ tiers_mode: e.mode,
127
+ tiers: e.tiers.map((e) => ({
128
+ up_to: e.upTo,
129
+ ...e.unitAmount === void 0 ? {} : { unit_amount: e.unitAmount },
130
+ ...e.unitAmountDecimal === void 0 ? {} : { unit_amount_decimal: e.unitAmountDecimal },
131
+ ...e.flatAmount === void 0 ? {} : { flat_amount: e.flatAmount }
132
+ }))
133
+ }), v = (e, t, n, r, i) => ({
103
134
  product: t,
104
135
  currency: e.currency,
105
- unit_amount: e.unitAmount,
136
+ ...i,
106
137
  recurring: {
107
138
  interval: e.interval,
108
139
  usage_type: e.usageType,
@@ -112,7 +143,7 @@ var n = e.API_VERSION, r = [
112
143
  lookup_key: e.lookupKey,
113
144
  ...r ? { transfer_lookup_key: !0 } : {},
114
145
  metadata: { voltro_key: e.lookupKey }
115
- }), u = (e, t, n, r, i, a) => {
146
+ }), y = (e, t, n, r, i, a) => {
116
147
  let o = `price ${e.lookupKey}`;
117
148
  if (t === null) return {
118
149
  finding: {
@@ -130,20 +161,46 @@ var n = e.API_VERSION, r = [
130
161
  },
131
162
  id: null
132
163
  };
164
+ if (e.tiers !== void 0 && e.unitAmount !== void 0) return {
165
+ finding: {
166
+ kind: "manual",
167
+ object: o,
168
+ detail: "declares both a flat unitAmount and tiers — a price is one or the other"
169
+ },
170
+ id: null
171
+ };
172
+ if (e.tiers !== void 0 && p(e.tiers)) return {
173
+ finding: {
174
+ kind: "manual",
175
+ object: o,
176
+ detail: "a tier declares both unitAmount and unitAmountDecimal, or a decimal amount Stripe does not accept (at most 12 decimal places)"
177
+ },
178
+ id: null
179
+ };
180
+ let s = e.tiers === void 0 ? e.unitAmount === void 0 ? null : { unit_amount: e.unitAmount } : h(e.tiers) ? _(e.tiers) : null;
133
181
  if (r !== null) {
134
- let i = c(e, t, n, r);
182
+ let i = g(e, t, n, r);
135
183
  if (i.length > 0) {
136
- let s = {
184
+ let c = s ?? (e.tiers === void 0 && r.unit_amount !== null ? { unit_amount: r.unit_amount } : null);
185
+ if (c === null) return {
186
+ finding: {
187
+ kind: "manual",
188
+ object: o,
189
+ detail: `${i.join(", ")} differ and the catalog cannot rebuild the amounts — replace it in the Dashboard`
190
+ },
191
+ id: r.id
192
+ };
193
+ let l = {
137
194
  method: "POST",
138
195
  path: "/v1/prices",
139
- params: l(e, t, n, !0)
196
+ params: v(e, t, n, !0, c)
140
197
  };
141
198
  return a ? {
142
199
  finding: {
143
200
  kind: "drift",
144
201
  object: o,
145
202
  detail: `replaced (${i.join(", ")}); existing subscriptions keep the old price`,
146
- apply: s
203
+ apply: l
147
204
  },
148
205
  id: null
149
206
  } : {
@@ -175,16 +232,23 @@ var n = e.API_VERSION, r = [
175
232
  id: r.id
176
233
  };
177
234
  }
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 ? {
235
+ let c = i.filter((r) => r.active && r.lookup_key === null && g(e, t, n, r).length === 0)[0];
236
+ return c === void 0 ? s === null ? {
237
+ finding: {
238
+ kind: "manual",
239
+ object: o,
240
+ detail: `create a ${e.currency} / ${e.interval} ${e.usageType}${e.tiers === void 0 ? "" : ` ${e.tiers.mode} tiered`} price on this product in the Stripe Dashboard with lookup key ${e.lookupKey}; amounts are kept in Stripe`
241
+ },
242
+ id: null
243
+ } : {
180
244
  finding: {
181
245
  kind: "create",
182
246
  object: o,
183
- detail: `${e.unitAmount} ${e.currency} / ${e.interval}`,
247
+ detail: "unit_amount" in s ? `${s.unit_amount} ${e.currency} / ${e.interval}` : `${s.tiers_mode} tiers ${e.tiers.tiers.map((e) => `${e.upTo}:${e.unitAmountDecimal ?? e.unitAmount ?? 0}${e.flatAmount === void 0 ? "" : `+${e.flatAmount}`}`).join(" ")} ${e.currency} / ${e.interval}`,
184
248
  apply: {
185
249
  method: "POST",
186
250
  path: "/v1/prices",
187
- params: l(e, t, n, !1)
251
+ params: v(e, t, n, !1, s)
188
252
  }
189
253
  },
190
254
  id: null
@@ -195,16 +259,16 @@ var n = e.API_VERSION, r = [
195
259
  detail: "existing price gets the lookup key",
196
260
  apply: {
197
261
  method: "POST",
198
- path: `/v1/prices/${s.id}`,
262
+ path: `/v1/prices/${c.id}`,
199
263
  params: {
200
264
  lookup_key: e.lookupKey,
201
265
  metadata: { voltro_key: e.lookupKey }
202
266
  }
203
267
  }
204
268
  },
205
- id: s.id
269
+ id: c.id
206
270
  };
207
- }, d = (e, t) => {
271
+ }, b = (e, t) => {
208
272
  let n = `meter ${e.eventName}`, r = t.find((t) => t.event_name === e.eventName);
209
273
  if (r === void 0) return {
210
274
  finding: {
@@ -267,7 +331,60 @@ var n = e.API_VERSION, r = [
267
331
  },
268
332
  id: r.id
269
333
  };
270
- }, f = (e, t) => {
334
+ }, x = (e, t) => {
335
+ let n = `retired price ${e}`, r = t.filter((t) => t.active && t.lookup_key === e);
336
+ return r.length === 0 ? {
337
+ kind: "ok",
338
+ object: n
339
+ } : r.length > 1 ? {
340
+ kind: "manual",
341
+ object: n,
342
+ detail: `${r.length} active prices carry this lookup key`
343
+ } : {
344
+ kind: "update",
345
+ object: n,
346
+ detail: "deactivate",
347
+ apply: {
348
+ method: "POST",
349
+ path: `/v1/prices/${r[0].id}`,
350
+ params: { active: !1 }
351
+ }
352
+ };
353
+ }, S = (e, t) => {
354
+ let n = `retired product ${e}`, r = t.filter((t) => t.active && t.metadata.voltro_key === e);
355
+ return r.length === 0 ? {
356
+ kind: "ok",
357
+ object: n
358
+ } : r.length > 1 ? {
359
+ kind: "manual",
360
+ object: n,
361
+ detail: `${r.length} active products carry this key`
362
+ } : {
363
+ kind: "update",
364
+ object: n,
365
+ detail: "archive",
366
+ apply: {
367
+ method: "POST",
368
+ path: `/v1/products/${r[0].id}`,
369
+ params: { active: !1 }
370
+ }
371
+ };
372
+ }, C = (e, t) => {
373
+ let n = `retired meter ${e}`, r = t.find((t) => t.event_name === e);
374
+ return r === void 0 || r.status !== "active" ? {
375
+ kind: "ok",
376
+ object: n
377
+ } : {
378
+ kind: "update",
379
+ object: n,
380
+ detail: "deactivate",
381
+ apply: {
382
+ method: "POST",
383
+ path: `/v1/billing/meters/${r.id}/deactivate`,
384
+ params: {}
385
+ }
386
+ };
387
+ }, w = (e, t) => {
271
388
  let n = "tax settings", r = t.head_office?.address ?? {}, i = [];
272
389
  for (let [t, n] of Object.entries(e.headOffice)) r[t] !== n && i.push(`head_office.${t}`);
273
390
  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" ? {
@@ -293,7 +410,7 @@ var n = e.API_VERSION, r = [
293
410
  }
294
411
  }
295
412
  };
296
- }, p = (e, t, n) => ({
413
+ }, T = (e, t) => ({
297
414
  business_profile: {
298
415
  headline: e.headline,
299
416
  privacy_policy_url: e.privacyPolicyUrl,
@@ -316,18 +433,10 @@ var n = e.API_VERSION, r = [
316
433
  enabled: !0,
317
434
  mode: "at_period_end"
318
435
  },
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
- }
436
+ subscription_update: { enabled: !1 }
328
437
  },
329
438
  metadata: { voltro_key: e.key }
330
- }), m = (e) => JSON.stringify([...e ?? []].sort()), h = (e, t) => {
439
+ }), E = (e) => JSON.stringify([...e ?? []].sort()), D = (e, t) => {
331
440
  let n = [], r = t.features, i = e.features;
332
441
  t.default_return_url !== e.default_return_url && n.push("default_return_url");
333
442
  let a = e.business_profile;
@@ -338,58 +447,48 @@ var n = e.API_VERSION, r = [
338
447
  "payment_method_update",
339
448
  "subscription_cancel"
340
449
  ]) 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 [{
450
+ return E(r.customer_update?.allowed_updates) !== E(i.customer_update.allowed_updates) && n.push("customer_update.allowed_updates"), r.subscription_cancel?.mode !== "at_period_end" && n.push("subscription_cancel.mode"), r.subscription_update?.enabled !== !1 && n.push("subscription_update.enabled"), n;
451
+ }, O = (e, t, n) => {
452
+ let r = "customer portal", i = T(e, t), a = n.find((t) => t.metadata.voltro_key === e.key) ?? n.find((t) => t.active && t.name === e.name);
453
+ if (a === void 0) return [{
355
454
  kind: "create",
356
- object: i,
455
+ object: r,
357
456
  detail: "self-service configuration",
358
457
  apply: {
359
458
  method: "POST",
360
459
  path: "/v1/billing_portal/configurations",
361
460
  params: {
362
- ...a,
461
+ ...i,
363
462
  name: e.name
364
463
  }
365
464
  }
366
- }, ...r.some((e) => e.is_default) ? [{
465
+ }, ...n.some((e) => e.is_default) ? [{
367
466
  kind: "manual",
368
- object: i,
467
+ object: r,
369
468
  detail: "another configuration is the default — make this one the default in the Dashboard"
370
469
  }] : []];
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",
470
+ let o = [], s = D(i, a), c = a.metadata.voltro_key !== e.key;
471
+ return s.length > 0 || c || !a.active ? o.push({
472
+ kind: c ? "adopt" : "update",
473
+ object: r,
474
+ detail: [...s, ...a.active ? [] : ["active"]].join(", ") || "catalog key",
376
475
  apply: {
377
476
  method: "POST",
378
- path: `/v1/billing_portal/configurations/${o.id}`,
477
+ path: `/v1/billing_portal/configurations/${a.id}`,
379
478
  params: {
380
- ...a,
479
+ ...i,
381
480
  active: !0
382
481
  }
383
482
  }
384
- }) : s.push({
483
+ }) : o.push({
385
484
  kind: "ok",
386
- object: i
387
- }), o.is_default || s.push({
485
+ object: r
486
+ }), a.is_default || o.push({
388
487
  kind: "manual",
389
- object: i,
488
+ object: r,
390
489
  detail: "not the account default — portal sessions would use another configuration"
391
- }), s;
392
- }, _ = (e, r) => {
490
+ }), o;
491
+ }, k = (e, r) => {
393
492
  if (e.webhookUrl === null) return {
394
493
  kind: "ok",
395
494
  object: "webhook endpoint (none: stripe listen forwards locally)"
@@ -435,7 +534,7 @@ var n = e.API_VERSION, r = [
435
534
  kind: "ok",
436
535
  object: i
437
536
  };
438
- }, v = (e) => {
537
+ }, A = (e) => {
439
538
  let t = [];
440
539
  for (let n of e.catalog.tax.registrationCountries) e.activeRegistrations.includes(n) || t.push({
441
540
  kind: "manual",
@@ -460,11 +559,11 @@ var n = e.API_VERSION, r = [
460
559
  object: "account tax id",
461
560
  detail: "not readable with this key — check Dashboard → Settings → Tax ids"
462
561
  });
463
- for (let n of e.catalog.dashboardReminders ?? r) t.push({
562
+ for (let n of e.catalog.dashboardReminders ?? i) t.push({
464
563
  kind: "manual",
465
564
  object: "dashboard",
466
565
  detail: `${n} — not checkable through the API`
467
566
  });
468
567
  return t;
469
568
  };
470
- 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 };
569
+ export { n as STRIPE_API_VERSION, i as STRIPE_DASHBOARD_REMINDERS, b as diffMeter, O as diffPortal, y as diffPrice, c as diffProduct, C as diffRetiredMeter, x as diffRetiredPrice, S as diffRetiredProduct, w as diffTaxSettings, k as diffWebhook, T as portalParams, r as retiredConflicts, A as sellingReadiness };