@jskit-ai/payments-core 0.2.15 → 0.2.17

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 CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ - 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.
5
6
  - `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.
6
7
  - 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.
7
8
  - `@jskit-ai/database-runtime` and `@jskit-ai/resource-runtime` are optional peer dependencies, as they are of assistant-runtime, instead of dependencies: an app without a database installs, composes and starts the package without them, where installing them made boot refuse it for want of a database driver. An app that uses its payment store has them through its database.
package/docs/contract.md CHANGED
@@ -42,6 +42,13 @@ 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
+ A test copy takes no real money. The application's `SIDE_EFFECTS` setting is
46
+ `live` only for the copy that acts for real; anything else, including unset,
47
+ means simulate. A simulating application refuses checkout and billing-portal
48
+ requests in the live environment with `payment_test_copy` (409), before any
49
+ provider write; the sandbox environment works either way. Pass
50
+ `sideEffects: resolveSideEffectsMode(env)` to `createPaymentCheckoutService`.
51
+
45
52
  An amount is an integer in the currency's smallest unit. ISO-looking currency
46
53
  syntax is not evidence the provider accepts that currency. Initial plans are
47
54
  one recurring item, quantity one, monthly or yearly. Logical plan IDs are source
package/locales/en.json CHANGED
@@ -17,7 +17,8 @@
17
17
  "payment_feature_required": "Your subscription does not include this feature.",
18
18
  "payment_insufficient_credits": "Not enough credits for this action.",
19
19
  "payment_debit_missing": "No debit exists for this reference.",
20
- "payment_customer_unbound": "Bind this provider customer before processing its events."
20
+ "payment_customer_unbound": "Bind this provider customer before processing its events.",
21
+ "payment_test_copy": "This test copy takes no real payments. Use the sandbox payment environment, or set SIDE_EFFECTS=live to take real payments."
21
22
  },
22
23
  "payments": {
23
24
  "scope": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/payments-core",
3
- "version": "0.2.15",
3
+ "version": "0.2.17",
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.210",
26
+ "@jskit-ai/resource-core": "0.1.212",
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.268",
73
- "@jskit-ai/kernel": "0.2.19",
74
- "@jskit-ai/resource-runtime": "0.2.24"
72
+ "@jskit-ai/database-runtime": "0.1.270",
73
+ "@jskit-ai/kernel": "0.2.21",
74
+ "@jskit-ai/resource-runtime": "0.2.26"
75
75
  },
76
76
  "peerDependenciesMeta": {
77
77
  "@jskit-ai/database-runtime": {
@@ -11,8 +11,11 @@ import { PaymentError, fingerprint } from "./errors.js";
11
11
  * @param {import("./types.js").PaymentMerchantScope} dependencies.merchantScope
12
12
  * @param {string} dependencies.returnUrl
13
13
  * @param {(actor: unknown, request: { subjectId: string, action: string }) => boolean | Promise<boolean>} dependencies.authorize
14
+ * @param {"live" | "simulate"} [dependencies.sideEffects] The application's SIDE_EFFECTS setting. Unless `live`, the
15
+ * application is a test copy: it takes no real money, so checkout and billing changes in the live payment
16
+ * environment are refused; the sandbox environment works either way.
14
17
  */
15
- function createPaymentCheckoutService({ adapter, store, payments, merchantScope, returnUrl, authorize }) {
18
+ function createPaymentCheckoutService({ adapter, store, payments, merchantScope, returnUrl, authorize, sideEffects = "simulate" }) {
16
19
  // readable-check: allow method-probe - input validation: the application passes its billable-subject authorization
17
20
  if (typeof authorize !== "function") throw new TypeError("Supply the application's billable-subject authorization function.");
18
21
  const target = new URL(returnUrl);
@@ -20,6 +23,12 @@ function createPaymentCheckoutService({ adapter, store, payments, merchantScope,
20
23
  throw new TypeError("Configure an HTTPS application return/checkout URL (HTTP is allowed only for local development).");
21
24
  }
22
25
  const merchant = structuredClone(merchantScope);
26
+ // Before any provider write: a test copy creates no live customer, checkout or billing session.
27
+ function requireRealMoneyAllowed() {
28
+ if (merchant.environment === "live" && sideEffects !== "live") {
29
+ throw new PaymentError("payment_test_copy", "This test copy takes no real payments. Use the sandbox payment environment, or set SIDE_EFFECTS=live to take real payments.", 409);
30
+ }
31
+ }
23
32
  async function allowed(actor, subjectId, action) {
24
33
  if (typeof subjectId !== "string" || !subjectId || subjectId.length > 200 || await authorize(actor, { subjectId, action }) !== true) {
25
34
  throw new PaymentError("payment_forbidden", "You cannot manage this billable account.", 403);
@@ -50,6 +59,7 @@ function createPaymentCheckoutService({ adapter, store, payments, merchantScope,
50
59
  }
51
60
  async function checkout({ actor, subjectId, email, planId, requestId }) {
52
61
  const scope = await allowed(actor, subjectId, "checkout");
62
+ requireRealMoneyAllowed();
53
63
  adapter.validatePlan(planId);
54
64
  if (typeof requestId !== "string" || !/^[A-Za-z0-9_-]{1,100}$/.test(requestId)) throw new PaymentError("payment_request_invalid", "Supply a stable checkout request identifier.");
55
65
  if (typeof email !== "string" || email.length > 320 || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) throw new PaymentError("payment_input_invalid", "Supply the authenticated billing contact's email.", 422, { messageCode: "payments.input.billingEmail" });
@@ -95,6 +105,7 @@ function createPaymentCheckoutService({ adapter, store, payments, merchantScope,
95
105
  }
96
106
  async function portal({ actor, subjectId }) {
97
107
  const scope = await allowed(actor, subjectId, "portal");
108
+ requireRealMoneyAllowed();
98
109
  return perform(scope, async (tx) => {
99
110
  if (!tx.state.customerId) throw new PaymentError("payment_customer_missing", "Create a billing account first.", 404);
100
111
  return { action: "portal", customerId: tx.state.customerId };
@@ -227,3 +227,23 @@ test('signed subscription lifecycle updates app access without crossing subjects
227
227
  }
228
228
  } finally { await migration.down(db); await db.destroy(); }
229
229
  });
230
+
231
+ test('a test copy takes no real payments: it refuses live checkout and billing before any provider write', async () => {
232
+ const live = { ...scope, environment: 'live' };
233
+ const adapter = {
234
+ validatePlan() {},
235
+ async createCustomer() { assert.fail('a test copy creates no live customer'); },
236
+ async createCheckout() { assert.fail('a test copy starts no live checkout'); },
237
+ async createPortal() { assert.fail('a test copy opens no live billing session'); }
238
+ };
239
+ const store = { async withAccount(_scope, run) { return run({ state: {} }); } };
240
+ const options = { adapter, store, payments: {}, merchantScope: live, returnUrl: 'https://app.example/billing', authorize: async () => true };
241
+ const input = { actor: 'tenant-a', subjectId: 'tenant-a', email: 'tenant@example.com', planId: 'pro', requestId: 'request-1' };
242
+ for (const sideEffects of [undefined, 'simulate', 'LIVE']) {
243
+ const service = createPaymentCheckoutService({ ...options, sideEffects });
244
+ await assert.rejects(service.checkout(input), { code: 'payment_test_copy', statusCode: 409, message: /set SIDE_EFFECTS=live/u });
245
+ await assert.rejects(service.portal(input), { code: 'payment_test_copy' });
246
+ }
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
+ });