@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
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { verifyWebhook } from "../docs/verify-webhook";
|
|
3
|
+
import { createBridge } from "../src/index";
|
|
4
|
+
import { APP_KEY, BRIDGE_KEY, WEBHOOK_SECRET, WEBHOOK_URL, recordingFetch, testEnv, testExecutionContext } from "./harness";
|
|
5
|
+
|
|
6
|
+
const ctx = testExecutionContext();
|
|
7
|
+
|
|
8
|
+
const PAID_EVENT = {
|
|
9
|
+
type: "payment.paid",
|
|
10
|
+
id: "evt_paid_pay_1",
|
|
11
|
+
payment: { id: "pay_1", kind: "donation", amountSatang: 50_000, description: "บริจาค", reference: "ref-1", mode: "live" },
|
|
12
|
+
payer: { name: "สมชาย ใจดี", maskedCitizenId: "1-xxxx-xxxxx-12-3" },
|
|
13
|
+
voucher: null,
|
|
14
|
+
paidAt: "2026-01-01T00:00:00.000Z",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
describe("relaying a payment event to the External System", () => {
|
|
18
|
+
it("POSTs the event unchanged to the webhook, signed, and answers 2xx when it is delivered", async () => {
|
|
19
|
+
const external = recordingFetch(() => new Response("ok", { status: 200 }));
|
|
20
|
+
const bridge = createBridge({ fetchImpl: external.fetchImpl, now: () => 1_800_000_000_000 });
|
|
21
|
+
|
|
22
|
+
const answer = await bridge.payment(PAID_EVENT as never, testEnv(), ctx);
|
|
23
|
+
|
|
24
|
+
expect(answer?.status).toBe(200);
|
|
25
|
+
expect(external.calls).toHaveLength(1);
|
|
26
|
+
const [seen] = external.calls;
|
|
27
|
+
expect(seen?.url).toBe(WEBHOOK_URL);
|
|
28
|
+
expect(seen?.method).toBe("POST");
|
|
29
|
+
expect(seen?.body).toBe(JSON.stringify(PAID_EVENT));
|
|
30
|
+
expect(seen?.headers.get("content-type")).toBe("application/json");
|
|
31
|
+
expect(seen?.headers.get("x-pple-webhook-id")).toBe("evt_paid_pay_1");
|
|
32
|
+
expect(seen?.headers.get("x-pple-webhook-timestamp")).toBe("1800000000");
|
|
33
|
+
// The documented check accepts it.
|
|
34
|
+
expect(
|
|
35
|
+
await verifyWebhook({
|
|
36
|
+
secret: WEBHOOK_SECRET,
|
|
37
|
+
body: seen!.body,
|
|
38
|
+
timestamp: seen!.headers.get("x-pple-webhook-timestamp")!,
|
|
39
|
+
signature: seen!.headers.get("x-pple-webhook-signature")!,
|
|
40
|
+
now: 1_800_000_000,
|
|
41
|
+
}),
|
|
42
|
+
).toBe(true);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("sends the same id on every retry of the same event, and a new timestamp and signature each time", async () => {
|
|
46
|
+
const external = recordingFetch(() => new Response(null, { status: 200 }));
|
|
47
|
+
let now = 1_800_000_000_000;
|
|
48
|
+
const bridge = createBridge({ fetchImpl: external.fetchImpl, now: () => now });
|
|
49
|
+
await bridge.payment(PAID_EVENT as never, testEnv(), ctx);
|
|
50
|
+
now += 300_000;
|
|
51
|
+
await bridge.payment(PAID_EVENT as never, testEnv(), ctx);
|
|
52
|
+
const [first, second] = external.calls;
|
|
53
|
+
expect(second?.headers.get("x-pple-webhook-id")).toBe(first?.headers.get("x-pple-webhook-id"));
|
|
54
|
+
expect(second?.body).toBe(first?.body);
|
|
55
|
+
expect(second?.headers.get("x-pple-webhook-timestamp")).not.toBe(first?.headers.get("x-pple-webhook-timestamp"));
|
|
56
|
+
expect(second?.headers.get("x-pple-webhook-signature")).not.toBe(first?.headers.get("x-pple-webhook-signature"));
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("is not verified by the documented check once the body, timestamp, secret or signature is wrong", async () => {
|
|
60
|
+
const external = recordingFetch(() => new Response(null, { status: 204 }));
|
|
61
|
+
const bridge = createBridge({ fetchImpl: external.fetchImpl, now: () => 1_800_000_000_000 });
|
|
62
|
+
await bridge.payment(PAID_EVENT as never, testEnv(), ctx);
|
|
63
|
+
const [seen] = external.calls;
|
|
64
|
+
const good = {
|
|
65
|
+
secret: WEBHOOK_SECRET,
|
|
66
|
+
body: seen!.body,
|
|
67
|
+
timestamp: seen!.headers.get("x-pple-webhook-timestamp")!,
|
|
68
|
+
signature: seen!.headers.get("x-pple-webhook-signature")!,
|
|
69
|
+
now: 1_800_000_000,
|
|
70
|
+
};
|
|
71
|
+
expect(await verifyWebhook(good)).toBe(true);
|
|
72
|
+
expect(await verifyWebhook({ ...good, body: good.body.replace("50000", "50001") })).toBe(false);
|
|
73
|
+
expect(await verifyWebhook({ ...good, timestamp: "1800000001" })).toBe(false);
|
|
74
|
+
expect(await verifyWebhook({ ...good, secret: BRIDGE_KEY })).toBe(false);
|
|
75
|
+
expect(await verifyWebhook({ ...good, signature: "v1=" + "0".repeat(64) })).toBe(false);
|
|
76
|
+
expect(await verifyWebhook({ ...good, signature: "garbage" })).toBe(false);
|
|
77
|
+
// A replay from long ago.
|
|
78
|
+
expect(await verifyWebhook({ ...good, now: 1_800_000_000 + 301 })).toBe(false);
|
|
79
|
+
expect(await verifyWebhook({ ...good, now: 1_800_000_000 + 301, toleranceSeconds: 600 })).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("never sends the App Key or the Bridge Key out", async () => {
|
|
83
|
+
const external = recordingFetch(() => new Response(null, { status: 200 }));
|
|
84
|
+
const bridge = createBridge({ fetchImpl: external.fetchImpl });
|
|
85
|
+
await bridge.payment(PAID_EVENT as never, testEnv(), ctx);
|
|
86
|
+
const [seen] = external.calls;
|
|
87
|
+
const everything = `${seen?.url} ${[...(seen?.headers ?? [])].join(" ")} ${seen?.body}`;
|
|
88
|
+
expect(everything).not.toContain(APP_KEY);
|
|
89
|
+
expect(everything).not.toContain(BRIDGE_KEY);
|
|
90
|
+
expect(seen?.headers.has("authorization")).toBe(false);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe("what the platform is told", () => {
|
|
95
|
+
const answerFor = async (answer: () => Response | Promise<Response>, env = testEnv(), options = {}) => {
|
|
96
|
+
const external = recordingFetch(answer);
|
|
97
|
+
const bridge = createBridge({ fetchImpl: external.fetchImpl, ...options });
|
|
98
|
+
return { answer: await bridge.payment(PAID_EVENT as never, env, ctx), calls: external.calls };
|
|
99
|
+
};
|
|
100
|
+
const delivered = (r: Response | void | undefined) => !!r && r.status >= 200 && r.status < 300;
|
|
101
|
+
|
|
102
|
+
it.each([200, 201, 202, 204])("a %i from the External System is delivered", async (status) => {
|
|
103
|
+
expect(delivered((await answerFor(() => new Response(null, { status }))).answer)).toBe(true);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it.each([301, 302, 400, 401, 404, 429, 500, 502, 503])("a %i from the External System is not delivered", async (status) => {
|
|
107
|
+
const { answer, calls } = await answerFor(() => new Response(null, { status, headers: { location: "https://elsewhere.test" } }));
|
|
108
|
+
expect(delivered(answer)).toBe(false);
|
|
109
|
+
expect(calls).toHaveLength(1); // a redirect is not followed with the signed body
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("a network error is not delivered", async () => {
|
|
113
|
+
const { answer } = await answerFor(() => { throw new TypeError("connection refused"); });
|
|
114
|
+
expect(delivered(answer)).toBe(false);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("a hang is not delivered, and the platform hears before its own 9 seconds are up", async () => {
|
|
118
|
+
const { answer } = await answerFor(
|
|
119
|
+
() => new Promise<Response>(() => {}),
|
|
120
|
+
testEnv(),
|
|
121
|
+
{ webhookTimeoutMs: 50 },
|
|
122
|
+
);
|
|
123
|
+
expect(delivered(answer)).toBe(false);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("the default timeout is under the platform's 9 seconds", async () => {
|
|
127
|
+
const { WEBHOOK_TIMEOUT_MS } = await import("../src/webhook");
|
|
128
|
+
expect(WEBHOOK_TIMEOUT_MS).toBeLessThan(9_000);
|
|
129
|
+
expect(WEBHOOK_TIMEOUT_MS).toBeGreaterThan(0);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it.each([
|
|
133
|
+
["no webhook URL", { WEBHOOK_URL: undefined }],
|
|
134
|
+
["an empty webhook URL", { WEBHOOK_URL: "" }],
|
|
135
|
+
["a webhook URL that is not https", { WEBHOOK_URL: "http://external.test/hook" }],
|
|
136
|
+
["a webhook URL that is not a URL", { WEBHOOK_URL: "not a url" }],
|
|
137
|
+
["no signing secret", { WEBHOOK_SECRET: undefined }],
|
|
138
|
+
])("%s is not delivered, and nothing is sent", async (_name, env) => {
|
|
139
|
+
const { answer, calls } = await answerFor(() => new Response(null, { status: 200 }), testEnv(env));
|
|
140
|
+
expect(delivered(answer)).toBe(false);
|
|
141
|
+
expect(calls).toEqual([]);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("an event with no id is not delivered", async () => {
|
|
145
|
+
const external = recordingFetch(() => new Response(null, { status: 200 }));
|
|
146
|
+
const bridge = createBridge({ fetchImpl: external.fetchImpl });
|
|
147
|
+
const { id: _id, ...noId } = PAID_EVENT;
|
|
148
|
+
expect(delivered(await bridge.payment(noId as never, testEnv(), ctx))).toBe(false);
|
|
149
|
+
expect(external.calls).toEqual([]);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe("what is logged", () => {
|
|
154
|
+
it("never a body, a key or a secret, on delivery or on failure", async () => {
|
|
155
|
+
const lines: string[] = [];
|
|
156
|
+
const spies = (["log", "info", "warn", "error", "debug"] as const).map((level) => {
|
|
157
|
+
const original = console[level];
|
|
158
|
+
console[level] = (...args: unknown[]) => void lines.push(args.map(String).join(" "));
|
|
159
|
+
return () => (console[level] = original);
|
|
160
|
+
});
|
|
161
|
+
try {
|
|
162
|
+
const failing = createBridge({ fetchImpl: async () => { throw new Error(`boom ${JSON.stringify(PAID_EVENT)}`); } });
|
|
163
|
+
await failing.payment(PAID_EVENT as never, testEnv(), ctx);
|
|
164
|
+
const refusing = createBridge({ fetchImpl: async () => new Response("payer สมชาย", { status: 500 }) });
|
|
165
|
+
await refusing.payment(PAID_EVENT as never, testEnv(), ctx);
|
|
166
|
+
const platform = createBridge({ fetchImpl: async () => { throw new Error(`down ${APP_KEY}`); } });
|
|
167
|
+
await platform.fetch(new Request("https://bridge.test/api/pay_1", { method: "POST", body: "สมชาย", headers: { authorization: `Bearer ${BRIDGE_KEY}` } }), testEnv(), ctx);
|
|
168
|
+
} finally {
|
|
169
|
+
spies.forEach((restore) => restore());
|
|
170
|
+
}
|
|
171
|
+
const logged = lines.join("\n");
|
|
172
|
+
for (const secret of ["สมชาย", "50000", APP_KEY, BRIDGE_KEY, WEBHOOK_SECRET]) expect(logged).not.toContain(secret);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// TypeScript config for the Worker, its tests and the signature check in docs/.
|
|
2
|
+
{
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"target": "es2022",
|
|
5
|
+
"lib": ["es2022"],
|
|
6
|
+
"module": "es2022",
|
|
7
|
+
"moduleResolution": "bundler",
|
|
8
|
+
"types": [
|
|
9
|
+
"@cloudflare/workers-types",
|
|
10
|
+
"@cloudflare/vitest-pool-workers/types",
|
|
11
|
+
"vite/client"
|
|
12
|
+
],
|
|
13
|
+
"strict": true,
|
|
14
|
+
"resolveJsonModule": true,
|
|
15
|
+
"noUncheckedIndexedAccess": true,
|
|
16
|
+
"noEmit": true,
|
|
17
|
+
"skipLibCheck": true,
|
|
18
|
+
"isolatedModules": true,
|
|
19
|
+
"forceConsistentCasingInFileNames": true
|
|
20
|
+
},
|
|
21
|
+
"include": ["src", "test", "docs", "recipes"]
|
|
22
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { cloudflareTest } from "@cloudflare/vitest-pool-workers";
|
|
2
|
+
import { defineConfig } from "vitest/config";
|
|
3
|
+
|
|
4
|
+
// Runs the tests inside the real Workers runtime (workerd), so the Web Crypto
|
|
5
|
+
// and `Response` behaviour they rely on is the production one.
|
|
6
|
+
export default defineConfig({
|
|
7
|
+
plugins: [cloudflareTest({ wrangler: { configPath: "./wrangler.jsonc" } })],
|
|
8
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// A Payment Bridge's Cloudflare configuration (ADR-0045). The platform uploads
|
|
2
|
+
// the Worker through the Cloudflare API and sets its bindings itself, so this
|
|
3
|
+
// file only drives local type generation and tests. There is no `assets`
|
|
4
|
+
// block: a Bridge serves no pages, and everything outside /api/* is a 404.
|
|
5
|
+
{
|
|
6
|
+
"name": "pple-app",
|
|
7
|
+
"main": "src/index.ts",
|
|
8
|
+
"compatibility_date": "2026-06-01"
|
|
9
|
+
|
|
10
|
+
// Config (set through the platform, never in this file):
|
|
11
|
+
// BRIDGE_KEY secret the key the External System calls /api/* with
|
|
12
|
+
// WEBHOOK_SECRET secret the key webhooks are signed with
|
|
13
|
+
// WEBHOOK_URL var where every payment event is POSTed (https)
|
|
14
|
+
// The App Key and PPLE_SERVICES_URL are bound by the platform.
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2
|