@hszrdm/create-commerce 1.21.0 → 1.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/cli.mjs +7 -1
- package/templates/backend/package.json +1 -1
- package/templates/backend/scripts/commerce-ops.mjs +12 -5
- package/templates/backend/scripts/core-restore.mjs +20 -19
- package/templates/backend/scripts/lib/core-backup-common.mjs +83 -0
- package/templates/backend/scripts/production-assets/restore.sh +1 -1
- package/templates/storefront/README.md +16 -5
- package/templates/storefront/package-lock.json +1646 -0
- package/templates/storefront/package.json +6 -2
- package/templates/storefront/src/app/api/checkout/trusted-shop/route.ts +19 -0
- package/templates/storefront/src/app/checkout/success/actions.ts +17 -0
- package/templates/storefront/src/app/checkout/success/page.tsx +32 -22
- package/templates/storefront/src/app/globals.css +5 -0
- package/templates/storefront/src/components/account-dashboard.tsx +35 -15
- package/templates/storefront/src/components/account-verification-panel.tsx +29 -17
- package/templates/storefront/src/components/arukereso-trusted-shop.tsx +5 -5
- package/templates/storefront/src/components/checkout-flow.tsx +7 -1
- package/templates/storefront/src/components/cms-content.tsx +3 -1
- package/templates/storefront/src/components/consent-banner.tsx +10 -10
- package/templates/storefront/src/components/order-detail.tsx +79 -47
- package/templates/storefront/src/lib/core/abandoned-cart.ts +5 -1
- package/templates/storefront/src/lib/core/account.ts +2 -0
- package/templates/storefront/src/lib/core/after-sales.ts +50 -13
- package/templates/storefront/src/lib/core/checkout-confirmation-server.ts +16 -0
- package/templates/storefront/src/lib/core/checkout-proof.ts +16 -0
- package/templates/storefront/src/lib/core/checkout.ts +5 -10
- package/templates/storefront/src/lib/core/sanitize-cms-html.ts +30 -0
- package/templates/storefront/src/lib/storefront-copy.ts +8 -0
- package/templates/storefront/tests/account-verification.test.mjs +12 -0
- package/templates/storefront/tests/cms-html.test.mjs +49 -0
- package/templates/storefront/tests/consent.test.mjs +120 -0
- package/templates/storefront/tests/ga4-bootstrap.test.mjs +1 -1
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
import Link from "next/link"
|
|
4
4
|
import { usePathname } from "next/navigation"
|
|
5
|
-
import { useCallback, useEffect, useMemo, useState } from "react"
|
|
5
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react"
|
|
6
6
|
import { accountApi } from "@/lib/core/account"
|
|
7
7
|
import { afterSalesApi, type AfterSalesCaseType } from "@/lib/core/after-sales"
|
|
8
|
-
import { medusaFetch } from "@/lib/core/client"
|
|
8
|
+
import { CoreApiError, medusaFetch } from "@/lib/core/client"
|
|
9
9
|
import {
|
|
10
10
|
asRecords,
|
|
11
11
|
formatMoney,
|
|
@@ -41,6 +41,7 @@ export function OrderDetail({ config, orderId }: { config: CoreConfig; orderId:
|
|
|
41
41
|
const [variants, setVariants] = useState<JsonRecord[]>([])
|
|
42
42
|
const [loading, setLoading] = useState(true)
|
|
43
43
|
const [error, setError] = useState<string | null>(null)
|
|
44
|
+
const [actionError, setActionError] = useState<string | null>(null)
|
|
44
45
|
const [message, setMessage] = useState<string | null>(null)
|
|
45
46
|
const [mode, setMode] = useState<CaseMode>("return")
|
|
46
47
|
const [itemId, setItemId] = useState("")
|
|
@@ -51,6 +52,7 @@ export function OrderDetail({ config, orderId }: { config: CoreConfig; orderId:
|
|
|
51
52
|
const [newVariantId, setNewVariantId] = useState("")
|
|
52
53
|
const [description, setDescription] = useState("")
|
|
53
54
|
const [busy, setBusy] = useState(false)
|
|
55
|
+
const submitting = useRef(false)
|
|
54
56
|
|
|
55
57
|
const load = useCallback(async () => {
|
|
56
58
|
setLoading(true)
|
|
@@ -110,6 +112,8 @@ export function OrderDetail({ config, orderId }: { config: CoreConfig; orderId:
|
|
|
110
112
|
const selectedItem = items.find((item) => stringValue(item.id) === itemId) || items[0]
|
|
111
113
|
const currency = stringValue(order.currency_code || config.store.default_currency).toUpperCase()
|
|
112
114
|
const afterConfig = after && isRecord(after.config) ? after.config : {}
|
|
115
|
+
const operation = isRecord(after?.operation) ? after.operation : null
|
|
116
|
+
const blocked = operation?.state === "processing" || operation?.state === "recovery_required"
|
|
113
117
|
const returns = asRecords(after?.returns)
|
|
114
118
|
const exchanges = asRecords(after?.exchanges)
|
|
115
119
|
const claims = asRecords(after?.claims)
|
|
@@ -119,57 +123,69 @@ export function OrderDetail({ config, orderId }: { config: CoreConfig; orderId:
|
|
|
119
123
|
...claims.map((entry) => ({ ...entry, __case_type: "claim" as const })),
|
|
120
124
|
].sort((a: VisibleCase, b: VisibleCase) => stringValue(b.created_at).localeCompare(stringValue(a.created_at)))
|
|
121
125
|
|
|
122
|
-
const
|
|
123
|
-
if (
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
+
const runAction = async (action: () => Promise<void>) => {
|
|
127
|
+
if (submitting.current || blocked) return
|
|
128
|
+
submitting.current = true
|
|
126
129
|
setBusy(true)
|
|
127
|
-
|
|
130
|
+
setActionError(null)
|
|
128
131
|
setMessage(null)
|
|
129
132
|
try {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
items: [{ ...baseItem, reason_id: reasonId || undefined }],
|
|
138
|
-
description: description || undefined,
|
|
139
|
-
locale: apiLocale,
|
|
140
|
-
})
|
|
141
|
-
} else if (mode === "exchange") {
|
|
142
|
-
if (!newVariantId) {
|
|
143
|
-
throw new Error(localized(locale, "Válassz csereterméket.", "Choose a replacement product."))
|
|
133
|
+
await action()
|
|
134
|
+
} catch (cause) {
|
|
135
|
+
if (cause instanceof CoreApiError && isRecord(cause.payload) && isRecord(cause.payload.operation)) {
|
|
136
|
+
const blockedOperation = cause.payload.operation
|
|
137
|
+
if (blockedOperation.state === "processing" || blockedOperation.state === "recovery_required") {
|
|
138
|
+
setAfter(current => ({ ...current, operation: blockedOperation }))
|
|
139
|
+
return
|
|
144
140
|
}
|
|
145
|
-
await afterSales.createExchange(orderId, {
|
|
146
|
-
return_items: [{ ...baseItem, reason_id: reasonId || undefined }],
|
|
147
|
-
new_items: [{ variant_id: newVariantId, quantity }],
|
|
148
|
-
description: description || undefined,
|
|
149
|
-
locale: apiLocale,
|
|
150
|
-
})
|
|
151
|
-
} else {
|
|
152
|
-
await afterSales.createClaim(orderId, {
|
|
153
|
-
type: claimType,
|
|
154
|
-
items: [{ ...baseItem, reason: claimReason }],
|
|
155
|
-
description: description || undefined,
|
|
156
|
-
locale: apiLocale,
|
|
157
|
-
})
|
|
158
141
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
} catch (cause) {
|
|
163
|
-
setError(
|
|
164
|
-
cause instanceof Error
|
|
165
|
-
? cause.message
|
|
166
|
-
: localized(locale, "A kérelem nem küldhető el.", "The request could not be submitted."),
|
|
167
|
-
)
|
|
142
|
+
setActionError(cause instanceof Error
|
|
143
|
+
? cause.message
|
|
144
|
+
: localized(locale, "A művelet sikertelen.", "The operation could not be completed."))
|
|
168
145
|
} finally {
|
|
146
|
+
submitting.current = false
|
|
169
147
|
setBusy(false)
|
|
170
148
|
}
|
|
171
149
|
}
|
|
172
150
|
|
|
151
|
+
const submit = () => runAction(async () => {
|
|
152
|
+
if (!selectedItem) {
|
|
153
|
+
throw new Error(localized(locale, "Válassz rendelési tételt.", "Choose an order item."))
|
|
154
|
+
}
|
|
155
|
+
const baseItem = {
|
|
156
|
+
id: stringValue(selectedItem.id),
|
|
157
|
+
quantity,
|
|
158
|
+
note: description || undefined,
|
|
159
|
+
}
|
|
160
|
+
if (mode === "return") {
|
|
161
|
+
await afterSales.createReturn(orderId, {
|
|
162
|
+
items: [{ ...baseItem, reason_id: reasonId || undefined }],
|
|
163
|
+
description: description || undefined,
|
|
164
|
+
locale: apiLocale,
|
|
165
|
+
})
|
|
166
|
+
} else if (mode === "exchange") {
|
|
167
|
+
if (!newVariantId) {
|
|
168
|
+
throw new Error(localized(locale, "Válassz csereterméket.", "Choose a replacement product."))
|
|
169
|
+
}
|
|
170
|
+
await afterSales.createExchange(orderId, {
|
|
171
|
+
return_items: [{ ...baseItem, reason_id: reasonId || undefined }],
|
|
172
|
+
new_items: [{ variant_id: newVariantId, quantity }],
|
|
173
|
+
description: description || undefined,
|
|
174
|
+
locale: apiLocale,
|
|
175
|
+
})
|
|
176
|
+
} else {
|
|
177
|
+
await afterSales.createClaim(orderId, {
|
|
178
|
+
type: claimType,
|
|
179
|
+
items: [{ ...baseItem, reason: claimReason }],
|
|
180
|
+
description: description || undefined,
|
|
181
|
+
locale: apiLocale,
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
setMessage(localized(locale, "A kérelem elküldve.", "Request submitted."))
|
|
185
|
+
setDescription("")
|
|
186
|
+
await load()
|
|
187
|
+
})
|
|
188
|
+
|
|
173
189
|
const canReturn = config.after_sales.enabled && config.after_sales.returns && afterConfig.returnsEnabled !== false
|
|
174
190
|
const canExchange = config.after_sales.enabled && config.after_sales.exchanges && afterConfig.exchangesEnabled !== false
|
|
175
191
|
const canClaim = config.after_sales.enabled && config.after_sales.claims && afterConfig.claimsEnabled !== false
|
|
@@ -254,9 +270,22 @@ export function OrderDetail({ config, orderId }: { config: CoreConfig; orderId:
|
|
|
254
270
|
) : null}
|
|
255
271
|
</div>
|
|
256
272
|
<label>{copy.description}<textarea value={description} rows={4} onChange={(event) => setDescription(event.target.value)} placeholder={copy.requestDescriptionPlaceholder} /></label>
|
|
257
|
-
<button className="button" type="button" disabled={busy} onClick={() => void submit()}>{busy ? copy.sending : copy.sendRequest}</button>
|
|
273
|
+
<button className="button" type="button" disabled={busy || blocked} onClick={() => void submit()}>{busy ? copy.sending : copy.sendRequest}</button>
|
|
258
274
|
{message ? <p className="success">{message}</p> : null}
|
|
259
|
-
{
|
|
275
|
+
{actionError ? <p className="error" role="alert">{actionError}</p> : null}
|
|
276
|
+
{blocked ? (
|
|
277
|
+
<div className="notice" role="status">
|
|
278
|
+
<p>{operation?.state === "processing"
|
|
279
|
+
? localized(locale, "A művelet feldolgozása folyamatban van.", "The request is being processed.")
|
|
280
|
+
: localized(locale, "Az ügy állapota ellenőrzést igényel. Kérjük, keresd az ügyfélszolgálatot.", "This case needs review. Please contact support.")}</p>
|
|
281
|
+
{operation?.id
|
|
282
|
+
? <p>{localized(locale, "Műveletazonosító", "Operation ID")} {stringValue(operation.id)}</p>
|
|
283
|
+
: operation?.case_id
|
|
284
|
+
? <p>{localized(locale, "Ügyazonosító", "Case ID")} {stringValue(operation.case_id)}</p>
|
|
285
|
+
: null}
|
|
286
|
+
<button type="button" className="text-button" onClick={() => void load()}>{localized(locale, "Állapot frissítése", "Refresh status")}</button>
|
|
287
|
+
</div>
|
|
288
|
+
) : null}
|
|
260
289
|
</section>
|
|
261
290
|
) : null}
|
|
262
291
|
|
|
@@ -271,8 +300,11 @@ export function OrderDetail({ config, orderId }: { config: CoreConfig; orderId:
|
|
|
271
300
|
</div>
|
|
272
301
|
<div>
|
|
273
302
|
<span className="status-pill">{stringValue(entry.request_status || entry.status || entry.change_status || "—")}</span>
|
|
274
|
-
{
|
|
275
|
-
? <button className="text-button danger" type="button" onClick={() => void
|
|
303
|
+
{(entry.customer_cancelable ?? (entry.request_status === "submitted")) === true
|
|
304
|
+
? <button className="text-button danger" type="button" disabled={busy || blocked} onClick={() => void runAction(async () => {
|
|
305
|
+
await afterSales.cancel(orderId, entry.__case_type, stringValue(entry.id))
|
|
306
|
+
await load()
|
|
307
|
+
})}>{copy.withdraw}</button>
|
|
276
308
|
: null}
|
|
277
309
|
</div>
|
|
278
310
|
</div>
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { CoreApiError, medusaFetch } from "./client"
|
|
2
|
+
import { rememberCartProof } from "./checkout-proof"
|
|
2
3
|
import type { CoreConfig } from "./types"
|
|
3
4
|
|
|
4
5
|
export type AbandonedCartRecoveryResult = {
|
|
5
6
|
valid: boolean
|
|
6
7
|
reason?: string
|
|
7
8
|
cart_id?: string
|
|
9
|
+
cart_proof?: string
|
|
8
10
|
locale?: "hu-HU" | "en-US" | string
|
|
9
11
|
coupon_code?: string | null
|
|
10
12
|
}
|
|
@@ -18,10 +20,12 @@ export function abandonedCartApi(config: CoreConfig) {
|
|
|
18
20
|
recover: async (token: string) => {
|
|
19
21
|
const query = new URLSearchParams({ token })
|
|
20
22
|
try {
|
|
21
|
-
|
|
23
|
+
const result = await medusaFetch<AbandonedCartRecoveryResult>(
|
|
22
24
|
`${recoveryEndpoint}?${query.toString()}`,
|
|
23
25
|
{ cache: "no-store" },
|
|
24
26
|
)
|
|
27
|
+
if (result.valid && result.cart_id) rememberCartProof(result.cart_id, result.cart_proof)
|
|
28
|
+
return result
|
|
25
29
|
} catch (cause) {
|
|
26
30
|
if (
|
|
27
31
|
cause instanceof CoreApiError &&
|
|
@@ -173,6 +173,8 @@ export function accountApi(config: CoreConfig) {
|
|
|
173
173
|
requestEmailVerification: () =>
|
|
174
174
|
medusaFetch<{
|
|
175
175
|
success: boolean
|
|
176
|
+
already_verified?: boolean
|
|
177
|
+
verification_requested?: boolean
|
|
176
178
|
retry_after_seconds: number
|
|
177
179
|
resend_available_at: string | null
|
|
178
180
|
}>(paths.email_verification, {
|
|
@@ -1,8 +1,54 @@
|
|
|
1
|
-
import { endpoint, medusaFetch } from "./client"
|
|
1
|
+
import { CoreApiError, endpoint, medusaFetch } from "./client"
|
|
2
2
|
import type { CoreConfig } from "./types"
|
|
3
3
|
|
|
4
4
|
export type AfterSalesCaseType = "return" | "exchange" | "claim"
|
|
5
5
|
|
|
6
|
+
async function submitRequest(path: string, body: Record<string, unknown>) {
|
|
7
|
+
const payload = JSON.stringify(body)
|
|
8
|
+
const key = await getAttemptKey(path, payload)
|
|
9
|
+
try {
|
|
10
|
+
const result = await medusaFetch(path, {
|
|
11
|
+
method: "POST",
|
|
12
|
+
body: payload,
|
|
13
|
+
headers: key ? { "Idempotency-Key": key.id } : undefined,
|
|
14
|
+
})
|
|
15
|
+
key?.finish()
|
|
16
|
+
return result
|
|
17
|
+
} catch (cause) {
|
|
18
|
+
if (key && cause instanceof CoreApiError &&
|
|
19
|
+
cause.payload && typeof cause.payload === "object" &&
|
|
20
|
+
"code" in cause.payload && cause.payload.code === "after_sales_failed_compensated") {
|
|
21
|
+
// A subsequent click can start a new attempt after verified rollback.
|
|
22
|
+
key.finish()
|
|
23
|
+
}
|
|
24
|
+
throw cause
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function getAttemptKey(path: string, payload: string) {
|
|
29
|
+
if (typeof window === "undefined" || !globalThis.crypto?.subtle) return null
|
|
30
|
+
try {
|
|
31
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(`${path}\n${payload}`))
|
|
32
|
+
const storageKey = `hszrdm-after-sales:${Array.from(new Uint8Array(digest), byte => byte.toString(16).padStart(2, "0")).join("")}`
|
|
33
|
+
const storage = window.sessionStorage
|
|
34
|
+
const id = storage.getItem(storageKey) || crypto.randomUUID()
|
|
35
|
+
storage.setItem(storageKey, id)
|
|
36
|
+
return {
|
|
37
|
+
id,
|
|
38
|
+
finish: () => {
|
|
39
|
+
try {
|
|
40
|
+
if (storage.getItem(storageKey) === id) storage.removeItem(storageKey)
|
|
41
|
+
} catch {
|
|
42
|
+
// The server already has the receipt even if browser storage is unavailable.
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
}
|
|
46
|
+
} catch {
|
|
47
|
+
// Restricted storage and non-secure origins use the server's legacy payload key.
|
|
48
|
+
return null
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
6
52
|
export function afterSalesApi(config: CoreConfig) {
|
|
7
53
|
const paths = config.after_sales.endpoints
|
|
8
54
|
|
|
@@ -20,22 +66,13 @@ export function afterSalesApi(config: CoreConfig) {
|
|
|
20
66
|
),
|
|
21
67
|
|
|
22
68
|
createReturn: (orderId: string, body: Record<string, unknown>) =>
|
|
23
|
-
|
|
24
|
-
method: "POST",
|
|
25
|
-
body: JSON.stringify(body),
|
|
26
|
-
}),
|
|
69
|
+
submitRequest(endpoint(paths.returns, { id: orderId }), body),
|
|
27
70
|
|
|
28
71
|
createExchange: (orderId: string, body: Record<string, unknown>) =>
|
|
29
|
-
|
|
30
|
-
method: "POST",
|
|
31
|
-
body: JSON.stringify(body),
|
|
32
|
-
}),
|
|
72
|
+
submitRequest(endpoint(paths.exchanges, { id: orderId }), body),
|
|
33
73
|
|
|
34
74
|
createClaim: (orderId: string, body: Record<string, unknown>) =>
|
|
35
|
-
|
|
36
|
-
method: "POST",
|
|
37
|
-
body: JSON.stringify(body),
|
|
38
|
-
}),
|
|
75
|
+
submitRequest(endpoint(paths.claims, { id: orderId }), body),
|
|
39
76
|
|
|
40
77
|
cancel: (orderId: string, caseType: AfterSalesCaseType, caseId: string) =>
|
|
41
78
|
medusaFetch(
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { cookies } from "next/headers"
|
|
2
|
+
import { CONFIRMATION_COOKIE, CONFIRMATION_HEADER } from "./checkout-proof"
|
|
3
|
+
|
|
4
|
+
export function validConfirmationInput(id: unknown, proof: unknown) {
|
|
5
|
+
return typeof id === "string" && /^[A-Za-z0-9_-]{1,100}$/.test(id) &&
|
|
6
|
+
typeof proof === "string" && proof.length <= 1024 && /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]{43}$/.test(proof)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export async function checkoutConfirmation() {
|
|
10
|
+
const value = (await cookies()).get(CONFIRMATION_COOKIE)?.value
|
|
11
|
+
if (!value) return null
|
|
12
|
+
const [id, payload, signature] = value.split(".")
|
|
13
|
+
const proof = `${payload}.${signature}`
|
|
14
|
+
if (!validConfirmationInput(id, proof)) return null
|
|
15
|
+
return { id, headers: { [CONFIRMATION_HEADER]: proof } }
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const CART_PROOF_KEY = "hszrdm:checkout-cart-proof:"
|
|
2
|
+
export const CONFIRMATION_COOKIE = "hszrdm_order_confirmation"
|
|
3
|
+
export const CONFIRMATION_HEADER = "x-hszrdm-order-proof"
|
|
4
|
+
|
|
5
|
+
export function rememberCartProof(cartId: string, proof: unknown) {
|
|
6
|
+
if (typeof proof === "string") window.localStorage.setItem(CART_PROOF_KEY + cartId, proof)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function cartProofHeaders(cartId: string): Record<string, string> {
|
|
10
|
+
const proof = window.localStorage.getItem(CART_PROOF_KEY + cartId)
|
|
11
|
+
return proof ? { "x-hszrdm-cart-proof": proof } : {}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function forgetCartProof(cartId: string) {
|
|
15
|
+
window.localStorage.removeItem(CART_PROOF_KEY + cartId)
|
|
16
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { cartProofHeaders, rememberCartProof } from "./checkout-proof"
|
|
1
2
|
import { endpoint, medusaFetch } from "./client"
|
|
2
3
|
import { trackGoogleAdsPurchase } from "./google-ads"
|
|
3
4
|
import {
|
|
@@ -82,15 +83,6 @@ export function checkoutApi(config: CoreConfig) {
|
|
|
82
83
|
method: "POST", body: JSON.stringify({ mode }),
|
|
83
84
|
},
|
|
84
85
|
)
|
|
85
|
-
if (mode === "bank_transfer" && config.payments.bank_transfer.enabled) {
|
|
86
|
-
const current = (await getCart(cartId)).cart
|
|
87
|
-
const metadata = isRecord(current.metadata) ? current.metadata : {}
|
|
88
|
-
const details = await bankTransferDetails()
|
|
89
|
-
const configured = isRecord(details.cart_metadata) ? details.cart_metadata : {}
|
|
90
|
-
await updateCart(cartId, {
|
|
91
|
-
metadata: { ...metadata, ...configured, core_payment_method: "bank_transfer" },
|
|
92
|
-
})
|
|
93
|
-
}
|
|
94
86
|
}
|
|
95
87
|
|
|
96
88
|
const saveFoxpostSelection = (
|
|
@@ -156,10 +148,12 @@ export function checkoutApi(config: CoreConfig) {
|
|
|
156
148
|
const region = await resolveDefaultRegion()
|
|
157
149
|
regionId = String(region.id)
|
|
158
150
|
}
|
|
159
|
-
|
|
151
|
+
const result = await medusaFetch<{ cart: Record<string, unknown>; cart_proof?: string }>(paths.create_cart, {
|
|
160
152
|
method: "POST",
|
|
161
153
|
body: JSON.stringify({ region_id: regionId, locale: config.store.locale, ...body }),
|
|
162
154
|
})
|
|
155
|
+
rememberCartProof(stringValue(result.cart.id), result.cart_proof)
|
|
156
|
+
return result
|
|
163
157
|
},
|
|
164
158
|
|
|
165
159
|
getCart,
|
|
@@ -367,6 +361,7 @@ export function checkoutApi(config: CoreConfig) {
|
|
|
367
361
|
{
|
|
368
362
|
method: "POST",
|
|
369
363
|
body: JSON.stringify({}),
|
|
364
|
+
headers: cartProofHeaders(cartId),
|
|
370
365
|
},
|
|
371
366
|
)
|
|
372
367
|
if (result.type === "order" && isRecord(result.order)) {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import sanitizeHtml from "sanitize-html"
|
|
2
|
+
|
|
3
|
+
/** Render translated or original CMS fragments with the same server-side policy. */
|
|
4
|
+
export function sanitizeCmsHtml(html: string): string {
|
|
5
|
+
return sanitizeHtml(html, {
|
|
6
|
+
allowedTags: [
|
|
7
|
+
"p", "br", "div", "span", "h1", "h2", "h3", "h4", "h5", "h6",
|
|
8
|
+
"b", "strong", "i", "em", "u", "s", "del", "ul", "ol", "li",
|
|
9
|
+
"blockquote", "pre", "code", "hr", "a", "img", "figure", "figcaption",
|
|
10
|
+
"table", "caption", "thead", "tbody", "tfoot", "tr", "th", "td",
|
|
11
|
+
],
|
|
12
|
+
allowedAttributes: {
|
|
13
|
+
a: ["href", "title", "rel"],
|
|
14
|
+
img: ["src", "alt", "title", "width", "height"],
|
|
15
|
+
ol: ["start"],
|
|
16
|
+
th: ["colspan", "rowspan", "scope"],
|
|
17
|
+
td: ["colspan", "rowspan"],
|
|
18
|
+
},
|
|
19
|
+
allowedSchemes: ["http", "https", "mailto", "tel"],
|
|
20
|
+
allowedSchemesByTag: { img: ["http", "https"] },
|
|
21
|
+
allowedSchemesAppliedToAttributes: ["href", "src"],
|
|
22
|
+
allowProtocolRelative: false,
|
|
23
|
+
parseStyleAttributes: false,
|
|
24
|
+
nonTextTags: [
|
|
25
|
+
"script", "style", "textarea", "option", "iframe", "object", "embed",
|
|
26
|
+
"svg", "math", "template", "noscript", "xmp",
|
|
27
|
+
],
|
|
28
|
+
transformTags: { a: sanitizeHtml.simpleTransform("a", { rel: "noopener noreferrer" }) },
|
|
29
|
+
})
|
|
30
|
+
}
|
|
@@ -194,6 +194,10 @@ const hu = {
|
|
|
194
194
|
previousRequests: "Korábbi kérelmek",
|
|
195
195
|
withdraw: "Visszavonás",
|
|
196
196
|
orderRecorded: "Rendelés rögzítve",
|
|
197
|
+
confirmationPaymentPending: "A fizetés még függőben van.",
|
|
198
|
+
confirmationPaymentPaid: "A fizetés megtörtént.",
|
|
199
|
+
confirmationPaymentCanceled: "A fizetést törölték.",
|
|
200
|
+
confirmationPaymentFailed: "A fizetés sikertelen volt.",
|
|
197
201
|
thankYou: "Köszönjük a rendelésed!",
|
|
198
202
|
orderNumber: "Rendelésszám",
|
|
199
203
|
continueShopping: "Tovább vásárolok",
|
|
@@ -409,6 +413,10 @@ const en: Record<CopyKey, string> = {
|
|
|
409
413
|
previousRequests: "Previous requests",
|
|
410
414
|
withdraw: "Withdraw",
|
|
411
415
|
orderRecorded: "Order received",
|
|
416
|
+
confirmationPaymentPending: "Payment is still pending.",
|
|
417
|
+
confirmationPaymentPaid: "Payment received.",
|
|
418
|
+
confirmationPaymentCanceled: "Payment was canceled.",
|
|
419
|
+
confirmationPaymentFailed: "Payment failed.",
|
|
412
420
|
thankYou: "Thank you for your order!",
|
|
413
421
|
orderNumber: "Order number",
|
|
414
422
|
continueShopping: "Continue shopping",
|
|
@@ -38,3 +38,15 @@ test("cart ownership is transferred only after email confirmation", () => {
|
|
|
38
38
|
assert.match(dashboard, /if \(signedIn && isAccountEmailVerified\(signedIn\)\)/)
|
|
39
39
|
assert.match(dashboard, /if \(updated && isAccountEmailVerified\(updated\)\)/)
|
|
40
40
|
})
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
test("verification links are removed before login and handed to the verification panel", () => {
|
|
44
|
+
const dashboard = read("components/account-dashboard.tsx")
|
|
45
|
+
const panel = read("components/account-verification-panel.tsx")
|
|
46
|
+
assert.match(dashboard, /new URLSearchParams\(url\.hash\.slice\(1\)\)/)
|
|
47
|
+
assert.match(dashboard, /window\.history\.replaceState/)
|
|
48
|
+
assert.match(dashboard, /linkCode=\{verificationLink\}/)
|
|
49
|
+
assert.match(panel, /void confirm\(linkCode\)/)
|
|
50
|
+
assert.match(panel, /inFlight\.current/)
|
|
51
|
+
assert.doesNotMatch(dashboard, /sessionStorage|localStorage/)
|
|
52
|
+
})
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import assert from "node:assert/strict"
|
|
2
|
+
import { readFileSync } from "node:fs"
|
|
3
|
+
import test from "node:test"
|
|
4
|
+
import { createElement } from "react"
|
|
5
|
+
import { renderToStaticMarkup } from "react-dom/server"
|
|
6
|
+
import { sanitizeCmsHtml } from "../src/lib/core/sanitize-cms-html.ts"
|
|
7
|
+
|
|
8
|
+
test("CMS sanitizer removes executable markup and unsafe URL attributes", () => {
|
|
9
|
+
const html = [
|
|
10
|
+
'<script>console.log("script fixture")</script>',
|
|
11
|
+
'<img src="/safe.png" onerror="console.log(1)">',
|
|
12
|
+
'<p style="color:red" onclick="console.log(1)">Hello</p>',
|
|
13
|
+
'<a href="javascript:void(0)">link</a>',
|
|
14
|
+
'<a href="javascript:void(0)">encoded</a>',
|
|
15
|
+
'<img src="data:text/html,fixture">',
|
|
16
|
+
'<svg onload="console.log(1)"></svg>',
|
|
17
|
+
'<math><mi>sample</mi></math>',
|
|
18
|
+
'<iframe src="https://example.test"></iframe>',
|
|
19
|
+
'<p><b>Nested<p>paragraph',
|
|
20
|
+
].join("")
|
|
21
|
+
const result = sanitizeCmsHtml(html)
|
|
22
|
+
assert.doesNotMatch(result, /<\/?(?:script|svg|math|iframe)\b|\son\w+=|\sstyle=|javascript:|data:text|console\.log/i)
|
|
23
|
+
assert.match(result, /src="\/safe\.png"/)
|
|
24
|
+
assert.match(result, /Nested/)
|
|
25
|
+
assert.match(result, /paragraph/)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test("SSR preserves localized formatting, links and images with the same policy", () => {
|
|
29
|
+
const html = '<h2>Árvíztűrő tükörfúrógép</h2><p><strong>English</strong> <em>CMS</em></p>' +
|
|
30
|
+
'<ol><li>One</li></ol><blockquote>Quote</blockquote>' +
|
|
31
|
+
'<a href="https://example.test/help" target="_blank">Link</a>' +
|
|
32
|
+
'<img src="/uploads/guide.png" alt="Útmutató">'
|
|
33
|
+
const result = renderToStaticMarkup(createElement("div", {
|
|
34
|
+
dangerouslySetInnerHTML: { __html: sanitizeCmsHtml(html) },
|
|
35
|
+
}))
|
|
36
|
+
assert.match(result, /<h2>Árvíztűrő tükörfúrógép<\/h2>/)
|
|
37
|
+
assert.match(result, /<strong>English<\/strong> <em>CMS<\/em>/)
|
|
38
|
+
assert.match(result, /<ol><li>One<\/li><\/ol><blockquote>Quote<\/blockquote>/)
|
|
39
|
+
assert.match(result, /<a href="https:\/\/example\.test\/help" rel="noopener noreferrer">Link<\/a>/)
|
|
40
|
+
assert.match(result, /<img src="\/uploads\/guide\.png" alt="Útmutató"/)
|
|
41
|
+
assert.doesNotMatch(result, /target=/)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test("the shared CMS component sanitizes HTML and React-escapes the text and block paths", () => {
|
|
45
|
+
const component = readFileSync(new URL("../src/components/cms-content.tsx", import.meta.url), "utf8")
|
|
46
|
+
assert.match(component, /__html: sanitizeCmsHtml\(entry\.body\)/)
|
|
47
|
+
assert.match(component, /<p key=\{index\}>\{paragraph\}<\/p>/)
|
|
48
|
+
assert.match(component, /<blockquote>\{text\}<\/blockquote>/)
|
|
49
|
+
})
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import assert from "node:assert/strict"
|
|
2
|
+
import { readFileSync } from "node:fs"
|
|
3
|
+
import { afterEach, test } from "node:test"
|
|
4
|
+
import {
|
|
5
|
+
COOKIE_CONSENT_KEY,
|
|
6
|
+
COOKIE_CONSENT_EVENT,
|
|
7
|
+
defaultConsentPreferences,
|
|
8
|
+
readConsentPreferences,
|
|
9
|
+
saveConsentPreferences,
|
|
10
|
+
toGoogleConsentState,
|
|
11
|
+
} from "../src/lib/core/consent.ts"
|
|
12
|
+
|
|
13
|
+
const originalWindow = globalThis.window
|
|
14
|
+
const originalCustomEvent = globalThis.CustomEvent
|
|
15
|
+
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
if (originalWindow === undefined) delete globalThis.window
|
|
18
|
+
else globalThis.window = originalWindow
|
|
19
|
+
if (originalCustomEvent === undefined) delete globalThis.CustomEvent
|
|
20
|
+
else globalThis.CustomEvent = originalCustomEvent
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test("first visit keeps necessary storage active and optional categories OFF", () => {
|
|
24
|
+
const preferences = defaultConsentPreferences()
|
|
25
|
+
assert.equal(preferences.necessary, true)
|
|
26
|
+
assert.equal(preferences.analytics, false)
|
|
27
|
+
assert.equal(preferences.marketing, false)
|
|
28
|
+
assert.deepEqual(toGoogleConsentState(preferences), {
|
|
29
|
+
analytics_storage: "denied",
|
|
30
|
+
ad_storage: "denied",
|
|
31
|
+
ad_user_data: "denied",
|
|
32
|
+
ad_personalization: "denied",
|
|
33
|
+
})
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test("Core banner shows accept all, save settings and reject optional in that order", () => {
|
|
37
|
+
const component = readFileSync(new URL("../src/components/consent-banner.tsx", import.meta.url), "utf8")
|
|
38
|
+
const css = readFileSync(new URL("../src/app/globals.css", import.meta.url), "utf8")
|
|
39
|
+
const start = component.indexOf('<div className="consent-actions">')
|
|
40
|
+
assert.ok(start >= 0)
|
|
41
|
+
const end = component.indexOf("</div>", start)
|
|
42
|
+
assert.ok(end > start)
|
|
43
|
+
const actions = component.slice(start, end)
|
|
44
|
+
const accept = actions.indexOf("Mindet elfogadom")
|
|
45
|
+
const save = actions.indexOf("Beállítások mentése")
|
|
46
|
+
const reject = actions.indexOf("Opcionálisak elutasítása")
|
|
47
|
+
assert.ok(accept >= 0 && save > accept && reject > save)
|
|
48
|
+
assert.equal((actions.match(/<button /g) || []).length, 3)
|
|
49
|
+
assert.match(actions, /save\(\{ analytics: true, marketing: true \}\)/)
|
|
50
|
+
assert.match(actions, /save\(\{ analytics, marketing \}\)/)
|
|
51
|
+
assert.match(actions, /save\(\{ analytics: false, marketing: false \}\)/)
|
|
52
|
+
assert.match(actions, /className="button secondary consent-reject"/)
|
|
53
|
+
assert.match(css, /\.consent-actions > \.consent-reject \{ min-height: 44px;/)
|
|
54
|
+
assert.match(css, /font-size: \.88rem/)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test("necessary is checked, optional categories are not prechecked, and saved choices are restored", () => {
|
|
58
|
+
const component = readFileSync(new URL("../src/components/consent-banner.tsx", import.meta.url), "utf8")
|
|
59
|
+
assert.match(component, /<input type="checkbox" checked disabled \/>/)
|
|
60
|
+
assert.match(component, /useState\(false\)/)
|
|
61
|
+
assert.match(component, /setAnalytics\(Boolean\(stored\?\.analytics\)\)/)
|
|
62
|
+
assert.match(component, /setMarketing\(Boolean\(stored\?\.marketing\)\)/)
|
|
63
|
+
assert.match(component, /setOpen\(!stored\)/)
|
|
64
|
+
assert.match(component, /setAnalytics\(Boolean\(latest\?\.analytics\)\)/)
|
|
65
|
+
assert.match(component, /setMarketing\(Boolean\(latest\?\.marketing\)\)/)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test("save, accept all and reject optional persist exact choices and notify analytics", () => {
|
|
69
|
+
const storage = new Map()
|
|
70
|
+
const commands = []
|
|
71
|
+
const events = []
|
|
72
|
+
globalThis.CustomEvent = class {
|
|
73
|
+
constructor(type, init) {
|
|
74
|
+
this.type = type
|
|
75
|
+
this.detail = init?.detail
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
globalThis.window = {
|
|
79
|
+
localStorage: {
|
|
80
|
+
getItem: (key) => storage.get(key) ?? null,
|
|
81
|
+
setItem: (key, value) => storage.set(key, value),
|
|
82
|
+
},
|
|
83
|
+
gtag: (...args) => commands.push(args),
|
|
84
|
+
dispatchEvent: (event) => { events.push(event); return true },
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
assert.equal(readConsentPreferences(), null)
|
|
88
|
+
|
|
89
|
+
const accepted = saveConsentPreferences({ analytics: true, marketing: true })
|
|
90
|
+
assert.equal(accepted?.necessary, true)
|
|
91
|
+
assert.equal(readConsentPreferences()?.analytics, true)
|
|
92
|
+
assert.equal(readConsentPreferences()?.marketing, true)
|
|
93
|
+
assert.equal(commands.at(-1)[2].analytics_storage, "granted")
|
|
94
|
+
assert.equal(commands.at(-1)[2].ad_storage, "granted")
|
|
95
|
+
|
|
96
|
+
const custom = saveConsentPreferences({ analytics: true, marketing: false })
|
|
97
|
+
assert.equal(custom?.analytics, true)
|
|
98
|
+
assert.equal(custom?.marketing, false)
|
|
99
|
+
assert.equal(commands.at(-1)[2].analytics_storage, "granted")
|
|
100
|
+
assert.equal(commands.at(-1)[2].ad_storage, "denied")
|
|
101
|
+
|
|
102
|
+
const rejected = saveConsentPreferences({ analytics: false, marketing: false })
|
|
103
|
+
assert.equal(rejected?.necessary, true)
|
|
104
|
+
assert.equal(readConsentPreferences()?.analytics, false)
|
|
105
|
+
assert.equal(readConsentPreferences()?.marketing, false)
|
|
106
|
+
assert.equal(commands.at(-1)[2].analytics_storage, "denied")
|
|
107
|
+
assert.equal(commands.at(-1)[2].ad_storage, "denied")
|
|
108
|
+
assert.equal(events.length, 3)
|
|
109
|
+
assert.ok(events.every(event => event.type === COOKIE_CONSENT_EVENT))
|
|
110
|
+
assert.ok(storage.has(COOKIE_CONSENT_KEY))
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test("tampered consent never silently opts into optional categories", () => {
|
|
114
|
+
globalThis.window = {
|
|
115
|
+
localStorage: {
|
|
116
|
+
getItem: () => JSON.stringify({ version: 1, necessary: true, analytics: "true", marketing: true }),
|
|
117
|
+
},
|
|
118
|
+
}
|
|
119
|
+
assert.equal(readConsentPreferences(), null)
|
|
120
|
+
})
|
|
@@ -57,7 +57,7 @@ test("payment success replays the purchase for GA4 and consent changes without c
|
|
|
57
57
|
const checkout = readFileSync(new URL("../src/lib/core/checkout.ts", import.meta.url), "utf8")
|
|
58
58
|
const tracking = readFileSync(new URL("../src/lib/core/tracking.ts", import.meta.url), "utf8")
|
|
59
59
|
|
|
60
|
-
assert.match(success, /<GoogleAnalyticsPurchase analytics=\{config\.analytics\} order=\{
|
|
60
|
+
assert.match(success, /<GoogleAnalyticsPurchase analytics=\{config\.analytics\} order=\{analyticsOrder\}/)
|
|
61
61
|
assert.match(component, /trackPurchase\(analytics, order\)/)
|
|
62
62
|
assert.match(component, /window\.addEventListener\(COOKIE_CONSENT_EVENT, send\)/)
|
|
63
63
|
assert.match(checkout, /trackPurchase\(config\.analytics, result\.order\)/)
|