@geonosis/billing 0.1.0 → 0.2.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 +6 -0
- package/README.md +2 -2
- package/dist/catalogue.cjs +40 -9
- package/dist/catalogue.d.cts +19 -8
- package/dist/catalogue.d.ts +19 -8
- package/dist/catalogue.js +40 -9
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/webhook.cjs +4 -0
- package/dist/webhook.d.cts +2 -1
- package/dist/webhook.d.ts +2 -1
- package/dist/webhook.js +4 -0
- package/package.json +10 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @geonosis/billing
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 5613836: For a Stripe account several products share (#230): `syncCatalogue` takes the product as `{ id, name, description?, metadata?, statementDescriptor? }`, creates it with them, and reports a held product whose values differ under `product`. `BillingPlan.metadata` rides on its price beside `plan`. `intakeWebhook` takes `ours` and answers another product's event `{ ignored: 'not ours' }` with 200 before recording it. A Stripe error other than a missing product now stops the sync; it read as missing and created the product again. The exported types that changed: `CatalogueReport` (gains `product`) and `CatalogueStripe` (`products.create` takes the product's params).
|
|
8
|
+
|
|
3
9
|
## 0.1.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -6,8 +6,8 @@ Stripe with the money safety built in. Stripe itself is injected, and no other d
|
|
|
6
6
|
pnpm add @geonosis/billing
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
- **`syncCatalogue`** makes the product and prices one declaration names. It creates what is missing, never replaces a price, and reports one that differs, so running it twice changes nothing.
|
|
10
|
-
- **`intakeWebhook`** verifies the signature, ignores the other Stripe mode, records an event once and starts its work. Stripe gets its answer quickly, a duplicate does nothing, and a start that fails answers 500 so Stripe delivers again.
|
|
9
|
+
- **`syncCatalogue`** makes the product and prices one declaration names. It creates what is missing, never replaces a price, and reports one that differs, so running it twice changes nothing. On an account several products share, the product carries `description`, `metadata` and `statementDescriptor`, and a plan's `metadata` rides on its price beside `plan`. A held product that differs is reported under `product`, never replaced. A Stripe error other than "no such product" stops the sync rather than create a second product.
|
|
10
|
+
- **`intakeWebhook`** verifies the signature, ignores the other Stripe mode and, given `ours`, another product's events on a shared account (answered `{ ignored: 'not ours' }` before anything is recorded), records an event once and starts its work. Stripe gets its answer quickly, a duplicate does nothing, and a start that fails answers 500 so Stripe delivers again.
|
|
11
11
|
- **`stuckAction`** decides what a cron does with an event that has sat unprocessed: create or restart its work up to `MAX_RESTARTS` times, then fail it and alert, so one bad event never retries forever.
|
|
12
12
|
|
|
13
13
|
Fulfilment stays with the product: it runs in a Workflow, and it must be idempotent.
|
package/dist/catalogue.cjs
CHANGED
|
@@ -44,10 +44,17 @@ __export(exports_catalogue, {
|
|
|
44
44
|
syncCatalogue: () => syncCatalogue
|
|
45
45
|
});
|
|
46
46
|
module.exports = __toCommonJS(exports_catalogue);
|
|
47
|
+
var productParams = (product) => ({
|
|
48
|
+
...product.description === undefined ? {} : { description: product.description },
|
|
49
|
+
id: product.id,
|
|
50
|
+
...product.metadata === undefined ? {} : { metadata: { ...product.metadata } },
|
|
51
|
+
name: product.name,
|
|
52
|
+
...product.statementDescriptor === undefined ? {} : { statement_descriptor: product.statementDescriptor }
|
|
53
|
+
});
|
|
47
54
|
var priceParams = (plan, product) => ({
|
|
48
55
|
currency: plan.currency,
|
|
49
56
|
lookup_key: plan.lookupKey,
|
|
50
|
-
metadata: { plan: plan.id },
|
|
57
|
+
metadata: { ...plan.metadata, plan: plan.id },
|
|
51
58
|
nickname: plan.name,
|
|
52
59
|
product,
|
|
53
60
|
...plan.interval === undefined ? {} : { recurring: { interval: plan.interval } },
|
|
@@ -61,18 +68,42 @@ var priceDifferences = (price, plan) => [
|
|
|
61
68
|
...price.currency === plan.currency ? [] : [difference("currency", price.currency, plan.currency)],
|
|
62
69
|
...(price.recurring?.interval ?? "none") === (plan.interval ?? "none") ? [] : [difference("interval", price.recurring?.interval ?? "none", plan.interval ?? "none")]
|
|
63
70
|
];
|
|
64
|
-
var
|
|
71
|
+
var recordOf = (value) => typeof value === "object" && value !== null && !Array.isArray(value) ? Object.fromEntries(Object.entries(value)) : {};
|
|
72
|
+
var isMissing = (error) => {
|
|
73
|
+
const said = recordOf(error);
|
|
74
|
+
return said["code"] === "resource_missing" || said["statusCode"] === 404;
|
|
75
|
+
};
|
|
76
|
+
var heldProduct = async (stripe, id) => {
|
|
65
77
|
try {
|
|
66
|
-
await stripe.products.retrieve(id);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
78
|
+
return recordOf(await stripe.products.retrieve(id));
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (isMissing(error))
|
|
81
|
+
return;
|
|
82
|
+
throw error;
|
|
70
83
|
}
|
|
71
84
|
};
|
|
85
|
+
var PRODUCT_FIELDS = [
|
|
86
|
+
"name",
|
|
87
|
+
"description",
|
|
88
|
+
"statement_descriptor"
|
|
89
|
+
];
|
|
90
|
+
var productDifferences = (held, product) => {
|
|
91
|
+
const declared = productParams(product);
|
|
92
|
+
const metadata = recordOf(held["metadata"]);
|
|
93
|
+
return [
|
|
94
|
+
...PRODUCT_FIELDS.flatMap((field) => declared[field] === undefined || held[field] === declared[field] ? [] : [difference(field, held[field], declared[field])]),
|
|
95
|
+
...Object.entries(declared.metadata ?? {}).flatMap(([key, value]) => metadata[key] === value ? [] : [difference(`metadata.${key}`, metadata[key], value)])
|
|
96
|
+
].toSorted((a, b) => a.localeCompare(b, "en-US"));
|
|
97
|
+
};
|
|
72
98
|
var syncCatalogue = async (stripe, product, plans) => {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
99
|
+
const stored = await heldProduct(stripe, product.id);
|
|
100
|
+
if (stored === undefined)
|
|
101
|
+
await stripe.products.create(productParams(product));
|
|
102
|
+
const report = {
|
|
103
|
+
created: [],
|
|
104
|
+
mismatched: [],
|
|
105
|
+
product: stored === undefined ? [] : productDifferences(stored, product)
|
|
106
|
+
};
|
|
76
107
|
for (const plan of plans) {
|
|
77
108
|
const { data } = await stripe.prices.list({ limit: 1, lookup_keys: [plan.lookupKey] });
|
|
78
109
|
const [held] = data;
|
package/dist/catalogue.d.cts
CHANGED
|
@@ -4,8 +4,23 @@ export type BillingPlan = {
|
|
|
4
4
|
id: string;
|
|
5
5
|
interval?: 'month' | 'year';
|
|
6
6
|
lookupKey: string;
|
|
7
|
+
metadata?: Readonly<Record<string, string>>;
|
|
7
8
|
name: string;
|
|
8
9
|
};
|
|
10
|
+
export type CatalogueProduct = {
|
|
11
|
+
description?: string;
|
|
12
|
+
id: string;
|
|
13
|
+
metadata?: Readonly<Record<string, string>>;
|
|
14
|
+
name: string;
|
|
15
|
+
statementDescriptor?: string;
|
|
16
|
+
};
|
|
17
|
+
type ProductParams = {
|
|
18
|
+
description?: string;
|
|
19
|
+
id: string;
|
|
20
|
+
metadata?: Record<string, string>;
|
|
21
|
+
name: string;
|
|
22
|
+
statement_descriptor?: string;
|
|
23
|
+
};
|
|
9
24
|
export type PriceHeld = {
|
|
10
25
|
active: boolean;
|
|
11
26
|
currency: string;
|
|
@@ -26,10 +41,7 @@ export type CatalogueStripe = {
|
|
|
26
41
|
}>;
|
|
27
42
|
};
|
|
28
43
|
products: {
|
|
29
|
-
create: (params:
|
|
30
|
-
id: string;
|
|
31
|
-
name: string;
|
|
32
|
-
}) => Promise<unknown>;
|
|
44
|
+
create: (params: ProductParams) => Promise<unknown>;
|
|
33
45
|
retrieve: (id: string) => Promise<unknown>;
|
|
34
46
|
};
|
|
35
47
|
};
|
|
@@ -54,8 +66,7 @@ export type CatalogueReport = {
|
|
|
54
66
|
differences: string[];
|
|
55
67
|
lookupKey: string;
|
|
56
68
|
}[];
|
|
69
|
+
product: string[];
|
|
57
70
|
};
|
|
58
|
-
export declare const syncCatalogue: (stripe: CatalogueStripe, product:
|
|
59
|
-
|
|
60
|
-
name: string;
|
|
61
|
-
}, plans: readonly BillingPlan[]) => Promise<CatalogueReport>;
|
|
71
|
+
export declare const syncCatalogue: (stripe: CatalogueStripe, product: CatalogueProduct, plans: readonly BillingPlan[]) => Promise<CatalogueReport>;
|
|
72
|
+
export {};
|
package/dist/catalogue.d.ts
CHANGED
|
@@ -4,8 +4,23 @@ export type BillingPlan = {
|
|
|
4
4
|
id: string;
|
|
5
5
|
interval?: 'month' | 'year';
|
|
6
6
|
lookupKey: string;
|
|
7
|
+
metadata?: Readonly<Record<string, string>>;
|
|
7
8
|
name: string;
|
|
8
9
|
};
|
|
10
|
+
export type CatalogueProduct = {
|
|
11
|
+
description?: string;
|
|
12
|
+
id: string;
|
|
13
|
+
metadata?: Readonly<Record<string, string>>;
|
|
14
|
+
name: string;
|
|
15
|
+
statementDescriptor?: string;
|
|
16
|
+
};
|
|
17
|
+
type ProductParams = {
|
|
18
|
+
description?: string;
|
|
19
|
+
id: string;
|
|
20
|
+
metadata?: Record<string, string>;
|
|
21
|
+
name: string;
|
|
22
|
+
statement_descriptor?: string;
|
|
23
|
+
};
|
|
9
24
|
export type PriceHeld = {
|
|
10
25
|
active: boolean;
|
|
11
26
|
currency: string;
|
|
@@ -26,10 +41,7 @@ export type CatalogueStripe = {
|
|
|
26
41
|
}>;
|
|
27
42
|
};
|
|
28
43
|
products: {
|
|
29
|
-
create: (params:
|
|
30
|
-
id: string;
|
|
31
|
-
name: string;
|
|
32
|
-
}) => Promise<unknown>;
|
|
44
|
+
create: (params: ProductParams) => Promise<unknown>;
|
|
33
45
|
retrieve: (id: string) => Promise<unknown>;
|
|
34
46
|
};
|
|
35
47
|
};
|
|
@@ -54,8 +66,7 @@ export type CatalogueReport = {
|
|
|
54
66
|
differences: string[];
|
|
55
67
|
lookupKey: string;
|
|
56
68
|
}[];
|
|
69
|
+
product: string[];
|
|
57
70
|
};
|
|
58
|
-
export declare const syncCatalogue: (stripe: CatalogueStripe, product:
|
|
59
|
-
|
|
60
|
-
name: string;
|
|
61
|
-
}, plans: readonly BillingPlan[]) => Promise<CatalogueReport>;
|
|
71
|
+
export declare const syncCatalogue: (stripe: CatalogueStripe, product: CatalogueProduct, plans: readonly BillingPlan[]) => Promise<CatalogueReport>;
|
|
72
|
+
export {};
|
package/dist/catalogue.js
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
// src/catalogue.ts
|
|
2
|
+
var productParams = (product) => ({
|
|
3
|
+
...product.description === undefined ? {} : { description: product.description },
|
|
4
|
+
id: product.id,
|
|
5
|
+
...product.metadata === undefined ? {} : { metadata: { ...product.metadata } },
|
|
6
|
+
name: product.name,
|
|
7
|
+
...product.statementDescriptor === undefined ? {} : { statement_descriptor: product.statementDescriptor }
|
|
8
|
+
});
|
|
2
9
|
var priceParams = (plan, product) => ({
|
|
3
10
|
currency: plan.currency,
|
|
4
11
|
lookup_key: plan.lookupKey,
|
|
5
|
-
metadata: { plan: plan.id },
|
|
12
|
+
metadata: { ...plan.metadata, plan: plan.id },
|
|
6
13
|
nickname: plan.name,
|
|
7
14
|
product,
|
|
8
15
|
...plan.interval === undefined ? {} : { recurring: { interval: plan.interval } },
|
|
@@ -16,18 +23,42 @@ var priceDifferences = (price, plan) => [
|
|
|
16
23
|
...price.currency === plan.currency ? [] : [difference("currency", price.currency, plan.currency)],
|
|
17
24
|
...(price.recurring?.interval ?? "none") === (plan.interval ?? "none") ? [] : [difference("interval", price.recurring?.interval ?? "none", plan.interval ?? "none")]
|
|
18
25
|
];
|
|
19
|
-
var
|
|
26
|
+
var recordOf = (value) => typeof value === "object" && value !== null && !Array.isArray(value) ? Object.fromEntries(Object.entries(value)) : {};
|
|
27
|
+
var isMissing = (error) => {
|
|
28
|
+
const said = recordOf(error);
|
|
29
|
+
return said["code"] === "resource_missing" || said["statusCode"] === 404;
|
|
30
|
+
};
|
|
31
|
+
var heldProduct = async (stripe, id) => {
|
|
20
32
|
try {
|
|
21
|
-
await stripe.products.retrieve(id);
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
33
|
+
return recordOf(await stripe.products.retrieve(id));
|
|
34
|
+
} catch (error) {
|
|
35
|
+
if (isMissing(error))
|
|
36
|
+
return;
|
|
37
|
+
throw error;
|
|
25
38
|
}
|
|
26
39
|
};
|
|
40
|
+
var PRODUCT_FIELDS = [
|
|
41
|
+
"name",
|
|
42
|
+
"description",
|
|
43
|
+
"statement_descriptor"
|
|
44
|
+
];
|
|
45
|
+
var productDifferences = (held, product) => {
|
|
46
|
+
const declared = productParams(product);
|
|
47
|
+
const metadata = recordOf(held["metadata"]);
|
|
48
|
+
return [
|
|
49
|
+
...PRODUCT_FIELDS.flatMap((field) => declared[field] === undefined || held[field] === declared[field] ? [] : [difference(field, held[field], declared[field])]),
|
|
50
|
+
...Object.entries(declared.metadata ?? {}).flatMap(([key, value]) => metadata[key] === value ? [] : [difference(`metadata.${key}`, metadata[key], value)])
|
|
51
|
+
].toSorted((a, b) => a.localeCompare(b, "en-US"));
|
|
52
|
+
};
|
|
27
53
|
var syncCatalogue = async (stripe, product, plans) => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
54
|
+
const stored = await heldProduct(stripe, product.id);
|
|
55
|
+
if (stored === undefined)
|
|
56
|
+
await stripe.products.create(productParams(product));
|
|
57
|
+
const report = {
|
|
58
|
+
created: [],
|
|
59
|
+
mismatched: [],
|
|
60
|
+
product: stored === undefined ? [] : productDifferences(stored, product)
|
|
61
|
+
};
|
|
31
62
|
for (const plan of plans) {
|
|
32
63
|
const { data } = await stripe.prices.list({ limit: 1, lookup_keys: [plan.lookupKey] });
|
|
33
64
|
const [held] = data;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { type BillingPlan, type CatalogueReport, type CatalogueStripe, priceDifferences, type PriceHeld, priceParams, syncCatalogue } from './catalogue.cjs';
|
|
1
|
+
export { type BillingPlan, type CatalogueProduct, type CatalogueReport, type CatalogueStripe, priceDifferences, type PriceHeld, priceParams, syncCatalogue } from './catalogue.cjs';
|
|
2
2
|
export { MAX_RESTARTS, stuckAction, type StuckAction } from './stuck.cjs';
|
|
3
3
|
export { type BillingEvent, intakeWebhook, isLiveKey, type Recorded, type WebhookAnswer } from './webhook.cjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { type BillingPlan, type CatalogueReport, type CatalogueStripe, priceDifferences, type PriceHeld, priceParams, syncCatalogue } from './catalogue.js';
|
|
1
|
+
export { type BillingPlan, type CatalogueProduct, type CatalogueReport, type CatalogueStripe, priceDifferences, type PriceHeld, priceParams, syncCatalogue } from './catalogue.js';
|
|
2
2
|
export { MAX_RESTARTS, stuckAction, type StuckAction } from './stuck.js';
|
|
3
3
|
export { type BillingEvent, intakeWebhook, isLiveKey, type Recorded, type WebhookAnswer } from './webhook.js';
|
package/dist/webhook.cjs
CHANGED
|
@@ -48,6 +48,7 @@ var isLiveKey = (key) => LIVE_KEY.test(key);
|
|
|
48
48
|
var intakeWebhook = async ({
|
|
49
49
|
handled,
|
|
50
50
|
live,
|
|
51
|
+
ours,
|
|
51
52
|
record,
|
|
52
53
|
start,
|
|
53
54
|
verify
|
|
@@ -60,6 +61,9 @@ var intakeWebhook = async ({
|
|
|
60
61
|
}
|
|
61
62
|
if (event.livemode !== live)
|
|
62
63
|
return { body: { ignored: "mode", received: true }, status: 200 };
|
|
64
|
+
if (ours !== undefined && !await ours(event)) {
|
|
65
|
+
return { body: { ignored: "not ours", received: true }, status: 200 };
|
|
66
|
+
}
|
|
63
67
|
const acted = handled.includes(event.type);
|
|
64
68
|
const recorded = await record(event, acted ? "received" : "ignored");
|
|
65
69
|
if (!acted)
|
package/dist/webhook.d.cts
CHANGED
|
@@ -12,9 +12,10 @@ export type WebhookAnswer = {
|
|
|
12
12
|
status: number;
|
|
13
13
|
};
|
|
14
14
|
export declare const isLiveKey: (key: string) => boolean;
|
|
15
|
-
export declare const intakeWebhook: ({ handled, live, record, start, verify }: {
|
|
15
|
+
export declare const intakeWebhook: ({ handled, live, ours, record, start, verify }: {
|
|
16
16
|
handled: readonly string[];
|
|
17
17
|
live: boolean;
|
|
18
|
+
ours?: (event: BillingEvent) => boolean | Promise<boolean>;
|
|
18
19
|
record: (event: BillingEvent, status: 'ignored' | 'received') => Promise<Recorded>;
|
|
19
20
|
start: (event: BillingEvent) => Promise<void>;
|
|
20
21
|
verify: () => Promise<BillingEvent>;
|
package/dist/webhook.d.ts
CHANGED
|
@@ -12,9 +12,10 @@ export type WebhookAnswer = {
|
|
|
12
12
|
status: number;
|
|
13
13
|
};
|
|
14
14
|
export declare const isLiveKey: (key: string) => boolean;
|
|
15
|
-
export declare const intakeWebhook: ({ handled, live, record, start, verify }: {
|
|
15
|
+
export declare const intakeWebhook: ({ handled, live, ours, record, start, verify }: {
|
|
16
16
|
handled: readonly string[];
|
|
17
17
|
live: boolean;
|
|
18
|
+
ours?: (event: BillingEvent) => boolean | Promise<boolean>;
|
|
18
19
|
record: (event: BillingEvent, status: 'ignored' | 'received') => Promise<Recorded>;
|
|
19
20
|
start: (event: BillingEvent) => Promise<void>;
|
|
20
21
|
verify: () => Promise<BillingEvent>;
|
package/dist/webhook.js
CHANGED
|
@@ -4,6 +4,7 @@ var isLiveKey = (key) => LIVE_KEY.test(key);
|
|
|
4
4
|
var intakeWebhook = async ({
|
|
5
5
|
handled,
|
|
6
6
|
live,
|
|
7
|
+
ours,
|
|
7
8
|
record,
|
|
8
9
|
start,
|
|
9
10
|
verify
|
|
@@ -16,6 +17,9 @@ var intakeWebhook = async ({
|
|
|
16
17
|
}
|
|
17
18
|
if (event.livemode !== live)
|
|
18
19
|
return { body: { ignored: "mode", received: true }, status: 200 };
|
|
20
|
+
if (ours !== undefined && !await ours(event)) {
|
|
21
|
+
return { body: { ignored: "not ours", received: true }, status: 200 };
|
|
22
|
+
}
|
|
19
23
|
const acted = handled.includes(event.type);
|
|
20
24
|
const recorded = await record(event, acted ? "received" : "ignored");
|
|
21
25
|
if (!acted)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geonosis/billing",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"types": "./dist/index.d.ts",
|
|
5
5
|
"description": "Stripe billing with the money safety built in: a catalogue from one declaration, webhook intake that records an event once, and a decision for an event that got stuck. Stripe is injected, and no other domain is named.",
|
|
6
6
|
"keywords": [
|
|
@@ -35,19 +35,19 @@
|
|
|
35
35
|
"CHANGELOG.md",
|
|
36
36
|
"dist"
|
|
37
37
|
],
|
|
38
|
-
"dependencies": {},
|
|
39
|
-
"engines": {
|
|
40
|
-
"node": ">=22"
|
|
41
|
-
},
|
|
42
|
-
"publishConfig": {
|
|
43
|
-
"access": "public"
|
|
44
|
-
},
|
|
45
38
|
"scripts": {
|
|
46
39
|
"build": "bun ../../tooling/build-step.ts bun ../../tooling/bundle.ts",
|
|
47
|
-
"typecheck": "
|
|
40
|
+
"typecheck": "bun check --no-script",
|
|
48
41
|
"lint": "oxlint --report-unused-disable-directives-severity=error --config ../../.oxlintrc.json .",
|
|
49
42
|
"test:unit": "vitest run --project unit --root ../.. --passWithNoTests \"$PWD\"",
|
|
50
43
|
"test:bin": "vitest run --project bin --root ../.. --passWithNoTests \"$PWD\"",
|
|
51
44
|
"test:exam": "vitest run --project exam --root ../.. --passWithNoTests \"$PWD\""
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=22"
|
|
49
|
+
},
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
52
|
}
|
|
53
|
-
}
|
|
53
|
+
}
|