@pplethai/cli 0.20.0 → 0.21.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/README.md +5 -3
- package/dist/bin.js +132 -131
- package/package.json +1 -1
- package/templates/golden-path/src/payment.ts +16 -5
- package/templates/golden-path/test/payment.test.ts +16 -0
- package/templates/payment-bridge/.github/workflows/ci.yml +21 -0
- package/templates/payment-bridge/CLAUDE.md +9 -0
- package/templates/payment-bridge/README.md +57 -0
- package/templates/payment-bridge/docs/verify-webhook.md +80 -0
- package/templates/payment-bridge/docs/verify-webhook.ts +29 -0
- package/templates/payment-bridge/gitignore +5 -0
- package/templates/payment-bridge/package-lock.json +4198 -0
- package/templates/payment-bridge/package.json +21 -0
- package/templates/payment-bridge/src/bridge-key.ts +18 -0
- package/templates/payment-bridge/src/env.ts +14 -0
- package/templates/payment-bridge/src/index.ts +39 -0
- package/templates/payment-bridge/src/platform.ts +49 -0
- package/templates/payment-bridge/src/webhook.ts +86 -0
- package/templates/payment-bridge/test/docs.test.ts +9 -0
- package/templates/payment-bridge/test/fixtures/payments-responses.json +71 -0
- package/templates/payment-bridge/test/harness.ts +78 -0
- package/templates/payment-bridge/test/inbound.test.ts +138 -0
- package/templates/payment-bridge/test/webhook.test.ts +174 -0
- package/templates/payment-bridge/tsconfig.json +22 -0
- package/templates/payment-bridge/vitest.config.ts +8 -0
- package/templates/payment-bridge/wrangler.jsonc +15 -0
- package/templates/payment-bridge.version +1 -0
package/package.json
CHANGED
|
@@ -58,9 +58,12 @@
|
|
|
58
58
|
// the Replacement, whose `receipt.replaces` names the Receipt it replaces. The
|
|
59
59
|
// Payment stays `paid`; its lookup's `receipt` is now the Replacement.
|
|
60
60
|
//
|
|
61
|
-
// A `paid` event with `mode: "test"` is a sandbox payment
|
|
62
|
-
//
|
|
63
|
-
// app's
|
|
61
|
+
// A `paid` event with `mode: "test"` is a sandbox payment. No money moved and
|
|
62
|
+
// no legal Receipt will exist for it. A Draft or Beta app's Payments are always
|
|
63
|
+
// `test`; a Live app's are `live` unless it created the Payment with
|
|
64
|
+
// `test: true`, to try its payment flow in production. So check `payment.mode`
|
|
65
|
+
// (the verified payment from `verifyPaymentEvent`, not just the event) before you
|
|
66
|
+
// hand over anything real (a membership, goods, access) for a Payment.
|
|
64
67
|
|
|
65
68
|
import { callService, platformError, type CallServiceEnv, type PlatformErrorCode } from "./call-service";
|
|
66
69
|
import type { Env } from "./env";
|
|
@@ -107,10 +110,18 @@ interface PaymentInputBase {
|
|
|
107
110
|
reference: string;
|
|
108
111
|
/** Where the Checkout Page sends the Payer afterwards. `https` (`http://localhost` locally). */
|
|
109
112
|
returnUrl: string;
|
|
113
|
+
/**
|
|
114
|
+
* Ask for a test Payment: sandbox Gateway, `TEST-…` Receipt, no real money. Allowed
|
|
115
|
+
* in every tier, so a Live app can test its flow in production. Leave it out (or
|
|
116
|
+
* `false`) for the tier's own mode: `live` at Live, `test` in Draft and Beta (a
|
|
117
|
+
* `false` never makes a Draft or Beta Payment live). Check `payment.mode` before
|
|
118
|
+
* handing over anything real.
|
|
119
|
+
*/
|
|
120
|
+
test?: boolean;
|
|
110
121
|
}
|
|
111
122
|
|
|
112
123
|
/**
|
|
113
|
-
* What you send to create a Payment. Every field is required
|
|
124
|
+
* What you send to create a Payment. Every field is required except `test`, and each kind
|
|
114
125
|
* carries what its receipt prints: a sale its 1 to 12 lines (which must add up to
|
|
115
126
|
* `amountSatang` exactly), a fundraising its event, a membership its term. A
|
|
116
127
|
* field of another kind is refused (`invalid_payment` naming it).
|
|
@@ -481,7 +492,7 @@ export interface PaymentReceiptIssuedEvent {
|
|
|
481
492
|
id: string;
|
|
482
493
|
/** The Issuer's own number, e.g. `2569-000123`. A test Receipt is `TEST-2569-000001`. */
|
|
483
494
|
number: string;
|
|
484
|
-
/** True for a
|
|
495
|
+
/** True for a test Payment's Receipt: watermarked, never a legal number. */
|
|
485
496
|
isTest: boolean;
|
|
486
497
|
/** ISO 8601. */
|
|
487
498
|
issuedAt: string;
|
|
@@ -59,6 +59,22 @@ describe("createPayment", () => {
|
|
|
59
59
|
]);
|
|
60
60
|
expect(body).toEqual(INPUT);
|
|
61
61
|
});
|
|
62
|
+
|
|
63
|
+
it("sends the optional `test` flag through, so a Live app can ask for a test Payment", async () => {
|
|
64
|
+
let body: unknown;
|
|
65
|
+
const h = makeHarness(
|
|
66
|
+
{
|
|
67
|
+
services: async (req) => {
|
|
68
|
+
body = await req.json();
|
|
69
|
+
return Response.json({ id: "pay_2", checkoutUrl: "https://pay.example.test/pay_2", state: "created" }, { status: 201 });
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
SERVICES,
|
|
73
|
+
);
|
|
74
|
+
const result = await createPayment(h.testEnv, { ...INPUT, test: true }, h.fetchImpl);
|
|
75
|
+
expect(result).toMatchObject({ ok: true, id: "pay_2" });
|
|
76
|
+
expect(body).toEqual({ ...INPUT, test: true });
|
|
77
|
+
});
|
|
62
78
|
});
|
|
63
79
|
|
|
64
80
|
const PAID: PaymentRecord = {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# CI for a Payment Bridge. Runs on every push and pull request: type-checks the
|
|
2
|
+
# Worker, runs its tests in the Workers runtime, and builds the deployable bundle.
|
|
3
|
+
name: CI
|
|
4
|
+
|
|
5
|
+
on:
|
|
6
|
+
push:
|
|
7
|
+
pull_request:
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
check:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v4
|
|
14
|
+
- uses: actions/setup-node@v4
|
|
15
|
+
with:
|
|
16
|
+
node-version: 22
|
|
17
|
+
cache: npm
|
|
18
|
+
- run: npm ci
|
|
19
|
+
- run: npm run typecheck
|
|
20
|
+
- run: npm test
|
|
21
|
+
- run: npm run build
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Payment Bridge
|
|
2
|
+
|
|
3
|
+
Made and kept by the platform (docs/adr/0045). Deploy it unchanged; do not add
|
|
4
|
+
routes, pages, storage, logging or retries. A Bridge whose code drifts from the
|
|
5
|
+
template is flagged in its Code Review. Read README.md first.
|
|
6
|
+
|
|
7
|
+
Two things must stay true of any change: the Bridge never sends its App Key
|
|
8
|
+
anywhere but `platform/payments`, and it never logs a request body, an event or
|
|
9
|
+
a key.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Payment Bridge
|
|
2
|
+
|
|
3
|
+
A Builder App that lets one system outside the platform (an **External System**)
|
|
4
|
+
take Payments exactly as a Builder App does (docs/adr/0045). **Deploy it
|
|
5
|
+
unchanged.** It has no pages, no database, no files and no queue. A Bridge whose
|
|
6
|
+
code drifts from this template is flagged in its Code Review.
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
External System ──/api/* + Bridge Key──▶ Bridge ──+ App Key──▶ platform/payments
|
|
10
|
+
External System ◀── signed webhook ─────── Bridge ◀── payment(event) ── platform
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Inbound: `/api/*`
|
|
14
|
+
|
|
15
|
+
Call the Bridge with `Authorization: Bearer <Bridge Key>`. The Bridge makes the
|
|
16
|
+
same call on `platform/payments` with its App Key, and hands the platform's answer
|
|
17
|
+
back as it came (status, body and `x-pple-error` included). What it carries on:
|
|
18
|
+
the method, everything after `/api` and the query, the body, `content-type`,
|
|
19
|
+
`accept`, and the Payer's PPLE ID token. So the platform's payments docs apply
|
|
20
|
+
to you word for word, with one difference: send the token as `pple-user-token`,
|
|
21
|
+
not `x-pple-user-token` (the platform strips every `x-pple-*` header before it
|
|
22
|
+
reaches the Bridge, and the Bridge renames it on the way out).
|
|
23
|
+
`POST /api` is `POST platform/payments`, and `POST /api/<id>/checkout-pass` with
|
|
24
|
+
the Payer's PPLE ID token in `pple-user-token` is a Checkout Pass.
|
|
25
|
+
|
|
26
|
+
A missing or wrong Bridge Key is a `401`. A platform that cannot be reached is a
|
|
27
|
+
`502`. Every other path is a `404`.
|
|
28
|
+
|
|
29
|
+
## Outbound: the webhook
|
|
30
|
+
|
|
31
|
+
When the platform has an outcome for one of your Payments, it calls the Bridge's
|
|
32
|
+
`payment(event)`. The Bridge `POST`s that event, unchanged, to `WEBHOOK_URL` at
|
|
33
|
+
once, and answers the platform with your status: any `2xx` is delivered; anything
|
|
34
|
+
else, an error or no answer within seven seconds is not, and the platform sends
|
|
35
|
+
the event again with the same id. The Bridge keeps nothing between attempts.
|
|
36
|
+
|
|
37
|
+
Each webhook is signed. See [docs/verify-webhook.md](docs/verify-webhook.md) for
|
|
38
|
+
what to check on your side.
|
|
39
|
+
|
|
40
|
+
## Config
|
|
41
|
+
|
|
42
|
+
Set in the platform (App Environment), never in this repo:
|
|
43
|
+
|
|
44
|
+
| Key | Kind | |
|
|
45
|
+
| --- | --- | --- |
|
|
46
|
+
| `BRIDGE_KEY` | secret | What your External System calls `/api/*` with. |
|
|
47
|
+
| `WEBHOOK_SECRET` | secret | Signs every webhook. Not the Bridge Key. |
|
|
48
|
+
| `WEBHOOK_URL` | variable | Where events are `POST`ed. An `https` address. With none, every event is answered `503` and the platform sends it again later. |
|
|
49
|
+
|
|
50
|
+
The App Key and the services address are bound by the platform; the Bridge never
|
|
51
|
+
puts the App Key on anything it sends out and never logs a body.
|
|
52
|
+
|
|
53
|
+
## Develop
|
|
54
|
+
|
|
55
|
+
`npm run typecheck`, `npm test`, `npm run build`. There is no offline mode:
|
|
56
|
+
test against your deployed Bridge at Beta, on the Gateway's sandbox (a Draft Bridge's calls are refused
|
|
57
|
+
with `needs_beta`).
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# Checking a webhook from your Payment Bridge
|
|
2
|
+
|
|
3
|
+
Every payment event reaches your webhook address as a `POST` with the event as
|
|
4
|
+
JSON, exactly as the platform's payments docs describe it, and three headers:
|
|
5
|
+
|
|
6
|
+
| Header | What it is |
|
|
7
|
+
| --- | --- |
|
|
8
|
+
| `x-pple-webhook-id` | The event's id, a copy of `id` in the body. The same on every attempt to tell you the same event: use it to dedupe. |
|
|
9
|
+
| `x-pple-webhook-timestamp` | When this attempt was sent, in Unix seconds. A new value on every attempt. |
|
|
10
|
+
| `x-pple-webhook-signature` | `v1=` and the hex of `HMAC-SHA256(secret, timestamp + "." + body)`. |
|
|
11
|
+
|
|
12
|
+
`body` is the raw request body, byte for byte: read it as text before you parse
|
|
13
|
+
it. `secret` is the **signing secret** you were given with your Bridge (its
|
|
14
|
+
`WEBHOOK_SECRET`). It is not your Bridge Key, and it never leaves you and your
|
|
15
|
+
Bridge.
|
|
16
|
+
|
|
17
|
+
Check, in this order, before you trust the event:
|
|
18
|
+
|
|
19
|
+
1. The signature matches (constant-time compare).
|
|
20
|
+
2. The timestamp is within five minutes of now, so a copy captured earlier
|
|
21
|
+
cannot be played back to you.
|
|
22
|
+
3. Take the event id from the **body** (`id`), not the header: only the body is
|
|
23
|
+
signed. If you already handled it, answer `200` and do nothing else.
|
|
24
|
+
|
|
25
|
+
Then answer. Any `2xx` tells the platform you have the event. Anything else, or
|
|
26
|
+
no answer within about seven seconds, tells it you do not, and it sends the
|
|
27
|
+
event again every few minutes with the same id.
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
// How an External System checks a webhook from its Payment Bridge. Plain Web
|
|
31
|
+
// Crypto, so it runs unchanged in Node 20+, Deno, Bun and Cloudflare Workers.
|
|
32
|
+
|
|
33
|
+
/** Headers the Bridge sets: `x-pple-webhook-timestamp` and `x-pple-webhook-signature`. */
|
|
34
|
+
export async function verifyWebhook(input: {
|
|
35
|
+
/** The signing secret you were given with the Bridge (its `WEBHOOK_SECRET`). */
|
|
36
|
+
secret: string;
|
|
37
|
+
/** The request body exactly as received: the raw text, not parsed and re-written JSON. */
|
|
38
|
+
body: string;
|
|
39
|
+
/** The `x-pple-webhook-timestamp` header, in Unix seconds. */
|
|
40
|
+
timestamp: string;
|
|
41
|
+
/** The `x-pple-webhook-signature` header, `v1=<hex>`. */
|
|
42
|
+
signature: string;
|
|
43
|
+
/** Refuse a timestamp further than this from now. Every attempt is signed afresh. */
|
|
44
|
+
toleranceSeconds?: number;
|
|
45
|
+
/** Unix seconds. Tests only. */
|
|
46
|
+
now?: number;
|
|
47
|
+
}): Promise<boolean> {
|
|
48
|
+
const { secret, body, timestamp, signature, toleranceSeconds = 300 } = input;
|
|
49
|
+
const now = input.now ?? Math.floor(Date.now() / 1000);
|
|
50
|
+
if (!/^\d+$/.test(timestamp) || Math.abs(now - Number(timestamp)) > toleranceSeconds) return false;
|
|
51
|
+
const given = /^v1=([0-9a-f]{64})$/.exec(signature)?.[1];
|
|
52
|
+
if (!given) return false;
|
|
53
|
+
|
|
54
|
+
const key = await crypto.subtle.importKey("raw", new TextEncoder().encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["verify"]);
|
|
55
|
+
const bytes = Uint8Array.from(given.match(/../g)!, (pair) => parseInt(pair, 16));
|
|
56
|
+
// `verify` compares in constant time.
|
|
57
|
+
return crypto.subtle.verify("HMAC", key, bytes, new TextEncoder().encode(`${timestamp}.${body}`));
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Use it in your handler:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
const body = await request.text();
|
|
65
|
+
const ok = await verifyWebhook({
|
|
66
|
+
secret: process.env.PPLE_WEBHOOK_SECRET!,
|
|
67
|
+
body,
|
|
68
|
+
timestamp: request.headers.get("x-pple-webhook-timestamp") ?? "",
|
|
69
|
+
signature: request.headers.get("x-pple-webhook-signature") ?? "",
|
|
70
|
+
});
|
|
71
|
+
if (!ok) return new Response(null, { status: 401 });
|
|
72
|
+
const event = JSON.parse(body);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Why the signing secret is its own
|
|
76
|
+
|
|
77
|
+
The Bridge Key lets someone *call* the platform through your Bridge. The
|
|
78
|
+
signing secret lets someone *pretend to be* your Bridge. They are kept apart
|
|
79
|
+
so that a leak or a rotation of one does not touch the other, and the Owner
|
|
80
|
+
can change either on its own.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// How an External System checks a webhook from its Payment Bridge. Plain Web
|
|
2
|
+
// Crypto, so it runs unchanged in Node 20+, Deno, Bun and Cloudflare Workers.
|
|
3
|
+
|
|
4
|
+
/** Headers the Bridge sets: `x-pple-webhook-timestamp` and `x-pple-webhook-signature`. */
|
|
5
|
+
export async function verifyWebhook(input: {
|
|
6
|
+
/** The signing secret you were given with the Bridge (its `WEBHOOK_SECRET`). */
|
|
7
|
+
secret: string;
|
|
8
|
+
/** The request body exactly as received: the raw text, not parsed and re-written JSON. */
|
|
9
|
+
body: string;
|
|
10
|
+
/** The `x-pple-webhook-timestamp` header, in Unix seconds. */
|
|
11
|
+
timestamp: string;
|
|
12
|
+
/** The `x-pple-webhook-signature` header, `v1=<hex>`. */
|
|
13
|
+
signature: string;
|
|
14
|
+
/** Refuse a timestamp further than this from now. Every attempt is signed afresh. */
|
|
15
|
+
toleranceSeconds?: number;
|
|
16
|
+
/** Unix seconds. Tests only. */
|
|
17
|
+
now?: number;
|
|
18
|
+
}): Promise<boolean> {
|
|
19
|
+
const { secret, body, timestamp, signature, toleranceSeconds = 300 } = input;
|
|
20
|
+
const now = input.now ?? Math.floor(Date.now() / 1000);
|
|
21
|
+
if (!/^\d+$/.test(timestamp) || Math.abs(now - Number(timestamp)) > toleranceSeconds) return false;
|
|
22
|
+
const given = /^v1=([0-9a-f]{64})$/.exec(signature)?.[1];
|
|
23
|
+
if (!given) return false;
|
|
24
|
+
|
|
25
|
+
const key = await crypto.subtle.importKey("raw", new TextEncoder().encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["verify"]);
|
|
26
|
+
const bytes = Uint8Array.from(given.match(/../g)!, (pair) => parseInt(pair, 16));
|
|
27
|
+
// `verify` compares in constant time.
|
|
28
|
+
return crypto.subtle.verify("HMAC", key, bytes, new TextEncoder().encode(`${timestamp}.${body}`));
|
|
29
|
+
}
|