@pplethai/cli 0.20.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.
Files changed (35) hide show
  1. package/README.md +5 -3
  2. package/dist/bin.js +132 -131
  3. package/package.json +1 -1
  4. package/templates/golden-path/CLAUDE.md +6 -2
  5. package/templates/golden-path/README.md +4 -1
  6. package/templates/golden-path/src/email.ts +4 -179
  7. package/templates/golden-path/src/index.ts +6 -0
  8. package/templates/golden-path/src/party-email.ts +183 -0
  9. package/templates/golden-path/src/payment.ts +162 -29
  10. package/templates/golden-path/test/fixtures/overlay/platform/payments/entry.md +1 -1
  11. package/templates/golden-path/test/fixtures/overlay/platform/payments/mocks/responses.json +27 -0
  12. package/templates/golden-path/test/payment-app.test.ts +1 -0
  13. package/templates/golden-path/test/payment.test.ts +107 -0
  14. package/templates/payment-bridge/.github/workflows/ci.yml +21 -0
  15. package/templates/payment-bridge/CLAUDE.md +9 -0
  16. package/templates/payment-bridge/README.md +57 -0
  17. package/templates/payment-bridge/docs/verify-webhook.md +80 -0
  18. package/templates/payment-bridge/docs/verify-webhook.ts +29 -0
  19. package/templates/payment-bridge/gitignore +5 -0
  20. package/templates/payment-bridge/package-lock.json +4198 -0
  21. package/templates/payment-bridge/package.json +21 -0
  22. package/templates/payment-bridge/src/bridge-key.ts +18 -0
  23. package/templates/payment-bridge/src/env.ts +14 -0
  24. package/templates/payment-bridge/src/index.ts +39 -0
  25. package/templates/payment-bridge/src/platform.ts +49 -0
  26. package/templates/payment-bridge/src/webhook.ts +86 -0
  27. package/templates/payment-bridge/test/docs.test.ts +9 -0
  28. package/templates/payment-bridge/test/fixtures/payments-responses.json +98 -0
  29. package/templates/payment-bridge/test/harness.ts +78 -0
  30. package/templates/payment-bridge/test/inbound.test.ts +138 -0
  31. package/templates/payment-bridge/test/webhook.test.ts +174 -0
  32. package/templates/payment-bridge/tsconfig.json +22 -0
  33. package/templates/payment-bridge/vitest.config.ts +8 -0
  34. package/templates/payment-bridge/wrangler.jsonc +15 -0
  35. package/templates/payment-bridge.version +1 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pplethai/cli",
3
- "version": "0.20.0",
3
+ "version": "0.22.0",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -122,8 +122,12 @@ Where the token comes from:
122
122
  them with `goToCheckout(paymentId)` from `web/pple.ts`, which posts the pass as
123
123
  a form from your page (never a link), so they skip signing in again; ask at
124
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`
126
- and `paymentHandler`. The outcome arrives at `payment` on the default export
125
+ someone else's; `POST /api/payments/:id/checkout` does it), `getPayment`, `issueReceipt`, `rejectPayment`, `verifyPaymentEvent`,
126
+ `acceptPayment` and `paymentHandler`. Holding stock or a slot while the Payer
127
+ pays: give `createPayment` an `expiresAt` (10 minutes to 30 days ahead), run your
128
+ own timer for the hold (`payment.expired` can come up to about 60 minutes late),
129
+ and answer `payment.late` (money confirmed after `expiresAt`) by checking your
130
+ hold: `acceptPayment` or `rejectPayment`. The outcome arrives at `payment` on the default export
127
131
  (`createPaymentHandler` in `src/index.ts` is the starting point): it verifies
128
132
  the event and **does not issue a Receipt**: you choose, issue at payment or
129
133
  verify first and then issue or reject (both are in the Skill's payments reference).
@@ -110,7 +110,10 @@ you, so a freshly created app is ready to `npm run dev` straight away.)
110
110
  outcome arrives at
111
111
  `payment` on the default export, where `verifyPaymentEvent` checks it and
112
112
  your code decides: `issueReceipt` or `rejectPayment`. The starter handler only
113
- verifies and does not issue a Receipt for you. You only ever see a masked
113
+ verifies and does not issue a Receipt for you. To hold something while the Payer
114
+ pays, pass `expiresAt` to `createPayment` and run your own timer: `payment.expired`
115
+ can come up to about an hour late, and money confirmed after `expiresAt` arrives
116
+ as `payment.late`: `acceptPayment` or `rejectPayment` it. You only ever see a masked
114
117
  citizen ID. Only a Live app takes real money. A system outside the platform
115
118
  needs a stub app: the `pple-platform` Skill has the recipe (it is not part of
116
119
  this template).
@@ -15,190 +15,15 @@
15
15
  // (`<p>${name}</p>`) becomes HTML: a name containing `<a href=…>` puts a link
16
16
  // in an email sent under the party's name. The platform removes scripts and
17
17
  // the like, but not links or images: only your code can tell your markup from
18
- // your users' text. Build HTML with the `html` tag below, which escapes every
18
+ // your users' text. Build HTML with the `html` tag, which escapes every
19
19
  // value it is given; `raw()` is the deliberate way to insert markup you wrote.
20
20
  //
21
- // The layout is hand-written table HTML with inline styles, which is what mail
22
- // apps (Gmail, Outlook, Apple Mail) render alike. Keep `content` simple:
23
- // paragraphs, links, lists, `<b>`. The logo is a PNG the platform serves.
21
+ // The layout and the `html` tag live in `./party-email` (the platform's own
22
+ // Receipt email uses the same file) and are re-exported here.
24
23
 
25
24
  import { callService, type CallServiceEnv } from "./call-service";
26
25
 
27
- /** HTML that is safe to put in an email as it is: built by `html`, or vouched for with `raw`. */
28
- export class SafeHtml {
29
- constructor(readonly value: string) {}
30
- toString(): string {
31
- return this.value;
32
- }
33
- }
34
-
35
- /** Text in, the same text as HTML out: `&`, `<`, `>`, `"` and `'` become entities. */
36
- export function escapeHtml(text: string): string {
37
- return text.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
38
- }
39
-
40
- /** Markup you wrote yourself, inserted as it is. Never pass it anything a user typed. */
41
- export function raw(trusted: string): SafeHtml {
42
- return new SafeHtml(trusted);
43
- }
44
-
45
- type HtmlValue = SafeHtml | string | number | boolean | null | undefined | readonly HtmlValue[];
46
-
47
- function render(value: HtmlValue): string {
48
- if (value instanceof SafeHtml) return value.value;
49
- if (Array.isArray(value)) return value.map(render).join("");
50
- if (value === null || value === undefined || value === false) return "";
51
- return escapeHtml(String(value));
52
- }
53
-
54
- /**
55
- * A tagged template that escapes every inserted value. Nest it freely: an
56
- * inserted `html` result (or a list of them) goes in as markup, not escaped twice.
57
- *
58
- * html`<p>สวัสดี ${user.name}</p>` // name is escaped
59
- * html`<ul>${items.map((i) => html`<li>${i}</li>`)}</ul>`
60
- */
61
- export function html(strings: TemplateStringsArray, ...values: HtmlValue[]): SafeHtml {
62
- let out = strings[0] ?? "";
63
- values.forEach((value, i) => {
64
- out += render(value) + (strings[i + 1] ?? "");
65
- });
66
- return new SafeHtml(out);
67
- }
68
-
69
- /** A link a reader may follow: `https:` or `mailto:` only, or this throws. */
70
- export function safeUrl(url: string): string {
71
- let parsed: URL;
72
- try {
73
- parsed = new URL(url);
74
- } catch {
75
- throw new Error(`not a URL: ${url}`);
76
- }
77
- if (parsed.protocol !== "https:" && parsed.protocol !== "mailto:") {
78
- throw new Error(`email links must be https: or mailto:, not ${parsed.protocol}`);
79
- }
80
- return parsed.toString();
81
- }
82
-
83
- /** The party's contact details, printed in every email's footer. */
84
- export const PARTY = {
85
- name: "พรรคประชาชน",
86
- logoUrl: "https://services.ppleth.ai/email-assets/logo.png",
87
- logoAlt: "ภาพโลโก้พรรคประชาชน",
88
- phone: "02-038-5100",
89
- phoneHref: "tel:+6620385100",
90
- hours: "จันทร์-ศุกร์ 10:00-18:00 น.",
91
- email: "office@peoplespartythailand.org",
92
- line: "@peoplesth",
93
- lineUrl: "https://line.me/R/ti/p/@peoplesth",
94
- address: ["พรรคประชาชน สำนักงานใหญ่", "เลขที่ 167 ชั้น 4 ซอย รามคำแหง 42", "แขวงหัวหมาก เขตบางกะปิ", "กรุงเทพมหานคร 10240"],
95
- } as const;
96
-
97
- const NAVY = "#0b3a66";
98
- const ORANGE = "#ff6413";
99
- const INK = "#1f2937";
100
- const FONT = "'IBM Plex Sans Thai Looped','Noto Sans Thai','Leelawadee UI',Tahoma,sans-serif";
101
-
102
- export interface PartyEmailInput {
103
- /** The big line at the top of the message. */
104
- heading: string;
105
- /** The message, built with `html`. */
106
- content: SafeHtml;
107
- /** The same message as plain text, for mail apps that show no HTML. */
108
- text: string;
109
- /** One call to action. Its URL must be `https:` or `mailto:`. */
110
- button?: { label: string; url: string };
111
- /** The grey line mail apps show beside the subject. Defaults to none. */
112
- preheader?: string;
113
- }
114
-
115
- // Dark mode. Mail apps treat it three ways, and the layout is built for all:
116
- // - Apple Mail, iOS Mail, Outlook for Mac honour `prefers-color-scheme`: the
117
- // <style> block below swaps the light card for a dark one.
118
- // - Gmail's apps ignore it and invert colours themselves: a navy block turns
119
- // light blue and white text turns dark. They leave a `background-image`
120
- // alone, so the navy header and the orange button are painted twice
121
- // (`bg()`). Text colours are still inverted, so nothing relies on white text
122
- // on a dark block: the header carries only the logo (an image, never
123
- // inverted), and the footer is light with dark text, which Gmail inverts
124
- // the way it inverts the body.
125
- // - Clients that do neither show the light layout as it is.
126
- const DARK_CSS = `
127
- :root { color-scheme: light dark; supported-color-schemes: light dark; }
128
- a[x-apple-data-detectors] { color: inherit !important; text-decoration: none !important; }
129
- @media (prefers-color-scheme: dark) {
130
- .pe-page { background: #0f141a !important; }
131
- .pe-card { background: #1a2129 !important; }
132
- .pe-ink, .pe-ink p, .pe-ink td, .pe-ink li { color: #e6e9ee !important; }
133
- .pe-heading { color: #ffb48a !important; }
134
- .pe-ink td { border-color: #2c3540 !important; }
135
- .pe-foot { background: #11171e !important; border-color: #2c3540 !important; }
136
- .pe-foot-title { color: #ffb48a !important; }
137
- .pe-foot-label { color: #8b96a5 !important; }
138
- .pe-foot-text, .pe-foot-text a { color: #d5dbe3 !important; }
139
- }`;
140
-
141
- /** A solid block Gmail's dark mode leaves alone: the colour twice, once as an image. */
142
- const bg = (color: string) => raw(`background-color:${color};background-image:linear-gradient(${color},${color})`);
143
-
144
- /** The party layout around your message: `{ html, body }` for `sendEmail`. */
145
- export function partyEmail(input: PartyEmailInput): { html: string; body: string } {
146
- const button = input.button
147
- ? html`<table role="presentation" cellpadding="0" cellspacing="0" style="margin:24px 0 8px"><tr><td style="border-radius:8px;${bg(ORANGE)}">
148
- <a href="${safeUrl(input.button.url)}" style="display:inline-block;padding:12px 24px;font-family:${raw(FONT)};font-size:16px;font-weight:bold;color:#ffffff;text-decoration:none;border-radius:8px">${input.button.label}</a>
149
- </td></tr></table>`
150
- : "";
151
-
152
- const contactRow = (label: string, value: SafeHtml) =>
153
- html`<tr><td class="pe-foot-label" style="padding:4px 12px 4px 0;font-size:14px;color:#6b7280;white-space:nowrap;vertical-align:top">${label}</td><td class="pe-foot-text" style="padding:4px 0;font-size:15px;color:${INK}">${value}</td></tr>`;
154
- const link = (href: string, text: string) => html`<a href="${href}" style="color:${INK};text-decoration:none">${text}</a>`;
155
- // A deliberate, styled link: left bare, Gmail and iOS underline the address in blue themselves.
156
- const mapsUrl = `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(PARTY.address.join(" "))}`;
157
-
158
- const page = html`<!doctype html>
159
- <html lang="th"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
160
- <meta name="color-scheme" content="light dark"><meta name="supported-color-schemes" content="light dark">
161
- <meta name="format-detection" content="telephone=no,address=no,email=no,date=no">
162
- <title>${input.heading}</title><style>${raw(DARK_CSS)}</style></head>
163
- <body class="body pe-page" style="margin:0;padding:0;background:#eef1f5">
164
- ${input.preheader ? html`<div style="display:none;max-height:0;overflow:hidden;opacity:0">${input.preheader}</div>` : ""}
165
- <table role="presentation" class="pe-page" width="100%" cellpadding="0" cellspacing="0" style="background:#eef1f5"><tr><td align="center" style="padding:24px 12px">
166
- <table role="presentation" class="pe-card" width="100%" cellpadding="0" cellspacing="0" style="max-width:600px;background:#ffffff;border-radius:12px;overflow:hidden;font-family:${raw(FONT)}">
167
- <tr><td style="${bg(NAVY)};padding:24px 32px"><img src="${PARTY.logoUrl}" width="176" height="50" alt="${PARTY.logoAlt}" style="display:block;border:0;color:#ffffff;font-size:20px;font-weight:bold"></td></tr>
168
- <tr><td class="pe-ink" style="padding:32px 32px 24px;color:${INK};font-size:16px;line-height:1.7">
169
- <h1 class="pe-heading" style="margin:0 0 16px;font-size:24px;line-height:1.4;color:${NAVY}">${input.heading}</h1>
170
- ${input.content}
171
- ${button}
172
- </td></tr>
173
- <tr><td class="pe-foot" style="background:#f3f5f8;border-top:1px solid #e3e7ed;padding:28px 32px">
174
- <div class="pe-foot-title" style="margin:0 0 12px;font-size:20px;font-weight:bold;color:${NAVY}">ติดต่อเรา</div>
175
- <table role="presentation" cellpadding="0" cellspacing="0">
176
- ${contactRow("โทร", html`${link(PARTY.phoneHref, PARTY.phone)} (${PARTY.hours})`)}
177
- ${contactRow("อีเมล", link(`mailto:${PARTY.email}`, PARTY.email))}
178
- ${contactRow("LINE", link(PARTY.lineUrl, PARTY.line))}
179
- </table>
180
- <div class="pe-foot-text" style="margin:16px 0 0;font-size:14px;line-height:1.6;color:#4b5563"><a href="${mapsUrl}" style="color:#4b5563;text-decoration:none">${PARTY.address.map((line, i) => html`${i ? raw("<br>") : ""}${line}`)}</a></div>
181
- </td></tr>
182
- </table>
183
- </td></tr></table>
184
- </body></html>`;
185
-
186
- const body = [
187
- input.heading,
188
- "",
189
- input.text,
190
- ...(input.button ? ["", `${input.button.label}: ${safeUrl(input.button.url)}`] : []),
191
- "",
192
- "—",
193
- `ติดต่อ${PARTY.name}`,
194
- `โทร ${PARTY.phone} (${PARTY.hours})`,
195
- `อีเมล ${PARTY.email}`,
196
- `LINE ${PARTY.line}`,
197
- ...PARTY.address,
198
- ].join("\n");
199
-
200
- return { html: page.value, body };
201
- }
26
+ export { escapeHtml, html, PARTY, partyEmail, raw, safeUrl, SafeHtml, type PartyEmailInput } from "./party-email";
202
27
 
203
28
  export interface SendEmailResult {
204
29
  status: number;
@@ -197,6 +197,7 @@ function paymentStatus(reason: PaymentReason): ContentfulStatusCode {
197
197
  case "user_token_invalid":
198
198
  return 401;
199
199
  case "not_paid":
200
+ case "not_late":
200
201
  case "has_receipt":
201
202
  case "mismatch":
202
203
  case "not_your_payment":
@@ -227,6 +228,11 @@ function paymentStatus(reason: PaymentReason): ContentfulStatusCode {
227
228
  * Verify first: your app must check something (an order in stock, a
228
229
  * membership that exists). Do the check, then `issueReceipt` when it passes
229
230
  * or `rejectPayment(env, event.payment.id, fetchImpl)` when it does not.
231
+ *
232
+ * Holding something while the Payer pays? Give `createPayment` an `expiresAt`,
233
+ * release your hold on your own timer, add a `"payment.expired"` function here,
234
+ * and a `"payment.late"` one: money confirmed after `expiresAt` waits for you to
235
+ * `acceptPayment` it (you can still serve it) or `rejectPayment` it (you cannot).
230
236
  */
231
237
  export function createPaymentHandler(overrides: AppOverrides = {}): PaymentHandler {
232
238
  const fetchImpl = overrides.fetchImpl;
@@ -0,0 +1,183 @@
1
+ // The party's email layout and safe HTML building: no imports, so the platform
2
+ // builds its own email (the Receipt, in workers/) from this same file and every
3
+ // email under the party's name looks alike. Apps use it through `./email`.
4
+ //
5
+ // The layout is hand-written table HTML with inline styles, which is what mail
6
+ // apps (Gmail, Outlook, Apple Mail) render alike. Keep `content` simple:
7
+ // paragraphs, links, lists, `<b>`. The logo is a PNG the platform serves.
8
+
9
+ /** HTML that is safe to put in an email as it is: built by `html`, or vouched for with `raw`. */
10
+ export class SafeHtml {
11
+ constructor(readonly value: string) {}
12
+ toString(): string {
13
+ return this.value;
14
+ }
15
+ }
16
+
17
+ /** Text in, the same text as HTML out: `&`, `<`, `>`, `"` and `'` become entities. */
18
+ export function escapeHtml(text: string): string {
19
+ return text.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
20
+ }
21
+
22
+ /** Markup you wrote yourself, inserted as it is. Never pass it anything a user typed. */
23
+ export function raw(trusted: string): SafeHtml {
24
+ return new SafeHtml(trusted);
25
+ }
26
+
27
+ type HtmlValue = SafeHtml | string | number | boolean | null | undefined | readonly HtmlValue[];
28
+
29
+ function render(value: HtmlValue): string {
30
+ if (value instanceof SafeHtml) return value.value;
31
+ if (Array.isArray(value)) return value.map(render).join("");
32
+ if (value === null || value === undefined || value === false) return "";
33
+ return escapeHtml(String(value));
34
+ }
35
+
36
+ /**
37
+ * A tagged template that escapes every inserted value. Nest it freely: an
38
+ * inserted `html` result (or a list of them) goes in as markup, not escaped twice.
39
+ *
40
+ * html`<p>สวัสดี ${user.name}</p>` // name is escaped
41
+ * html`<ul>${items.map((i) => html`<li>${i}</li>`)}</ul>`
42
+ */
43
+ export function html(strings: TemplateStringsArray, ...values: HtmlValue[]): SafeHtml {
44
+ let out = strings[0] ?? "";
45
+ values.forEach((value, i) => {
46
+ out += render(value) + (strings[i + 1] ?? "");
47
+ });
48
+ return new SafeHtml(out);
49
+ }
50
+
51
+ /** A link a reader may follow: `https:` or `mailto:` only, or this throws. */
52
+ export function safeUrl(url: string): string {
53
+ let parsed: URL;
54
+ try {
55
+ parsed = new URL(url);
56
+ } catch {
57
+ throw new Error(`not a URL: ${url}`);
58
+ }
59
+ if (parsed.protocol !== "https:" && parsed.protocol !== "mailto:") {
60
+ throw new Error(`email links must be https: or mailto:, not ${parsed.protocol}`);
61
+ }
62
+ return parsed.toString();
63
+ }
64
+
65
+ /** The party's contact details, printed in every email's footer. */
66
+ export const PARTY = {
67
+ name: "พรรคประชาชน",
68
+ logoUrl: "https://services.ppleth.ai/email-assets/logo.png",
69
+ logoAlt: "ภาพโลโก้พรรคประชาชน",
70
+ phone: "02-038-5100",
71
+ phoneHref: "tel:+6620385100",
72
+ hours: "จันทร์-ศุกร์ 10:00-18:00 น.",
73
+ email: "office@peoplespartythailand.org",
74
+ line: "@peoplesth",
75
+ lineUrl: "https://line.me/R/ti/p/@peoplesth",
76
+ address: ["พรรคประชาชน สำนักงานใหญ่", "เลขที่ 167 ชั้น 4 ซอย รามคำแหง 42", "แขวงหัวหมาก เขตบางกะปิ", "กรุงเทพมหานคร 10240"],
77
+ } as const;
78
+
79
+ const NAVY = "#0b3a66";
80
+ const ORANGE = "#ff6413";
81
+ const INK = "#1f2937";
82
+ const FONT = "'IBM Plex Sans Thai Looped','Noto Sans Thai','Leelawadee UI',Tahoma,sans-serif";
83
+
84
+ export interface PartyEmailInput {
85
+ /** The big line at the top of the message. */
86
+ heading: string;
87
+ /** The message, built with `html`. */
88
+ content: SafeHtml;
89
+ /** The same message as plain text, for mail apps that show no HTML. */
90
+ text: string;
91
+ /** One call to action. Its URL must be `https:` or `mailto:`. */
92
+ button?: { label: string; url: string };
93
+ /** The grey line mail apps show beside the subject. Defaults to none. */
94
+ preheader?: string;
95
+ }
96
+
97
+ // Dark mode. Mail apps treat it three ways, and the layout is built for all:
98
+ // - Apple Mail, iOS Mail, Outlook for Mac honour `prefers-color-scheme`: the
99
+ // <style> block below swaps the light card for a dark one.
100
+ // - Gmail's apps ignore it and invert colours themselves: a navy block turns
101
+ // light blue and white text turns dark. They leave a `background-image`
102
+ // alone, so the navy header and the orange button are painted twice
103
+ // (`bg()`). Text colours are still inverted, so nothing relies on white text
104
+ // on a dark block: the header carries only the logo (an image, never
105
+ // inverted), and the footer is light with dark text, which Gmail inverts
106
+ // the way it inverts the body.
107
+ // - Clients that do neither show the light layout as it is.
108
+ const DARK_CSS = `
109
+ :root { color-scheme: light dark; supported-color-schemes: light dark; }
110
+ a[x-apple-data-detectors] { color: inherit !important; text-decoration: none !important; }
111
+ @media (prefers-color-scheme: dark) {
112
+ .pe-page { background: #0f141a !important; }
113
+ .pe-card { background: #1a2129 !important; }
114
+ .pe-ink, .pe-ink p, .pe-ink td, .pe-ink li { color: #e6e9ee !important; }
115
+ .pe-heading { color: #ffb48a !important; }
116
+ .pe-ink td { border-color: #2c3540 !important; }
117
+ .pe-foot { background: #11171e !important; border-color: #2c3540 !important; }
118
+ .pe-foot-title { color: #ffb48a !important; }
119
+ .pe-foot-label { color: #8b96a5 !important; }
120
+ .pe-foot-text, .pe-foot-text a { color: #d5dbe3 !important; }
121
+ }`;
122
+
123
+ /** A solid block Gmail's dark mode leaves alone: the colour twice, once as an image. */
124
+ const bg = (color: string) => raw(`background-color:${color};background-image:linear-gradient(${color},${color})`);
125
+
126
+ /** The party layout around your message: `{ html, body }` for `sendEmail`. */
127
+ export function partyEmail(input: PartyEmailInput): { html: string; body: string } {
128
+ const button = input.button
129
+ ? html`<table role="presentation" cellpadding="0" cellspacing="0" style="margin:24px 0 8px"><tr><td style="border-radius:8px;${bg(ORANGE)}">
130
+ <a href="${safeUrl(input.button.url)}" style="display:inline-block;padding:12px 24px;font-family:${raw(FONT)};font-size:16px;font-weight:bold;color:#ffffff;text-decoration:none;border-radius:8px">${input.button.label}</a>
131
+ </td></tr></table>`
132
+ : "";
133
+
134
+ const contactRow = (label: string, value: SafeHtml) =>
135
+ html`<tr><td class="pe-foot-label" style="padding:4px 12px 4px 0;font-size:14px;color:#6b7280;white-space:nowrap;vertical-align:top">${label}</td><td class="pe-foot-text" style="padding:4px 0;font-size:15px;color:${INK}">${value}</td></tr>`;
136
+ const link = (href: string, text: string) => html`<a href="${href}" style="color:${INK};text-decoration:none">${text}</a>`;
137
+ // A deliberate, styled link: left bare, Gmail and iOS underline the address in blue themselves.
138
+ const mapsUrl = `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(PARTY.address.join(" "))}`;
139
+
140
+ const page = html`<!doctype html>
141
+ <html lang="th"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
142
+ <meta name="color-scheme" content="light dark"><meta name="supported-color-schemes" content="light dark">
143
+ <meta name="format-detection" content="telephone=no,address=no,email=no,date=no">
144
+ <title>${input.heading}</title><style>${raw(DARK_CSS)}</style></head>
145
+ <body class="body pe-page" style="margin:0;padding:0;background:#eef1f5">
146
+ ${input.preheader ? html`<div style="display:none;max-height:0;overflow:hidden;opacity:0">${input.preheader}</div>` : ""}
147
+ <table role="presentation" class="pe-page" width="100%" cellpadding="0" cellspacing="0" style="background:#eef1f5"><tr><td align="center" style="padding:24px 12px">
148
+ <table role="presentation" class="pe-card" width="100%" cellpadding="0" cellspacing="0" style="max-width:600px;background:#ffffff;border-radius:12px;overflow:hidden;font-family:${raw(FONT)}">
149
+ <tr><td style="${bg(NAVY)};padding:24px 32px"><img src="${PARTY.logoUrl}" width="176" height="50" alt="${PARTY.logoAlt}" style="display:block;border:0;color:#ffffff;font-size:20px;font-weight:bold"></td></tr>
150
+ <tr><td class="pe-ink" style="padding:32px 32px 24px;color:${INK};font-size:16px;line-height:1.7">
151
+ <h1 class="pe-heading" style="margin:0 0 16px;font-size:24px;line-height:1.4;color:${NAVY}">${input.heading}</h1>
152
+ ${input.content}
153
+ ${button}
154
+ </td></tr>
155
+ <tr><td class="pe-foot" style="background:#f3f5f8;border-top:1px solid #e3e7ed;padding:28px 32px">
156
+ <div class="pe-foot-title" style="margin:0 0 12px;font-size:20px;font-weight:bold;color:${NAVY}">ติดต่อเรา</div>
157
+ <table role="presentation" cellpadding="0" cellspacing="0">
158
+ ${contactRow("โทร", html`${link(PARTY.phoneHref, PARTY.phone)} (${PARTY.hours})`)}
159
+ ${contactRow("อีเมล", link(`mailto:${PARTY.email}`, PARTY.email))}
160
+ ${contactRow("LINE", link(PARTY.lineUrl, PARTY.line))}
161
+ </table>
162
+ <div class="pe-foot-text" style="margin:16px 0 0;font-size:14px;line-height:1.6;color:#4b5563"><a href="${mapsUrl}" style="color:#4b5563;text-decoration:none">${PARTY.address.map((line, i) => html`${i ? raw("<br>") : ""}${line}`)}</a></div>
163
+ </td></tr>
164
+ </table>
165
+ </td></tr></table>
166
+ </body></html>`;
167
+
168
+ const body = [
169
+ input.heading,
170
+ "",
171
+ input.text,
172
+ ...(input.button ? ["", `${input.button.label}: ${safeUrl(input.button.url)}`] : []),
173
+ "",
174
+ "—",
175
+ `ติดต่อ${PARTY.name}`,
176
+ `โทร ${PARTY.phone} (${PARTY.hours})`,
177
+ `อีเมล ${PARTY.email}`,
178
+ `LINE ${PARTY.line}`,
179
+ ...PARTY.address,
180
+ ].join("\n");
181
+
182
+ return { html: page.value, body };
183
+ }