@ultimabase/stripe 0.0.0-first.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.
Files changed (130) hide show
  1. package/README.md +7 -0
  2. package/dist/accounts.d.ts +9 -0
  3. package/dist/accounts.d.ts.map +1 -0
  4. package/dist/accounts.js +20 -0
  5. package/dist/apply.d.ts +26 -0
  6. package/dist/apply.d.ts.map +1 -0
  7. package/dist/apply.js +73 -0
  8. package/dist/audit.d.ts +3 -0
  9. package/dist/audit.d.ts.map +1 -0
  10. package/dist/audit.js +123 -0
  11. package/dist/billing-gateway.d.ts +158 -0
  12. package/dist/billing-gateway.d.ts.map +1 -0
  13. package/dist/billing-gateway.js +23 -0
  14. package/dist/billing.d.ts +130 -0
  15. package/dist/billing.d.ts.map +1 -0
  16. package/dist/billing.js +128 -0
  17. package/dist/calls.d.ts +61 -0
  18. package/dist/calls.d.ts.map +1 -0
  19. package/dist/calls.js +172 -0
  20. package/dist/catalog.d.ts +54 -0
  21. package/dist/catalog.d.ts.map +1 -0
  22. package/dist/catalog.js +182 -0
  23. package/dist/credit.d.ts +16 -0
  24. package/dist/credit.d.ts.map +1 -0
  25. package/dist/credit.js +18 -0
  26. package/dist/credits.d.ts +38 -0
  27. package/dist/credits.d.ts.map +1 -0
  28. package/dist/credits.js +138 -0
  29. package/dist/customer.d.ts +25 -0
  30. package/dist/customer.d.ts.map +1 -0
  31. package/dist/customer.js +88 -0
  32. package/dist/describe.d.ts +4 -0
  33. package/dist/describe.d.ts.map +1 -0
  34. package/dist/describe.js +56 -0
  35. package/dist/duplicates.d.ts +12 -0
  36. package/dist/duplicates.d.ts.map +1 -0
  37. package/dist/duplicates.js +24 -0
  38. package/dist/fake-billing.d.ts +72 -0
  39. package/dist/fake-billing.d.ts.map +1 -0
  40. package/dist/fake-billing.js +290 -0
  41. package/dist/gateway.d.ts +108 -0
  42. package/dist/gateway.d.ts.map +1 -0
  43. package/dist/gateway.js +196 -0
  44. package/dist/index.d.ts +15 -0
  45. package/dist/index.d.ts.map +1 -0
  46. package/dist/index.js +6 -0
  47. package/dist/job-input.d.ts +6 -0
  48. package/dist/job-input.d.ts.map +1 -0
  49. package/dist/job-input.js +20 -0
  50. package/dist/jobs.d.ts +16 -0
  51. package/dist/jobs.d.ts.map +1 -0
  52. package/dist/jobs.js +189 -0
  53. package/dist/keys.d.ts +10 -0
  54. package/dist/keys.d.ts.map +1 -0
  55. package/dist/keys.js +30 -0
  56. package/dist/kinds.d.ts +11 -0
  57. package/dist/kinds.d.ts.map +1 -0
  58. package/dist/kinds.js +4 -0
  59. package/dist/models.d.ts +71 -0
  60. package/dist/models.d.ts.map +1 -0
  61. package/dist/models.js +41 -0
  62. package/dist/plugin.d.ts +41 -0
  63. package/dist/plugin.d.ts.map +1 -0
  64. package/dist/plugin.js +229 -0
  65. package/dist/portal.d.ts +23 -0
  66. package/dist/portal.d.ts.map +1 -0
  67. package/dist/portal.js +66 -0
  68. package/dist/product.d.ts +57 -0
  69. package/dist/product.d.ts.map +1 -0
  70. package/dist/product.js +113 -0
  71. package/dist/running.d.ts +61 -0
  72. package/dist/running.d.ts.map +1 -0
  73. package/dist/running.js +35 -0
  74. package/dist/sdk-billing.d.ts +8 -0
  75. package/dist/sdk-billing.d.ts.map +1 -0
  76. package/dist/sdk-billing.js +200 -0
  77. package/dist/sdk-common.d.ts +13 -0
  78. package/dist/sdk-common.d.ts.map +1 -0
  79. package/dist/sdk-common.js +41 -0
  80. package/dist/sdk-customer.d.ts +9 -0
  81. package/dist/sdk-customer.d.ts.map +1 -0
  82. package/dist/sdk-customer.js +149 -0
  83. package/dist/sdk-invoices.d.ts +10 -0
  84. package/dist/sdk-invoices.d.ts.map +1 -0
  85. package/dist/sdk-invoices.js +53 -0
  86. package/dist/sdk-recovery.d.ts +5 -0
  87. package/dist/sdk-recovery.d.ts.map +1 -0
  88. package/dist/sdk-recovery.js +81 -0
  89. package/dist/sync.d.ts +8 -0
  90. package/dist/sync.d.ts.map +1 -0
  91. package/dist/sync.js +196 -0
  92. package/dist/testing.d.ts +24 -0
  93. package/dist/testing.d.ts.map +1 -0
  94. package/dist/testing.js +100 -0
  95. package/dist/webhook.d.ts +4 -0
  96. package/dist/webhook.d.ts.map +1 -0
  97. package/dist/webhook.js +73 -0
  98. package/package.json +51 -0
  99. package/src/accounts.ts +30 -0
  100. package/src/apply.ts +99 -0
  101. package/src/audit.ts +171 -0
  102. package/src/billing-gateway.ts +188 -0
  103. package/src/billing.ts +361 -0
  104. package/src/calls.ts +280 -0
  105. package/src/catalog.ts +327 -0
  106. package/src/credit.ts +36 -0
  107. package/src/credits.ts +206 -0
  108. package/src/customer.ts +113 -0
  109. package/src/describe.ts +76 -0
  110. package/src/duplicates.ts +45 -0
  111. package/src/fake-billing.ts +388 -0
  112. package/src/gateway.ts +333 -0
  113. package/src/index.ts +35 -0
  114. package/src/job-input.ts +24 -0
  115. package/src/jobs.ts +230 -0
  116. package/src/keys.ts +40 -0
  117. package/src/kinds.ts +15 -0
  118. package/src/models.ts +107 -0
  119. package/src/plugin.ts +307 -0
  120. package/src/portal.ts +95 -0
  121. package/src/product.ts +203 -0
  122. package/src/running.ts +104 -0
  123. package/src/sdk-billing.ts +220 -0
  124. package/src/sdk-common.ts +57 -0
  125. package/src/sdk-customer.ts +203 -0
  126. package/src/sdk-invoices.ts +81 -0
  127. package/src/sdk-recovery.ts +93 -0
  128. package/src/sync.ts +246 -0
  129. package/src/testing.ts +135 -0
  130. package/src/webhook.ts +100 -0
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @ultimabase/stripe
2
+
3
+ Stripe for UltimaBase. The application declares what it sells in code, subscriptions, products paid once and the credits they give, and its deploy script brings the Stripe account and its customer portal to that catalog, printing the plan first. `stripePlugin` sells it on Stripe's Checkout and customer portal, and Stripe's events keep the application's subscriptions, purchases, credit balances and their history in step; an hourly job reads Stripe's events to catch one the webhook missed, and a daily job logs a balance that is not the sum of its history or a payment left with no purchase. `stripe.grant` gives credits without a payment and `stripe.spend` spends them.
4
+
5
+ In this repository, `yarn test:stripe` runs the package against a Stripe sandbox, given `STRIPE_SECRET_KEY=sk_test_…` and the Stripe CLI (`brew install stripe/stripe-cli/stripe`), which forwards Stripe's own signed events to the webhook; CI runs it each night and before `main` publishes (D-378).
6
+
7
+ Documentation: [Stripe](https://ultimabase.dev/docs/stripe).
@@ -0,0 +1,9 @@
1
+ import type { BillingGateway, RecoveryGateway } from './billing-gateway.ts';
2
+ import { type StripeGateway } from './gateway.ts';
3
+ /** Everything the plugin asks a Stripe account. */
4
+ export type Gateway = StripeGateway & BillingGateway & RecoveryGateway;
5
+ /** Makes `secret` reach `gateway` instead of Stripe. */
6
+ export declare const addFake: (secret: string, gateway: Gateway) => void;
7
+ /** The gateway of `secret`: a fake's, or Stripe's API. */
8
+ export declare const gatewayOf: (secret: string) => Gateway;
9
+ //# sourceMappingURL=accounts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"accounts.d.ts","sourceRoot":"","sources":["../src/accounts.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAG3E,OAAO,EAAE,KAAK,aAAa,EAA4B,MAAM,cAAc,CAAA;AAE3E,mDAAmD;AACnD,MAAM,MAAM,OAAO,GAAG,aAAa,GAAG,cAAc,GAAG,eAAe,CAAA;AAItE,wDAAwD;AACxD,eAAO,MAAM,OAAO,WAAY,MAAM,WAAW,OAAO,KAAG,IAE1D,CAAA;AAED,0DAA0D;AAC1D,eAAO,MAAM,SAAS,WAAY,MAAM,KAAG,OAS1C,CAAA"}
@@ -0,0 +1,20 @@
1
+ import { sdkBilling } from './sdk-billing.js';
2
+ import { sdkRecovery } from './sdk-recovery.js';
3
+ import { sdkGateway, stripeClient } from './gateway.js';
4
+ const fakes = new Map();
5
+ /** Makes `secret` reach `gateway` instead of Stripe. */
6
+ export const addFake = (secret, gateway) => {
7
+ fakes.set(secret, gateway);
8
+ };
9
+ /** The gateway of `secret`: a fake's, or Stripe's API. */
10
+ export const gatewayOf = (secret) => {
11
+ const fake = fakes.get(secret);
12
+ if (fake !== undefined)
13
+ return fake;
14
+ const client = stripeClient(secret);
15
+ return {
16
+ ...sdkGateway(client),
17
+ ...sdkBilling(client),
18
+ ...sdkRecovery(client),
19
+ };
20
+ };
@@ -0,0 +1,26 @@
1
+ import { type Catalog } from './catalog.ts';
2
+ import { type PortalOptions } from './portal.ts';
3
+ import type { Product } from './product.ts';
4
+ /** What `applyStripeCatalog` brings a Stripe account to. */
5
+ export interface StripeCatalogOptions {
6
+ /** The account's secret key; `undefined` is refused, as an unset variable. */
7
+ readonly secret: string | undefined;
8
+ /** Every price's currency, in lowercase: `eur`. */
9
+ readonly currency: string;
10
+ /** `'inclusive'`, by default, when the amounts include the taxes. */
11
+ readonly taxBehavior?: 'inclusive' | 'exclusive';
12
+ readonly products: readonly Product[];
13
+ readonly portal: PortalOptions;
14
+ /** Prints the plan and changes nothing. */
15
+ readonly dryRun?: boolean;
16
+ }
17
+ type CatalogOptions = Pick<StripeCatalogOptions, 'currency' | 'taxBehavior' | 'products' | 'portal'>;
18
+ /**
19
+ * The catalog of `options`, shared with `stripePlugin`; refuses what Stripe
20
+ * would refuse, and a product named twice (D-372 point 5).
21
+ */
22
+ export declare function catalogOf(caller: string, options: CatalogOptions): Catalog;
23
+ /** Brings the Stripe account of `secret` to the catalog, unless `dryRun`. */
24
+ export declare function applyStripeCatalog(options: StripeCatalogOptions): Promise<void>;
25
+ export {};
26
+ //# sourceMappingURL=apply.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"apply.d.ts","sourceRoot":"","sources":["../src/apply.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,OAAO,EAA6B,MAAM,cAAc,CAAA;AAGtE,OAAO,EAAE,KAAK,aAAa,EAAe,MAAM,aAAa,CAAA;AAC7D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAE3C,4DAA4D;AAC5D,MAAM,WAAW,oBAAoB;IACpC,8EAA8E;IAC9E,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;IACnC,mDAAmD;IACnD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB,qEAAqE;IACrE,QAAQ,CAAC,WAAW,CAAC,EAAE,WAAW,GAAG,WAAW,CAAA;IAChD,QAAQ,CAAC,QAAQ,EAAE,SAAS,OAAO,EAAE,CAAA;IACrC,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAA;IAC9B,2CAA2C;IAC3C,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CACzB;AAgBD,KAAK,cAAc,GAAG,IAAI,CACzB,oBAAoB,EACpB,UAAU,GAAG,aAAa,GAAG,UAAU,GAAG,QAAQ,CAClD,CAAA;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAsB1E;AAED,6EAA6E;AAC7E,wBAAsB,kBAAkB,CACvC,OAAO,EAAE,oBAAoB,GAC3B,OAAO,CAAC,IAAI,CAAC,CAuBf"}
package/dist/apply.js ADDED
@@ -0,0 +1,73 @@
1
+ // `applyStripeCatalog` (D-372 point 4): an application's deploy script
2
+ // brings its Stripe account to the catalog, with no running application.
3
+ // It prints the plan, a line a change, and applies it unless `dryRun`.
4
+ import { Refused } from 'ultimabase';
5
+ import { gatewayOf } from './accounts.js';
6
+ import { applyCatalog, planCatalog } from './catalog.js';
7
+ import { describeStep } from './describe.js';
8
+ import { ownKeys } from './keys.js';
9
+ import { checkPortal } from './portal.js';
10
+ const entry = 'applyStripeCatalog';
11
+ const optionKeys = [
12
+ 'secret',
13
+ 'currency',
14
+ 'taxBehavior',
15
+ 'products',
16
+ 'portal',
17
+ 'dryRun',
18
+ ];
19
+ const print = (line) => {
20
+ process.stdout.write(`${line}\n`);
21
+ };
22
+ /**
23
+ * The catalog of `options`, shared with `stripePlugin`; refuses what Stripe
24
+ * would refuse, and a product named twice (D-372 point 5).
25
+ */
26
+ export function catalogOf(caller, options) {
27
+ const { currency, taxBehavior = 'inclusive', products, portal } = options;
28
+ if (typeof currency !== 'string' || !/^[a-z]{3}$/u.test(currency)) {
29
+ throw new Refused(`${caller}: currency is a lowercase ISO code, as eur.`);
30
+ }
31
+ if (!['inclusive', 'exclusive'].includes(taxBehavior)) {
32
+ throw new Refused(`${caller}: taxBehavior is 'inclusive' or 'exclusive'.`);
33
+ }
34
+ // `Array.isArray` would type the products `any[]`.
35
+ const listed = products;
36
+ if (!Array.isArray(listed)) {
37
+ throw new Refused(`${caller}: products lists what defineProduct returns.`);
38
+ }
39
+ const keys = new Set();
40
+ for (const { key } of products) {
41
+ if (keys.has(key)) {
42
+ throw new Refused(`${caller}: two products have the key ${key}.`);
43
+ }
44
+ keys.add(key);
45
+ }
46
+ checkPortal(portal);
47
+ return { currency, taxBehavior, products, portal };
48
+ }
49
+ /** Brings the Stripe account of `secret` to the catalog, unless `dryRun`. */
50
+ export async function applyStripeCatalog(options) {
51
+ ownKeys(entry, options, optionKeys);
52
+ const { secret, dryRun = false } = options;
53
+ if (typeof secret !== 'string' || secret === '') {
54
+ throw new Refused(`${entry}: secret is the account's secret key, sk_….`);
55
+ }
56
+ const catalog = catalogOf(entry, options);
57
+ const { currency } = catalog;
58
+ const gateway = gatewayOf(secret);
59
+ const state = await gateway.readCatalog();
60
+ const steps = planCatalog(catalog, state);
61
+ const mode = secret.includes('_live_') ? 'live' : 'test';
62
+ if (steps.length === 0) {
63
+ print(`Stripe catalog (${mode} mode): up to date.`);
64
+ return;
65
+ }
66
+ print(`Stripe catalog (${mode} mode): ${String(steps.length)} change${steps.length === 1 ? '' : 's'}${dryRun ? ', dry run' : ''}:`);
67
+ for (const step of steps)
68
+ print(` ${describeStep(step, currency)}`);
69
+ if (dryRun)
70
+ return;
71
+ await applyCatalog(catalog, gateway, state, steps);
72
+ print('Stripe catalog: applied.');
73
+ }
@@ -0,0 +1,3 @@
1
+ import type { RecurringJob } from '@ultimabase/jobs';
2
+ export declare const auditJob: RecurringJob<'stripe.audit'>;
3
+ //# sourceMappingURL=audit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"audit.d.ts","sourceRoot":"","sources":["../src/audit.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AAoKpD,eAAO,MAAM,QAAQ,EAAE,YAAY,CAAC,cAAc,CAIhD,CAAA"}
package/dist/audit.js ADDED
@@ -0,0 +1,123 @@
1
+ import { defineJob } from '@ultimabase/jobs';
2
+ import { transaction } from 'ultimabase';
3
+ import { M, StripeCustomer } from './models.js';
4
+ import { runningOf } from './running.js';
5
+ const hour = 60 * 60 * 1000;
6
+ /** The owners, or the customers, read at once. */
7
+ const page = 500;
8
+ /** The oldest payment read: two runs overlap, so a skipped one loses none. */
9
+ const oldest = 48 * hour;
10
+ /** The newest payment read: the hourly reconciliation heals it first. */
11
+ const newest = 2 * hour;
12
+ const idOf = (value) => {
13
+ const id = typeof value === 'object' && value !== null
14
+ ? Reflect.get(value, 'id')
15
+ : undefined;
16
+ return typeof id === 'string' ? id : undefined;
17
+ };
18
+ const numberOf = (value) => typeof value === 'number' ? value : 0;
19
+ /** Each owner's sums, by `owner credit`, from rows or groups. */
20
+ function sums(rows, read) {
21
+ const found = new Map();
22
+ for (const row of rows) {
23
+ const owner = idOf(row.owner);
24
+ const { credit } = row;
25
+ if (owner === undefined || typeof credit !== 'string')
26
+ continue;
27
+ found.set(`${owner} ${credit}`, { owner, credit, sum: read(row) });
28
+ }
29
+ return found;
30
+ }
31
+ /** Logs each balance of these owners that is not the sum of its history. */
32
+ async function auditOwners(balance, history, ids, logger) {
33
+ const where = { owner: { is: { id: { in: ids } } } };
34
+ const [balances, moves] = await transaction(async () => [
35
+ await balance.queries.findMany({
36
+ where,
37
+ select: {
38
+ owner: { select: { id: true } },
39
+ credit: true,
40
+ amount: true,
41
+ },
42
+ }, M),
43
+ await history.queries.groupBy({ by: ['owner', 'credit'], where, _sum: { change: true } }, M),
44
+ ], { isolation: 'serializable' });
45
+ const held = sums(balances, (row) => numberOf(row.amount));
46
+ const summed = sums(moves, (row) => numberOf(Reflect.get(Object(row['_sum']), 'change')));
47
+ for (const key of new Set([...held.keys(), ...summed.keys()])) {
48
+ const one = held.get(key) ?? summed.get(key);
49
+ const amount = held.get(key)?.sum ?? 0;
50
+ const sum = summed.get(key)?.sum ?? 0;
51
+ if (one === undefined || amount === sum)
52
+ continue;
53
+ const { owner, credit } = one;
54
+ logger.error({ owner, credit, balance: amount, history: sum }, 'A balance is not the sum of its history.');
55
+ }
56
+ }
57
+ /** Each balance is the sum of its history (D-379 point 2). */
58
+ async function auditBalances(running, logger) {
59
+ const { balance, history } = running;
60
+ if (balance === undefined || history === undefined)
61
+ return;
62
+ let cursor;
63
+ for (;;) {
64
+ const owners = await balance.payer.queries.findMany({
65
+ select: { id: true },
66
+ orderBy: { id: 'asc' },
67
+ take: page,
68
+ ...(cursor === undefined ? {} : { cursor: { id: cursor }, skip: 1 }),
69
+ }, M);
70
+ const ids = owners.map(({ id }) => id);
71
+ if (ids.length > 0)
72
+ await auditOwners(balance, history, ids, logger);
73
+ cursor = ids.at(-1);
74
+ if (ids.length < page)
75
+ return;
76
+ }
77
+ }
78
+ /** Each payment Stripe took has its purchase (D-379 point 3). */
79
+ async function auditPayments(gateway, purchase, running, logger) {
80
+ const now = Date.now();
81
+ const changed = await gateway.changedSince(new Date(now - oldest));
82
+ const ids = [...changed.keys()];
83
+ for (let at = 0; at < ids.length; at += page) {
84
+ const stripeId = { in: ids.slice(at, at + page) };
85
+ const where = { stripeId, model: purchase.payer.name };
86
+ for (const row of await StripeCustomer.findMany({ where }, M)) {
87
+ if (row.stripeId === null)
88
+ continue;
89
+ const { payments } = await gateway.readCustomer(row.stripeId);
90
+ const paid = payments.filter(({ product, paidAt }) => {
91
+ const age = now - paidAt.getTime();
92
+ return (product !== null &&
93
+ running.products.has(product) &&
94
+ age >= newest &&
95
+ age <= oldest);
96
+ });
97
+ if (paid.length === 0)
98
+ continue;
99
+ const kept = await purchase.queries.findMany({ where: { stripeId: { in: paid.map(({ id }) => id) } } }, M);
100
+ const known = new Set(kept.map((one) => one.stripeId));
101
+ for (const { id, customerId, paidAt } of paid) {
102
+ if (known.has(id))
103
+ continue;
104
+ logger.error({ payment: id, customer: customerId, paidAt: paidAt.toISOString() }, 'A paid payment has no purchase.');
105
+ }
106
+ }
107
+ }
108
+ }
109
+ /** Every day: the ledger's balances, then Stripe's payments (D-379). */
110
+ async function audit(logger) {
111
+ const running = runningOf('stripe.audit');
112
+ await auditBalances(running, logger);
113
+ // Last, so that an unreachable Stripe leaves the ledger audited.
114
+ const { stripe, purchase } = running;
115
+ if (stripe !== undefined && purchase !== undefined) {
116
+ await auditPayments(stripe.gateway, purchase, running, logger);
117
+ }
118
+ }
119
+ export const auditJob = defineJob({
120
+ name: 'stripe.audit',
121
+ every: 24 * hour,
122
+ run: ({ logger }) => audit(logger),
123
+ });
@@ -0,0 +1,158 @@
1
+ import { PublicError } from 'ultimabase';
2
+ /** What Stripe shows of a payer (D-371). */
3
+ export interface CustomerProfile {
4
+ readonly email?: string | null;
5
+ readonly name?: string | null;
6
+ /** Checkout's and the portal's language: `fr`. */
7
+ readonly locale?: string | null;
8
+ }
9
+ /** A product key and a price name, from a price's metadata. */
10
+ export interface PriceKeys {
11
+ readonly product: string | null;
12
+ readonly price: string | null;
13
+ }
14
+ /** A subscription, as the plugin reads it. */
15
+ export interface StripeSubscription extends PriceKeys {
16
+ readonly id: string;
17
+ readonly customerId: string;
18
+ readonly status: string;
19
+ readonly quantity: number;
20
+ /** One period's price times the quantity, before discounts and taxes. */
21
+ readonly amount: number;
22
+ readonly every: string;
23
+ readonly everyCount: number;
24
+ readonly currentPeriodEnd: Date;
25
+ readonly cancelAt: Date | null;
26
+ readonly canceledAt: Date | null;
27
+ readonly trialEnd: Date | null;
28
+ readonly startedAt: Date;
29
+ /** A change waiting for the period's end. */
30
+ readonly scheduled: (PriceKeys & {
31
+ readonly at: Date;
32
+ }) | null;
33
+ /** The card its Checkout left on it, moved to its customer (D-380). */
34
+ readonly card: string | null;
35
+ }
36
+ /** Stripe's statuses of a subscription that still gives access. */
37
+ export declare const liveStatuses: readonly string[];
38
+ /** A one-time payment of a catalog product. */
39
+ export interface StripePayment {
40
+ readonly id: string;
41
+ readonly customerId: string;
42
+ readonly product: string | null;
43
+ /** How many of the product were paid: what it gives, so many times. */
44
+ readonly quantity: number;
45
+ readonly amount: number;
46
+ readonly refunded: number;
47
+ /** Whether a dispute of it was lost. */
48
+ readonly disputeLost: boolean;
49
+ readonly paidAt: Date;
50
+ readonly receiptUrl: string | null;
51
+ }
52
+ /** A paid invoice of a subscription (D-377 point 4). */
53
+ export interface StripeInvoice {
54
+ readonly id: string;
55
+ readonly customerId: string;
56
+ /** Its lines that bill a whole period above zero: no proration, no trial. */
57
+ readonly periods: readonly (PriceKeys & {
58
+ readonly quantity: number;
59
+ /** When the period it bills ends. */
60
+ readonly end: Date;
61
+ })[];
62
+ readonly amount: number;
63
+ readonly refunded: number;
64
+ readonly disputeLost: boolean;
65
+ }
66
+ /** What a customer holds. */
67
+ export interface CustomerState {
68
+ /** Deleted, or never found. */
69
+ readonly deleted: boolean;
70
+ readonly subscriptions: readonly StripeSubscription[];
71
+ readonly payments: readonly StripePayment[];
72
+ readonly invoices: readonly StripeInvoice[];
73
+ }
74
+ /** A Checkout the plugin opens. */
75
+ export interface CheckoutWrite {
76
+ readonly customerId: string;
77
+ /** Its `client_reference_id`: the payer's id. */
78
+ readonly reference: string;
79
+ readonly mode: 'subscription' | 'payment';
80
+ readonly product: string;
81
+ readonly lookupKey: string;
82
+ readonly quantity: number;
83
+ readonly trialDays: number | null;
84
+ readonly terms: boolean;
85
+ readonly promotionCodes: boolean;
86
+ readonly successUrl: string;
87
+ readonly cancelUrl: string;
88
+ readonly metadata: Readonly<Record<string, string>>;
89
+ }
90
+ /** The portal's page a session opens on. */
91
+ export type PortalFlow = {
92
+ readonly kind: 'home';
93
+ } | {
94
+ readonly kind: 'change';
95
+ readonly subscriptionId: string;
96
+ readonly lookupKey: string;
97
+ } | {
98
+ readonly kind: 'cancel';
99
+ readonly subscriptionId: string;
100
+ };
101
+ /** A portal session the plugin opens. */
102
+ export interface PortalWrite {
103
+ readonly customerId: string;
104
+ /** The managed configuration, or `null` for the account's default. */
105
+ readonly configuration: string | null;
106
+ readonly returnUrl: string;
107
+ readonly flow: PortalFlow;
108
+ }
109
+ /** What the plugin asks Stripe of its payers. */
110
+ export interface BillingGateway {
111
+ /** Creates a customer; the same key gives the same one. */
112
+ readonly createCustomer: (profile: CustomerProfile, idempotencyKey: string) => Promise<string>;
113
+ readonly updateCustomer: (id: string, profile: CustomerProfile) => Promise<void>;
114
+ /** Deletes a customer, which ends its subscriptions; a missing one is gone. */
115
+ readonly deleteCustomer: (id: string) => Promise<void>;
116
+ /**
117
+ * What a customer holds; if asked, of the last 400 days, its paid
118
+ * invoices and its Checkouts a full discount made free.
119
+ */
120
+ readonly readCustomer: (id: string, options?: {
121
+ readonly invoices?: boolean;
122
+ readonly free?: boolean;
123
+ }) => Promise<CustomerState>;
124
+ /** The customer an event's object belongs to, if any. */
125
+ readonly customerOf: (type: string, id: string) => Promise<string | null>;
126
+ /** Opens a Checkout; returns its URL. */
127
+ readonly openCheckout: (checkout: CheckoutWrite) => Promise<string>;
128
+ /** Expires the customer's open Checkouts. */
129
+ readonly expireCheckouts: (customerId: string) => Promise<void>;
130
+ /** A Checkout's customer, or `null` for an unknown id. */
131
+ readonly checkoutCustomer: (id: string) => Promise<string | null>;
132
+ /** Opens a portal session; returns its URL. */
133
+ readonly openPortal: (portal: PortalWrite) => Promise<string>;
134
+ /** Makes a subscription's card its customer's, then clears its own (D-380). */
135
+ readonly moveCard: (one: Pick<StripeSubscription, 'id' | 'customerId'> & {
136
+ card: string;
137
+ }) => Promise<void>;
138
+ }
139
+ /** What the plugin asks Stripe to set right what went wrong (D-375). */
140
+ export interface RecoveryGateway {
141
+ /**
142
+ * Ends a subscription at once, unless it ended, and refunds its latest
143
+ * payment; done again, it changes nothing.
144
+ */
145
+ readonly cancelAndRefund: (id: string) => Promise<void>;
146
+ /**
147
+ * The customers whose objects Stripe's events changed after `since`, with
148
+ * the time of each one's last event.
149
+ */
150
+ readonly changedSince: (since: Date) => Promise<ReadonlyMap<string, Date>>;
151
+ }
152
+ /** An object Stripe does not hold, or no longer does. */
153
+ export declare class Missing extends Error {
154
+ constructor(id: string);
155
+ }
156
+ /** `PAYMENT_UNAVAILABLE` (D-371): Stripe cannot be reached, or no key is set. */
157
+ export declare const unavailable: (cause?: unknown) => PublicError;
158
+ //# sourceMappingURL=billing-gateway.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"billing-gateway.d.ts","sourceRoot":"","sources":["../src/billing-gateway.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAExC,4CAA4C;AAC5C,MAAM,WAAW,eAAe;IAC/B,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC9B,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC7B,kDAAkD;IAClD,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAC/B;AAED,+DAA+D;AAC/D,MAAM,WAAW,SAAS;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;IAC/B,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;CAC7B;AAED,8CAA8C;AAC9C,MAAM,WAAW,kBAAmB,SAAQ,SAAS;IACpD,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAA;IACnB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;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DAAyD;AACzD,qBAAa,OAAQ,SAAQ,KAAK;IACjC,YAAY,EAAE,EAAE,MAAM,EAGrB;CACD;AAED,iFAAiF;AACjF,eAAO,MAAM,WAAW,WAAY,OAAO,KAAG,WAG3C,CAAA"}
@@ -0,0 +1,23 @@
1
+ // What the plugin asks Stripe of its payers: customers, Checkouts, the
2
+ // portal, and what a customer holds. `sdkBilling` reaches Stripe; the fake
3
+ // of `@ultimabase/stripe/testing` answers in memory.
4
+ import { PublicError } from 'ultimabase';
5
+ /** Stripe's statuses of a subscription that still gives access. */
6
+ export const liveStatuses = [
7
+ 'active',
8
+ 'trialing',
9
+ 'past_due',
10
+ 'unpaid',
11
+ 'paused',
12
+ ];
13
+ /** An object Stripe does not hold, or no longer does. */
14
+ export class Missing extends Error {
15
+ constructor(id) {
16
+ super(`Stripe holds no ${id}.`);
17
+ this.name = 'Missing';
18
+ }
19
+ }
20
+ /** `PAYMENT_UNAVAILABLE` (D-371): Stripe cannot be reached, or no key is set. */
21
+ export const unavailable = (cause) => new PublicError('PAYMENT_UNAVAILABLE', 'Payments are unavailable.', {
22
+ cause,
23
+ });
@@ -0,0 +1,130 @@
1
+ import type { Field, FieldShapes, IndexShape, ModelFields, ModelIndexes, ModelOf, ModelUse, One, PluginTypes, ScalarOptions } from 'ultimabase';
2
+ import type { Stripe } from './plugin.ts';
3
+ import type { Every } from './product.ts';
4
+ type Uses = readonly ModelUse<string, PluginTypes>[];
5
+ /** A billing model's `use`: the plugin first, then the developer's. */
6
+ export type WithStripe<U extends Uses> = readonly [
7
+ ModelUse<'stripe', Stripe>,
8
+ ...U
9
+ ];
10
+ type Text = Field<string, ScalarOptions<string>>;
11
+ type Int = Field<number, {
12
+ readonly integer: true;
13
+ }>;
14
+ type Moment = Field<Date, ScalarOptions<Date>>;
15
+ type Optional<V> = Field<V | null, {
16
+ readonly optional: true;
17
+ }>;
18
+ type StripeId = Field<string, {
19
+ readonly unique: true;
20
+ }>;
21
+ type PayerOf<P> = One<P, {
22
+ readonly onDelete: 'cascade';
23
+ }>;
24
+ type OneOf<V extends string> = Field<V, ScalarOptions<string>>;
25
+ /** Stripe's statuses of a subscription. */
26
+ export type SubscriptionStatus = 'incomplete' | 'incomplete_expired' | 'trialing' | 'active' | 'past_due' | 'canceled' | 'unpaid' | 'paused';
27
+ /** The fields `defineSubscription` adds; `P` is the payer's thunk. */
28
+ export type SubscriptionFields<P> = {
29
+ readonly payer: PayerOf<P>;
30
+ readonly stripeId: StripeId;
31
+ /** The product's key and the price's name (D-372). */
32
+ readonly product: Text;
33
+ readonly price: Text;
34
+ readonly status: OneOf<SubscriptionStatus>;
35
+ readonly quantity: Int;
36
+ /**
37
+ * What one period costs, times the quantity, before discounts and taxes,
38
+ * in the currency's smallest unit: the price the payer subscribed at.
39
+ */
40
+ readonly amount: Int;
41
+ /** The price's interval: every `everyCount` `every`. */
42
+ readonly every: OneOf<Every>;
43
+ readonly everyCount: Int;
44
+ readonly currentPeriodEnd: Moment;
45
+ readonly cancelAt: Optional<Date>;
46
+ readonly canceledAt: Optional<Date>;
47
+ readonly trialEnd: Optional<Date>;
48
+ readonly startedAt: Moment;
49
+ /** A change that waits for the period's end (D-373 point 8). */
50
+ readonly scheduledProduct: Optional<string>;
51
+ readonly scheduledPrice: Optional<string>;
52
+ readonly scheduledAt: Optional<Date>;
53
+ };
54
+ /** The fields `definePurchase` adds. */
55
+ export type PurchaseFields<P> = {
56
+ readonly payer: PayerOf<P>;
57
+ readonly stripeId: StripeId;
58
+ readonly product: Text;
59
+ readonly quantity: Int;
60
+ /** In the currency's smallest unit. */
61
+ readonly amount: Int;
62
+ readonly refunded: Int;
63
+ readonly status: OneOf<'paid' | 'refunded'>;
64
+ readonly paidAt: Moment;
65
+ /** Stripe's receipt page. */
66
+ readonly receiptUrl: Optional<string>;
67
+ };
68
+ /** The fields `defineBalance` adds. */
69
+ export type BalanceFields<P> = {
70
+ readonly owner: PayerOf<P>;
71
+ /** The credit's key (D-372). */
72
+ readonly credit: Text;
73
+ /** Never below zero. */
74
+ readonly amount: Int;
75
+ };
76
+ /** The fields `defineCreditHistory` adds: one move of credits (D-377). */
77
+ export type CreditHistoryFields<P> = {
78
+ readonly owner: PayerOf<P>;
79
+ readonly credit: Text;
80
+ /** Positive when given, negative when spent or taken back. */
81
+ readonly change: Int;
82
+ /** `purchase`, `subscription`, `refund`, or the application's own. */
83
+ readonly reason: Text;
84
+ /** The Stripe payment or invoice that moved it. */
85
+ readonly payment: Optional<string>;
86
+ };
87
+ type Definition<N, F, U extends Uses, X, A> = {
88
+ readonly name: N;
89
+ /** The stored table's or collection's name (D-63). */
90
+ readonly dbName?: string;
91
+ readonly use?: U;
92
+ readonly indexes?: ModelIndexes<X, F & A, WithStripe<U>>;
93
+ };
94
+ type Own<F, U extends Uses> = {
95
+ readonly fields: ModelFields<F, WithStripe<U>>;
96
+ };
97
+ /** The application's subscriptions, with its own fields (D-373). */
98
+ export declare function defineSubscription<const N extends string, const P extends () => object, const F extends FieldShapes, const U extends Uses = [], const X extends readonly IndexShape[] = []>(definition: Definition<N, F, U, X, SubscriptionFields<P>> & Own<F, U> & {
99
+ readonly payer: P;
100
+ }): ModelOf<N, F & SubscriptionFields<P>, WithStripe<U>, X>;
101
+ /** The application's subscriptions (D-373). */
102
+ export declare function defineSubscription<const N extends string, const P extends () => object, const U extends Uses = [], const X extends readonly IndexShape[] = []>(definition: Definition<N, {}, U, X, SubscriptionFields<P>> & {
103
+ readonly payer: P;
104
+ }): ModelOf<N, SubscriptionFields<P>, WithStripe<U>, X>;
105
+ /** The application's one-time purchases, with its own fields (D-373). */
106
+ export declare function definePurchase<const N extends string, const P extends () => object, const F extends FieldShapes, const U extends Uses = [], const X extends readonly IndexShape[] = []>(definition: Definition<N, F, U, X, PurchaseFields<P>> & Own<F, U> & {
107
+ readonly payer: P;
108
+ }): ModelOf<N, F & PurchaseFields<P>, WithStripe<U>, X>;
109
+ /** The application's one-time purchases (D-373). */
110
+ export declare function definePurchase<const N extends string, const P extends () => object, const U extends Uses = [], const X extends readonly IndexShape[] = []>(definition: Definition<N, {}, U, X, PurchaseFields<P>> & {
111
+ readonly payer: P;
112
+ }): ModelOf<N, PurchaseFields<P>, WithStripe<U>, X>;
113
+ /** The application's credit balances, with its own fields (D-374). */
114
+ export declare function defineBalance<const N extends string, const P extends () => object, const F extends FieldShapes, const U extends Uses = [], const X extends readonly IndexShape[] = []>(definition: Definition<N, F, U, X, BalanceFields<P>> & Own<F, U> & {
115
+ readonly owner: P;
116
+ }): ModelOf<N, F & BalanceFields<P>, WithStripe<U>, X>;
117
+ /** The application's credit balances (D-374). */
118
+ export declare function defineBalance<const N extends string, const P extends () => object, const U extends Uses = [], const X extends readonly IndexShape[] = []>(definition: Definition<N, {}, U, X, BalanceFields<P>> & {
119
+ readonly owner: P;
120
+ }): ModelOf<N, BalanceFields<P>, WithStripe<U>, X>;
121
+ /** The application's history of credits, with its own fields (D-377). */
122
+ export declare function defineCreditHistory<const N extends string, const P extends () => object, const F extends FieldShapes, const U extends Uses = [], const X extends readonly IndexShape[] = []>(definition: Definition<N, F, U, X, CreditHistoryFields<P>> & Own<F, U> & {
123
+ readonly owner: P;
124
+ }): ModelOf<N, F & CreditHistoryFields<P>, WithStripe<U>, X>;
125
+ /** The application's history of credits: each move, given or spent (D-377). */
126
+ export declare function defineCreditHistory<const N extends string, const P extends () => object, const U extends Uses = [], const X extends readonly IndexShape[] = []>(definition: Definition<N, {}, U, X, CreditHistoryFields<P>> & {
127
+ readonly owner: P;
128
+ }): ModelOf<N, CreditHistoryFields<P>, WithStripe<U>, X>;
129
+ export {};
130
+ //# sourceMappingURL=billing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"billing.d.ts","sourceRoot":"","sources":["../src/billing.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACX,KAAK,EACL,WAAW,EACX,UAAU,EACV,WAAW,EACX,YAAY,EACZ,OAAO,EACP,QAAQ,EACR,GAAG,EACH,WAAW,EACX,aAAa,EACb,MAAM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