@pplethai/cli 0.19.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/CLAUDE.md +5 -1
- package/templates/golden-path/README.md +7 -1
- package/templates/golden-path/src/index.ts +20 -0
- package/templates/golden-path/src/payment.ts +79 -7
- package/templates/golden-path/test/fixtures/overlay/platform/payments/mocks/responses.json +9 -0
- package/templates/golden-path/test/payment-app.test.ts +45 -0
- package/templates/golden-path/test/payment-offline.test.ts +25 -1
- package/templates/golden-path/test/payment.test.ts +105 -0
- package/templates/golden-path/test/payments-offline.ts +2 -0
- package/templates/golden-path/web/pple.ts +26 -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
|
@@ -118,7 +118,11 @@ Where the token comes from:
|
|
|
118
118
|
membership fee (`term`) through the Private Platform
|
|
119
119
|
Service `platform/payments` (Use Grant needed; Beta or Live). `src/payment.ts`
|
|
120
120
|
has `createPayment` (send the Payer to its `checkoutUrl`; never build a card
|
|
121
|
-
form), `
|
|
121
|
+
form), `createCheckoutPass` (when the Payer is signed in and taps to pay, send
|
|
122
|
+
them with `goToCheckout(paymentId)` from `web/pple.ts`, which posts the pass as
|
|
123
|
+
a form from your page (never a link), so they skip signing in again; ask at
|
|
124
|
+
the tap, not at create time; `not_your_payment` is a `409`: the Payment is
|
|
125
|
+
someone else's; `POST /api/payments/:id/checkout` does it), `getPayment`, `issueReceipt`, `rejectPayment`, `verifyPaymentEvent`
|
|
122
126
|
and `paymentHandler`. The outcome arrives at `payment` on the default export
|
|
123
127
|
(`createPaymentHandler` in `src/index.ts` is the starting point): it verifies
|
|
124
128
|
the event and **does not issue a Receipt**: you choose, issue at payment or
|
|
@@ -101,7 +101,13 @@ you, so a freshly created app is ready to `npm run dev` straight away.)
|
|
|
101
101
|
|
|
102
102
|
- **Take a payment:** `createPayment` in `src/payment.ts` asks the platform's
|
|
103
103
|
`platform/payments` Service for a Payment; send the Payer to the
|
|
104
|
-
`checkoutUrl` it returns (never build a card form).
|
|
104
|
+
`checkoutUrl` it returns (never build a card form). When the Payer is signed
|
|
105
|
+
in, `createCheckoutPass` (`POST /api/payments/:id/checkout`) gives a
|
|
106
|
+
`checkoutUrl` and a one-use `pass` that skip signing in again: ask for it when
|
|
107
|
+
they tap to pay, post the pass as a form from your page (`goToCheckout` in
|
|
108
|
+
`web/pple.ts`, never a link),
|
|
109
|
+
and treat `not_your_payment` (`409`) as "this Payment is someone else's". The
|
|
110
|
+
outcome arrives at
|
|
105
111
|
`payment` on the default export, where `verifyPaymentEvent` checks it and
|
|
106
112
|
your code decides: `issueReceipt` or `rejectPayment`. The starter handler only
|
|
107
113
|
verifies and does not issue a Receipt for you. You only ever see a masked
|
|
@@ -13,6 +13,7 @@ import { devWorkerFetch } from "./dev-registry";
|
|
|
13
13
|
import { getFile, listFiles, putFile } from "./files";
|
|
14
14
|
import { closeInteraction, sendNotification } from "./notify";
|
|
15
15
|
import {
|
|
16
|
+
createCheckoutPass,
|
|
16
17
|
createPayment,
|
|
17
18
|
getPayment,
|
|
18
19
|
paymentHandler,
|
|
@@ -163,6 +164,21 @@ export function createApp(overrides: AppOverrides = {}) {
|
|
|
163
164
|
return c.json({ id: created.id, checkoutUrl: created.checkoutUrl }, 201);
|
|
164
165
|
});
|
|
165
166
|
|
|
167
|
+
// The Payer taps to pay: mint a Checkout Pass with their own token (ADR-0043).
|
|
168
|
+
// The page posts `pass` to `checkoutUrl` as a form, from this app's own
|
|
169
|
+
// address (`goToCheckout` in web/pple.ts), and they open the Checkout Page
|
|
170
|
+
// already signed in, with no PPLE ID screen. Ask now, not when you create the
|
|
171
|
+
// Payment: a pass lasts a few minutes and works once, and each call mints a
|
|
172
|
+
// fresh one. Without a token
|
|
173
|
+
// (or on `user_token_invalid`) fall back to the `checkoutUrl` the create call
|
|
174
|
+
// answered: the Payer then signs in on the Checkout Page.
|
|
175
|
+
app.post("/payments/:id/checkout", async (c) => {
|
|
176
|
+
const token = (c.req.header("authorization") ?? "").replace(/^Bearer\s+/i, "").trim();
|
|
177
|
+
const pass = await createCheckoutPass(c.env, c.req.param("id"), token, fetchImpl);
|
|
178
|
+
if (!pass.ok) return c.json({ error: pass.reason }, paymentStatus(pass.reason));
|
|
179
|
+
return c.json({ checkoutUrl: pass.checkoutUrl, pass: pass.pass }, 201);
|
|
180
|
+
});
|
|
181
|
+
|
|
166
182
|
app.get("/payments/:id", async (c) => {
|
|
167
183
|
const read = await getPayment(c.env, c.req.param("id"), fetchImpl);
|
|
168
184
|
if (!read.ok) return c.json({ error: read.reason }, paymentStatus(read.reason));
|
|
@@ -178,9 +194,13 @@ function paymentStatus(reason: PaymentReason): ContentfulStatusCode {
|
|
|
178
194
|
return 404;
|
|
179
195
|
case "invalid_payment":
|
|
180
196
|
return 400;
|
|
197
|
+
case "user_token_invalid":
|
|
198
|
+
return 401;
|
|
181
199
|
case "not_paid":
|
|
182
200
|
case "has_receipt":
|
|
183
201
|
case "mismatch":
|
|
202
|
+
case "not_your_payment":
|
|
203
|
+
case "not_payable":
|
|
184
204
|
return 409;
|
|
185
205
|
default:
|
|
186
206
|
// Not granted, still Draft, no Checkout Page, platform down: not the
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Payments (docs/adr/0037). The helpers below wrap the Private Platform Service
|
|
2
2
|
// `platform/payments`: `createPayment`, `getPayment`, `issueReceipt`,
|
|
3
|
-
// `rejectPayment`, `verifyPaymentEvent` and `paymentHandler`. Each answers
|
|
3
|
+
// `rejectPayment`, `createCheckoutPass`, `verifyPaymentEvent` and `paymentHandler`. Each answers
|
|
4
4
|
// `{ ok: true, … }` or `{ ok: false, reason }`. Offline (`npm run dev` with a
|
|
5
5
|
// Persona) they answer from the entry's mocks. The rest of this comment is the
|
|
6
6
|
// outcome the platform sends you.
|
|
@@ -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).
|
|
@@ -144,6 +155,9 @@ export type PaymentReason =
|
|
|
144
155
|
| "not_paid"
|
|
145
156
|
| "has_receipt"
|
|
146
157
|
| "mismatch"
|
|
158
|
+
| "user_token_invalid"
|
|
159
|
+
| "not_your_payment"
|
|
160
|
+
| "not_payable"
|
|
147
161
|
| "checkout_unavailable"
|
|
148
162
|
| "upstream_unavailable";
|
|
149
163
|
|
|
@@ -250,6 +264,49 @@ export async function rejectPayment(
|
|
|
250
264
|
return { ok: true, payment: answer.body as unknown as PaymentRecord };
|
|
251
265
|
}
|
|
252
266
|
|
|
267
|
+
/**
|
|
268
|
+
* Ask the platform for a **Checkout Pass**: the Checkout Page's address and a
|
|
269
|
+
* one-use `pass`. Your page posts `pass` to `checkoutUrl` as a form (the web's
|
|
270
|
+
* `goToCheckout` does), and the Payer opens the Checkout Page already signed in
|
|
271
|
+
* as themselves, with no PPLE ID screen (ADR-0043). Only a form posted from
|
|
272
|
+
* your app's own address redeems it: never put the pass in a link. Prefer it to the bare
|
|
273
|
+
* `checkoutUrl` of `createPayment` whenever the Payer is signed in.
|
|
274
|
+
*
|
|
275
|
+
* Call it when the Payer taps to pay, not when you create the Payment: a pass
|
|
276
|
+
* lasts a few minutes and works once, and each call mints a fresh one. Pass the
|
|
277
|
+
* Payer's own PPLE ID token (the one your app checked on this request). The
|
|
278
|
+
* platform checks it itself and ties the Payment to that Payer.
|
|
279
|
+
*
|
|
280
|
+
* - `user_token_invalid`: no token, or the platform refused it. Fall back to
|
|
281
|
+
* `createPayment`'s `checkoutUrl`: the Payer signs in on the Checkout Page.
|
|
282
|
+
* - `not_your_payment` (`409`): the Payment already belongs to another Payer.
|
|
283
|
+
* Do not retry: create a new Payment for this Payer, or tell them it is not theirs.
|
|
284
|
+
* - `not_payable`: the Payment is already paid or cancelled. Read it with
|
|
285
|
+
* `getPayment` and show its state.
|
|
286
|
+
* - `not_found`: no such Payment of yours.
|
|
287
|
+
*/
|
|
288
|
+
export async function createCheckoutPass(
|
|
289
|
+
env: PaymentsEnv,
|
|
290
|
+
id: string,
|
|
291
|
+
userToken: string,
|
|
292
|
+
fetchImpl?: typeof fetch,
|
|
293
|
+
): Promise<PaymentResult<{ checkoutUrl: string; pass: string }>> {
|
|
294
|
+
// Offline a Persona signs the Payer in and there is no token to send.
|
|
295
|
+
const offline = import.meta.env.DEV && env.DEV_PERSONA !== "none";
|
|
296
|
+
if (!offline && userToken.trim() === "") return refused("user_token_invalid");
|
|
297
|
+
const answer = await callPayments(
|
|
298
|
+
env,
|
|
299
|
+
`/${encodeURIComponent(id)}/checkout-pass`,
|
|
300
|
+
fetchImpl,
|
|
301
|
+
{ method: "POST" },
|
|
302
|
+
{ userToken, passRefusals: true },
|
|
303
|
+
);
|
|
304
|
+
if (!answer.ok) return answer;
|
|
305
|
+
const { checkoutUrl, pass } = answer.body;
|
|
306
|
+
if (typeof checkoutUrl !== "string" || typeof pass !== "string") return refused("upstream_unavailable");
|
|
307
|
+
return { ok: true, checkoutUrl, pass };
|
|
308
|
+
}
|
|
309
|
+
|
|
253
310
|
/** The states a Payment can be in once the platform has sent an event of that type, and any time after. */
|
|
254
311
|
const STATES_AFTER: Record<string, readonly PaymentRecord["state"][]> = {
|
|
255
312
|
"payment.paid": ["paid", "rejected", "refunding", "refunded"],
|
|
@@ -330,6 +387,7 @@ function platformReason(code: PlatformErrorCode | null): PaymentReason {
|
|
|
330
387
|
case "invalid_app_key":
|
|
331
388
|
case "not_granted":
|
|
332
389
|
case "needs_beta":
|
|
390
|
+
case "user_token_invalid":
|
|
333
391
|
return code;
|
|
334
392
|
default:
|
|
335
393
|
return "upstream_unavailable";
|
|
@@ -344,6 +402,12 @@ async function callPayments(
|
|
|
344
402
|
path: string,
|
|
345
403
|
fetchImpl: typeof fetch | undefined,
|
|
346
404
|
init: RequestInit,
|
|
405
|
+
call: {
|
|
406
|
+
/** The Payer's PPLE ID token. Only the Checkout Pass call sends one. */
|
|
407
|
+
userToken?: string;
|
|
408
|
+
/** The Checkout Pass route's own refusals: `user_token_invalid`, `payment_tied_to_another_payer` and `payment_ended`. */
|
|
409
|
+
passRefusals?: boolean;
|
|
410
|
+
} = {},
|
|
347
411
|
): Promise<Answer> {
|
|
348
412
|
// Offline (`npm run dev` with a Persona) `callService` answers from the mocks
|
|
349
413
|
// and needs no bindings.
|
|
@@ -352,7 +416,10 @@ async function callPayments(
|
|
|
352
416
|
|
|
353
417
|
let res: Response;
|
|
354
418
|
try {
|
|
355
|
-
res = await callService(env, "platform/payments", path, init,
|
|
419
|
+
res = await callService(env, "platform/payments", path, init, {
|
|
420
|
+
...(fetchImpl ? { fetchImpl } : {}),
|
|
421
|
+
...(call.userToken !== undefined ? { userToken: call.userToken } : {}),
|
|
422
|
+
});
|
|
356
423
|
} catch {
|
|
357
424
|
return refused("upstream_unavailable");
|
|
358
425
|
}
|
|
@@ -365,6 +432,11 @@ async function callPayments(
|
|
|
365
432
|
const error = typeof body.error === "string" ? body.error : undefined;
|
|
366
433
|
const reason = SERVICE_REASONS.find((r) => r === error);
|
|
367
434
|
if (reason) return refused(reason, typeof body.field === "string" ? body.field : undefined);
|
|
435
|
+
if (call.passRefusals && !res.ok) {
|
|
436
|
+
if (error === "user_token_invalid") return refused("user_token_invalid");
|
|
437
|
+
if (error === "payment_tied_to_another_payer") return refused("not_your_payment");
|
|
438
|
+
if (error === "payment_ended") return refused("not_payable");
|
|
439
|
+
}
|
|
368
440
|
if (!res.ok) return refused("upstream_unavailable");
|
|
369
441
|
return { ok: true, status: res.status, body };
|
|
370
442
|
}
|
|
@@ -420,7 +492,7 @@ export interface PaymentReceiptIssuedEvent {
|
|
|
420
492
|
id: string;
|
|
421
493
|
/** The Issuer's own number, e.g. `2569-000123`. A test Receipt is `TEST-2569-000001`. */
|
|
422
494
|
number: string;
|
|
423
|
-
/** True for a
|
|
495
|
+
/** True for a test Payment's Receipt: watermarked, never a legal number. */
|
|
424
496
|
isTest: boolean;
|
|
425
497
|
/** ISO 8601. */
|
|
426
498
|
issuedAt: string;
|
|
@@ -9,6 +9,15 @@
|
|
|
9
9
|
"state": "created"
|
|
10
10
|
}
|
|
11
11
|
},
|
|
12
|
+
{
|
|
13
|
+
"method": "POST",
|
|
14
|
+
"path": "/pay_offline-mock/checkout-pass",
|
|
15
|
+
"status": 201,
|
|
16
|
+
"body": {
|
|
17
|
+
"checkoutUrl": "https://pay.example.invalid/pay_offline-mock",
|
|
18
|
+
"pass": "offline-mock-pass"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
12
21
|
{
|
|
13
22
|
"method": "GET",
|
|
14
23
|
"path": "/pay_offline-mock",
|
|
@@ -67,6 +67,51 @@ describe("a fresh template app, offline", () => {
|
|
|
67
67
|
},
|
|
68
68
|
);
|
|
69
69
|
|
|
70
|
+
it.each(["hq-staff", "member", "kyc-verified-nonmember", "delegate"])(
|
|
71
|
+
"sends %s to checkout with a Checkout Pass when they tap to pay (ADR-0043)",
|
|
72
|
+
async (persona) => {
|
|
73
|
+
const h = makeHarness({ services: offlinePayments(persona) }, { ...SERVICES, DEV_PERSONA: persona });
|
|
74
|
+
|
|
75
|
+
// The Payer taps to pay: the app mints a pass now, not at create time.
|
|
76
|
+
const res = await h.request("/api/payments/pay_offline-mock/checkout", { method: "POST" });
|
|
77
|
+
|
|
78
|
+
expect(res.status).toBe(201);
|
|
79
|
+
expect(await res.json()).toEqual({ checkoutUrl: "https://pay.example.invalid/pay_offline-mock", pass: "offline-mock-pass" });
|
|
80
|
+
expect(h.calls.services.map((c) => `${c.method} ${c.path}`)).toEqual([
|
|
81
|
+
"POST /platform/payments/pay_offline-mock/checkout-pass",
|
|
82
|
+
]);
|
|
83
|
+
},
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
it("sends the Payer's own token with the pass call, and no other call carries it", async () => {
|
|
87
|
+
const tokens: Record<string, string | null> = {};
|
|
88
|
+
const h = makeHarness(
|
|
89
|
+
{
|
|
90
|
+
services: (req) => {
|
|
91
|
+
tokens[new URL(req.url).pathname] = req.headers.get("x-pple-user-token");
|
|
92
|
+
return Response.json({ checkoutUrl: "https://pay.example.test/pay_1", pass: "p" }, { status: 201 });
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
{ ...SERVICES, DEV_PERSONA: "member" },
|
|
96
|
+
);
|
|
97
|
+
const res = await h.request("/api/payments/pay_1/checkout", { method: "POST" }, "payer-token");
|
|
98
|
+
|
|
99
|
+
expect(res.status).toBe(201);
|
|
100
|
+
expect(tokens).toEqual({ "/platform/payments/pay_1/checkout-pass": "payer-token" });
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("tells the Payer when the Payment belongs to someone else (409)", async () => {
|
|
104
|
+
const h = makeHarness(
|
|
105
|
+
{ services: () => Response.json({ error: "payment_tied_to_another_payer" }, { status: 409 }) },
|
|
106
|
+
{ ...SERVICES, DEV_PERSONA: "member" },
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
const res = await h.request("/api/payments/pay_1/checkout", { method: "POST" });
|
|
110
|
+
|
|
111
|
+
expect(res.status).toBe(409);
|
|
112
|
+
expect(await res.json()).toEqual({ error: "not_your_payment" });
|
|
113
|
+
});
|
|
114
|
+
|
|
70
115
|
// The Checkout Page turns an unverified Payer away, so the app says so first.
|
|
71
116
|
// The mocks cannot: the Service never sees who is signed in.
|
|
72
117
|
it.each(["unverified", "kyc-pending"])("does not start a payment for %s: they must verify first", async (persona) => {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
import { describe, expect, it } from "vitest";
|
|
10
10
|
import { DEV_PERSONAS } from "../src/dev-personas";
|
|
11
|
-
import { createPayment, getPayment, issueReceipt, rejectPayment } from "../src/payment";
|
|
11
|
+
import { createCheckoutPass, createPayment, getPayment, issueReceipt, rejectPayment } from "../src/payment";
|
|
12
12
|
import { offlinePayments } from "./payments-offline";
|
|
13
13
|
|
|
14
14
|
/** `callService` as `npm run dev` runs it for this Persona. */
|
|
@@ -64,4 +64,28 @@ describe("offline payments", () => {
|
|
|
64
64
|
payment: { state: "refunded", refund: { status: "refunded" } },
|
|
65
65
|
});
|
|
66
66
|
});
|
|
67
|
+
|
|
68
|
+
// A signed-in Payer's app mints a pass the Checkout Page would redeem
|
|
69
|
+
// (ADR-0043). Offline there is no Checkout Page: the mock answers the address.
|
|
70
|
+
it.each([...personas, "intern"])("%s: a Checkout Pass is minted and answers an address to go to", async (persona) => {
|
|
71
|
+
const fetchImpl = offlineAs(persona);
|
|
72
|
+
|
|
73
|
+
const pass = await createCheckoutPass(env, "pay_offline-mock", "payer-token", fetchImpl);
|
|
74
|
+
|
|
75
|
+
expect(pass).toEqual({ ok: true, checkoutUrl: expect.stringMatching(/^https:\/\/.+\/pay_offline-mock$/), pass: "offline-mock-pass" });
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("mints a pass with no token while a Persona is signed in, as `npm run dev` does", async () => {
|
|
79
|
+
// `npm run dev` signs the Payer in as the Persona and sends no PPLE ID token,
|
|
80
|
+
// so a blank token is fine offline and refused against the real platform.
|
|
81
|
+
const fetchImpl = offlineAs("member");
|
|
82
|
+
|
|
83
|
+
expect(await createCheckoutPass({ ...env, DEV_PERSONA: "member" }, "pay_offline-mock", "", fetchImpl)).toMatchObject({
|
|
84
|
+
ok: true,
|
|
85
|
+
});
|
|
86
|
+
expect(await createCheckoutPass(env, "pay_offline-mock", "", fetchImpl)).toEqual({
|
|
87
|
+
ok: false,
|
|
88
|
+
reason: "user_token_invalid",
|
|
89
|
+
});
|
|
90
|
+
});
|
|
67
91
|
});
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import { describe, expect, it } from "vitest";
|
|
8
8
|
import {
|
|
9
|
+
createCheckoutPass,
|
|
9
10
|
createPayment,
|
|
10
11
|
getPayment,
|
|
11
12
|
issueReceipt,
|
|
@@ -58,6 +59,22 @@ describe("createPayment", () => {
|
|
|
58
59
|
]);
|
|
59
60
|
expect(body).toEqual(INPUT);
|
|
60
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
|
+
});
|
|
61
78
|
});
|
|
62
79
|
|
|
63
80
|
const PAID: PaymentRecord = {
|
|
@@ -79,6 +96,94 @@ const PAID: PaymentRecord = {
|
|
|
79
96
|
|
|
80
97
|
const RECEIPT = { id: "rcp_1", number: "TEST-2569-000001", isTest: true, issuedAt: "2026-11-15T09:01:00.000Z", replaces: null };
|
|
81
98
|
|
|
99
|
+
describe("createCheckoutPass", () => {
|
|
100
|
+
it("asks for a pass with the Payer's token, and answers the address to send them to", async () => {
|
|
101
|
+
let seen: { token: string | null; auth: string | null; body: string } | undefined;
|
|
102
|
+
const h = makeHarness(
|
|
103
|
+
{
|
|
104
|
+
services: async (req) => {
|
|
105
|
+
seen = {
|
|
106
|
+
token: req.headers.get("x-pple-user-token"),
|
|
107
|
+
auth: req.headers.get("authorization"),
|
|
108
|
+
body: await req.text(),
|
|
109
|
+
};
|
|
110
|
+
return Response.json({ checkoutUrl: "https://pay.example.test/pay_1", pass: "abc" }, { status: 201 });
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
SERVICES,
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
const result = await createCheckoutPass(h.testEnv, "pay_1", "payer-token", h.fetchImpl);
|
|
117
|
+
|
|
118
|
+
expect(result).toEqual({ ok: true, checkoutUrl: "https://pay.example.test/pay_1", pass: "abc" });
|
|
119
|
+
expect(h.calls.services).toEqual([
|
|
120
|
+
expect.objectContaining({ path: "/platform/payments/pay_1/checkout-pass", method: "POST" }),
|
|
121
|
+
]);
|
|
122
|
+
// The user token travels as the Payer's own; the App Key still names the app.
|
|
123
|
+
expect(seen).toEqual({ token: "payer-token", auth: "Bearer app-key", body: "" });
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("sends no token with any other call", async () => {
|
|
127
|
+
let token: string | null = "unset";
|
|
128
|
+
const h = makeHarness(
|
|
129
|
+
{
|
|
130
|
+
services: (req) => {
|
|
131
|
+
token = req.headers.get("x-pple-user-token");
|
|
132
|
+
return Response.json({}, { status: 404 });
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
SERVICES,
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
await getPayment(h.testEnv, "pay_1", h.fetchImpl);
|
|
139
|
+
|
|
140
|
+
expect(token).toBeNull();
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const refusals: [string, Response, string][] = [
|
|
144
|
+
["a token the platform refuses", serviceRefusal("user_token_invalid", 401), "user_token_invalid"],
|
|
145
|
+
["the Service's own token refusal", Response.json({ error: "user_token_invalid" }, { status: 401 }), "user_token_invalid"],
|
|
146
|
+
[
|
|
147
|
+
"a Payment that belongs to another Payer",
|
|
148
|
+
Response.json({ error: "payment_tied_to_another_payer" }, { status: 409 }),
|
|
149
|
+
"not_your_payment",
|
|
150
|
+
],
|
|
151
|
+
["a Payment that is paid, refused or cancelled", Response.json({ error: "payment_ended" }, { status: 409 }), "not_payable"],
|
|
152
|
+
["a bare 409", new Response(null, { status: 409 }), "upstream_unavailable"],
|
|
153
|
+
["a Payment that is not there", Response.json({ error: "not_found" }, { status: 404 }), "not_found"],
|
|
154
|
+
["no Use Grant", serviceRefusal("not_granted", 403), "not_granted"],
|
|
155
|
+
["a Service that answers nonsense", new Response("oops", { status: 500 }), "upstream_unavailable"],
|
|
156
|
+
["a 201 with no address", Response.json({}, { status: 201 }), "upstream_unavailable"],
|
|
157
|
+
["a 201 with no pass", Response.json({ checkoutUrl: "https://pay.example.test/pay_1" }, { status: 201 }), "upstream_unavailable"],
|
|
158
|
+
];
|
|
159
|
+
|
|
160
|
+
it.each(refusals)("names %s", async (_name, answer, reason) => {
|
|
161
|
+
const h = makeHarness({ services: () => answer.clone() }, SERVICES);
|
|
162
|
+
|
|
163
|
+
expect(await createCheckoutPass(h.testEnv, "pay_1", "payer-token", h.fetchImpl)).toEqual({ ok: false, reason });
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("does not call the platform without a token", async () => {
|
|
167
|
+
const h = makeHarness({}, SERVICES);
|
|
168
|
+
|
|
169
|
+
expect(await createCheckoutPass(h.testEnv, "pay_1", " ", h.fetchImpl)).toEqual({
|
|
170
|
+
ok: false,
|
|
171
|
+
reason: "user_token_invalid",
|
|
172
|
+
});
|
|
173
|
+
expect(h.calls.services).toEqual([]);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("says not_configured, and calls nothing, when the platform bindings are missing", async () => {
|
|
177
|
+
const h = makeHarness({}, { PPLE_SERVICES_URL: undefined, PPLE_APP_KEY: undefined, DEV_PERSONA: "none" });
|
|
178
|
+
|
|
179
|
+
expect(await createCheckoutPass(h.testEnv, "pay_1", "payer-token", h.fetchImpl)).toEqual({
|
|
180
|
+
ok: false,
|
|
181
|
+
reason: "not_configured",
|
|
182
|
+
});
|
|
183
|
+
expect(h.calls.services).toEqual([]);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
82
187
|
describe("every helper names why the platform said no", () => {
|
|
83
188
|
const cases: [string, Response, string, string?][] = [
|
|
84
189
|
["the gate refusing the App Key", serviceRefusal("invalid_app_key", 401), "invalid_app_key"],
|
|
@@ -25,6 +25,8 @@ export function offlinePayments(persona: string): (req: Request) => Response {
|
|
|
25
25
|
path: url.pathname.slice(prefix.length),
|
|
26
26
|
method: req.method,
|
|
27
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")! } : {}),
|
|
28
30
|
overlayRoot: "overlay/",
|
|
29
31
|
});
|
|
30
32
|
};
|
|
@@ -96,3 +96,29 @@ export async function apiJson<T>(path: string, init: RequestInit = {}): Promise<
|
|
|
96
96
|
}
|
|
97
97
|
return res.json() as Promise<T>;
|
|
98
98
|
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The Payer taps "pay": send them to the Checkout Page already signed in
|
|
102
|
+
* (a Checkout Pass, ADR-0043). Asks your Worker's `POST /api/payments/:id/checkout`
|
|
103
|
+
* for a fresh pass and posts it to the Checkout Page as a form, from this page,
|
|
104
|
+
* which is the only way the platform redeems one. On a refusal it answers the
|
|
105
|
+
* reason instead: on `user_token_invalid` send the Payer to the `checkoutUrl`
|
|
106
|
+
* `createPayment` answered (they sign in there); `not_your_payment` and
|
|
107
|
+
* `not_payable` are in the Skill's payments reference.
|
|
108
|
+
*/
|
|
109
|
+
export async function goToCheckout(paymentId: string): Promise<{ error: string }> {
|
|
110
|
+
const res = await api(`/api/payments/${encodeURIComponent(paymentId)}/checkout`, { method: "POST" });
|
|
111
|
+
const body = (await res.json().catch(() => ({}))) as { checkoutUrl?: string; pass?: string; error?: string };
|
|
112
|
+
if (res.status !== 201 || !body.checkoutUrl || !body.pass) return { error: body.error ?? `request failed (${res.status})` };
|
|
113
|
+
const form = document.createElement("form");
|
|
114
|
+
form.method = "POST";
|
|
115
|
+
form.action = body.checkoutUrl;
|
|
116
|
+
const field = document.createElement("input");
|
|
117
|
+
field.type = "hidden";
|
|
118
|
+
field.name = "pass";
|
|
119
|
+
field.value = body.pass;
|
|
120
|
+
form.append(field);
|
|
121
|
+
document.body.append(form);
|
|
122
|
+
form.submit();
|
|
123
|
+
return new Promise(() => {}); // the page is leaving
|
|
124
|
+
}
|
|
@@ -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`).
|