@pplethai/cli 0.22.0 → 0.23.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.
Files changed (118) hide show
  1. package/README.md +12 -14
  2. package/dist/bin.js +102 -102
  3. package/package.json +3 -6
  4. package/templates/golden-path/.dev.vars.example +0 -33
  5. package/templates/golden-path/.env.example +0 -10
  6. package/templates/golden-path/.github/workflows/ci.yml +0 -22
  7. package/templates/golden-path/AGENTS.md +0 -1
  8. package/templates/golden-path/CLAUDE.md +0 -191
  9. package/templates/golden-path/README.md +0 -121
  10. package/templates/golden-path/gitignore +0 -17
  11. package/templates/golden-path/index.html +0 -16
  12. package/templates/golden-path/migrations/0001_init.sql +0 -10
  13. package/templates/golden-path/overlay-mtime.mjs +0 -30
  14. package/templates/golden-path/package-lock.json +0 -4318
  15. package/templates/golden-path/package.json +0 -41
  16. package/templates/golden-path/scripts/assert-dev-compiled-out.mjs +0 -45
  17. package/templates/golden-path/shared/api.ts +0 -325
  18. package/templates/golden-path/src/auth.ts +0 -230
  19. package/templates/golden-path/src/call-service.ts +0 -147
  20. package/templates/golden-path/src/dataset-query.ts +0 -470
  21. package/templates/golden-path/src/dataset-schema.ts +0 -398
  22. package/templates/golden-path/src/db.ts +0 -32
  23. package/templates/golden-path/src/dev-call-service.ts +0 -392
  24. package/templates/golden-path/src/dev-personas.ts +0 -186
  25. package/templates/golden-path/src/dev-read-dataset.ts +0 -444
  26. package/templates/golden-path/src/dev-registry.ts +0 -294
  27. package/templates/golden-path/src/email.ts +0 -49
  28. package/templates/golden-path/src/entry-name.ts +0 -114
  29. package/templates/golden-path/src/env.ts +0 -38
  30. package/templates/golden-path/src/files.ts +0 -38
  31. package/templates/golden-path/src/identity-header.ts +0 -17
  32. package/templates/golden-path/src/index.ts +0 -282
  33. package/templates/golden-path/src/legal-identity.ts +0 -180
  34. package/templates/golden-path/src/levels.ts +0 -132
  35. package/templates/golden-path/src/notification-response.ts +0 -55
  36. package/templates/golden-path/src/notify.ts +0 -311
  37. package/templates/golden-path/src/party-email.ts +0 -183
  38. package/templates/golden-path/src/payment.ts +0 -676
  39. package/templates/golden-path/src/read-dataset.ts +0 -490
  40. package/templates/golden-path/src/registry.ts +0 -128
  41. package/templates/golden-path/src/vite-env.d.ts +0 -27
  42. package/templates/golden-path/test/apply-migrations.ts +0 -4
  43. package/templates/golden-path/test/call-service.test.ts +0 -288
  44. package/templates/golden-path/test/dev-call-service.test.ts +0 -455
  45. package/templates/golden-path/test/dev-read-dataset.test.ts +0 -379
  46. package/templates/golden-path/test/dev-registry.test.ts +0 -404
  47. package/templates/golden-path/test/effective-sub.test.ts +0 -101
  48. package/templates/golden-path/test/email-offline.test.ts +0 -51
  49. package/templates/golden-path/test/email.test.ts +0 -117
  50. package/templates/golden-path/test/env.d.ts +0 -17
  51. package/templates/golden-path/test/fixtures/overlay/branch-list/entry.md +0 -85
  52. package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/audience.json +0 -3
  53. package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/named.json +0 -3
  54. package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/offices.json +0 -6
  55. package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/roster.json +0 -3
  56. package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/summary.json +0 -1
  57. package/templates/golden-path/test/fixtures/overlay/catalog/entry.md +0 -8
  58. package/templates/golden-path/test/fixtures/overlay/catalog/mocks/responses.json +0 -7
  59. package/templates/golden-path/test/fixtures/overlay/election/2027/score/entry.md +0 -11
  60. package/templates/golden-path/test/fixtures/overlay/election/2027/score/mocks/responses.json +0 -7
  61. package/templates/golden-path/test/fixtures/overlay/election/results/entry.md +0 -24
  62. package/templates/golden-path/test/fixtures/overlay/election/results/mocks/turnout.json +0 -1
  63. package/templates/golden-path/test/fixtures/overlay/orders/entry.md +0 -12
  64. package/templates/golden-path/test/fixtures/overlay/orders/mocks/responses.json +0 -36
  65. package/templates/golden-path/test/fixtures/overlay/platform/email/entry.md +0 -12
  66. package/templates/golden-path/test/fixtures/overlay/platform/email/mocks/responses.json +0 -11
  67. package/templates/golden-path/test/fixtures/overlay/platform/legal-identity/entry.md +0 -8
  68. package/templates/golden-path/test/fixtures/overlay/platform/legal-identity/mocks/responses.json +0 -13
  69. package/templates/golden-path/test/fixtures/overlay/platform/payments/entry.md +0 -12
  70. package/templates/golden-path/test/fixtures/overlay/platform/payments/mocks/responses.json +0 -98
  71. package/templates/golden-path/test/fixtures/overlay/platform/sms/entry.md +0 -10
  72. package/templates/golden-path/test/fixtures/overlay/platform/sms/mocks/responses.json +0 -7
  73. package/templates/golden-path/test/fixtures/overlay/score/entry.md +0 -10
  74. package/templates/golden-path/test/fixtures/overlay/score/mocks/responses.json +0 -7
  75. package/templates/golden-path/test/harness.ts +0 -153
  76. package/templates/golden-path/test/interaction.test.ts +0 -230
  77. package/templates/golden-path/test/legal-identity.test.ts +0 -323
  78. package/templates/golden-path/test/membership.test.ts +0 -105
  79. package/templates/golden-path/test/notification-response.test.ts +0 -79
  80. package/templates/golden-path/test/payment-app.test.ts +0 -211
  81. package/templates/golden-path/test/payment-offline.test.ts +0 -91
  82. package/templates/golden-path/test/payment.test.ts +0 -518
  83. package/templates/golden-path/test/payments-offline.ts +0 -33
  84. package/templates/golden-path/test/read-dataset.test.ts +0 -387
  85. package/templates/golden-path/test/service-call.test.ts +0 -166
  86. package/templates/golden-path/test/smoke.test.ts +0 -464
  87. package/templates/golden-path/tsconfig.json +0 -20
  88. package/templates/golden-path/tsconfig.worker.json +0 -21
  89. package/templates/golden-path/vite.config.ts +0 -12
  90. package/templates/golden-path/vitest.config.ts +0 -28
  91. package/templates/golden-path/web/App.tsx +0 -478
  92. package/templates/golden-path/web/app.css +0 -83
  93. package/templates/golden-path/web/main.tsx +0 -51
  94. package/templates/golden-path/web/pple.ts +0 -124
  95. package/templates/golden-path/web/vite-env.d.ts +0 -10
  96. package/templates/golden-path/wrangler.jsonc +0 -89
  97. package/templates/payment-bridge/.github/workflows/ci.yml +0 -21
  98. package/templates/payment-bridge/CLAUDE.md +0 -9
  99. package/templates/payment-bridge/README.md +0 -57
  100. package/templates/payment-bridge/docs/verify-webhook.md +0 -80
  101. package/templates/payment-bridge/docs/verify-webhook.ts +0 -29
  102. package/templates/payment-bridge/gitignore +0 -5
  103. package/templates/payment-bridge/package-lock.json +0 -4198
  104. package/templates/payment-bridge/package.json +0 -21
  105. package/templates/payment-bridge/src/bridge-key.ts +0 -18
  106. package/templates/payment-bridge/src/env.ts +0 -14
  107. package/templates/payment-bridge/src/index.ts +0 -39
  108. package/templates/payment-bridge/src/platform.ts +0 -49
  109. package/templates/payment-bridge/src/webhook.ts +0 -86
  110. package/templates/payment-bridge/test/docs.test.ts +0 -9
  111. package/templates/payment-bridge/test/fixtures/payments-responses.json +0 -98
  112. package/templates/payment-bridge/test/harness.ts +0 -78
  113. package/templates/payment-bridge/test/inbound.test.ts +0 -138
  114. package/templates/payment-bridge/test/webhook.test.ts +0 -174
  115. package/templates/payment-bridge/tsconfig.json +0 -22
  116. package/templates/payment-bridge/vitest.config.ts +0 -8
  117. package/templates/payment-bridge/wrangler.jsonc +0 -15
  118. package/templates/payment-bridge.version +0 -1
@@ -1,518 +0,0 @@
1
- // Payment helpers (docs/adr/0037). Each helper wraps one call to the Private
2
- // Platform Service `platform/payments` and turns every answer the Service or
3
- // the gate can give into a named reason, so a Builder switches on `reason`
4
- // and never reads a status. The fake Service below answers as the real one
5
- // does (registry/payments/entry.md).
6
-
7
- import { describe, expect, it } from "vitest";
8
- import {
9
- createCheckoutPass,
10
- createPayment,
11
- getPayment,
12
- issueReceipt,
13
- acceptPayment,
14
- paymentHandler,
15
- rejectPayment,
16
- verifyPaymentEvent,
17
- type CreatePaymentInput,
18
- type PaymentEvent,
19
- type PaymentPaidEvent,
20
- type PaymentRecord,
21
- } from "../src/payment";
22
- import { makeHarness, serviceRefusal, testExecutionContext } from "./harness";
23
-
24
- const INPUT: CreatePaymentInput = {
25
- kind: "donation",
26
- amountSatang: 50_000,
27
- description: "บริจาคสนับสนุนพรรค",
28
- reference: "order-1",
29
- returnUrl: "https://my-app.ppleth.ai/thanks",
30
- };
31
-
32
- const SERVICES = { PPLE_SERVICES_URL: "https://services.test", PPLE_APP_KEY: "app-key", DEV_PERSONA: "none" };
33
-
34
- describe("createPayment", () => {
35
- it("asks platform/payments for a Payment and answers the Checkout address to send the Payer to", async () => {
36
- let body: unknown;
37
- const h = makeHarness(
38
- {
39
- services: async (req) => {
40
- body = await req.json();
41
- return Response.json(
42
- { id: "pay_1", checkoutUrl: "https://pay.example.test/pay_1", state: "created" },
43
- { status: 201 },
44
- );
45
- },
46
- },
47
- SERVICES,
48
- );
49
-
50
- const result = await createPayment(h.testEnv, INPUT, h.fetchImpl);
51
-
52
- expect(result).toEqual({
53
- ok: true,
54
- id: "pay_1",
55
- checkoutUrl: "https://pay.example.test/pay_1",
56
- state: "created",
57
- });
58
- expect(h.calls.services).toEqual([
59
- expect.objectContaining({ path: "/platform/payments", method: "POST" }),
60
- ]);
61
- expect(body).toEqual(INPUT);
62
- });
63
-
64
- it("sends the optional `test` flag through, so a Live app can ask for a test Payment", async () => {
65
- let body: unknown;
66
- const h = makeHarness(
67
- {
68
- services: async (req) => {
69
- body = await req.json();
70
- return Response.json({ id: "pay_2", checkoutUrl: "https://pay.example.test/pay_2", state: "created" }, { status: 201 });
71
- },
72
- },
73
- SERVICES,
74
- );
75
- const result = await createPayment(h.testEnv, { ...INPUT, test: true }, h.fetchImpl);
76
- expect(result).toMatchObject({ ok: true, id: "pay_2" });
77
- expect(body).toEqual({ ...INPUT, test: true });
78
- });
79
-
80
- describe("expiresAt (#467)", () => {
81
- const sent = async (expiresAt: string | Date) => {
82
- let body: Record<string, unknown> = {};
83
- const h = makeHarness(
84
- {
85
- services: async (req) => {
86
- body = (await req.json()) as Record<string, unknown>;
87
- return Response.json({ id: "pay_1", checkoutUrl: "https://pay.example.test/pay_1", state: "created" }, { status: 201 });
88
- },
89
- },
90
- SERVICES,
91
- );
92
- const result = await createPayment(h.testEnv, { ...INPUT, expiresAt }, h.fetchImpl);
93
- return { result, body };
94
- };
95
-
96
- it("sends a string with its offset as written", async () => {
97
- const { result, body } = await sent("2026-12-01T14:30:00+07:00");
98
- expect(result).toMatchObject({ ok: true });
99
- expect(body.expiresAt).toBe("2026-12-01T14:30:00+07:00");
100
- });
101
-
102
- it("sends a Date as an ISO 8601 instant", async () => {
103
- const { body } = await sent(new Date("2026-12-01T07:30:00.000Z"));
104
- expect(body.expiresAt).toBe("2026-12-01T07:30:00.000Z");
105
- });
106
-
107
- it("leaves expiresAt out when none is given", async () => {
108
- let body: Record<string, unknown> = {};
109
- const h = makeHarness(
110
- { services: async (req) => ((body = (await req.json()) as Record<string, unknown>), Response.json({ id: "pay_1", checkoutUrl: "https://pay.example.test/pay_1" }, { status: 201 })) },
111
- SERVICES,
112
- );
113
- await createPayment(h.testEnv, INPUT, h.fetchImpl);
114
- expect("expiresAt" in body).toBe(false);
115
- });
116
-
117
- it("refuses an invalid Date without calling the platform, naming the field", async () => {
118
- const h = makeHarness({ services: () => Response.json({}, { status: 500 }) }, SERVICES);
119
- const result = await createPayment(h.testEnv, { ...INPUT, expiresAt: new Date("nope") }, h.fetchImpl);
120
- expect(result).toEqual({ ok: false, reason: "invalid_payment", field: "expiresAt" });
121
- expect(h.calls.services).toEqual([]);
122
- });
123
- });
124
- });
125
-
126
- describe("acceptPayment", () => {
127
- it("asks the platform to accept a Late Payment and answers the Payment, now paid", async () => {
128
- const h = makeHarness({ services: () => Response.json({ ...LATE, state: "paid" }) }, SERVICES);
129
-
130
- const result = await acceptPayment(h.testEnv, "pay_1", h.fetchImpl);
131
-
132
- expect(result).toMatchObject({ ok: true, payment: { state: "paid" } });
133
- expect(h.calls.services).toEqual([expect.objectContaining({ path: "/platform/payments/pay_1/accept", method: "POST" })]);
134
- });
135
-
136
- it("says not_late for a Payment that is not a Late Payment, and not_found for another app's", async () => {
137
- const notLate = makeHarness({ services: () => Response.json({ error: "not_late" }, { status: 409 }) }, SERVICES);
138
- const missing = makeHarness({ services: () => Response.json({ error: "not_found" }, { status: 404 }) }, SERVICES);
139
-
140
- expect(await acceptPayment(notLate.testEnv, "pay_1", notLate.fetchImpl)).toEqual({ ok: false, reason: "not_late" });
141
- expect(await acceptPayment(missing.testEnv, "pay_1", missing.fetchImpl)).toEqual({ ok: false, reason: "not_found" });
142
- });
143
- });
144
-
145
- const PAID: PaymentRecord = {
146
- id: "pay_1",
147
- kind: "donation",
148
- amountSatang: 50_000,
149
- description: "บริจาคสนับสนุนพรรค",
150
- reference: "order-1",
151
- state: "paid",
152
- mode: "test",
153
- receiptTiming: "later",
154
- payer: { name: "นางสาวสมหญิง ใจดี", maskedCitizenId: "1-1037-xxxxx-56-1" },
155
- voucher: { number: "TEST-PV-000001", nameTh: "หลักฐานการรับชำระเงิน", isReceipt: false },
156
- receipt: null,
157
- expiresAt: null,
158
- paidAt: "2026-11-15T09:00:00.000Z",
159
- refusal: null,
160
- undecided: false,
161
- refund: null,
162
- };
163
-
164
- const RECEIPT = { id: "rcp_1", number: "TEST-2569-000001", isTest: true, issuedAt: "2026-11-15T09:01:00.000Z", replaces: null };
165
-
166
- describe("createCheckoutPass", () => {
167
- it("asks for a pass with the Payer's token, and answers the address to send them to", async () => {
168
- let seen: { token: string | null; auth: string | null; body: string } | undefined;
169
- const h = makeHarness(
170
- {
171
- services: async (req) => {
172
- seen = {
173
- token: req.headers.get("x-pple-user-token"),
174
- auth: req.headers.get("authorization"),
175
- body: await req.text(),
176
- };
177
- return Response.json({ checkoutUrl: "https://pay.example.test/pay_1", pass: "abc" }, { status: 201 });
178
- },
179
- },
180
- SERVICES,
181
- );
182
-
183
- const result = await createCheckoutPass(h.testEnv, "pay_1", "payer-token", h.fetchImpl);
184
-
185
- expect(result).toEqual({ ok: true, checkoutUrl: "https://pay.example.test/pay_1", pass: "abc" });
186
- expect(h.calls.services).toEqual([
187
- expect.objectContaining({ path: "/platform/payments/pay_1/checkout-pass", method: "POST" }),
188
- ]);
189
- // The user token travels as the Payer's own; the App Key still names the app.
190
- expect(seen).toEqual({ token: "payer-token", auth: "Bearer app-key", body: "" });
191
- });
192
-
193
- it("sends no token with any other call", async () => {
194
- let token: string | null = "unset";
195
- const h = makeHarness(
196
- {
197
- services: (req) => {
198
- token = req.headers.get("x-pple-user-token");
199
- return Response.json({}, { status: 404 });
200
- },
201
- },
202
- SERVICES,
203
- );
204
-
205
- await getPayment(h.testEnv, "pay_1", h.fetchImpl);
206
-
207
- expect(token).toBeNull();
208
- });
209
-
210
- const refusals: [string, Response, string][] = [
211
- ["a token the platform refuses", serviceRefusal("user_token_invalid", 401), "user_token_invalid"],
212
- ["the Service's own token refusal", Response.json({ error: "user_token_invalid" }, { status: 401 }), "user_token_invalid"],
213
- [
214
- "a Payment that belongs to another Payer",
215
- Response.json({ error: "payment_tied_to_another_payer" }, { status: 409 }),
216
- "not_your_payment",
217
- ],
218
- ["a Payment that is paid, refused or cancelled", Response.json({ error: "payment_ended" }, { status: 409 }), "not_payable"],
219
- ["a bare 409", new Response(null, { status: 409 }), "upstream_unavailable"],
220
- ["a Payment that is not there", Response.json({ error: "not_found" }, { status: 404 }), "not_found"],
221
- ["no Use Grant", serviceRefusal("not_granted", 403), "not_granted"],
222
- ["a Service that answers nonsense", new Response("oops", { status: 500 }), "upstream_unavailable"],
223
- ["a 201 with no address", Response.json({}, { status: 201 }), "upstream_unavailable"],
224
- ["a 201 with no pass", Response.json({ checkoutUrl: "https://pay.example.test/pay_1" }, { status: 201 }), "upstream_unavailable"],
225
- ];
226
-
227
- it.each(refusals)("names %s", async (_name, answer, reason) => {
228
- const h = makeHarness({ services: () => answer.clone() }, SERVICES);
229
-
230
- expect(await createCheckoutPass(h.testEnv, "pay_1", "payer-token", h.fetchImpl)).toEqual({ ok: false, reason });
231
- });
232
-
233
- it("does not call the platform without a token", async () => {
234
- const h = makeHarness({}, SERVICES);
235
-
236
- expect(await createCheckoutPass(h.testEnv, "pay_1", " ", h.fetchImpl)).toEqual({
237
- ok: false,
238
- reason: "user_token_invalid",
239
- });
240
- expect(h.calls.services).toEqual([]);
241
- });
242
-
243
- it("says not_configured, and calls nothing, when the platform bindings are missing", async () => {
244
- const h = makeHarness({}, { PPLE_SERVICES_URL: undefined, PPLE_APP_KEY: undefined, DEV_PERSONA: "none" });
245
-
246
- expect(await createCheckoutPass(h.testEnv, "pay_1", "payer-token", h.fetchImpl)).toEqual({
247
- ok: false,
248
- reason: "not_configured",
249
- });
250
- expect(h.calls.services).toEqual([]);
251
- });
252
- });
253
-
254
- describe("every helper names why the platform said no", () => {
255
- const cases: [string, Response, string, string?][] = [
256
- ["the gate refusing the App Key", serviceRefusal("invalid_app_key", 401), "invalid_app_key"],
257
- ["no Use Grant", serviceRefusal("not_granted", 403), "not_granted"],
258
- ["a Draft app that holds the grant", serviceRefusal("needs_beta", 403), "needs_beta"],
259
- ["the gate's other refusals", serviceRefusal("provider_unavailable", 502), "upstream_unavailable"],
260
- [
261
- "a bad field",
262
- Response.json({ error: "invalid_payment", field: "amountSatang" }, { status: 400 }),
263
- "invalid_payment",
264
- "amountSatang",
265
- ],
266
- ["no Checkout Page configured", Response.json({ error: "checkout_unavailable" }, { status: 503 }), "checkout_unavailable"],
267
- ["a Service that answers nonsense", new Response("oops", { status: 500 }), "upstream_unavailable"],
268
- ];
269
-
270
- it.each(cases)("%s", async (_name, answer, reason, field) => {
271
- const h = makeHarness({ services: () => answer.clone() }, SERVICES);
272
- const expected = { ok: false, reason, ...(field ? { field } : {}) };
273
-
274
- expect(await createPayment(h.testEnv, INPUT, h.fetchImpl)).toEqual(expected);
275
- expect(await getPayment(h.testEnv, "pay_1", h.fetchImpl)).toEqual(expected);
276
- expect(await issueReceipt(h.testEnv, "pay_1", h.fetchImpl)).toEqual(expected);
277
- expect(await rejectPayment(h.testEnv, "pay_1", h.fetchImpl)).toEqual(expected);
278
- });
279
-
280
- it("says not_configured, and calls nothing, when the platform bindings are missing", async () => {
281
- const h = makeHarness({}, { PPLE_SERVICES_URL: undefined, PPLE_APP_KEY: undefined, DEV_PERSONA: "none" });
282
-
283
- expect(await createPayment(h.testEnv, INPUT, h.fetchImpl)).toEqual({ ok: false, reason: "not_configured" });
284
- expect(h.calls.services).toEqual([]);
285
- });
286
-
287
- it("says upstream_unavailable when the call itself fails", async () => {
288
- const h = makeHarness({}, SERVICES);
289
- const down: typeof fetch = () => Promise.reject(new Error("offline"));
290
-
291
- expect(await getPayment(h.testEnv, "pay_1", down)).toEqual({ ok: false, reason: "upstream_unavailable" });
292
- });
293
- });
294
-
295
- describe("getPayment", () => {
296
- it("reads the Payment back, the one source of truth for its state", async () => {
297
- const h = makeHarness({ services: () => Response.json(PAID) }, SERVICES);
298
-
299
- expect(await getPayment(h.testEnv, "pay_1", h.fetchImpl)).toEqual({ ok: true, payment: PAID });
300
- expect(h.calls.services).toEqual([expect.objectContaining({ path: "/platform/payments/pay_1", method: "GET" })]);
301
- });
302
-
303
- it("says not_found for another app's Payment, or one that does not exist", async () => {
304
- const h = makeHarness({ services: () => Response.json({ error: "not_found" }, { status: 404 }) }, SERVICES);
305
-
306
- expect(await getPayment(h.testEnv, "pay_other", h.fetchImpl)).toEqual({ ok: false, reason: "not_found" });
307
- });
308
-
309
- it("never lets an id change the path it calls", async () => {
310
- const h = makeHarness({ services: () => Response.json({ error: "not_found" }, { status: 404 }) }, SERVICES);
311
-
312
- await getPayment(h.testEnv, "../x?y=1", h.fetchImpl);
313
-
314
- expect(h.calls.services[0]!.path).toBe("/platform/payments/..%2Fx%3Fy%3D1");
315
- });
316
- });
317
-
318
- describe("issueReceipt", () => {
319
- it("issues the Receipt, and says whether it was issued now or before", async () => {
320
- let answered = 0;
321
- const h = makeHarness(
322
- { services: () => Response.json({ receipt: RECEIPT }, { status: answered++ === 0 ? 201 : 200 }) },
323
- SERVICES,
324
- );
325
-
326
- expect(await issueReceipt(h.testEnv, "pay_1", h.fetchImpl)).toEqual({ ok: true, receipt: RECEIPT, issuedNow: true });
327
- expect(await issueReceipt(h.testEnv, "pay_1", h.fetchImpl)).toEqual({ ok: true, receipt: RECEIPT, issuedNow: false });
328
- expect(h.calls.services[0]).toMatchObject({ path: "/platform/payments/pay_1/receipt", method: "POST" });
329
- });
330
-
331
- it("says not_paid when there is nothing to issue a Receipt for", async () => {
332
- const h = makeHarness({ services: () => Response.json({ error: "not_paid" }, { status: 409 }) }, SERVICES);
333
-
334
- expect(await issueReceipt(h.testEnv, "pay_1", h.fetchImpl)).toEqual({ ok: false, reason: "not_paid" });
335
- });
336
- });
337
-
338
- describe("rejectPayment", () => {
339
- it("rejects a paid Payment and answers it, refunded or with a Refund Owed", async () => {
340
- const owed = { ...PAID, state: "rejected", refund: { status: "owed" } };
341
- const h = makeHarness({ services: () => Response.json(owed) }, SERVICES);
342
-
343
- expect(await rejectPayment(h.testEnv, "pay_1", h.fetchImpl)).toEqual({ ok: true, payment: owed });
344
- expect(h.calls.services[0]).toMatchObject({ path: "/platform/payments/pay_1/reject", method: "POST" });
345
- });
346
-
347
- it("says has_receipt once a Receipt exists: only a Finance Officer can refund it", async () => {
348
- const h = makeHarness({ services: () => Response.json({ error: "has_receipt" }, { status: 409 }) }, SERVICES);
349
-
350
- expect(await rejectPayment(h.testEnv, "pay_1", h.fetchImpl)).toEqual({ ok: false, reason: "has_receipt" });
351
- });
352
- });
353
-
354
- const PAID_EVENT: PaymentPaidEvent = {
355
- type: "payment.paid",
356
- id: "evt_1",
357
- payment: {
358
- id: "pay_1",
359
- kind: "donation",
360
- amountSatang: 50_000,
361
- description: "บริจาคสนับสนุนพรรค",
362
- reference: "order-1",
363
- mode: "test",
364
- expiresAt: null,
365
- },
366
- payer: PAID.payer,
367
- voucher: PAID.voucher,
368
- paidAt: PAID.paidAt,
369
- };
370
-
371
- /** A Payment whose money was confirmed after its expiresAt, held for the app. */
372
- const LATE: PaymentRecord = { ...PAID, state: "late", voucher: null, expiresAt: "2026-11-15T08:55:00.000Z" };
373
-
374
- describe("verifyPaymentEvent", () => {
375
- // The platform authenticates the delivery itself (the wrapper's own secret
376
- // never reaches your code), and your handler is handed the parsed event only.
377
- // To act on money, read the Payment back and compare: what the platform says
378
- // now is the truth, and it is what you get back.
379
- it("reads the Payment back and hands you the platform's version when the event matches it", async () => {
380
- const h = makeHarness({ services: () => Response.json(PAID) }, SERVICES);
381
-
382
- expect(await verifyPaymentEvent(h.testEnv, PAID_EVENT, h.fetchImpl)).toEqual({ ok: true, payment: PAID });
383
- expect(h.calls.services[0]).toMatchObject({ path: "/platform/payments/pay_1", method: "GET" });
384
- });
385
-
386
- it.each([
387
- ["another amount", { amountSatang: 1 }],
388
- ["another reference", { reference: "order-2" }],
389
- ["another kind", { kind: "membership_fee" }],
390
- ["another mode", { mode: "live" }],
391
- ])("refuses an event that claims %s than the Payment has", async (_name, claim) => {
392
- const h = makeHarness({ services: () => Response.json(PAID) }, SERVICES);
393
- const forged = { ...PAID_EVENT, payment: { ...PAID_EVENT.payment, ...claim } } as PaymentEvent;
394
-
395
- expect(await verifyPaymentEvent(h.testEnv, forged, h.fetchImpl)).toEqual({ ok: false, reason: "mismatch" });
396
- });
397
-
398
- it("refuses a paid event for a Payment that is not paid", async () => {
399
- const h = makeHarness({ services: () => Response.json({ ...PAID, state: "created", payer: null }) }, SERVICES);
400
-
401
- expect(await verifyPaymentEvent(h.testEnv, PAID_EVENT, h.fetchImpl)).toEqual({ ok: false, reason: "mismatch" });
402
- });
403
-
404
- it("accepts a late paid event for a Payment that has moved on since", async () => {
405
- const h = makeHarness({ services: () => Response.json({ ...PAID, state: "rejected" }) }, SERVICES);
406
-
407
- const result = await verifyPaymentEvent(h.testEnv, PAID_EVENT, h.fetchImpl);
408
-
409
- expect(result).toMatchObject({ ok: true, payment: { state: "rejected" } });
410
- });
411
-
412
- it.each([
413
- ["payment.refused", ["refused"], ["paid", "created"]],
414
- ["payment.expired", ["expired"], ["created", "pending", "refused", "paid", "late"]],
415
- ["payment.late", ["late", "paid", "rejected", "refunding", "refunded"], ["created", "pending", "refused", "expired"]],
416
- ["payment.rejected", ["rejected", "refunded"], ["paid"]],
417
- ["payment.refunded", ["refunded"], ["rejected", "paid"]],
418
- ["payment.receipt_issued", ["paid", "refunding", "refunded"], ["created", "refused"]],
419
- ["payment.receipt_voided", ["refunding", "refunded", "paid"], ["created", "refused"]],
420
- ])("checks the state %s implies", async (type, agrees, disagrees) => {
421
- const event = { ...PAID_EVENT, type } as unknown as PaymentEvent;
422
- for (const state of agrees) {
423
- const h = makeHarness({ services: () => Response.json({ ...PAID, state }) }, SERVICES);
424
- expect(await verifyPaymentEvent(h.testEnv, event, h.fetchImpl)).toMatchObject({ ok: true });
425
- }
426
- for (const state of disagrees) {
427
- const h = makeHarness({ services: () => Response.json({ ...PAID, state }) }, SERVICES);
428
- expect(await verifyPaymentEvent(h.testEnv, event, h.fetchImpl)).toEqual({ ok: false, reason: "mismatch" });
429
- }
430
- });
431
-
432
- it("says not_found for a Payment that is not this app's, and passes a refusal on", async () => {
433
- const missing = makeHarness({ services: () => Response.json({ error: "not_found" }, { status: 404 }) }, SERVICES);
434
- const down = makeHarness({ services: () => serviceRefusal("provider_unavailable", 502) }, SERVICES);
435
-
436
- expect(await verifyPaymentEvent(missing.testEnv, PAID_EVENT, missing.fetchImpl)).toEqual({ ok: false, reason: "not_found" });
437
- expect(await verifyPaymentEvent(down.testEnv, PAID_EVENT, down.fetchImpl)).toEqual({ ok: false, reason: "upstream_unavailable" });
438
- });
439
- });
440
-
441
- describe("paymentHandler", () => {
442
- const ctx = testExecutionContext();
443
- const env = makeHarness({}, SERVICES).testEnv;
444
-
445
- it("calls the function for the event's type, with the event typed for it", async () => {
446
- const seen: string[] = [];
447
- const handler = paymentHandler({
448
- "payment.paid": (event) => {
449
- seen.push(`${event.type}:${event.voucher?.number}`);
450
- },
451
- });
452
-
453
- expect(await handler(PAID_EVENT, env, ctx)).toBeUndefined();
454
- expect(seen).toEqual(["payment.paid:TEST-PV-000001"]);
455
- });
456
-
457
- it("hands over a payment.expired event and a payment.late event, each typed for it", async () => {
458
- const seen: string[] = [];
459
- const handler = paymentHandler({
460
- "payment.expired": (event) => {
461
- seen.push(`expired ${event.payment.id} ${event.expiredAt}`);
462
- },
463
- "payment.late": (event) => {
464
- seen.push(`late ${event.payment.id} ${event.payer?.name} ${event.paidAt}`);
465
- },
466
- });
467
- const expired = { type: "payment.expired", id: "evt_expired_pay_1", payment: PAID_EVENT.payment, expiredAt: "2026-11-15T10:00:00.000Z" } satisfies PaymentEvent;
468
- const late = { type: "payment.late", id: "evt_late_pay_1", payment: PAID_EVENT.payment, payer: PAID.payer, paidAt: "2026-11-15T09:00:00.000Z" } satisfies PaymentEvent;
469
-
470
- expect(await handler(expired, env, ctx)).toBeUndefined();
471
- expect(await handler(late, env, ctx)).toBeUndefined();
472
- expect(seen).toEqual(["expired pay_1 2026-11-15T10:00:00.000Z", "late pay_1 นางสาวสมหญิง ใจดี 2026-11-15T09:00:00.000Z"]);
473
- });
474
-
475
- it("hands over a Replacement Receipt's event with the Receipt it replaces", async () => {
476
- const seen: string[] = [];
477
- const handler = paymentHandler({
478
- "payment.receipt_issued": (event) => {
479
- seen.push(`${event.receipt.number} replaces ${event.receipt.replaces?.number ?? "nothing"}`);
480
- },
481
- });
482
- const replacement = {
483
- type: "payment.receipt_issued",
484
- id: "evt_receipt_issued_rcp_2",
485
- payment: PAID_EVENT.payment,
486
- payer: { name: "นางสาวสมหญิง ใจงาม", maskedCitizenId: "x-xxxx-xxxxx-78-9" },
487
- receipt: { ...RECEIPT, id: "rcp_2", number: "TEST-2569-000002", replaces: { id: "rcp_1", number: "TEST-2569-000001" } },
488
- } satisfies PaymentEvent;
489
-
490
- expect(await handler(replacement, env, ctx)).toBeUndefined();
491
- expect(await handler({ ...replacement, receipt: RECEIPT }, env, ctx)).toBeUndefined();
492
- expect(seen).toEqual(["TEST-2569-000002 replaces TEST-2569-000001", "TEST-2569-000001 replaces nothing"]);
493
- });
494
-
495
- it("answers 2xx to a type it does not handle, so a later outcome is not retried forever", async () => {
496
- const handler = paymentHandler({ "payment.paid": () => {} });
497
- const later = { ...PAID_EVENT, type: "payment.voided" } as unknown as PaymentEvent;
498
-
499
- expect(await handler({ ...PAID_EVENT, type: "payment.refunded", refundedAt: null } as PaymentEvent, env, ctx)).toBeUndefined();
500
- expect(await handler(later, env, ctx)).toBeUndefined();
501
- });
502
-
503
- it("keeps what the function returns: a non-2xx Response is 'not delivered' and is sent again", async () => {
504
- const handler = paymentHandler({ "payment.paid": () => new Response(null, { status: 503 }) });
505
-
506
- expect(((await handler(PAID_EVENT, env, ctx)) as Response).status).toBe(503);
507
- });
508
-
509
- it("lets a throw through, which the platform records as 'not delivered'", async () => {
510
- const handler = paymentHandler({
511
- "payment.paid": () => {
512
- throw new Error("db down");
513
- },
514
- });
515
-
516
- await expect(Promise.resolve().then(() => handler(PAID_EVENT, env, ctx))).rejects.toThrow("db down");
517
- });
518
- });
@@ -1,33 +0,0 @@
1
- // The `platform/payments` mocks, served the way `npm run dev` serves them
2
- // (ADR-0026): through the offline matcher, as a Persona. Tests use it as the
3
- // fake Platform Service, so a test runs the answers a Builder sees offline, not
4
- // a hand-written copy of them.
5
-
6
- import { answerFromOverlay } from "../src/dev-call-service";
7
-
8
- // The mocks as a fresh app has them (test/fixtures/overlay/platform/payments),
9
- // laid out as `pple registry` writes them into `.pple/registry/`. They are a
10
- // copy of the registry's own, which a Builder's app does not carry; the repo
11
- // keeps the copy equal to the original (recipes/registry-fixtures.test.ts).
12
- const files: Record<string, unknown> = Object.fromEntries(
13
- Object.entries(
14
- import.meta.glob("./fixtures/overlay/platform/payments/**/*", { eager: true, query: "?raw", import: "default" }),
15
- ).map(([file, text]) => [file.replace("./fixtures/", ""), text]),
16
- );
17
-
18
- /** Handles `{PPLE_SERVICES_URL}/platform/payments/…` as the mocks would for `persona`. */
19
- export function offlinePayments(persona: string): (req: Request) => Response {
20
- return (req) => {
21
- const prefix = "/platform/payments";
22
- const url = new URL(req.url);
23
- return answerFromOverlay(files, {
24
- entry: "platform/payments",
25
- path: url.pathname.slice(prefix.length),
26
- method: req.method,
27
- persona,
28
- // As `callService` offline does: the Payer's token, when one is sent.
29
- ...(req.headers.get("x-pple-user-token") ? { userToken: req.headers.get("x-pple-user-token")! } : {}),
30
- overlayRoot: "overlay/",
31
- });
32
- };
33
- }