@voltro/plugin-billing 0.84.0 → 0.86.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/CHANGELOG.md +1215 -0
- package/README.md +7 -0
- package/THIRD-PARTY-NOTICES.md +118 -600
- package/dist/catalog.js +471 -0
- package/dist/dunning.js +209 -0
- package/dist/entitlement.js +41 -0
- package/dist/flushClaim.js +20 -0
- package/dist/index.d.ts +19 -24
- package/dist/index.js +158 -2776
- package/dist/metering.js +74 -0
- package/dist/plans.js +24 -0
- package/dist/providers.js +133 -0
- package/dist/provision.js +153 -0
- package/dist/rpc.d.ts +72 -73
- package/dist/service.js +615 -0
- package/dist/stores.js +425 -0
- package/dist/stripeClient.js +53 -0
- package/dist/stripeProvider.js +352 -0
- package/dist/stripeRest.js +52 -0
- package/dist/table.js +54 -0
- package/dist/types.d.ts +19 -24
- package/dist/types.js +1 -1
- package/dist/webhook.js +45 -0
- package/package.json +7 -7
package/dist/catalog.js
ADDED
|
@@ -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 };
|
package/dist/dunning.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { BillingError as e, DEFAULT_PLAN as t } from "./types.js";
|
|
2
|
+
import { Effect as n } from "effect";
|
|
3
|
+
import { and as r, eq as i, id as a, table as o, text as s, timestamp as c } from "@voltro/database";
|
|
4
|
+
//#region src/dunning.ts
|
|
5
|
+
var l = "_voltro_billing_dunning_notices", u = o(l, {
|
|
6
|
+
id: a({ prefix: "bdun" }),
|
|
7
|
+
tenantId: s().maxLength(191),
|
|
8
|
+
episode: s().maxLength(32),
|
|
9
|
+
stepId: s().maxLength(64),
|
|
10
|
+
sentAt: c()
|
|
11
|
+
}).unique("byBillingDunningNotice", [
|
|
12
|
+
"tenantId",
|
|
13
|
+
"episode",
|
|
14
|
+
"stepId"
|
|
15
|
+
]), d = "locked", f = [
|
|
16
|
+
{
|
|
17
|
+
id: "payment-failed",
|
|
18
|
+
afterHours: 0
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
id: "reminder",
|
|
22
|
+
afterHours: 72
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
id: "final-warning",
|
|
26
|
+
afterHours: 144
|
|
27
|
+
}
|
|
28
|
+
], p = 168, m = "hard", h = (e, t, n) => {
|
|
29
|
+
if (e === void 0 || e.trim() === "") return t;
|
|
30
|
+
let r = Number(e);
|
|
31
|
+
if (!Number.isFinite(r) || r < 0) throw Error(`billingPlugin: ${n} must be a non-negative number, got '${e}'`);
|
|
32
|
+
return r;
|
|
33
|
+
}, g = (e, t) => {
|
|
34
|
+
let n = t.VOLTRO_BILLING_DUNNING;
|
|
35
|
+
if (n !== void 0 && n !== "" && n !== "on" && n !== "off") throw Error(`billingPlugin: VOLTRO_BILLING_DUNNING must be 'on' or 'off', got '${n}'`);
|
|
36
|
+
let r = n === "off" ? !1 : n === "on" ? !0 : e?.enabled ?? !0, i = t.VOLTRO_BILLING_LOCKOUT;
|
|
37
|
+
if (i !== void 0 && i !== "" && i !== "hard" && i !== "soft") throw Error(`billingPlugin: VOLTRO_BILLING_LOCKOUT must be 'hard' or 'soft', got '${i}'`);
|
|
38
|
+
let a = i === "hard" || i === "soft" ? i : e?.lockout ?? "hard", o = h(t.VOLTRO_BILLING_GRACE_HOURS, e?.graceHours ?? 168, "VOLTRO_BILLING_GRACE_HOURS"), s = e?.steps ?? f, c = t.VOLTRO_BILLING_DUNNING_STEP_HOURS, l = s;
|
|
39
|
+
if (c !== void 0 && c.trim() !== "") {
|
|
40
|
+
let e = c.split(",").map((e) => e.trim());
|
|
41
|
+
if (e.length !== s.length) throw Error(`billingPlugin: VOLTRO_BILLING_DUNNING_STEP_HOURS lists ${e.length} value(s) but the sequence declares ${s.length} step(s) (${s.map((e) => e.id).join(", ")})`);
|
|
42
|
+
l = s.map((t, n) => ({
|
|
43
|
+
id: t.id,
|
|
44
|
+
afterHours: h(e[n], t.afterHours, "VOLTRO_BILLING_DUNNING_STEP_HOURS")
|
|
45
|
+
}));
|
|
46
|
+
}
|
|
47
|
+
let u = /* @__PURE__ */ new Set();
|
|
48
|
+
for (let e of l) {
|
|
49
|
+
if (e.id === "locked") throw Error(`billingPlugin: '${d}' is the reserved lockout step id`);
|
|
50
|
+
if (u.has(e.id)) throw Error(`billingPlugin: duplicate dunning step id '${e.id}'`);
|
|
51
|
+
u.add(e.id);
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
enabled: r,
|
|
55
|
+
graceHours: o,
|
|
56
|
+
lockout: a,
|
|
57
|
+
steps: [...l].sort((e, t) => e.afterHours - t.afterHours),
|
|
58
|
+
...e?.notify === void 0 ? {} : { notify: e.notify },
|
|
59
|
+
...e?.resolveRecipient === void 0 ? {} : { resolveRecipient: e.resolveRecipient },
|
|
60
|
+
...e?.portalReturnUrl === void 0 ? {} : { portalReturnUrl: e.portalReturnUrl }
|
|
61
|
+
};
|
|
62
|
+
}, _ = 36e5, v = (e) => String(e.getTime()), y = (e, t) => new Date(e.getTime() + t * _), b = (e, t, n) => {
|
|
63
|
+
let r = (n.getTime() - t.getTime()) / _;
|
|
64
|
+
return e.filter((e) => r >= e.afterHours);
|
|
65
|
+
}, x = (e, n, r, i) => {
|
|
66
|
+
let a = {
|
|
67
|
+
tenantId: e?.tenantId ?? i ?? "",
|
|
68
|
+
lockout: n.lockout
|
|
69
|
+
};
|
|
70
|
+
if (e === null) return {
|
|
71
|
+
...a,
|
|
72
|
+
plan: t,
|
|
73
|
+
billedPlan: t,
|
|
74
|
+
status: "none",
|
|
75
|
+
entitled: !0,
|
|
76
|
+
inGrace: !1,
|
|
77
|
+
graceEndsAt: null,
|
|
78
|
+
lockedSince: null
|
|
79
|
+
};
|
|
80
|
+
if (e.status === "active" || e.status === "trialing") return {
|
|
81
|
+
...a,
|
|
82
|
+
plan: e.plan,
|
|
83
|
+
billedPlan: e.plan,
|
|
84
|
+
status: e.status,
|
|
85
|
+
entitled: !0,
|
|
86
|
+
inGrace: !1,
|
|
87
|
+
graceEndsAt: null,
|
|
88
|
+
lockedSince: null
|
|
89
|
+
};
|
|
90
|
+
if (e.status === "pastDue") {
|
|
91
|
+
if (!n.enabled) return {
|
|
92
|
+
...a,
|
|
93
|
+
plan: t,
|
|
94
|
+
billedPlan: e.plan,
|
|
95
|
+
status: e.status,
|
|
96
|
+
entitled: !0,
|
|
97
|
+
inGrace: !1,
|
|
98
|
+
graceEndsAt: null,
|
|
99
|
+
lockedSince: null
|
|
100
|
+
};
|
|
101
|
+
if (e.pastDueSince === null) return {
|
|
102
|
+
...a,
|
|
103
|
+
plan: e.plan,
|
|
104
|
+
billedPlan: e.plan,
|
|
105
|
+
status: e.status,
|
|
106
|
+
entitled: !0,
|
|
107
|
+
inGrace: !0,
|
|
108
|
+
graceEndsAt: null,
|
|
109
|
+
lockedSince: null
|
|
110
|
+
};
|
|
111
|
+
let i = y(e.pastDueSince, n.graceHours), o = r.getTime() >= i.getTime();
|
|
112
|
+
return {
|
|
113
|
+
...a,
|
|
114
|
+
plan: o && n.lockout === "hard" ? t : e.plan,
|
|
115
|
+
billedPlan: e.plan,
|
|
116
|
+
status: e.status,
|
|
117
|
+
entitled: !o,
|
|
118
|
+
inGrace: !o,
|
|
119
|
+
graceEndsAt: i,
|
|
120
|
+
lockedSince: o ? i : null
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
...a,
|
|
125
|
+
plan: t,
|
|
126
|
+
billedPlan: e.plan,
|
|
127
|
+
status: e.status,
|
|
128
|
+
entitled: !0,
|
|
129
|
+
inGrace: !1,
|
|
130
|
+
graceEndsAt: null,
|
|
131
|
+
lockedSince: null
|
|
132
|
+
};
|
|
133
|
+
}, S = (e) => e.toISOString().slice(0, 10), C = (e) => {
|
|
134
|
+
let t = e.portalUrl, n = t === null ? "<p>Please update your payment method in the billing settings.</p>" : `<p><a href="${t}">Update your payment method</a></p>`, r = t === null ? "Please update your payment method in the billing settings." : `Update your payment method: ${t}`;
|
|
135
|
+
if (e.locked) return {
|
|
136
|
+
subject: "Your subscription has been suspended",
|
|
137
|
+
html: `<p>We were unable to collect payment, and your subscription is now suspended.</p>${n}`,
|
|
138
|
+
text: `We were unable to collect payment, and your subscription is now suspended.\n\n${r}`
|
|
139
|
+
};
|
|
140
|
+
let i = S(e.graceEndsAt), a = e.stepId === "payment-failed" ? "Your last payment could not be collected." : "Your payment is still outstanding.";
|
|
141
|
+
return {
|
|
142
|
+
subject: "Action needed: your payment could not be collected",
|
|
143
|
+
html: `<p>${a} Your subscription stays active until ${i}.</p>${n}`,
|
|
144
|
+
text: `${a} Your subscription stays active until ${i}.\n\n${r}`
|
|
145
|
+
};
|
|
146
|
+
}, w = (e) => (t) => t.to === null ? n.void : e.send({
|
|
147
|
+
to: t.to,
|
|
148
|
+
subject: t.subject,
|
|
149
|
+
html: t.html,
|
|
150
|
+
text: t.text
|
|
151
|
+
}).pipe(n.asVoid), T = (e, t, n) => `${e}\u0000${t}\u0000${n}`, E = () => {
|
|
152
|
+
let e = /* @__PURE__ */ new Map();
|
|
153
|
+
return {
|
|
154
|
+
claim: (t, r, i, a) => n.sync(() => {
|
|
155
|
+
let n = T(t, r, i);
|
|
156
|
+
return !e.has(n) && (e.set(n, {
|
|
157
|
+
tenantId: t,
|
|
158
|
+
episode: r,
|
|
159
|
+
stepId: i,
|
|
160
|
+
sentAt: a
|
|
161
|
+
}), !0);
|
|
162
|
+
}),
|
|
163
|
+
sentSteps: (t, r) => n.sync(() => [...e.values()].filter((e) => e.tenantId === t && e.episode === r))
|
|
164
|
+
};
|
|
165
|
+
}, D = () => `bdun_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`, O = (t) => ({
|
|
166
|
+
claim: (r, i, a, o) => n.tryPromise({
|
|
167
|
+
try: async () => {
|
|
168
|
+
let e = D(), n = await t.insertIgnore(l, {
|
|
169
|
+
id: e,
|
|
170
|
+
tenantId: r,
|
|
171
|
+
episode: i,
|
|
172
|
+
stepId: a,
|
|
173
|
+
sentAt: o
|
|
174
|
+
}, { conflictColumns: [
|
|
175
|
+
"tenantId",
|
|
176
|
+
"episode",
|
|
177
|
+
"stepId"
|
|
178
|
+
] });
|
|
179
|
+
return String(n.id) === e;
|
|
180
|
+
},
|
|
181
|
+
catch: (t) => new e({
|
|
182
|
+
source: "dunningNoticeStore",
|
|
183
|
+
message: String(t?.message ?? t),
|
|
184
|
+
transient: !0
|
|
185
|
+
})
|
|
186
|
+
}),
|
|
187
|
+
sentSteps: (a, o) => n.tryPromise({
|
|
188
|
+
try: async () => (await t.query({
|
|
189
|
+
table: l,
|
|
190
|
+
predicate: r(i("tenantId", a), i("episode", o)),
|
|
191
|
+
order: [],
|
|
192
|
+
take: void 0,
|
|
193
|
+
skip: void 0,
|
|
194
|
+
projection: void 0
|
|
195
|
+
})).map((e) => ({
|
|
196
|
+
tenantId: String(e.tenantId),
|
|
197
|
+
episode: String(e.episode),
|
|
198
|
+
stepId: String(e.stepId),
|
|
199
|
+
sentAt: e.sentAt == null ? null : new Date(e.sentAt)
|
|
200
|
+
})),
|
|
201
|
+
catch: (t) => new e({
|
|
202
|
+
source: "dunningNoticeStore",
|
|
203
|
+
message: String(t?.message ?? t),
|
|
204
|
+
transient: !0
|
|
205
|
+
})
|
|
206
|
+
})
|
|
207
|
+
});
|
|
208
|
+
//#endregion
|
|
209
|
+
export { l as BILLING_DUNNING_NOTICES_TABLE, f as DEFAULT_DUNNING_STEPS, p as DEFAULT_GRACE_HOURS, m as DEFAULT_LOCKOUT, d as DUNNING_LOCK_STEP_ID, u as billingDunningNoticesTable, O as dataStoreDunningNoticeStore, C as defaultDunningEmail, b as dueSteps, w as dunningMailNotifier, v as episodeKeyOf, x as evaluateBillingEntitlement, y as graceEndOf, E as memoryDunningNoticeStore, g as resolveDunningConfig };
|