@ultimabase/stripe 0.0.0-first.0 → 0.0.0-stage

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 +2 -6
  2. package/package.json +3 -48
  3. package/dist/accounts.d.ts +0 -9
  4. package/dist/accounts.d.ts.map +0 -1
  5. package/dist/accounts.js +0 -20
  6. package/dist/apply.d.ts +0 -26
  7. package/dist/apply.d.ts.map +0 -1
  8. package/dist/apply.js +0 -73
  9. package/dist/audit.d.ts +0 -3
  10. package/dist/audit.d.ts.map +0 -1
  11. package/dist/audit.js +0 -123
  12. package/dist/billing-gateway.d.ts +0 -158
  13. package/dist/billing-gateway.d.ts.map +0 -1
  14. package/dist/billing-gateway.js +0 -23
  15. package/dist/billing.d.ts +0 -130
  16. package/dist/billing.d.ts.map +0 -1
  17. package/dist/billing.js +0 -128
  18. package/dist/calls.d.ts +0 -61
  19. package/dist/calls.d.ts.map +0 -1
  20. package/dist/calls.js +0 -172
  21. package/dist/catalog.d.ts +0 -54
  22. package/dist/catalog.d.ts.map +0 -1
  23. package/dist/catalog.js +0 -182
  24. package/dist/credit.d.ts +0 -16
  25. package/dist/credit.d.ts.map +0 -1
  26. package/dist/credit.js +0 -18
  27. package/dist/credits.d.ts +0 -38
  28. package/dist/credits.d.ts.map +0 -1
  29. package/dist/credits.js +0 -138
  30. package/dist/customer.d.ts +0 -25
  31. package/dist/customer.d.ts.map +0 -1
  32. package/dist/customer.js +0 -88
  33. package/dist/describe.d.ts +0 -4
  34. package/dist/describe.d.ts.map +0 -1
  35. package/dist/describe.js +0 -56
  36. package/dist/duplicates.d.ts +0 -12
  37. package/dist/duplicates.d.ts.map +0 -1
  38. package/dist/duplicates.js +0 -24
  39. package/dist/fake-billing.d.ts +0 -72
  40. package/dist/fake-billing.d.ts.map +0 -1
  41. package/dist/fake-billing.js +0 -290
  42. package/dist/gateway.d.ts +0 -108
  43. package/dist/gateway.d.ts.map +0 -1
  44. package/dist/gateway.js +0 -196
  45. package/dist/index.d.ts +0 -15
  46. package/dist/index.d.ts.map +0 -1
  47. package/dist/index.js +0 -6
  48. package/dist/job-input.d.ts +0 -6
  49. package/dist/job-input.d.ts.map +0 -1
  50. package/dist/job-input.js +0 -20
  51. package/dist/jobs.d.ts +0 -16
  52. package/dist/jobs.d.ts.map +0 -1
  53. package/dist/jobs.js +0 -189
  54. package/dist/keys.d.ts +0 -10
  55. package/dist/keys.d.ts.map +0 -1
  56. package/dist/keys.js +0 -30
  57. package/dist/kinds.d.ts +0 -11
  58. package/dist/kinds.d.ts.map +0 -1
  59. package/dist/kinds.js +0 -4
  60. package/dist/models.d.ts +0 -71
  61. package/dist/models.d.ts.map +0 -1
  62. package/dist/models.js +0 -41
  63. package/dist/plugin.d.ts +0 -41
  64. package/dist/plugin.d.ts.map +0 -1
  65. package/dist/plugin.js +0 -229
  66. package/dist/portal.d.ts +0 -23
  67. package/dist/portal.d.ts.map +0 -1
  68. package/dist/portal.js +0 -66
  69. package/dist/product.d.ts +0 -57
  70. package/dist/product.d.ts.map +0 -1
  71. package/dist/product.js +0 -113
  72. package/dist/running.d.ts +0 -61
  73. package/dist/running.d.ts.map +0 -1
  74. package/dist/running.js +0 -35
  75. package/dist/sdk-billing.d.ts +0 -8
  76. package/dist/sdk-billing.d.ts.map +0 -1
  77. package/dist/sdk-billing.js +0 -200
  78. package/dist/sdk-common.d.ts +0 -13
  79. package/dist/sdk-common.d.ts.map +0 -1
  80. package/dist/sdk-common.js +0 -41
  81. package/dist/sdk-customer.d.ts +0 -9
  82. package/dist/sdk-customer.d.ts.map +0 -1
  83. package/dist/sdk-customer.js +0 -149
  84. package/dist/sdk-invoices.d.ts +0 -10
  85. package/dist/sdk-invoices.d.ts.map +0 -1
  86. package/dist/sdk-invoices.js +0 -53
  87. package/dist/sdk-recovery.d.ts +0 -5
  88. package/dist/sdk-recovery.d.ts.map +0 -1
  89. package/dist/sdk-recovery.js +0 -81
  90. package/dist/sync.d.ts +0 -8
  91. package/dist/sync.d.ts.map +0 -1
  92. package/dist/sync.js +0 -196
  93. package/dist/testing.d.ts +0 -24
  94. package/dist/testing.d.ts.map +0 -1
  95. package/dist/testing.js +0 -100
  96. package/dist/webhook.d.ts +0 -4
  97. package/dist/webhook.d.ts.map +0 -1
  98. package/dist/webhook.js +0 -73
  99. package/src/accounts.ts +0 -30
  100. package/src/apply.ts +0 -99
  101. package/src/audit.ts +0 -171
  102. package/src/billing-gateway.ts +0 -188
  103. package/src/billing.ts +0 -361
  104. package/src/calls.ts +0 -280
  105. package/src/catalog.ts +0 -327
  106. package/src/credit.ts +0 -36
  107. package/src/credits.ts +0 -206
  108. package/src/customer.ts +0 -113
  109. package/src/describe.ts +0 -76
  110. package/src/duplicates.ts +0 -45
  111. package/src/fake-billing.ts +0 -388
  112. package/src/gateway.ts +0 -333
  113. package/src/index.ts +0 -35
  114. package/src/job-input.ts +0 -24
  115. package/src/jobs.ts +0 -230
  116. package/src/keys.ts +0 -40
  117. package/src/kinds.ts +0 -15
  118. package/src/models.ts +0 -107
  119. package/src/plugin.ts +0 -307
  120. package/src/portal.ts +0 -95
  121. package/src/product.ts +0 -203
  122. package/src/running.ts +0 -104
  123. package/src/sdk-billing.ts +0 -220
  124. package/src/sdk-common.ts +0 -57
  125. package/src/sdk-customer.ts +0 -203
  126. package/src/sdk-invoices.ts +0 -81
  127. package/src/sdk-recovery.ts +0 -93
  128. package/src/sync.ts +0 -246
  129. package/src/testing.ts +0 -135
  130. package/src/webhook.ts +0 -100
package/README.md CHANGED
@@ -1,7 +1,3 @@
1
- # @ultimabase/stripe
1
+ # Temporary Holding Version
2
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).
3
+ This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
package/package.json CHANGED
@@ -1,51 +1,6 @@
1
1
  {
2
2
  "name": "@ultimabase/stripe",
3
- "version": "0.0.0-first.0",
4
- "description": "Stripe for UltimaBase: a catalog declared in code, sold on Stripe's Checkout, with subscriptions, purchases and credits kept in the application's models.",
5
- "type": "module",
6
- "license": "Apache-2.0",
7
- "homepage": "https://ultimabase.dev/docs/stripe",
8
- "repository": {
9
- "type": "git",
10
- "url": "git+https://github.com/MrEkinox/ultimabase.git",
11
- "directory": "packages/stripe"
12
- },
13
- "engines": {
14
- "node": ">=24.7.0"
15
- },
16
- "exports": {
17
- ".": "./dist/index.js",
18
- "./testing": "./dist/testing.js"
19
- },
20
- "files": [
21
- "dist",
22
- "src",
23
- "!src/**/*.test.ts",
24
- "!src/test"
25
- ],
26
- "scripts": {
27
- "prepack": "rm -rf dist && yarn run -T tsc --project tsconfig.build.json"
28
- },
29
- "publishConfig": {
30
- "exports": {
31
- ".": "./dist/index.js",
32
- "./testing": "./dist/testing.js"
33
- }
34
- },
35
- "peerDependencies": {
36
- "@ultimabase/http": "^0.7.0",
37
- "@ultimabase/jobs": "^0.7.0",
38
- "ultimabase": "^0.7.0"
39
- },
40
- "devDependencies": {
41
- "@ultimabase/http": "0.7.0",
42
- "@ultimabase/jobs": "0.7.0",
43
- "@ultimabase/testing": "0.7.0",
44
- "ultimabase": "0.7.0",
45
- "vite": "8.3.4",
46
- "vitest": "5.0.5"
47
- },
48
- "dependencies": {
49
- "stripe": "23.1.0"
50
- }
3
+ "version": "0.0.0-stage",
4
+ "stub": true,
5
+ "description": "Temporary package placeholder for staged publishing"
51
6
  }
@@ -1,9 +0,0 @@
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
@@ -1 +0,0 @@
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"}
package/dist/accounts.js DELETED
@@ -1,20 +0,0 @@
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
- };
package/dist/apply.d.ts DELETED
@@ -1,26 +0,0 @@
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
@@ -1 +0,0 @@
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 DELETED
@@ -1,73 +0,0 @@
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
- }
package/dist/audit.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import type { RecurringJob } from '@ultimabase/jobs';
2
- export declare const auditJob: RecurringJob<'stripe.audit'>;
3
- //# sourceMappingURL=audit.d.ts.map
@@ -1 +0,0 @@
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 DELETED
@@ -1,123 +0,0 @@
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
- });
@@ -1,158 +0,0 @@
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
@@ -1 +0,0 @@
1
- {"version":3,"file":"billing-gateway.d.ts","sourceRoot":"","sources":["../src/billing-gateway.ts"],"names":[],"mappings":"AAG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CAAC,MAAM,GAAG,IAAI,CAAC,CAAA;IACjE,+CAA+C;IAC/C,QAAQ,CAAC,UAAU,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;IAC7D,+EAA+E;IAC/E,QAAQ,CAAC,QAAQ,EAAE,CAClB,GAAG,EAAE,IAAI,CAAC,kBAAkB,EAAE,IAAI,GAAG,YAAY,CAAC,GAAG;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,KACjE,OAAO,CAAC,IAAI,CAAC,CAAA;CAClB;AAED,wEAAwE;AACxE,MAAM,WAAW,eAAe;IAC/B;;;OAGG;IACH,QAAQ,CAAC,eAAe,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IACvD;;;OAGG;IACH,QAAQ,CAAC,YAAY,EAAE,CAAC,KAAK,EAAE,IAAI,KAAK,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAA;CAC1E;AAED,yDAAyD;AACzD,qBAAa,OAAQ,SAAQ,KAAK;IACjC,YAAY,EAAE,EAAE,MAAM,EAGrB;CACD;AAED,iFAAiF;AACjF,eAAO,MAAM,WAAW,WAAY,OAAO,KAAG,WAG3C,CAAA"}
@@ -1,23 +0,0 @@
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
- });