@molecule/api-resource-payment 1.0.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/LICENSE +115 -0
- package/dist/__tests__/index.test.d.ts +5 -0
- package/dist/__tests__/index.test.d.ts.map +1 -0
- package/dist/__tests__/index.test.js +85 -0
- package/dist/__tests__/index.test.js.map +1 -0
- package/dist/__tests__/planService.test.d.ts +5 -0
- package/dist/__tests__/planService.test.d.ts.map +1 -0
- package/dist/__tests__/planService.test.js +166 -0
- package/dist/__tests__/planService.test.js.map +1 -0
- package/dist/__tests__/plans.test.d.ts +5 -0
- package/dist/__tests__/plans.test.d.ts.map +1 -0
- package/dist/__tests__/plans.test.js +296 -0
- package/dist/__tests__/plans.test.js.map +1 -0
- package/dist/__tests__/recordService.test.d.ts +5 -0
- package/dist/__tests__/recordService.test.d.ts.map +1 -0
- package/dist/__tests__/recordService.test.js +214 -0
- package/dist/__tests__/recordService.test.js.map +1 -0
- package/dist/__tests__/resource.test.d.ts +5 -0
- package/dist/__tests__/resource.test.d.ts.map +1 -0
- package/dist/__tests__/resource.test.js +56 -0
- package/dist/__tests__/resource.test.js.map +1 -0
- package/dist/__tests__/schema.test.d.ts +5 -0
- package/dist/__tests__/schema.test.d.ts.map +1 -0
- package/dist/__tests__/schema.test.js +275 -0
- package/dist/__tests__/schema.test.js.map +1 -0
- package/dist/__tests__/utilities.test.d.ts +5 -0
- package/dist/__tests__/utilities.test.d.ts.map +1 -0
- package/dist/__tests__/utilities.test.js +59 -0
- package/dist/__tests__/utilities.test.js.map +1 -0
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +19 -0
- package/dist/browser-guard.js.map +1 -0
- package/dist/i18n.d.ts +10 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.js +18 -0
- package/dist/i18n.js.map +1 -0
- package/dist/index.d.ts +96 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +96 -0
- package/dist/index.js.map +1 -0
- package/dist/planService.d.ts +9 -0
- package/dist/planService.d.ts.map +1 -0
- package/dist/planService.js +53 -0
- package/dist/planService.js.map +1 -0
- package/dist/plans.d.ts +34 -0
- package/dist/plans.d.ts.map +1 -0
- package/dist/plans.js +167 -0
- package/dist/plans.js.map +1 -0
- package/dist/recordService.d.ts +33 -0
- package/dist/recordService.d.ts.map +1 -0
- package/dist/recordService.js +162 -0
- package/dist/recordService.js.map +1 -0
- package/dist/resource.d.ts +4 -0
- package/dist/resource.d.ts.map +1 -0
- package/dist/resource.js +22 -0
- package/dist/resource.js.map +1 -0
- package/dist/schema.d.ts +58 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +38 -0
- package/dist/schema.js.map +1 -0
- package/dist/types.d.ts +83 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/dist/utilities.d.ts +8 -0
- package/dist/utilities.d.ts.map +1 -0
- package/dist/utilities.js +25 -0
- package/dist/utilities.js.map +1 -0
- package/package.json +54 -0
- package/setup/payments.sql +22 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Payment type definitions.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Every available platform.
|
|
7
|
+
*
|
|
8
|
+
* Open string type so new payment platforms can be added without modifying
|
|
9
|
+
* this package. Well-known values include 'stripe', 'apple', and 'google'.
|
|
10
|
+
*/
|
|
11
|
+
export type PlatformKey = string;
|
|
12
|
+
/**
|
|
13
|
+
* Every available plan alias.
|
|
14
|
+
*/
|
|
15
|
+
export type PlanAlias = '' | 'monthly' | 'yearly';
|
|
16
|
+
/**
|
|
17
|
+
* Every available plan period.
|
|
18
|
+
*/
|
|
19
|
+
export type PlanPeriod = '' | 'month' | 'year';
|
|
20
|
+
/**
|
|
21
|
+
* The payment's properties.
|
|
22
|
+
*/
|
|
23
|
+
export interface Props {
|
|
24
|
+
id: string;
|
|
25
|
+
createdAt: string;
|
|
26
|
+
updatedAt: string;
|
|
27
|
+
userId: string;
|
|
28
|
+
platformKey: PlatformKey;
|
|
29
|
+
transactionId?: string;
|
|
30
|
+
productId: string;
|
|
31
|
+
data?: Record<string, unknown>;
|
|
32
|
+
receipt?: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Properties when creating a payment.
|
|
36
|
+
*/
|
|
37
|
+
export type CreateProps = Pick<Props, 'createdAt' | 'updatedAt' | 'userId' | 'platformKey' | 'transactionId' | 'productId' | 'data' | 'receipt'>;
|
|
38
|
+
/**
|
|
39
|
+
* Properties when updating a payment.
|
|
40
|
+
*/
|
|
41
|
+
export type UpdateProps = Partial<Pick<Props, 'data' | 'receipt'>>;
|
|
42
|
+
/**
|
|
43
|
+
* A plan's properties.
|
|
44
|
+
*/
|
|
45
|
+
export interface Plan {
|
|
46
|
+
planKey: string;
|
|
47
|
+
platformKey: PlatformKey;
|
|
48
|
+
platformProductId: string;
|
|
49
|
+
/**
|
|
50
|
+
* The platform's PRICE identifiers that grant this plan (e.g. Stripe
|
|
51
|
+
* `price_…` ids). Apps configure prices — not products — in their env
|
|
52
|
+
* (`STRIPE_<APP>_PRO_MONTHLY` etc.), so plan resolution matches an
|
|
53
|
+
* incoming platform identifier against `platformProductId` OR membership
|
|
54
|
+
* in this list.
|
|
55
|
+
*/
|
|
56
|
+
platformPriceIds?: string[];
|
|
57
|
+
alias: PlanAlias;
|
|
58
|
+
period: PlanPeriod;
|
|
59
|
+
price: string;
|
|
60
|
+
autoRenews?: boolean;
|
|
61
|
+
title: string;
|
|
62
|
+
/** i18n key for {@link Plan.title}. Optional — plans registered cross-package (e.g. by a billing router bridging its tiers) carry plain text only. */
|
|
63
|
+
titleKey?: string;
|
|
64
|
+
description: string;
|
|
65
|
+
/** i18n key for {@link Plan.description}. Optional — see {@link Plan.titleKey}. */
|
|
66
|
+
descriptionKey?: string;
|
|
67
|
+
shortDescription?: string;
|
|
68
|
+
shortDescriptionKey?: string;
|
|
69
|
+
highlightedDescription?: string;
|
|
70
|
+
highlightedDescriptionKey?: string;
|
|
71
|
+
capabilities: {
|
|
72
|
+
premium: boolean;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Resource type.
|
|
77
|
+
*/
|
|
78
|
+
export interface Resource {
|
|
79
|
+
name: string;
|
|
80
|
+
tableName: string;
|
|
81
|
+
schema: unknown;
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;GAKG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,CAAA;AAEhC;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,EAAE,GAAG,SAAS,GAAG,QAAQ,CAAA;AAEjD;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,EAAE,GAAG,OAAO,GAAG,MAAM,CAAA;AAE9C;;GAEG;AACH,MAAM,WAAW,KAAK;IACpB,EAAE,EAAE,MAAM,CAAA;IACV,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,WAAW,CAAA;IACxB,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,SAAS,EAAE,MAAM,CAAA;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC9B,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,IAAI,CAC5B,KAAK,EACH,WAAW,GACX,WAAW,GACX,QAAQ,GACR,aAAa,GACb,eAAe,GACf,WAAW,GACX,MAAM,GACN,SAAS,CACZ,CAAA;AAED;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAA;AAElE;;GAEG;AACH,MAAM,WAAW,IAAI;IACnB,OAAO,EAAE,MAAM,CAAA;IACf,WAAW,EAAE,WAAW,CAAA;IACxB,iBAAiB,EAAE,MAAM,CAAA;IACzB;;;;;;OAMG;IACH,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAA;IAC3B,KAAK,EAAE,SAAS,CAAA;IAChB,MAAM,EAAE,UAAU,CAAA;IAClB,KAAK,EAAE,MAAM,CAAA;IACb,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,KAAK,EAAE,MAAM,CAAA;IACb,sJAAsJ;IACtJ,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,WAAW,EAAE,MAAM,CAAA;IACnB,mFAAmF;IACnF,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,mBAAmB,CAAC,EAAE,MAAM,CAAA;IAC5B,sBAAsB,CAAC,EAAE,MAAM,CAAA;IAC/B,yBAAyB,CAAC,EAAE,MAAM,CAAA;IAClC,YAAY,EAAE;QACZ,OAAO,EAAE,OAAO,CAAA;KACjB,CAAA;CACF;AAED;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,OAAO,CAAA;CAChB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { PlanPeriod } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Get the duration in milliseconds for a billing period.
|
|
4
|
+
* @param period - The billing period ('month' or 'year').
|
|
5
|
+
* @returns The period duration in milliseconds.
|
|
6
|
+
*/
|
|
7
|
+
export declare const getPeriodTime: (period: PlanPeriod) => number;
|
|
8
|
+
//# sourceMappingURL=utilities.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utilities.d.ts","sourceRoot":"","sources":["../src/utilities.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAa5C;;;;GAIG;AACH,eAAO,MAAM,aAAa,GAAI,QAAQ,UAAU,KAAG,MASlD,CAAA"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const getOneYearTime = () => {
|
|
2
|
+
const date = new Date();
|
|
3
|
+
const oneYearFromNow = new Date(new Date().setFullYear(date.getFullYear() + 1));
|
|
4
|
+
return oneYearFromNow.getTime() - date.getTime();
|
|
5
|
+
};
|
|
6
|
+
const getOneMonthTime = () => {
|
|
7
|
+
const oneYearTime = getOneYearTime();
|
|
8
|
+
return Math.round(oneYearTime / 12);
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Get the duration in milliseconds for a billing period.
|
|
12
|
+
* @param period - The billing period ('month' or 'year').
|
|
13
|
+
* @returns The period duration in milliseconds.
|
|
14
|
+
*/
|
|
15
|
+
export const getPeriodTime = (period) => {
|
|
16
|
+
switch (period) {
|
|
17
|
+
case 'month':
|
|
18
|
+
return getOneMonthTime();
|
|
19
|
+
case 'year':
|
|
20
|
+
return getOneYearTime();
|
|
21
|
+
default:
|
|
22
|
+
return 0;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=utilities.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utilities.js","sourceRoot":"","sources":["../src/utilities.ts"],"names":[],"mappings":"AAEA,MAAM,cAAc,GAAG,GAAW,EAAE;IAClC,MAAM,IAAI,GAAG,IAAI,IAAI,EAAE,CAAA;IACvB,MAAM,cAAc,GAAG,IAAI,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,CAAC,CAAA;IAC/E,OAAO,cAAc,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,OAAO,EAAE,CAAA;AAClD,CAAC,CAAA;AAED,MAAM,eAAe,GAAG,GAAW,EAAE;IACnC,MAAM,WAAW,GAAG,cAAc,EAAE,CAAA;IACpC,OAAO,IAAI,CAAC,KAAK,CAAC,WAAW,GAAG,EAAE,CAAC,CAAA;AACrC,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,MAAkB,EAAU,EAAE;IAC1D,QAAQ,MAAM,EAAE,CAAC;QACf,KAAK,OAAO;YACV,OAAO,eAAe,EAAE,CAAA;QAC1B,KAAK,MAAM;YACT,OAAO,cAAc,EAAE,CAAA;QACzB;YACE,OAAO,CAAC,CAAA;IACZ,CAAC;AACH,CAAC,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/api-resource-payment",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Payment resource with subscription plan management",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"setup"
|
|
22
|
+
],
|
|
23
|
+
"license": "Apache-2.0",
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"zod": "4.4.3"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@molecule/api-bond": "1.0.0",
|
|
29
|
+
"@molecule/api-locales-payment": "1.0.0",
|
|
30
|
+
"@molecule/api-payments": "1.0.0",
|
|
31
|
+
"@types/node": "26.1.2",
|
|
32
|
+
"typescript": "6.0.3",
|
|
33
|
+
"vitest": "4.1.10"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@molecule/api-bond": "^1.0.0",
|
|
37
|
+
"@molecule/api-database": "^1.0.0",
|
|
38
|
+
"@molecule/api-i18n": "^1.0.0",
|
|
39
|
+
"@molecule/api-locales-payment": "^1.0.0",
|
|
40
|
+
"@molecule/api-logger": "^1.0.0",
|
|
41
|
+
"@molecule/api-payments": "^1.0.0",
|
|
42
|
+
"@molecule/api-resource": "^1.0.0"
|
|
43
|
+
},
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
47
|
+
"directory": "packages/api/resources/payment"
|
|
48
|
+
},
|
|
49
|
+
"homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/api/resources/payment",
|
|
50
|
+
"bugs": "https://github.com/molecule-dev/molecule/issues",
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
CREATE TABLE IF NOT EXISTS "payments" (
|
|
2
|
+
"id" uuid UNIQUE PRIMARY KEY NOT NULL,
|
|
3
|
+
"createdAt" timestamptz DEFAULT current_timestamp NOT NULL,
|
|
4
|
+
"updatedAt" timestamptz DEFAULT current_timestamp NOT NULL,
|
|
5
|
+
"userId" uuid NOT NULL,
|
|
6
|
+
"transactionId" text,
|
|
7
|
+
"productId" text,
|
|
8
|
+
"platformKey" text,
|
|
9
|
+
"data" jsonb,
|
|
10
|
+
"receipt" text
|
|
11
|
+
);
|
|
12
|
+
CREATE INDEX IF NOT EXISTS "payments_createdAt_index" ON "payments" ("createdAt");
|
|
13
|
+
CREATE INDEX IF NOT EXISTS "payments_updatedAt_index" ON "payments" ("updatedAt");
|
|
14
|
+
CREATE INDEX IF NOT EXISTS "payments_userId_index" ON "payments" ("userId");
|
|
15
|
+
-- A platform transaction must bind to exactly ONE account (first-claim-wins).
|
|
16
|
+
-- This UNIQUE index is the race-safe backstop for receipt/subscription replay
|
|
17
|
+
-- (R2-1): a second account inserting the same (platformKey, transactionId)
|
|
18
|
+
-- fails, so it can never be granted the plan. Also serves the transactionId
|
|
19
|
+
-- lookup the plain index used to. NULL transactionIds are allowed/non-unique.
|
|
20
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "payments_platformKey_transactionId_unique" ON "payments" ("platformKey", "transactionId");
|
|
21
|
+
CREATE INDEX IF NOT EXISTS "payments_productId_index" ON "payments" ("productId");
|
|
22
|
+
CREATE INDEX IF NOT EXISTS "payments_platformKey_index" ON "payments" ("platformKey");
|