@pplethai/cli 0.20.0 → 0.22.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/CLAUDE.md +6 -2
- package/templates/golden-path/README.md +4 -1
- package/templates/golden-path/src/email.ts +4 -179
- package/templates/golden-path/src/index.ts +6 -0
- package/templates/golden-path/src/party-email.ts +183 -0
- package/templates/golden-path/src/payment.ts +162 -29
- package/templates/golden-path/test/fixtures/overlay/platform/payments/entry.md +1 -1
- package/templates/golden-path/test/fixtures/overlay/platform/payments/mocks/responses.json +27 -0
- package/templates/golden-path/test/payment-app.test.ts +1 -0
- package/templates/golden-path/test/payment.test.ts +107 -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 +98 -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
|
@@ -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
|
+
}
|