@pplethai/cli 0.18.0 → 0.20.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/dist/bin.js +2 -2
- package/package.json +1 -1
- package/templates/golden-path/CLAUDE.md +10 -1
- package/templates/golden-path/README.md +7 -1
- package/templates/golden-path/src/email.ts +224 -0
- package/templates/golden-path/src/index.ts +20 -0
- package/templates/golden-path/src/payment.ts +63 -2
- package/templates/golden-path/test/email.test.ts +117 -0
- package/templates/golden-path/test/fixtures/overlay/platform/payments/entry.md +2 -2
- 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 +89 -0
- package/templates/golden-path/test/payments-offline.ts +2 -0
- package/templates/golden-path/web/pple.ts +26 -0
|
@@ -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,
|
|
@@ -79,6 +80,94 @@ const PAID: PaymentRecord = {
|
|
|
79
80
|
|
|
80
81
|
const RECEIPT = { id: "rcp_1", number: "TEST-2569-000001", isTest: true, issuedAt: "2026-11-15T09:01:00.000Z", replaces: null };
|
|
81
82
|
|
|
83
|
+
describe("createCheckoutPass", () => {
|
|
84
|
+
it("asks for a pass with the Payer's token, and answers the address to send them to", async () => {
|
|
85
|
+
let seen: { token: string | null; auth: string | null; body: string } | undefined;
|
|
86
|
+
const h = makeHarness(
|
|
87
|
+
{
|
|
88
|
+
services: async (req) => {
|
|
89
|
+
seen = {
|
|
90
|
+
token: req.headers.get("x-pple-user-token"),
|
|
91
|
+
auth: req.headers.get("authorization"),
|
|
92
|
+
body: await req.text(),
|
|
93
|
+
};
|
|
94
|
+
return Response.json({ checkoutUrl: "https://pay.example.test/pay_1", pass: "abc" }, { status: 201 });
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
SERVICES,
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const result = await createCheckoutPass(h.testEnv, "pay_1", "payer-token", h.fetchImpl);
|
|
101
|
+
|
|
102
|
+
expect(result).toEqual({ ok: true, checkoutUrl: "https://pay.example.test/pay_1", pass: "abc" });
|
|
103
|
+
expect(h.calls.services).toEqual([
|
|
104
|
+
expect.objectContaining({ path: "/platform/payments/pay_1/checkout-pass", method: "POST" }),
|
|
105
|
+
]);
|
|
106
|
+
// The user token travels as the Payer's own; the App Key still names the app.
|
|
107
|
+
expect(seen).toEqual({ token: "payer-token", auth: "Bearer app-key", body: "" });
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("sends no token with any other call", async () => {
|
|
111
|
+
let token: string | null = "unset";
|
|
112
|
+
const h = makeHarness(
|
|
113
|
+
{
|
|
114
|
+
services: (req) => {
|
|
115
|
+
token = req.headers.get("x-pple-user-token");
|
|
116
|
+
return Response.json({}, { status: 404 });
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
SERVICES,
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
await getPayment(h.testEnv, "pay_1", h.fetchImpl);
|
|
123
|
+
|
|
124
|
+
expect(token).toBeNull();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const refusals: [string, Response, string][] = [
|
|
128
|
+
["a token the platform refuses", serviceRefusal("user_token_invalid", 401), "user_token_invalid"],
|
|
129
|
+
["the Service's own token refusal", Response.json({ error: "user_token_invalid" }, { status: 401 }), "user_token_invalid"],
|
|
130
|
+
[
|
|
131
|
+
"a Payment that belongs to another Payer",
|
|
132
|
+
Response.json({ error: "payment_tied_to_another_payer" }, { status: 409 }),
|
|
133
|
+
"not_your_payment",
|
|
134
|
+
],
|
|
135
|
+
["a Payment that is paid, refused or cancelled", Response.json({ error: "payment_ended" }, { status: 409 }), "not_payable"],
|
|
136
|
+
["a bare 409", new Response(null, { status: 409 }), "upstream_unavailable"],
|
|
137
|
+
["a Payment that is not there", Response.json({ error: "not_found" }, { status: 404 }), "not_found"],
|
|
138
|
+
["no Use Grant", serviceRefusal("not_granted", 403), "not_granted"],
|
|
139
|
+
["a Service that answers nonsense", new Response("oops", { status: 500 }), "upstream_unavailable"],
|
|
140
|
+
["a 201 with no address", Response.json({}, { status: 201 }), "upstream_unavailable"],
|
|
141
|
+
["a 201 with no pass", Response.json({ checkoutUrl: "https://pay.example.test/pay_1" }, { status: 201 }), "upstream_unavailable"],
|
|
142
|
+
];
|
|
143
|
+
|
|
144
|
+
it.each(refusals)("names %s", async (_name, answer, reason) => {
|
|
145
|
+
const h = makeHarness({ services: () => answer.clone() }, SERVICES);
|
|
146
|
+
|
|
147
|
+
expect(await createCheckoutPass(h.testEnv, "pay_1", "payer-token", h.fetchImpl)).toEqual({ ok: false, reason });
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("does not call the platform without a token", async () => {
|
|
151
|
+
const h = makeHarness({}, SERVICES);
|
|
152
|
+
|
|
153
|
+
expect(await createCheckoutPass(h.testEnv, "pay_1", " ", h.fetchImpl)).toEqual({
|
|
154
|
+
ok: false,
|
|
155
|
+
reason: "user_token_invalid",
|
|
156
|
+
});
|
|
157
|
+
expect(h.calls.services).toEqual([]);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("says not_configured, and calls nothing, when the platform bindings are missing", async () => {
|
|
161
|
+
const h = makeHarness({}, { PPLE_SERVICES_URL: undefined, PPLE_APP_KEY: undefined, DEV_PERSONA: "none" });
|
|
162
|
+
|
|
163
|
+
expect(await createCheckoutPass(h.testEnv, "pay_1", "payer-token", h.fetchImpl)).toEqual({
|
|
164
|
+
ok: false,
|
|
165
|
+
reason: "not_configured",
|
|
166
|
+
});
|
|
167
|
+
expect(h.calls.services).toEqual([]);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
82
171
|
describe("every helper names why the platform said no", () => {
|
|
83
172
|
const cases: [string, Response, string, string?][] = [
|
|
84
173
|
["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
|
+
}
|