@pplethai/cli 0.21.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/dist/bin.js +2 -2
- 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 +147 -25
- 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 +91 -0
- package/templates/payment-bridge/test/fixtures/payments-responses.json +27 -0
- package/templates/payment-bridge.version +1 -1
|
@@ -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`, `createCheckoutPass`, `verifyPaymentEvent` and `paymentHandler`. Each answers
|
|
3
|
+
// `rejectPayment`, `createCheckoutPass`, `acceptPayment`, `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.
|
|
@@ -24,9 +24,12 @@
|
|
|
24
24
|
// type you do not handle: later outcomes arrive as new types.
|
|
25
25
|
//
|
|
26
26
|
// A paid Payment has a Payment Voucher, which is not a receipt. The legal
|
|
27
|
-
// Receipt
|
|
28
|
-
//
|
|
29
|
-
//
|
|
27
|
+
// Receipt comes when the Payment's `receiptTiming` says. With
|
|
28
|
+
// `receiptTiming: "on_payment"` the platform issues it the moment the Payment is
|
|
29
|
+
// paid (and you cannot reject it then). Otherwise (`"later"`, the default) it
|
|
30
|
+
// is issued when YOU ask: `POST platform/payments/<id>/receipt`, from this
|
|
31
|
+
// handler or after your own checks. Asking twice is safe. Either way the
|
|
32
|
+
// platform then calls you with `payment.receipt_issued`.
|
|
30
33
|
//
|
|
31
34
|
// The event carries what the Receipt will print and no more: the Payer's full
|
|
32
35
|
// name and a masked citizen ID. You never receive the full citizen ID, and you
|
|
@@ -58,6 +61,32 @@
|
|
|
58
61
|
// the Replacement, whose `receipt.replaces` names the Receipt it replaces. The
|
|
59
62
|
// Payment stays `paid`; its lookup's `receipt` is now the Replacement.
|
|
60
63
|
//
|
|
64
|
+
// A Payment that expires. If your app holds something for the Payer while they
|
|
65
|
+
// pay (stock, a ticket, a slot), give `createPayment` an `expiresAt` (10 minutes
|
|
66
|
+
// to 30 days ahead, with an explicit offset). The Checkout Page then shows the
|
|
67
|
+
// Payer a deadline (a time, not a countdown), starts no payment within 2
|
|
68
|
+
// minutes of it, and every Charge is cut to end by it; a Checkout Pass
|
|
69
|
+
// asked for after it is `not_payable`. The Payment turns `expired` and you get
|
|
70
|
+
// `payment.expired` once nothing opened before `expiresAt` can still be paid:
|
|
71
|
+
// usually within one sweep (minutes) of `expiresAt`, never more than about 60
|
|
72
|
+
// minutes after it. So run your OWN timer for your hold, and release it at
|
|
73
|
+
// `expiresAt` without waiting for the event; treat the event as the platform's
|
|
74
|
+
// confirmation that no payment is pending.
|
|
75
|
+
//
|
|
76
|
+
// `expired` is final. Money the Gateway confirms AFTER `expiresAt` (a QR paid at
|
|
77
|
+
// the last second, a notification delayed past the deadline) does not pay the
|
|
78
|
+
// Payment: it becomes a **Late Payment**, state `late`, with no Payment Voucher
|
|
79
|
+
// yet, and you hear `payment.late` (payer name, `paidAt`). Your hold may be gone,
|
|
80
|
+
// so decide: `acceptPayment(id)` if you can still serve it (the Payment is `paid`,
|
|
81
|
+
// the Voucher is issued and you hear `payment.paid`), or `rejectPayment(id)` if you
|
|
82
|
+
// cannot (a card is refunded; a QR payment becomes a Refund Owed). Check your hold
|
|
83
|
+
// in your `payment.late` handler and answer one or the other. A Late Payment counts
|
|
84
|
+
// toward the Payer's yearly cap while it waits. Say nothing and it reaches the
|
|
85
|
+
// Finance Officer as Undecided after the party's deadline; nothing is refunded
|
|
86
|
+
// automatically. You hear exactly one of `payment.paid` (on time, or a Late Payment
|
|
87
|
+
// you accepted) or `payment.expired`; `payment.late` comes first for a late
|
|
88
|
+
// `payment.paid` or `payment.rejected`.
|
|
89
|
+
//
|
|
61
90
|
// A `paid` event with `mode: "test"` is a sandbox payment. No money moved and
|
|
62
91
|
// no legal Receipt will exist for it. A Draft or Beta app's Payments are always
|
|
63
92
|
// `test`; a Live app's are `live` unless it created the Payment with
|
|
@@ -118,10 +147,32 @@ interface PaymentInputBase {
|
|
|
118
147
|
* handing over anything real.
|
|
119
148
|
*/
|
|
120
149
|
test?: boolean;
|
|
150
|
+
/**
|
|
151
|
+
* Optional: when the payable window closes, if your app holds something for the
|
|
152
|
+
* Payer while they pay (stock, a ticket, a slot). An ISO 8601 string with an
|
|
153
|
+
* explicit offset (`2026-12-01T14:30:00+07:00`, or `Z`) or a `Date`. It must be
|
|
154
|
+
* at least 10 minutes and at most 30 days from now, or the platform answers
|
|
155
|
+
* `invalid_payment` with `field: "expiresAt"`. Fixed at creation: there is no way to
|
|
156
|
+
* extend it, so create a new Payment when the Payer needs more time. See
|
|
157
|
+
* "A Payment that expires" at the top of this file.
|
|
158
|
+
*/
|
|
159
|
+
expiresAt?: string | Date;
|
|
160
|
+
/**
|
|
161
|
+
* Optional: who issues the legal Receipt. `"later"` (the default): you do, with
|
|
162
|
+
* `issueReceipt`, at once or after your own checks, and you may `rejectPayment`
|
|
163
|
+
* until then. `"on_payment"`: the platform issues it the moment the Payment is
|
|
164
|
+
* paid, so there is nothing to call, and it cannot be rejected after (only a
|
|
165
|
+
* Finance Officer can refund it). A payment that took the Payer over the yearly
|
|
166
|
+
* cap is the exception: it is left for you or the Finance Officer to decide.
|
|
167
|
+
*/
|
|
168
|
+
receiptTiming?: ReceiptTiming;
|
|
121
169
|
}
|
|
122
170
|
|
|
171
|
+
/** When a Payment's Receipt is issued: by your app (`later`), or by the platform the moment it is paid (`on_payment`). */
|
|
172
|
+
export type ReceiptTiming = "later" | "on_payment";
|
|
173
|
+
|
|
123
174
|
/**
|
|
124
|
-
* What you send to create a Payment. Every field is required except `test`, and each kind
|
|
175
|
+
* What you send to create a Payment. Every field is required except `test`, `expiresAt` and `receiptTiming`, and each kind
|
|
125
176
|
* carries what its receipt prints: a sale its 1 to 12 lines (which must add up to
|
|
126
177
|
* `amountSatang` exactly), a fundraising its event, a membership its term. A
|
|
127
178
|
* field of another kind is refused (`invalid_payment` naming it).
|
|
@@ -153,6 +204,7 @@ export type PaymentReason =
|
|
|
153
204
|
| "invalid_payment"
|
|
154
205
|
| "not_found"
|
|
155
206
|
| "not_paid"
|
|
207
|
+
| "not_late"
|
|
156
208
|
| "has_receipt"
|
|
157
209
|
| "mismatch"
|
|
158
210
|
| "user_token_invalid"
|
|
@@ -183,15 +235,20 @@ export interface PaymentRecord extends PaymentKindDetails {
|
|
|
183
235
|
amountSatang: number;
|
|
184
236
|
description: string;
|
|
185
237
|
reference: string;
|
|
186
|
-
state: "created" | "pending" | "paid" | "refused" | "rejected" | "refunding" | "refunded";
|
|
238
|
+
state: "created" | "pending" | "paid" | "refused" | "rejected" | "refunding" | "refunded" | "expired" | "late";
|
|
187
239
|
mode: "live" | "test";
|
|
188
240
|
payer: { name: string; maskedCitizenId: string } | null;
|
|
189
241
|
voucher: { number: string; nameTh: string; isReceipt: false } | null;
|
|
190
242
|
receipt: PaymentReceipt | null;
|
|
243
|
+
/** ISO 8601 (UTC): when the window you gave it closes; null when you gave none. */
|
|
244
|
+
expiresAt: string | null;
|
|
245
|
+
/** ISO 8601. When the Gateway says the Payer paid (the platform's own record time when it does not say). */
|
|
191
246
|
paidAt: string | null;
|
|
192
247
|
refusal: { code: string; reasonTh: string } | null;
|
|
193
248
|
/** True for a paid Payment neither issued nor rejected within the party's deadline. You can still do either. */
|
|
194
249
|
undecided: boolean;
|
|
250
|
+
/** As created: `later` unless you asked for `on_payment`. */
|
|
251
|
+
receiptTiming: ReceiptTiming;
|
|
195
252
|
refund: { status: "refunded" | "owed" | "pending" } | null;
|
|
196
253
|
}
|
|
197
254
|
|
|
@@ -209,10 +266,17 @@ export async function createPayment(
|
|
|
209
266
|
input: CreatePaymentInput,
|
|
210
267
|
fetchImpl?: typeof fetch,
|
|
211
268
|
): Promise<PaymentResult<{ id: string; checkoutUrl: string; state: "created" }>> {
|
|
269
|
+
const { expiresAt, ...rest } = input;
|
|
270
|
+
let sent: CreatePaymentInput = input;
|
|
271
|
+
if (expiresAt !== undefined) {
|
|
272
|
+
// A Date is sent as the instant it is; one that is not a time never leaves the app.
|
|
273
|
+
if (expiresAt instanceof Date && Number.isNaN(expiresAt.getTime())) return refused("invalid_payment", "expiresAt");
|
|
274
|
+
sent = { ...rest, expiresAt: expiresAt instanceof Date ? expiresAt.toISOString() : expiresAt } as CreatePaymentInput;
|
|
275
|
+
}
|
|
212
276
|
const answer = await callPayments(env, "", fetchImpl, {
|
|
213
277
|
method: "POST",
|
|
214
278
|
headers: { "content-type": "application/json" },
|
|
215
|
-
body: JSON.stringify(
|
|
279
|
+
body: JSON.stringify(sent),
|
|
216
280
|
});
|
|
217
281
|
if (!answer.ok) return answer;
|
|
218
282
|
const { id, checkoutUrl } = answer.body;
|
|
@@ -220,6 +284,22 @@ export async function createPayment(
|
|
|
220
284
|
return { ok: true, id, checkoutUrl, state: "created" };
|
|
221
285
|
}
|
|
222
286
|
|
|
287
|
+
/**
|
|
288
|
+
* Accept a Late Payment (`payment.late`): you can still serve what the Payer paid
|
|
289
|
+
* for, so the Payment is `paid`, its Payment Voucher is issued and you hear
|
|
290
|
+
* `payment.paid` as usual. Safe to repeat: it answers the same Payment again. Anything
|
|
291
|
+
* that is not a Late Payment says `not_late` (to refuse it, use `rejectPayment`).
|
|
292
|
+
*/
|
|
293
|
+
export async function acceptPayment(
|
|
294
|
+
env: PaymentsEnv,
|
|
295
|
+
id: string,
|
|
296
|
+
fetchImpl?: typeof fetch,
|
|
297
|
+
): Promise<PaymentResult<{ payment: PaymentRecord }>> {
|
|
298
|
+
const answer = await callPayments(env, `/${encodeURIComponent(id)}/accept`, fetchImpl, { method: "POST" });
|
|
299
|
+
if (!answer.ok) return answer;
|
|
300
|
+
return { ok: true, payment: answer.body as unknown as PaymentRecord };
|
|
301
|
+
}
|
|
302
|
+
|
|
223
303
|
/** Read a Payment back. The platform's answer, not an event, is the truth about its state. */
|
|
224
304
|
export async function getPayment(
|
|
225
305
|
env: PaymentsEnv,
|
|
@@ -281,8 +361,9 @@ export async function rejectPayment(
|
|
|
281
361
|
* `createPayment`'s `checkoutUrl`: the Payer signs in on the Checkout Page.
|
|
282
362
|
* - `not_your_payment` (`409`): the Payment already belongs to another Payer.
|
|
283
363
|
* Do not retry: create a new Payment for this Payer, or tell them it is not theirs.
|
|
284
|
-
* - `not_payable`: the Payment
|
|
285
|
-
*
|
|
364
|
+
* - `not_payable`: the Payment can no longer be paid: it is paid, late, refused,
|
|
365
|
+
* rejected, refunding, refunded or expired, or its `expiresAt` has passed.
|
|
366
|
+
* Read it with `getPayment` and show its state.
|
|
286
367
|
* - `not_found`: no such Payment of yours.
|
|
287
368
|
*/
|
|
288
369
|
export async function createCheckoutPass(
|
|
@@ -310,6 +391,9 @@ export async function createCheckoutPass(
|
|
|
310
391
|
/** The states a Payment can be in once the platform has sent an event of that type, and any time after. */
|
|
311
392
|
const STATES_AFTER: Record<string, readonly PaymentRecord["state"][]> = {
|
|
312
393
|
"payment.paid": ["paid", "rejected", "refunding", "refunded"],
|
|
394
|
+
"payment.expired": ["expired"],
|
|
395
|
+
// A Late Payment waits for you to accept or reject it, then moves on.
|
|
396
|
+
"payment.late": ["late", "paid", "rejected", "refunding", "refunded"],
|
|
313
397
|
// A Finance Officer can refund a Payment after its Receipt (`refunding`, then `refunded`).
|
|
314
398
|
"payment.receipt_issued": ["paid", "refunding", "refunded"],
|
|
315
399
|
"payment.receipt_voided": ["refunding", "refunded", "paid"],
|
|
@@ -395,7 +479,7 @@ function platformReason(code: PlatformErrorCode | null): PaymentReason {
|
|
|
395
479
|
}
|
|
396
480
|
|
|
397
481
|
/** The Service's own answers, by `error` in the body. */
|
|
398
|
-
const SERVICE_REASONS = ["invalid_payment", "not_found", "not_paid", "has_receipt", "checkout_unavailable"] as const;
|
|
482
|
+
const SERVICE_REASONS = ["invalid_payment", "not_found", "not_paid", "not_late", "has_receipt", "checkout_unavailable"] as const;
|
|
399
483
|
|
|
400
484
|
async function callPayments(
|
|
401
485
|
env: PaymentsEnv,
|
|
@@ -441,24 +525,60 @@ async function callPayments(
|
|
|
441
525
|
return { ok: true, status: res.status, body };
|
|
442
526
|
}
|
|
443
527
|
|
|
528
|
+
/** The Payment as an event carries it. */
|
|
529
|
+
export type PaymentEventPayment = PaymentKindDetails & {
|
|
530
|
+
id: string;
|
|
531
|
+
kind: PaymentKind;
|
|
532
|
+
/** 100 satang is 1 baht. */
|
|
533
|
+
amountSatang: number;
|
|
534
|
+
description: string;
|
|
535
|
+
/** Your own id, as you sent it. */
|
|
536
|
+
reference: string;
|
|
537
|
+
mode: "live" | "test";
|
|
538
|
+
};
|
|
539
|
+
|
|
444
540
|
export interface PaymentPaidEvent {
|
|
445
541
|
type: "payment.paid";
|
|
446
542
|
/** The same on every delivery of this outcome. */
|
|
447
543
|
id: string;
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
kind: PaymentKind;
|
|
451
|
-
/** 100 satang is 1 baht. */
|
|
452
|
-
amountSatang: number;
|
|
453
|
-
description: string;
|
|
454
|
-
/** Your own id, as you sent it. */
|
|
455
|
-
reference: string;
|
|
456
|
-
mode: "live" | "test";
|
|
457
|
-
};
|
|
544
|
+
/** With the `expiresAt` you gave it (null when none). */
|
|
545
|
+
payment: PaymentEventPayment & { expiresAt: string | null };
|
|
458
546
|
payer: { name: string; maskedCitizenId: string } | null;
|
|
459
547
|
/** The platform's acknowledgement that the money arrived. Not a receipt. */
|
|
460
548
|
voucher: { number: string; nameTh: string; isReceipt: false } | null;
|
|
549
|
+
/** ISO 8601. When the Gateway says the Payer paid; the time the platform recorded it when the Gateway does not say. */
|
|
550
|
+
paidAt: string | null;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* The `expiresAt` you gave the Payment passed and nothing was paid, and no Charge opened
|
|
555
|
+
* before it can still be paid. Release what you held. You hear it up to about 60 minutes
|
|
556
|
+
* after `expiresAt` (usually within one sweep, a few minutes), so run your own timer for the
|
|
557
|
+
* hold. `expired` is final.
|
|
558
|
+
*/
|
|
559
|
+
export interface PaymentExpiredEvent {
|
|
560
|
+
type: "payment.expired";
|
|
561
|
+
/** The same on every delivery of this outcome. */
|
|
562
|
+
id: string;
|
|
563
|
+
payment: PaymentPaidEvent["payment"];
|
|
461
564
|
/** ISO 8601. */
|
|
565
|
+
expiredAt: string | null;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* The Gateway confirmed money for the Payment AFTER its `expiresAt`: a Late Payment. It is
|
|
570
|
+
* held, neither paid nor refunded, and has no Payment Voucher yet. Check your hold and answer
|
|
571
|
+
* with `acceptPayment(id)` (you can still serve it) or `rejectPayment(id)` (you cannot). It
|
|
572
|
+
* counts toward the Payer's yearly cap meanwhile. Unanswered, it goes to the Finance Officer
|
|
573
|
+
* as Undecided after the party's deadline.
|
|
574
|
+
*/
|
|
575
|
+
export interface PaymentLateEvent {
|
|
576
|
+
type: "payment.late";
|
|
577
|
+
/** The same on every delivery of this outcome. */
|
|
578
|
+
id: string;
|
|
579
|
+
payment: PaymentPaidEvent["payment"];
|
|
580
|
+
payer: { name: string; maskedCitizenId: string } | null;
|
|
581
|
+
/** ISO 8601. When the Gateway says the Payer paid (before or after `expiresAt`; it was confirmed after). */
|
|
462
582
|
paidAt: string | null;
|
|
463
583
|
}
|
|
464
584
|
|
|
@@ -471,7 +591,7 @@ export interface PaymentRefusedEvent {
|
|
|
471
591
|
type: "payment.refused";
|
|
472
592
|
/** The same on every delivery of this outcome. */
|
|
473
593
|
id: string;
|
|
474
|
-
payment:
|
|
594
|
+
payment: PaymentEventPayment;
|
|
475
595
|
/** `reasonTh` is for the Payer: show it as is. */
|
|
476
596
|
refusal: { code: "below_minimum" | "above_maximum" | "over_yearly_cap"; reasonTh: string } | null;
|
|
477
597
|
/** ISO 8601. */
|
|
@@ -486,7 +606,7 @@ export interface PaymentReceiptIssuedEvent {
|
|
|
486
606
|
type: "payment.receipt_issued";
|
|
487
607
|
/** The same on every delivery of this Receipt's event. */
|
|
488
608
|
id: string;
|
|
489
|
-
payment:
|
|
609
|
+
payment: PaymentEventPayment;
|
|
490
610
|
payer: { name: string; maskedCitizenId: string };
|
|
491
611
|
receipt: {
|
|
492
612
|
id: string;
|
|
@@ -506,7 +626,7 @@ export interface PaymentRejectedEvent {
|
|
|
506
626
|
type: "payment.rejected";
|
|
507
627
|
/** The same on every delivery of this outcome. */
|
|
508
628
|
id: string;
|
|
509
|
-
payment:
|
|
629
|
+
payment: PaymentEventPayment;
|
|
510
630
|
/** ISO 8601. */
|
|
511
631
|
rejectedAt: string | null;
|
|
512
632
|
}
|
|
@@ -516,7 +636,7 @@ export interface PaymentRefundedEvent {
|
|
|
516
636
|
type: "payment.refunded";
|
|
517
637
|
/** The same on every delivery of this outcome. */
|
|
518
638
|
id: string;
|
|
519
|
-
payment:
|
|
639
|
+
payment: PaymentEventPayment;
|
|
520
640
|
/** ISO 8601. */
|
|
521
641
|
refundedAt: string | null;
|
|
522
642
|
}
|
|
@@ -532,7 +652,7 @@ export interface PaymentReceiptVoidedEvent {
|
|
|
532
652
|
type: "payment.receipt_voided";
|
|
533
653
|
/** The same on every delivery of this Receipt's event. */
|
|
534
654
|
id: string;
|
|
535
|
-
payment:
|
|
655
|
+
payment: PaymentEventPayment;
|
|
536
656
|
receipt: PaymentReceiptIssuedEvent["receipt"] & {
|
|
537
657
|
/** ISO 8601. */
|
|
538
658
|
voidedAt: string | null;
|
|
@@ -541,6 +661,8 @@ export interface PaymentReceiptVoidedEvent {
|
|
|
541
661
|
|
|
542
662
|
export type PaymentEvent =
|
|
543
663
|
| PaymentPaidEvent
|
|
664
|
+
| PaymentExpiredEvent
|
|
665
|
+
| PaymentLateEvent
|
|
544
666
|
| PaymentRefusedEvent
|
|
545
667
|
| PaymentReceiptIssuedEvent
|
|
546
668
|
| PaymentReceiptVoidedEvent
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
kind: service
|
|
3
3
|
visibility: private
|
|
4
4
|
userToken: optional
|
|
5
|
-
description: รับเงินบริจาค ค่าสินค้า/บริการ
|
|
5
|
+
description: รับเงินบริจาค ค่าสินค้า/บริการ กิจกรรมระดมทุน และค่าบำรุงพรรคผ่านหน้าชำระเงินของแพลตฟอร์ม
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
# Payments
|
|
@@ -30,9 +30,11 @@
|
|
|
30
30
|
"reference": "offline-mock",
|
|
31
31
|
"state": "paid",
|
|
32
32
|
"mode": "test",
|
|
33
|
+
"receiptTiming": "later",
|
|
33
34
|
"payer": { "name": "บุคคลทดสอบ หนึ่ง", "maskedCitizenId": "x-xxxx-xxxxx-xx-0" },
|
|
34
35
|
"voucher": { "number": "TEST-PV-000001", "nameTh": "หลักฐานการรับชำระเงิน (ไม่ใช่ใบเสร็จรับเงิน)", "isReceipt": false },
|
|
35
36
|
"receipt": null,
|
|
37
|
+
"expiresAt": null,
|
|
36
38
|
"paidAt": "2026-01-01T00:00:00.000Z",
|
|
37
39
|
"refusal": null,
|
|
38
40
|
"undecided": false,
|
|
@@ -59,13 +61,38 @@
|
|
|
59
61
|
"reference": "offline-mock",
|
|
60
62
|
"state": "refunded",
|
|
61
63
|
"mode": "test",
|
|
64
|
+
"receiptTiming": "later",
|
|
62
65
|
"payer": { "name": "บุคคลทดสอบ หนึ่ง", "maskedCitizenId": "x-xxxx-xxxxx-xx-0" },
|
|
63
66
|
"voucher": { "number": "TEST-PV-000001", "nameTh": "หลักฐานการรับชำระเงิน (ไม่ใช่ใบเสร็จรับเงิน)", "isReceipt": false },
|
|
64
67
|
"receipt": null,
|
|
68
|
+
"expiresAt": null,
|
|
65
69
|
"paidAt": "2026-01-01T00:00:00.000Z",
|
|
66
70
|
"refusal": null,
|
|
67
71
|
"undecided": false,
|
|
68
72
|
"refund": { "status": "refunded" }
|
|
69
73
|
}
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
"method": "POST",
|
|
77
|
+
"path": "/pay_offline-mock/accept",
|
|
78
|
+
"status": 200,
|
|
79
|
+
"body": {
|
|
80
|
+
"id": "pay_offline-mock",
|
|
81
|
+
"kind": "donation",
|
|
82
|
+
"amountSatang": 50000,
|
|
83
|
+
"description": "รายการทดสอบ",
|
|
84
|
+
"reference": "offline-mock",
|
|
85
|
+
"state": "paid",
|
|
86
|
+
"mode": "test",
|
|
87
|
+
"receiptTiming": "later",
|
|
88
|
+
"payer": { "name": "บุคคลทดสอบ หนึ่ง", "maskedCitizenId": "x-xxxx-xxxxx-xx-0" },
|
|
89
|
+
"voucher": { "number": "TEST-PV-000001", "nameTh": "หลักฐานการรับชำระเงิน (ไม่ใช่ใบเสร็จรับเงิน)", "isReceipt": false },
|
|
90
|
+
"receipt": null,
|
|
91
|
+
"expiresAt": "2025-12-31T23:30:00.000Z",
|
|
92
|
+
"paidAt": "2026-01-01T00:00:00.000Z",
|
|
93
|
+
"refusal": null,
|
|
94
|
+
"undecided": false,
|
|
95
|
+
"refund": null
|
|
96
|
+
}
|
|
70
97
|
}
|
|
71
98
|
]
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
createPayment,
|
|
11
11
|
getPayment,
|
|
12
12
|
issueReceipt,
|
|
13
|
+
acceptPayment,
|
|
13
14
|
paymentHandler,
|
|
14
15
|
rejectPayment,
|
|
15
16
|
verifyPaymentEvent,
|
|
@@ -75,6 +76,70 @@ describe("createPayment", () => {
|
|
|
75
76
|
expect(result).toMatchObject({ ok: true, id: "pay_2" });
|
|
76
77
|
expect(body).toEqual({ ...INPUT, test: true });
|
|
77
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
|
+
});
|
|
78
143
|
});
|
|
79
144
|
|
|
80
145
|
const PAID: PaymentRecord = {
|
|
@@ -85,9 +150,11 @@ const PAID: PaymentRecord = {
|
|
|
85
150
|
reference: "order-1",
|
|
86
151
|
state: "paid",
|
|
87
152
|
mode: "test",
|
|
153
|
+
receiptTiming: "later",
|
|
88
154
|
payer: { name: "นางสาวสมหญิง ใจดี", maskedCitizenId: "1-1037-xxxxx-56-1" },
|
|
89
155
|
voucher: { number: "TEST-PV-000001", nameTh: "หลักฐานการรับชำระเงิน", isReceipt: false },
|
|
90
156
|
receipt: null,
|
|
157
|
+
expiresAt: null,
|
|
91
158
|
paidAt: "2026-11-15T09:00:00.000Z",
|
|
92
159
|
refusal: null,
|
|
93
160
|
undecided: false,
|
|
@@ -294,12 +361,16 @@ const PAID_EVENT: PaymentPaidEvent = {
|
|
|
294
361
|
description: "บริจาคสนับสนุนพรรค",
|
|
295
362
|
reference: "order-1",
|
|
296
363
|
mode: "test",
|
|
364
|
+
expiresAt: null,
|
|
297
365
|
},
|
|
298
366
|
payer: PAID.payer,
|
|
299
367
|
voucher: PAID.voucher,
|
|
300
368
|
paidAt: PAID.paidAt,
|
|
301
369
|
};
|
|
302
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
|
+
|
|
303
374
|
describe("verifyPaymentEvent", () => {
|
|
304
375
|
// The platform authenticates the delivery itself (the wrapper's own secret
|
|
305
376
|
// never reaches your code), and your handler is handed the parsed event only.
|
|
@@ -340,6 +411,8 @@ describe("verifyPaymentEvent", () => {
|
|
|
340
411
|
|
|
341
412
|
it.each([
|
|
342
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"]],
|
|
343
416
|
["payment.rejected", ["rejected", "refunded"], ["paid"]],
|
|
344
417
|
["payment.refunded", ["refunded"], ["rejected", "paid"]],
|
|
345
418
|
["payment.receipt_issued", ["paid", "refunding", "refunded"], ["created", "refused"]],
|
|
@@ -381,6 +454,24 @@ describe("paymentHandler", () => {
|
|
|
381
454
|
expect(seen).toEqual(["payment.paid:TEST-PV-000001"]);
|
|
382
455
|
});
|
|
383
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
|
+
|
|
384
475
|
it("hands over a Replacement Receipt's event with the Receipt it replaces", async () => {
|
|
385
476
|
const seen: string[] = [];
|
|
386
477
|
const handler = paymentHandler({
|
|
@@ -30,9 +30,11 @@
|
|
|
30
30
|
"reference": "offline-mock",
|
|
31
31
|
"state": "paid",
|
|
32
32
|
"mode": "test",
|
|
33
|
+
"receiptTiming": "later",
|
|
33
34
|
"payer": { "name": "บุคคลทดสอบ หนึ่ง", "maskedCitizenId": "x-xxxx-xxxxx-xx-0" },
|
|
34
35
|
"voucher": { "number": "TEST-PV-000001", "nameTh": "หลักฐานการรับชำระเงิน (ไม่ใช่ใบเสร็จรับเงิน)", "isReceipt": false },
|
|
35
36
|
"receipt": null,
|
|
37
|
+
"expiresAt": null,
|
|
36
38
|
"paidAt": "2026-01-01T00:00:00.000Z",
|
|
37
39
|
"refusal": null,
|
|
38
40
|
"undecided": false,
|
|
@@ -59,13 +61,38 @@
|
|
|
59
61
|
"reference": "offline-mock",
|
|
60
62
|
"state": "refunded",
|
|
61
63
|
"mode": "test",
|
|
64
|
+
"receiptTiming": "later",
|
|
62
65
|
"payer": { "name": "บุคคลทดสอบ หนึ่ง", "maskedCitizenId": "x-xxxx-xxxxx-xx-0" },
|
|
63
66
|
"voucher": { "number": "TEST-PV-000001", "nameTh": "หลักฐานการรับชำระเงิน (ไม่ใช่ใบเสร็จรับเงิน)", "isReceipt": false },
|
|
64
67
|
"receipt": null,
|
|
68
|
+
"expiresAt": null,
|
|
65
69
|
"paidAt": "2026-01-01T00:00:00.000Z",
|
|
66
70
|
"refusal": null,
|
|
67
71
|
"undecided": false,
|
|
68
72
|
"refund": { "status": "refunded" }
|
|
69
73
|
}
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
"method": "POST",
|
|
77
|
+
"path": "/pay_offline-mock/accept",
|
|
78
|
+
"status": 200,
|
|
79
|
+
"body": {
|
|
80
|
+
"id": "pay_offline-mock",
|
|
81
|
+
"kind": "donation",
|
|
82
|
+
"amountSatang": 50000,
|
|
83
|
+
"description": "รายการทดสอบ",
|
|
84
|
+
"reference": "offline-mock",
|
|
85
|
+
"state": "paid",
|
|
86
|
+
"mode": "test",
|
|
87
|
+
"receiptTiming": "later",
|
|
88
|
+
"payer": { "name": "บุคคลทดสอบ หนึ่ง", "maskedCitizenId": "x-xxxx-xxxxx-xx-0" },
|
|
89
|
+
"voucher": { "number": "TEST-PV-000001", "nameTh": "หลักฐานการรับชำระเงิน (ไม่ใช่ใบเสร็จรับเงิน)", "isReceipt": false },
|
|
90
|
+
"receipt": null,
|
|
91
|
+
"expiresAt": "2025-12-31T23:30:00.000Z",
|
|
92
|
+
"paidAt": "2026-01-01T00:00:00.000Z",
|
|
93
|
+
"refusal": null,
|
|
94
|
+
"undecided": false,
|
|
95
|
+
"refund": null
|
|
96
|
+
}
|
|
70
97
|
}
|
|
71
98
|
]
|
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
4
|