@jskit-ai/payments-core 0.2.27 → 0.2.28
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 +1 -0
- package/docs/contract.md +5 -6
- package/docs/standalone.md +4 -1
- package/locales/en.json +1 -1
- package/package.json +5 -5
- package/src/server/checkout.js +9 -7
- package/test/checkout.test.js +22 -13
- package/test/history.test.js +3 -3
- package/test/withoutDatabase.test.js +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- **Breaking:** `createPaymentCheckoutService` takes `appEnv` (the application's `APP_ENV`, `appEnvironment(env)`) in place of `sideEffects`, and requires it. Only production takes real payments: checkout and the billing portal in the live payment environment are refused elsewhere with `payment_sandbox_only` (409, "Only production takes real payments: use the sandbox payment environment."), which replaces `payment_test_copy`, before any provider write. The sandbox environment works everywhere.
|
|
5
6
|
- A test copy takes no real money: `createPaymentCheckoutService` takes `sideEffects` (default `simulate`), and unless it is `live`, checkout and the billing portal in the live payment environment are refused with `payment_test_copy` (409) before any provider write. The sandbox environment works either way.
|
|
6
7
|
- `PAYMENT_STORE_FEATURE` is no longer exported from `PaymentStoreFeature.js` (no public entry point offered it): the feature's id is written as `"payments.store"`, so `jskit check` sees a module that requires that capability where it names it.
|
|
7
8
|
- payment_entries declares its foreign key, account_key to payment_accounts.account_key (`foreignKeys`), so json-rest-api 3.1.0 can skip the visibility query a system-context read makes for a record such a key guarantees exists. `jskit test --databases` checks each against an app's test database. `paymentStore.mysql.test.js` proves it matches the migration on MariaDB.
|
package/docs/contract.md
CHANGED
|
@@ -42,12 +42,11 @@ bindings. The same source declaration can name both environments, but callers
|
|
|
42
42
|
cannot select live operations merely by submitting `environment: live`.
|
|
43
43
|
The server owns environment selection and supplies the matching Env projection.
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
`sideEffects: resolveSideEffectsMode(env)` to `createPaymentCheckoutService`.
|
|
45
|
+
Only production takes real money. The application's `APP_ENV` decides: in
|
|
46
|
+
development, test and staging, checkout and billing-portal requests in the live
|
|
47
|
+
payment environment are refused with `payment_sandbox_only` (409), before any
|
|
48
|
+
provider write; the sandbox environment works everywhere. Pass
|
|
49
|
+
`appEnv: appEnvironment(env)` to `createPaymentCheckoutService`.
|
|
51
50
|
|
|
52
51
|
An amount is an integer in the currency's smallest unit. ISO-looking currency
|
|
53
52
|
syntax is not evidence the provider accepts that currency. Initial plans are
|
package/docs/standalone.md
CHANGED
|
@@ -98,6 +98,7 @@ import { createPaymentService } from '@jskit-ai/payments-core/server';
|
|
|
98
98
|
import { createStripePaymentAdapter } from '@jskit-ai/payments-core/server/stripe';
|
|
99
99
|
import { createPaymentCheckoutService } from '@jskit-ai/payments-core/server/checkout';
|
|
100
100
|
import { createPaymentCatalogue } from '@jskit-ai/payments-core/server/catalogue';
|
|
101
|
+
import { appEnvironment } from '@jskit-ai/kernel/shared/support/appEnvironment';
|
|
101
102
|
|
|
102
103
|
const document = JSON.parse(await readFile(configurationUrl, 'utf8'));
|
|
103
104
|
// Also run the application's complete connector validation with its provider list.
|
|
@@ -129,7 +130,9 @@ const adapter = createStripePaymentAdapter({
|
|
|
129
130
|
});
|
|
130
131
|
const catalogue = createPaymentCatalogue({ store, adapter, scope: merchantScope, configuration });
|
|
131
132
|
const checkout = createPaymentCheckoutService({
|
|
132
|
-
adapter, store, payments, merchantScope, returnUrl, authorize: authorizeBilling
|
|
133
|
+
adapter, store, payments, merchantScope, returnUrl, authorize: authorizeBilling,
|
|
134
|
+
// Only production (APP_ENV) takes real money; elsewhere the live payment environment is refused.
|
|
135
|
+
appEnv: appEnvironment(process.env)
|
|
133
136
|
});
|
|
134
137
|
```
|
|
135
138
|
|
package/locales/en.json
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"payment_insufficient_credits": "Not enough credits for this action.",
|
|
19
19
|
"payment_debit_missing": "No debit exists for this reference.",
|
|
20
20
|
"payment_customer_unbound": "Bind this provider customer before processing its events.",
|
|
21
|
-
"
|
|
21
|
+
"payment_sandbox_only": "Only production takes real payments: use the sandbox payment environment."
|
|
22
22
|
},
|
|
23
23
|
"payments": {
|
|
24
24
|
"scope": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/payments-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.28",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Application-owned payment configuration, subscription access and transactional usage credits.",
|
|
6
6
|
"exports": {
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"test": "jskit test"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@jskit-ai/resource-core": "0.1.
|
|
26
|
+
"@jskit-ai/resource-core": "0.1.218",
|
|
27
27
|
"@paddle/paddle-node-sdk": "3.10.0",
|
|
28
28
|
"ajv": "8.20.0",
|
|
29
29
|
"stripe": "22.6.2"
|
|
@@ -69,9 +69,9 @@
|
|
|
69
69
|
}
|
|
70
70
|
},
|
|
71
71
|
"peerDependencies": {
|
|
72
|
-
"@jskit-ai/database-runtime": "0.1.
|
|
73
|
-
"@jskit-ai/kernel": "0.2.
|
|
74
|
-
"@jskit-ai/resource-runtime": "0.2.
|
|
72
|
+
"@jskit-ai/database-runtime": "0.1.276",
|
|
73
|
+
"@jskit-ai/kernel": "0.2.27",
|
|
74
|
+
"@jskit-ai/resource-runtime": "0.2.37"
|
|
75
75
|
},
|
|
76
76
|
"peerDependenciesMeta": {
|
|
77
77
|
"@jskit-ai/database-runtime": {
|
package/src/server/checkout.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { APP_ENVIRONMENTS } from "@jskit-ai/kernel/shared/support/appEnvironment";
|
|
2
3
|
import { PaymentError, fingerprint } from "./errors.js";
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -11,22 +12,23 @@ import { PaymentError, fingerprint } from "./errors.js";
|
|
|
11
12
|
* @param {import("./types.js").PaymentMerchantScope} dependencies.merchantScope
|
|
12
13
|
* @param {string} dependencies.returnUrl
|
|
13
14
|
* @param {(actor: unknown, request: { subjectId: string, action: string }) => boolean | Promise<boolean>} dependencies.authorize
|
|
14
|
-
* @param {"
|
|
15
|
-
*
|
|
16
|
-
* environment are refused; the sandbox environment works
|
|
15
|
+
* @param {"development" | "test" | "staging" | "production"} dependencies.appEnv The application's APP_ENV
|
|
16
|
+
* (`appEnvironment(env)`). Only production takes real money: elsewhere checkout and billing changes in the live
|
|
17
|
+
* payment environment are refused; the sandbox environment works everywhere.
|
|
17
18
|
*/
|
|
18
|
-
function createPaymentCheckoutService({ adapter, store, payments, merchantScope, returnUrl, authorize,
|
|
19
|
+
function createPaymentCheckoutService({ adapter, store, payments, merchantScope, returnUrl, authorize, appEnv }) {
|
|
19
20
|
// readable-check: allow method-probe - input validation: the application passes its billable-subject authorization
|
|
20
21
|
if (typeof authorize !== "function") throw new TypeError("Supply the application's billable-subject authorization function.");
|
|
22
|
+
if (!APP_ENVIRONMENTS.includes(appEnv)) throw new TypeError("Supply appEnv, the application's APP_ENV: appEnvironment(env).");
|
|
21
23
|
const target = new URL(returnUrl);
|
|
22
24
|
if ((target.protocol !== "https:" && !(target.protocol === "http:" && ["localhost", "127.0.0.1", "[::1]"].includes(target.hostname))) || target.username || target.password || target.hash) {
|
|
23
25
|
throw new TypeError("Configure an HTTPS application return/checkout URL (HTTP is allowed only for local development).");
|
|
24
26
|
}
|
|
25
27
|
const merchant = structuredClone(merchantScope);
|
|
26
|
-
// Before any provider write:
|
|
28
|
+
// Before any provider write: outside production nothing creates a live customer, checkout or billing session.
|
|
27
29
|
function requireRealMoneyAllowed() {
|
|
28
|
-
if (merchant.environment === "live" &&
|
|
29
|
-
throw new PaymentError("
|
|
30
|
+
if (merchant.environment === "live" && appEnv !== "production") {
|
|
31
|
+
throw new PaymentError("payment_sandbox_only", "Only production takes real payments: use the sandbox payment environment.", 409);
|
|
30
32
|
}
|
|
31
33
|
}
|
|
32
34
|
async function allowed(actor, subjectId, action) {
|
package/test/checkout.test.js
CHANGED
|
@@ -50,7 +50,7 @@ test('checkout authorizes before provider access, persists intent, deduplicates
|
|
|
50
50
|
},
|
|
51
51
|
async createPortal() { return { id: 'portal_a', url: 'https://billing.stripe.com/example' }; }
|
|
52
52
|
};
|
|
53
|
-
const service = createPaymentCheckoutService({ adapter, store, payments: { inspect: async () => ({ balance: 0, features: [], subscriptions: [] }) }, merchantScope: scope, returnUrl: 'https://app.example/billing', authorize: async (actor, { subjectId }) => actor === subjectId });
|
|
53
|
+
const service = createPaymentCheckoutService({ appEnv: 'test', adapter, store, payments: { inspect: async () => ({ balance: 0, features: [], subscriptions: [] }) }, merchantScope: scope, returnUrl: 'https://app.example/billing', authorize: async (actor, { subjectId }) => actor === subjectId });
|
|
54
54
|
const input = { actor: 'tenant-a', subjectId: 'tenant-a', email: 'tenant@example.com', planId: 'pro', requestId: 'request-1' };
|
|
55
55
|
try {
|
|
56
56
|
await assert.rejects(service.checkout({ ...input, actor: 'tenant-b' }), { code: 'payment_forbidden' });
|
|
@@ -111,7 +111,7 @@ test('signed standalone Stripe invoices are ignored before customer lookup; malf
|
|
|
111
111
|
const client = new Stripe('sk_test_fixture', { maxNetworkRetries: 0 });
|
|
112
112
|
const adapter = createStripePaymentAdapter({ client, webhookSecret: secret, environment: 'sandbox', providerAccountId: 'acct_a', priceBindings: {} });
|
|
113
113
|
let reconciliations = 0;
|
|
114
|
-
const service = createPaymentCheckoutService({ adapter, merchantScope: scope, returnUrl: 'https://app.example/billing', authorize: async () => false,
|
|
114
|
+
const service = createPaymentCheckoutService({ appEnv: 'test', adapter, merchantScope: scope, returnUrl: 'https://app.example/billing', authorize: async () => false,
|
|
115
115
|
payments: { async reconcileEvent() { reconciliations++; } } });
|
|
116
116
|
const event = { id: 'evt_oneoff', type: 'invoice.paid', livemode: false, data: { object: { id: 'in_oneoff', customer: 'cus_unrelated', parent: null } } };
|
|
117
117
|
const rawBody = Buffer.from(JSON.stringify(event));
|
|
@@ -189,7 +189,7 @@ test('signed subscription lifecycle updates app access without crossing subjects
|
|
|
189
189
|
webhooks: paddleSdk.webhooks,
|
|
190
190
|
subscriptions: { get: async () => { loads++; return { id: 'sub_a', customerId, status, items: [{ quantity: 1, price: { id: 'pri_a' } }], currentBillingPeriod: { endsAt: new Date(end).toISOString() } }; } }
|
|
191
191
|
} });
|
|
192
|
-
const checkout = createPaymentCheckoutService({ adapter, store, payments, merchantScope: merchant, returnUrl: 'https://app.example/billing', authorize: async () => false });
|
|
192
|
+
const checkout = createPaymentCheckoutService({ appEnv: 'test', adapter, store, payments, merchantScope: merchant, returnUrl: 'https://app.example/billing', authorize: async () => false });
|
|
193
193
|
await store.bindCustomer(merchant, 'customer_a', 'tenant-a');
|
|
194
194
|
const deliver = async (id, eventCustomer = 'customer_a') => {
|
|
195
195
|
const payload = provider === 'stripe'
|
|
@@ -228,22 +228,31 @@ test('signed subscription lifecycle updates app access without crossing subjects
|
|
|
228
228
|
} finally { await migration.down(db); await db.destroy(); }
|
|
229
229
|
});
|
|
230
230
|
|
|
231
|
-
test('
|
|
231
|
+
test('only production takes real payments: staging, development and test refuse live checkout and billing before any provider write', async () => {
|
|
232
232
|
const live = { ...scope, environment: 'live' };
|
|
233
233
|
const adapter = {
|
|
234
234
|
validatePlan() {},
|
|
235
|
-
async createCustomer() { assert.fail('
|
|
236
|
-
async createCheckout() { assert.fail('
|
|
237
|
-
async createPortal() { assert.fail('
|
|
235
|
+
async createCustomer() { assert.fail('outside production nothing creates a live customer'); },
|
|
236
|
+
async createCheckout() { assert.fail('outside production nothing starts a live checkout'); },
|
|
237
|
+
async createPortal() { assert.fail('outside production nothing opens a live billing session'); }
|
|
238
238
|
};
|
|
239
239
|
const store = { async withAccount(_scope, run) { return run({ state: {} }); } };
|
|
240
240
|
const options = { adapter, store, payments: {}, merchantScope: live, returnUrl: 'https://app.example/billing', authorize: async () => true };
|
|
241
241
|
const input = { actor: 'tenant-a', subjectId: 'tenant-a', email: 'tenant@example.com', planId: 'pro', requestId: 'request-1' };
|
|
242
|
-
for (const
|
|
243
|
-
const service = createPaymentCheckoutService({ ...options,
|
|
244
|
-
await assert.rejects(service.checkout(input), {
|
|
245
|
-
|
|
242
|
+
for (const appEnv of ['staging', 'development', 'test']) {
|
|
243
|
+
const service = createPaymentCheckoutService({ ...options, appEnv });
|
|
244
|
+
await assert.rejects(service.checkout(input), {
|
|
245
|
+
code: 'payment_sandbox_only', statusCode: 409, message: 'Only production takes real payments: use the sandbox payment environment.'
|
|
246
|
+
}, appEnv);
|
|
247
|
+
await assert.rejects(service.portal(input), { code: 'payment_sandbox_only' }, appEnv);
|
|
248
|
+
}
|
|
249
|
+
// Production passes the gate: here the next refusal is the missing billing account.
|
|
250
|
+
await assert.rejects(createPaymentCheckoutService({ ...options, appEnv: 'production' }).portal(input), { code: 'payment_customer_missing' });
|
|
251
|
+
// The sandbox works in staging: the same gate lets it through to the missing billing account.
|
|
252
|
+
await assert.rejects(createPaymentCheckoutService({ ...options, merchantScope: scope, appEnv: 'staging' }).portal(input), { code: 'payment_customer_missing' });
|
|
253
|
+
for (const appEnv of [undefined, 'live', 'Production']) {
|
|
254
|
+
assert.throws(() => createPaymentCheckoutService({ ...options, appEnv }), {
|
|
255
|
+
name: 'TypeError', message: "Supply appEnv, the application's APP_ENV: appEnvironment(env)."
|
|
256
|
+
});
|
|
246
257
|
}
|
|
247
|
-
// A live application passes the gate: here the next refusal is the missing billing account.
|
|
248
|
-
await assert.rejects(createPaymentCheckoutService({ ...options, sideEffects: 'live' }).portal(input), { code: 'payment_customer_missing' });
|
|
249
258
|
});
|
package/test/history.test.js
CHANGED
|
@@ -12,7 +12,7 @@ test('every subject billing entry point requires explicit authorization before t
|
|
|
12
12
|
const actions = { account: 'account', history: 'history', checkout: 'checkout', portal: 'portal', reconcilePending: 'reconcile' };
|
|
13
13
|
for (const decision of [false, undefined, null, 'true', 1, { allowed: true }]) {
|
|
14
14
|
const requests = [];
|
|
15
|
-
const service = createPaymentCheckoutService({ merchantScope: merchant, returnUrl: 'https://example.com/billing',
|
|
15
|
+
const service = createPaymentCheckoutService({ appEnv: 'test', merchantScope: merchant, returnUrl: 'https://example.com/billing',
|
|
16
16
|
store: forbidden, adapter: forbidden, payments: forbidden,
|
|
17
17
|
authorize: async (actor, request) => { requests.push({ actor, ...request }); return decision; }
|
|
18
18
|
});
|
|
@@ -30,7 +30,7 @@ test('every subject billing entry point requires explicit authorization before t
|
|
|
30
30
|
test('history authorizes the subject before storage or network and never accepts a caller customer ID', async () => {
|
|
31
31
|
const reads = [];
|
|
32
32
|
let boundCustomer = 'cus_owned';
|
|
33
|
-
const service = createPaymentCheckoutService({ merchantScope: merchant, returnUrl: 'https://example.com/billing',
|
|
33
|
+
const service = createPaymentCheckoutService({ appEnv: 'test', merchantScope: merchant, returnUrl: 'https://example.com/billing',
|
|
34
34
|
authorize: async (actor, request) => actor === 'billing-admin' && request.subjectId === 'tenant-a' && request.action === 'history',
|
|
35
35
|
store: { async inspect(scope) { reads.push(scope); return { customerId: boundCustomer }; } },
|
|
36
36
|
adapter: { async readHistory(query) { reads.push(query); return { collection: query.collection, items: [], nextCursor: null }; } }
|
|
@@ -51,7 +51,7 @@ test('history authorizes the subject before storage or network and never accepts
|
|
|
51
51
|
});
|
|
52
52
|
|
|
53
53
|
test('history errors do not expose provider credentials or diagnostics', async () => {
|
|
54
|
-
const service = createPaymentCheckoutService({ merchantScope: merchant, returnUrl: 'https://example.com/billing', authorize: async () => true,
|
|
54
|
+
const service = createPaymentCheckoutService({ appEnv: 'test', merchantScope: merchant, returnUrl: 'https://example.com/billing', authorize: async () => true,
|
|
55
55
|
store: { inspect: async () => ({ customerId: 'cus_a' }) }, adapter: { readHistory: async () => { throw new Error('sk_secret customer_private@example.com'); } } });
|
|
56
56
|
await assert.rejects(service.history({ actor: 'admin', subjectId: 'a', collection: 'transactions' }), (error) => {
|
|
57
57
|
assert.equal(error.code, 'payment_history_unavailable');
|
|
@@ -6,6 +6,7 @@ import test from "node:test";
|
|
|
6
6
|
import { createInstalledRuntime } from "@jskit-ai/kernel/server/platform";
|
|
7
7
|
import { silentLogger } from "@jskit-ai/kernel/server/runtime/logger";
|
|
8
8
|
|
|
9
|
+
const TEST_ENV = Object.freeze({ APP_ENV: "test" });
|
|
9
10
|
const TABLE_RUNTIME = ["@jskit-ai/database-runtime", "@jskit-ai/resource-runtime"];
|
|
10
11
|
const readManifest = async (url) => JSON.parse(await readFile(url, "utf8"));
|
|
11
12
|
|
|
@@ -40,14 +41,14 @@ test("an app without a database installs @jskit-ai/payments-core without the tab
|
|
|
40
41
|
assert.equal(manifest.peerDependenciesMeta?.[name]?.optional, true, `${name} is an optional peer`);
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
const started = await createInstalledRuntime({ appRoot: await installedApp(t, [manifest]), logger: silentLogger });
|
|
44
|
+
const started = await createInstalledRuntime({ appRoot: await installedApp(t, [manifest]), env: TEST_ENV, logger: silentLogger });
|
|
44
45
|
assert.ok(started.packageOrder.includes(manifest.name));
|
|
45
46
|
assert.equal(started.packageOrder.some((name) => TABLE_RUNTIME.includes(name)), false);
|
|
46
47
|
|
|
47
48
|
// Installed as a dependency, the table runtime would need a database driver the app does not have.
|
|
48
49
|
const databaseRuntime = await readManifest(new URL("../../database-runtime/package.json", import.meta.url));
|
|
49
50
|
await assert.rejects(
|
|
50
|
-
createInstalledRuntime({ appRoot: await installedApp(t, [manifest, databaseRuntime]), logger: silentLogger }),
|
|
51
|
+
createInstalledRuntime({ appRoot: await installedApp(t, [manifest, databaseRuntime]), env: TEST_ENV, logger: silentLogger }),
|
|
51
52
|
/requires capability runtime\.database\.driver, but no installed package provides it/u
|
|
52
53
|
);
|
|
53
54
|
});
|