@jskit-ai/payments-core 0.2.37 → 0.2.38
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 +5 -0
- package/README.md +72 -8
- package/contracts/configuration.schema.json +56 -1
- package/contracts/conformance.json +484 -0
- package/docs/conformance.md +34 -5
- package/docs/contract.md +59 -2
- package/docs/standalone.md +18 -4
- package/locales/en.json +17 -5
- package/migrations/payments_core_top_ups.cjs +28 -0
- package/package.json +6 -2
- package/src/server/checkout.js +111 -5
- package/src/server/errors.js +4 -3
- package/src/server/paymentResources.js +16 -2
- package/src/server/paymentStore.js +35 -3
- package/src/server/stripe.js +112 -1
- package/src/server/topUps.js +97 -0
- package/src/server/types.js +76 -1
- package/src/shared/configuration.js +9 -0
- package/src/shared/topUps.js +5 -0
- package/test/checkout.test.js +4 -4
- package/test/conformance.test.js +76 -1
- package/test/history.test.js +1 -1
- package/test/i18nCatalog.test.js +3 -1
- package/test/paymentStore.mysql.test.js +3 -3
- package/test/paymentStoreFeature.test.js +15 -3
- package/test/support/fakeStripe.js +112 -0
- package/test/support/migrations.js +11 -0
- package/test/support/topUpApp.js +85 -0
- package/test/topUps.mysql.test.js +104 -0
- package/test/topUps.test.js +307 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- Prepaid top-ups. `extensions.payments.topUps` (`name`, `currency`, `minimumAmount`, `maximumAmount`, amounts in the currency's smallest unit, Stripe's floor of 50) sells one-off Stripe Checkout payments of an amount the person chooses; `plans` may be empty only beside it. A Paddle connection with top-ups fails validation at its `integrationId` (`payments.configuration.paddleTopUps`), and an inverted range at `topUps/maximumAmount` (`payments.configuration.topUpRange`). `createPaymentCheckoutService` takes `topUps: { offer, credit }` and gains `topUp({actor, subjectId, amount, requestId, email?, description?})` and `confirmTopUp({actor, subjectId, topUpId})`, both authorized as action `topUp`; its `webhook` settles the four `checkout.session.*` top-up events. The verified webhook and the return page's confirmation (which reads the session from Stripe with the server's key) settle through one path: under the top-up's row lock, only the move to paid calls the application's `credit({reference, subjectId, amount, currency}, options)` inside the same transaction, so a top-up is credited once whichever door reports it first. The subject comes from the top-up written down at checkout, never from the event. New table `payment_top_ups` (migration `payments_core_top_ups.cjs`, in the payment tables' collation on MySQL and MariaDB); the store gains `createTopUp`, `inspectTopUp` and `withTopUp`. `TOP_UP_RETURN_PARAMETER` (`"paymentTopUp"`) is exported from `@jskit-ai/payments-core/shared/topUps`.
|
|
6
|
+
- **Breaking:** `createStripePaymentAdapter` requires `appEnv` (`appEnvironment(env)`) and refuses a live key (`sk_live_`, `rk_live_`) unless it is `production`, with `payment_live_key_refused` (409); test keys work everywhere. It takes `apiBaseUrl`, a stand-in for Stripe's API, in development and tests only (`payment_stand_in_refused`, 409, elsewhere).
|
|
7
|
+
- `PaymentError` takes `cause` in its options; a provider failure while starting or confirming a top-up is `payment_provider_unavailable` (502) with the provider's error as its cause.
|
|
8
|
+
- New messages: `jskit.errors.payment_live_key_refused`, `payment_stand_in_refused`, `payment_top_ups_unavailable`, `payment_top_up_amount_invalid`, `payment_top_up_missing`, `payment_provider_unavailable`; `jskit.payments.input.topUpDescription`, `jskit.payments.reference.otherAmount`, `jskit.payments.provider.topUpSession`, `jskit.payments.provider.topUpAmount`, `jskit.payments.configuration.paddleTopUps`, `jskit.payments.configuration.topUpRange`.
|
|
9
|
+
|
|
5
10
|
- The `webhook` and `deployment` readiness checks' text names the release in use ("Verify the release in use runs with this configuration and environment."), where it said the deployed webhook and the intended release; the check ids are unchanged.
|
|
6
11
|
- **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.
|
|
7
12
|
- 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.
|
package/README.md
CHANGED
|
@@ -6,9 +6,11 @@ is required to run them. PHP applications implement the same documented
|
|
|
6
6
|
contract using their own framework; they do not install this runtime.
|
|
7
7
|
|
|
8
8
|
Current implementation: a strict payment declaration, transactional Knex
|
|
9
|
-
storage, subscription feature checks, usage credits,
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
storage, subscription feature checks, usage credits, prepaid top-ups (one-off
|
|
10
|
+
Stripe Checkout payments credited once to the application's own balance),
|
|
11
|
+
official-SDK Stripe and Paddle webhook verification, subscription
|
|
12
|
+
reconciliation, checkout and customer portal adapters, and an explicitly
|
|
13
|
+
authorized checkout composition service.
|
|
12
14
|
Reviewed catalogue publication preserves environment-bound product/price IDs and
|
|
13
15
|
records ambiguous provider writes for recovery. Editor execution wiring and
|
|
14
16
|
automatic merchant onboarding remain separate unfinished work.
|
|
@@ -24,6 +26,9 @@ they are not an editor configuration database.
|
|
|
24
26
|
Public API imports:
|
|
25
27
|
|
|
26
28
|
- `@jskit-ai/payments-core/shared`: `validatePaymentConfiguration(document)`.
|
|
29
|
+
- `@jskit-ai/payments-core/shared/topUps`: `TOP_UP_RETURN_PARAMETER`
|
|
30
|
+
(`"paymentTopUp"`), the return URL's query parameter naming the top-up a
|
|
31
|
+
person comes back from.
|
|
27
32
|
- `@jskit-ai/payments-core/server/storage`: `PaymentStoreFeature`, the payment
|
|
28
33
|
tables' one owner: registered in the server providers, it owns them through
|
|
29
34
|
`runtime.resources` and provides the payment store as `payments.store`. A
|
|
@@ -38,8 +43,8 @@ The app authorizes billing identities and mounts its own routes. A billable
|
|
|
38
43
|
subject may be a user or an organization. Never derive that subject from an
|
|
39
44
|
untrusted customer ID or webhook metadata. The checkout service requires an
|
|
40
45
|
`authorize(actor, { subjectId, action })` function that returns exactly `true`.
|
|
41
|
-
The app must distinguish `account`, `history`, `checkout`, `portal`
|
|
42
|
-
administrator-only `reconcile`. `history` reads customer-scoped subscriptions
|
|
46
|
+
The app must distinguish `account`, `history`, `checkout`, `portal`, `topUp`
|
|
47
|
+
and administrator-only `reconcile`. `history` reads customer-scoped subscriptions
|
|
43
48
|
and invoice/transaction pages with no provider writes; its contract and CLI
|
|
44
49
|
composition are in [standalone setup](docs/standalone.md#provider-billing-history-from-an-app-or-cli).
|
|
45
50
|
Core grant/debit/reconciliation methods are trusted server APIs, not public
|
|
@@ -67,8 +72,15 @@ For concrete file loading, Env resolution and provider composition, see
|
|
|
67
72
|
Create one Stripe or Paddle adapter using `apiKey`, `webhookSecret`,
|
|
68
73
|
`environment`, `providerAccountId` (Stripe), `taxCategory` (Paddle), and `priceBindings`. Account
|
|
69
74
|
identity in this configuration is not a claim of provider verification.
|
|
75
|
+
The Stripe adapter also takes `appEnv` (`appEnvironment(env)`), and refuses
|
|
76
|
+
a live key (`sk_live_`, `rk_live_`) unless it is `production`
|
|
77
|
+
(`payment_live_key_refused`); test keys work everywhere. Development and
|
|
78
|
+
tests may point it at a stand-in for Stripe's API with `apiBaseUrl` (an
|
|
79
|
+
origin such as `http://127.0.0.1:12111`), which other environments refuse.
|
|
70
80
|
5. Create the checkout service with `adapter`, `store`, `payments`,
|
|
71
|
-
`merchantScope`, `returnUrl`, and the app's `authorize` function
|
|
81
|
+
`merchantScope`, `returnUrl`, `appEnv` and the app's `authorize` function,
|
|
82
|
+
plus `topUps: { offer: configuration.topUps, credit }` when it sells top-ups
|
|
83
|
+
([Prepaid top-ups](#prepaid-top-ups)).
|
|
72
84
|
6. Authenticated app routes call `checkout({actor, subjectId, email, planId,
|
|
73
85
|
requestId})` or `portal({actor, subjectId})`. Keep the returned checkout
|
|
74
86
|
request ID stable on transport retries; begin a new request for an expired
|
|
@@ -91,6 +103,51 @@ Checkout and a return page. Both providers' customer portals remain provider
|
|
|
91
103
|
hosted. Provider account registration and Paddle domain approval are separate
|
|
92
104
|
setup tasks; a successful API request does not prove approval.
|
|
93
105
|
|
|
106
|
+
## Prepaid top-ups
|
|
107
|
+
|
|
108
|
+
A configuration with `topUps` (see [the contract](docs/contract.md#prepaid-top-ups))
|
|
109
|
+
sells one-off Stripe Checkout payments of an amount the person chooses within
|
|
110
|
+
its limits, credited once to the application's own balance:
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
const checkout = createPaymentCheckoutService({
|
|
114
|
+
adapter, store, payments, merchantScope, returnUrl, authorize, appEnv: appEnvironment(env),
|
|
115
|
+
topUps: {
|
|
116
|
+
offer: configuration.topUps,
|
|
117
|
+
// Inside the transaction that marks the top-up paid; write with `options` so both commit or neither does.
|
|
118
|
+
credit: ({ reference, subjectId, amount, currency }, options) => balances.add({ reference, subjectId, amount, currency }, options)
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
The checkout service then has:
|
|
124
|
+
|
|
125
|
+
- `topUp({actor, subjectId, amount, requestId, email?, description?})`:
|
|
126
|
+
authorizes action `topUp`, checks the amount (an integer in the currency's
|
|
127
|
+
smallest unit, within `minimumAmount`..`maximumAmount`), writes the top-up
|
|
128
|
+
down and opens its Checkout payment. Answers
|
|
129
|
+
`{topUpId, status, amount, currency, url}`; navigate to `url` (in a framed
|
|
130
|
+
page, `@jskit-ai/payments-web`'s `openPaymentPage(url)` opens it in a new
|
|
131
|
+
tab). The same `requestId` answers the same top-up.
|
|
132
|
+
- `confirmTopUp({actor, subjectId, topUpId})`: the return page's call, with the
|
|
133
|
+
`paymentTopUp` query parameter Checkout returns with. It authorizes action
|
|
134
|
+
`topUp`, reads the session from Stripe with the server's key and settles it.
|
|
135
|
+
Answers the same shape plus `credited`, true only for the call that credited.
|
|
136
|
+
- `webhook({rawBody, signature})` settles the top-ups' Checkout events
|
|
137
|
+
(`checkout.session.completed`, `checkout.session.async_payment_succeeded`,
|
|
138
|
+
`checkout.session.async_payment_failed`, `checkout.session.expired`) through
|
|
139
|
+
the same path, before subscription events.
|
|
140
|
+
|
|
141
|
+
`credit` is called once per paid top-up with a stable `reference`
|
|
142
|
+
(`top-up:<topUpId>`), the subject the top-up was written down for, the amount
|
|
143
|
+
and the currency, and the options of the transaction that settles it. Its
|
|
144
|
+
handle calls take those options (spread under the application's own context
|
|
145
|
+
where it needs one) and join that transaction. It runs again only after its
|
|
146
|
+
transaction rolled back, so it writes nothing outside it; a balance kept
|
|
147
|
+
elsewhere must be idempotent on the reference. Composing top-ups with a Paddle
|
|
148
|
+
adapter, or without `offer` or `credit`, throws a TypeError naming the fix; a
|
|
149
|
+
service composed without them answers `topUp` with `payment_top_ups_unavailable`.
|
|
150
|
+
|
|
94
151
|
## Failures and evidence
|
|
95
152
|
|
|
96
153
|
Checkout writes an intent before contacting the provider. A crash or ambiguous
|
|
@@ -112,7 +169,12 @@ features or allowances.
|
|
|
112
169
|
Focused fixture evidence currently covers raw signatures with both official
|
|
113
170
|
SDKs, storage rollback, duplicate grants/renewals, subject/environment isolation,
|
|
114
171
|
concurrent debit requests, expired refunds, checkout authorization, uncertain
|
|
115
|
-
writes and explicit recovery.
|
|
172
|
+
writes and explicit recovery. Top-ups are tested through the real Stripe SDK
|
|
173
|
+
against a stand-in for Stripe's API (`test/support/fakeStripe.js`): checkout
|
|
174
|
+
creation and authorization, limits, each webhook outcome, the return page's
|
|
175
|
+
confirmation, foreign and tampered sessions, a failing credit, and live-key and
|
|
176
|
+
stand-in refusals; `test/topUps.mysql.test.js` races the webhook and the return
|
|
177
|
+
page over one payment on MariaDB and sees it credited once. SQLite fixtures do not establish PostgreSQL/MySQL
|
|
116
178
|
multi-process locking behavior. Live provider accounts and full generated-app
|
|
117
179
|
acceptance have not been exercised.
|
|
118
180
|
|
|
@@ -133,7 +195,9 @@ existing subscribers; publication does not cancel their subscriptions.
|
|
|
133
195
|
|
|
134
196
|
Configure Stripe deliveries for `invoice.paid`, `invoice.payment_failed`,
|
|
135
197
|
`invoice.payment_action_required`, and the `customer.subscription.*` lifecycle
|
|
136
|
-
used by the application
|
|
198
|
+
used by the application, and, for top-ups, `checkout.session.completed`,
|
|
199
|
+
`checkout.session.async_payment_succeeded`,
|
|
200
|
+
`checkout.session.async_payment_failed` and `checkout.session.expired`. Configure Paddle for `transaction.completed` and
|
|
137
201
|
`subscription.*` lifecycle deliveries. The provider adapter selects relevant
|
|
138
202
|
subscription events only after signature verification; signed standalone
|
|
139
203
|
invoices/transactions are ignored before application customer lookup.
|
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
},
|
|
28
28
|
"plans": {
|
|
29
29
|
"type": "object",
|
|
30
|
-
"minProperties": 1,
|
|
31
30
|
"maxProperties": 100,
|
|
32
31
|
"propertyNames": {
|
|
33
32
|
"$ref": "#/definitions/id"
|
|
@@ -35,8 +34,33 @@
|
|
|
35
34
|
"additionalProperties": {
|
|
36
35
|
"$ref": "#/definitions/plan"
|
|
37
36
|
}
|
|
37
|
+
},
|
|
38
|
+
"topUps": {
|
|
39
|
+
"$ref": "#/definitions/topUps"
|
|
38
40
|
}
|
|
39
41
|
},
|
|
42
|
+
"anyOf": [
|
|
43
|
+
{
|
|
44
|
+
"type": "object",
|
|
45
|
+
"properties": {
|
|
46
|
+
"plans": {
|
|
47
|
+
"type": "object",
|
|
48
|
+
"minProperties": 1
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
"type": "object",
|
|
54
|
+
"required": [
|
|
55
|
+
"topUps"
|
|
56
|
+
],
|
|
57
|
+
"properties": {
|
|
58
|
+
"topUps": {
|
|
59
|
+
"type": "object"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
],
|
|
40
64
|
"definitions": {
|
|
41
65
|
"id": {
|
|
42
66
|
"type": "string",
|
|
@@ -143,6 +167,37 @@
|
|
|
143
167
|
"maximum": 1000000000
|
|
144
168
|
}
|
|
145
169
|
}
|
|
170
|
+
},
|
|
171
|
+
"topUps": {
|
|
172
|
+
"type": "object",
|
|
173
|
+
"additionalProperties": false,
|
|
174
|
+
"required": [
|
|
175
|
+
"name",
|
|
176
|
+
"currency",
|
|
177
|
+
"minimumAmount",
|
|
178
|
+
"maximumAmount"
|
|
179
|
+
],
|
|
180
|
+
"properties": {
|
|
181
|
+
"name": {
|
|
182
|
+
"type": "string",
|
|
183
|
+
"minLength": 1,
|
|
184
|
+
"maxLength": 200
|
|
185
|
+
},
|
|
186
|
+
"currency": {
|
|
187
|
+
"type": "string",
|
|
188
|
+
"pattern": "^[A-Z]{3}$"
|
|
189
|
+
},
|
|
190
|
+
"minimumAmount": {
|
|
191
|
+
"type": "integer",
|
|
192
|
+
"minimum": 50,
|
|
193
|
+
"maximum": 99999999
|
|
194
|
+
},
|
|
195
|
+
"maximumAmount": {
|
|
196
|
+
"type": "integer",
|
|
197
|
+
"minimum": 50,
|
|
198
|
+
"maximum": 99999999
|
|
199
|
+
}
|
|
200
|
+
}
|
|
146
201
|
}
|
|
147
202
|
}
|
|
148
203
|
}
|