@jarwizz/create-jarshop 0.1.9 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +1 -0
- package/dist/cli.js.map +1 -1
- package/dist/generator.d.ts +11 -1
- package/dist/generator.js +45 -11
- package/dist/generator.js.map +1 -1
- package/dist/presets.d.ts +26 -0
- package/dist/presets.js +94 -0
- package/dist/presets.js.map +1 -0
- package/dist/template/CONTEXT.md +4 -2
- package/dist/template/README.md +255 -3
- package/dist/template/env.example.template +19 -0
- package/dist/template/gitignore.template +1 -0
- package/dist/template/jarshop.config.ts +31 -0
- package/dist/template/package.json +4 -0
- package/dist/template/pnpm-lock.yaml +3284 -1815
- package/dist/template/pnpm-workspace.yaml +6 -1
- package/dist/template/src/client/features.ts +41 -0
- package/dist/template/src/client/invoicing.ts +29 -0
- package/dist/template/src/dev-storefront-environment.test.ts +45 -0
- package/dist/template/src/dev-storefront-environment.ts +32 -0
- package/dist/template/src/dev-storefront.ts +3 -12
- package/dist/template/src/environment.d.ts +8 -0
- package/dist/template/src/invoice-email-recover.ts +59 -0
- package/dist/template/src/jarshop-config-loader.ts +118 -0
- package/dist/template/src/jarshop-config.test.ts +335 -0
- package/dist/template/src/jarshop-config.ts +439 -0
- package/dist/template/src/jarshop-features.test.ts +222 -0
- package/dist/template/src/jarshop-features.ts +112 -0
- package/dist/template/src/operation-codes.ts +34 -0
- package/dist/template/src/packeta-config.test.ts +95 -0
- package/dist/template/src/packeta-countries.test.ts +161 -0
- package/dist/template/src/packeta-countries.ts +120 -0
- package/dist/template/src/packeta-validation.test.ts +35 -0
- package/dist/template/src/packeta-validation.ts +52 -0
- package/dist/template/src/payment-return-url.test.ts +57 -0
- package/dist/template/src/payment-return-url.ts +34 -0
- package/dist/template/src/plugins/jarshop-checkout/confirmation-view.test.ts +79 -0
- package/dist/template/src/plugins/jarshop-checkout/confirmation.ts +51 -0
- package/dist/template/src/plugins/jarshop-checkout/dashboard/index.tsx +1 -0
- package/dist/template/src/plugins/jarshop-checkout/invoice-delivery.plugin.ts +79 -0
- package/dist/template/src/plugins/jarshop-checkout/invoice-email-events.ts +35 -0
- package/dist/template/src/plugins/jarshop-checkout/invoice-email-lifecycle.ts +235 -0
- package/dist/template/src/plugins/jarshop-checkout/invoice-email-worker.test.ts +426 -0
- package/dist/template/src/plugins/jarshop-checkout/invoice-email.test.ts +107 -0
- package/dist/template/src/plugins/jarshop-checkout/jarshop-checkout.plugin.ts +161 -57
- package/dist/template/src/plugins/jarshop-checkout/market-channels.test.ts +58 -0
- package/dist/template/src/plugins/jarshop-checkout/market-channels.ts +87 -0
- package/dist/template/src/plugins/jarshop-checkout/operation-code-check.ts +36 -0
- package/dist/template/src/plugins/jarshop-checkout/order-email-configuration.entity.ts +1 -1
- package/dist/template/src/plugins/jarshop-checkout/order-email-configuration.ts +1 -0
- package/dist/template/src/plugins/jarshop-checkout/order-email-delivery-core.ts +94 -1
- package/dist/template/src/plugins/jarshop-checkout/order-email-delivery.entity.ts +1 -0
- package/dist/template/src/plugins/jarshop-checkout/order-email-outbox.ts +83 -1
- package/dist/template/src/plugins/jarshop-checkout/order-email-worker.ts +136 -1
- package/dist/template/src/plugins/jarshop-checkout/shipping-tax.test.ts +109 -0
- package/dist/template/src/plugins/jarshop-checkout/shipping.test.ts +156 -0
- package/dist/template/src/plugins/jarshop-checkout/shipping.ts +75 -11
- package/dist/template/src/seed.ts +427 -184
- package/dist/template/src/vendure-config.ts +258 -113
- package/dist/template/storefront/README.md +7 -0
- package/dist/template/storefront/app/[locale]/[slug]/page.tsx +3 -2
- package/dist/template/storefront/app/[locale]/account/login/page.tsx +3 -2
- package/dist/template/storefront/app/[locale]/account/page.tsx +10 -4
- package/dist/template/storefront/app/[locale]/account/password-reset/callback/route.ts +4 -2
- package/dist/template/storefront/app/[locale]/account/password-reset/complete/page.tsx +3 -2
- package/dist/template/storefront/app/[locale]/account/password-reset/page.tsx +3 -2
- package/dist/template/storefront/app/[locale]/account/register/page.tsx +3 -2
- package/dist/template/storefront/app/[locale]/account/verify/complete/route.ts +4 -2
- package/dist/template/storefront/app/[locale]/account/verify/page.tsx +3 -2
- package/dist/template/storefront/app/[locale]/cart/page.tsx +3 -2
- package/dist/template/storefront/app/[locale]/checkout/actions.ts +43 -0
- package/dist/template/storefront/app/[locale]/checkout/checkout-form.tsx +188 -36
- package/dist/template/storefront/app/[locale]/checkout/confirmation/route.ts +2 -2
- package/dist/template/storefront/app/[locale]/checkout/packeta-chooser.tsx +96 -0
- package/dist/template/storefront/app/[locale]/checkout/page.tsx +60 -2
- package/dist/template/storefront/app/[locale]/checkout/payment-return/actions.ts +55 -0
- package/dist/template/storefront/app/[locale]/checkout/payment-return/page.tsx +32 -0
- package/dist/template/storefront/app/[locale]/checkout/payment-return/payment-return.tsx +84 -0
- package/dist/template/storefront/app/[locale]/checkout/success/page.tsx +32 -2
- package/dist/template/storefront/app/[locale]/layout.tsx +3 -2
- package/dist/template/storefront/app/[locale]/page.tsx +3 -2
- package/dist/template/storefront/app/globals.css +16 -0
- package/dist/template/storefront/app/layout.tsx +6 -2
- package/dist/template/storefront/lib/cart.ts +2 -4
- package/dist/template/storefront/lib/checkout-contract.test.ts +18 -1
- package/dist/template/storefront/lib/checkout-contract.ts +8 -0
- package/dist/template/storefront/lib/checkout-countries.test.ts +61 -0
- package/dist/template/storefront/lib/checkout-countries.ts +67 -0
- package/dist/template/storefront/lib/checkout.ts +22 -11
- package/dist/template/storefront/lib/commerce.ts +262 -2
- package/dist/template/storefront/lib/confirmation.test.ts +116 -0
- package/dist/template/storefront/lib/confirmation.ts +13 -7
- package/dist/template/storefront/lib/i18n.test.ts +32 -12
- package/dist/template/storefront/lib/i18n.ts +29 -8
- package/dist/template/storefront/lib/market-config.ts +40 -0
- package/dist/template/storefront/lib/market-routing.test.ts +107 -0
- package/dist/template/storefront/lib/market-routing.ts +57 -0
- package/dist/template/storefront/lib/market.ts +51 -0
- package/dist/template/storefront/lib/packeta-selection.test.ts +141 -0
- package/dist/template/storefront/lib/packeta-selection.ts +98 -0
- package/dist/template/storefront/lib/payment-return-actions.test.ts +61 -0
- package/dist/template/storefront/lib/payment-return-page.test.ts +30 -0
- package/dist/template/storefront/lib/payment-return-polling.test.ts +37 -0
- package/dist/template/storefront/lib/payment-return-polling.ts +58 -0
- package/dist/template/storefront/messages/en.json +13 -1
- package/dist/template/storefront/messages/sk.json +13 -1
- package/dist/template/storefront/package.json +2 -1
- package/dist/template/storefront/proxy.ts +35 -0
- package/dist/template/turbo.json +5 -1
- package/dist/template-manifest.json +1 -1
- package/dist/template.js +1 -0
- package/dist/template.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { parseJarShopConfig } from "../../src/jarshop-config";
|
|
4
|
+
import { marketForRequestHost, routeMarketRequest } from "./market-routing";
|
|
5
|
+
|
|
6
|
+
const slovakia = {
|
|
7
|
+
code: "sk",
|
|
8
|
+
channelToken: "sk",
|
|
9
|
+
defaultLanguage: "sk",
|
|
10
|
+
languages: ["sk", "en"],
|
|
11
|
+
currency: "EUR",
|
|
12
|
+
countries: ["SK"],
|
|
13
|
+
domains: ["shop.example.sk"],
|
|
14
|
+
};
|
|
15
|
+
const czechia = {
|
|
16
|
+
code: "cz",
|
|
17
|
+
channelToken: "cz",
|
|
18
|
+
defaultLanguage: "cs",
|
|
19
|
+
languages: ["cs"],
|
|
20
|
+
currency: "CZK",
|
|
21
|
+
countries: ["CZ"],
|
|
22
|
+
domains: ["shop.example.cz"],
|
|
23
|
+
};
|
|
24
|
+
const multiMarket = parseJarShopConfig({
|
|
25
|
+
defaultMarket: "sk",
|
|
26
|
+
markets: [slovakia, czechia],
|
|
27
|
+
});
|
|
28
|
+
const singleMarket = parseJarShopConfig({
|
|
29
|
+
defaultMarket: "sk",
|
|
30
|
+
markets: [{ ...slovakia, domains: [] }],
|
|
31
|
+
});
|
|
32
|
+
const production = { NODE_ENV: "production", JARSHOP_DEV_MARKET: "cz" };
|
|
33
|
+
const development = { NODE_ENV: "development" };
|
|
34
|
+
|
|
35
|
+
describe("storefront market routing", () => {
|
|
36
|
+
it("answers an unknown production host of a multi-market project with 404", () => {
|
|
37
|
+
expect(
|
|
38
|
+
routeMarketRequest(
|
|
39
|
+
{ host: "unknown.example", pathname: "/sk", search: "" },
|
|
40
|
+
production,
|
|
41
|
+
multiMarket,
|
|
42
|
+
),
|
|
43
|
+
).toEqual({ kind: "not-found" });
|
|
44
|
+
// JARSHOP_DEV_MARKET is a development aid and never chooses a market in production.
|
|
45
|
+
expect(
|
|
46
|
+
marketForRequestHost("localhost:3000", production, multiMarket),
|
|
47
|
+
).toBeUndefined();
|
|
48
|
+
expect(
|
|
49
|
+
marketForRequestHost("shop.example.cz", production, multiMarket)?.code,
|
|
50
|
+
).toBe("cz");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("keeps the development and single-market fallbacks", () => {
|
|
54
|
+
expect(
|
|
55
|
+
marketForRequestHost("localhost:3001", development, multiMarket)?.code,
|
|
56
|
+
).toBe("sk");
|
|
57
|
+
expect(
|
|
58
|
+
marketForRequestHost(
|
|
59
|
+
"localhost:3001",
|
|
60
|
+
{ ...development, JARSHOP_DEV_MARKET: "cz" },
|
|
61
|
+
multiMarket,
|
|
62
|
+
)?.code,
|
|
63
|
+
).toBe("cz");
|
|
64
|
+
expect(
|
|
65
|
+
marketForRequestHost("10.0.0.5:3000", production, singleMarket)?.code,
|
|
66
|
+
).toBe("sk");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("serves only the market's own path languages", () => {
|
|
70
|
+
const czech = { host: "shop.example.cz", search: "?page=2" };
|
|
71
|
+
expect(
|
|
72
|
+
routeMarketRequest(
|
|
73
|
+
{ ...czech, pathname: "/cs/checkout" },
|
|
74
|
+
production,
|
|
75
|
+
multiMarket,
|
|
76
|
+
),
|
|
77
|
+
).toMatchObject({ kind: "serve", market: { code: "cz" } });
|
|
78
|
+
// A real language the market does not serve redirects, keeping the path.
|
|
79
|
+
expect(
|
|
80
|
+
routeMarketRequest(
|
|
81
|
+
{ ...czech, pathname: "/en/checkout" },
|
|
82
|
+
production,
|
|
83
|
+
multiMarket,
|
|
84
|
+
),
|
|
85
|
+
).toEqual({ kind: "redirect", location: "/cs/checkout?page=2" });
|
|
86
|
+
expect(
|
|
87
|
+
routeMarketRequest(
|
|
88
|
+
{ host: "shop.example.sk", pathname: "/en-GB/cart", search: "" },
|
|
89
|
+
production,
|
|
90
|
+
multiMarket,
|
|
91
|
+
),
|
|
92
|
+
).toEqual({ kind: "redirect", location: "/en/cart" });
|
|
93
|
+
expect(
|
|
94
|
+
routeMarketRequest(
|
|
95
|
+
{ ...czech, pathname: "/", search: "" },
|
|
96
|
+
production,
|
|
97
|
+
multiMarket,
|
|
98
|
+
),
|
|
99
|
+
).toEqual({ kind: "redirect", location: "/cs" });
|
|
100
|
+
// A segment that is no language at all is not a page.
|
|
101
|
+
for (const pathname of ["/xx/checkout", "/catalog", "/en-GB-x/cart"]) {
|
|
102
|
+
expect(
|
|
103
|
+
routeMarketRequest({ ...czech, pathname }, production, multiMarket),
|
|
104
|
+
).toEqual({ kind: "not-found" });
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
jarShopConfig,
|
|
3
|
+
resolveMarketForHost,
|
|
4
|
+
routeMarketPath,
|
|
5
|
+
type JarShopConfig,
|
|
6
|
+
type JarShopMarket,
|
|
7
|
+
} from "./market-config";
|
|
8
|
+
|
|
9
|
+
export interface MarketRequest {
|
|
10
|
+
/** `X-Forwarded-Host` when a proxy sets it, otherwise `Host`. */
|
|
11
|
+
readonly host: string | null | undefined;
|
|
12
|
+
readonly pathname: string;
|
|
13
|
+
/** The query string including its leading `?`, or an empty string. */
|
|
14
|
+
readonly search: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface MarketEnvironment {
|
|
18
|
+
readonly NODE_ENV?: string;
|
|
19
|
+
readonly JARSHOP_DEV_MARKET?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type MarketRequestRoute =
|
|
23
|
+
| { readonly kind: "serve"; readonly market: JarShopMarket }
|
|
24
|
+
| { readonly kind: "redirect"; readonly location: string }
|
|
25
|
+
| { readonly kind: "not-found" };
|
|
26
|
+
|
|
27
|
+
/** The request market, or `undefined` for an unknown production host. */
|
|
28
|
+
export function marketForRequestHost(
|
|
29
|
+
host: string | null | undefined,
|
|
30
|
+
environment: MarketEnvironment = process.env,
|
|
31
|
+
config: JarShopConfig = jarShopConfig,
|
|
32
|
+
): JarShopMarket | undefined {
|
|
33
|
+
const production = environment.NODE_ENV === "production";
|
|
34
|
+
return resolveMarketForHost(config, host, {
|
|
35
|
+
production,
|
|
36
|
+
...(production ? {} : { fallbackMarket: environment.JARSHOP_DEV_MARKET }),
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The storefront proxy's decision for one request. An unknown production host
|
|
42
|
+
* of a multi-market project and a path segment that is no language are not
|
|
43
|
+
* found; a language the market does not serve redirects to the market's
|
|
44
|
+
* language with the rest of the path and the query kept.
|
|
45
|
+
*/
|
|
46
|
+
export function routeMarketRequest(
|
|
47
|
+
request: MarketRequest,
|
|
48
|
+
environment: MarketEnvironment = process.env,
|
|
49
|
+
config: JarShopConfig = jarShopConfig,
|
|
50
|
+
): MarketRequestRoute {
|
|
51
|
+
const market = marketForRequestHost(request.host, environment, config);
|
|
52
|
+
if (!market) return { kind: "not-found" };
|
|
53
|
+
const route = routeMarketPath(market, request.pathname);
|
|
54
|
+
if (route.kind === "serve") return { kind: "serve", market };
|
|
55
|
+
if (route.kind === "not-found") return route;
|
|
56
|
+
return { kind: "redirect", location: `${route.pathname}${request.search}` };
|
|
57
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Server-only: `next/headers` cannot be imported by a client component.
|
|
2
|
+
import { headers } from "next/headers";
|
|
3
|
+
import { notFound } from "next/navigation";
|
|
4
|
+
|
|
5
|
+
import { resolveMarketLanguage, type JarShopMarket } from "./market-config";
|
|
6
|
+
import { marketForRequestHost } from "./market-routing";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The market of the current request, chosen by its host name. In development
|
|
10
|
+
* a host that no market lists falls back to `JARSHOP_DEV_MARKET` and then to
|
|
11
|
+
* the default market, and a single-market project serves its market on any
|
|
12
|
+
* host. In production a multi-market project answers an unknown host with 404.
|
|
13
|
+
*/
|
|
14
|
+
export async function getRequestMarket(): Promise<JarShopMarket> {
|
|
15
|
+
const requestHeaders = await headers();
|
|
16
|
+
const market = marketForRequestHost(
|
|
17
|
+
requestHeaders.get("x-forwarded-host") ?? requestHeaders.get("host"),
|
|
18
|
+
);
|
|
19
|
+
if (!market) notFound();
|
|
20
|
+
return market;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface RequestLocale {
|
|
24
|
+
readonly market: JarShopMarket;
|
|
25
|
+
/** A language the request market serves; also the commerce `languageCode`. */
|
|
26
|
+
readonly language: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Restricts the `[locale]` path segment to the request market. The proxy
|
|
31
|
+
* already redirects a language the market does not serve; this keeps a page
|
|
32
|
+
* reached any other way in the market's default language.
|
|
33
|
+
*/
|
|
34
|
+
export async function resolveRequestLocale(
|
|
35
|
+
locale: string | undefined,
|
|
36
|
+
): Promise<RequestLocale> {
|
|
37
|
+
const market = await getRequestMarket();
|
|
38
|
+
return { market, language: resolveMarketLanguage(market, locale) };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* `fetch` for the Commerce API that addresses the request market's Vendure
|
|
43
|
+
* Channel. Every storefront commerce call goes through it, so carts, prices
|
|
44
|
+
* and orders always stay inside the market the shopper is visiting.
|
|
45
|
+
*/
|
|
46
|
+
export const marketFetch: typeof fetch = async (input, init) => {
|
|
47
|
+
const market = await getRequestMarket();
|
|
48
|
+
const requestHeaders = new Headers(init?.headers);
|
|
49
|
+
requestHeaders.set("vendure-token", market.channelToken);
|
|
50
|
+
return fetch(input, { ...init, headers: requestHeaders });
|
|
51
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
checkoutSelectionReducer,
|
|
5
|
+
initialCheckoutSelectionState,
|
|
6
|
+
isCheckoutSelectionBusy,
|
|
7
|
+
visiblePickupPoint,
|
|
8
|
+
} from "./packeta-selection";
|
|
9
|
+
|
|
10
|
+
const accepted = {
|
|
11
|
+
shippingMethodCode: "packeta-pickup",
|
|
12
|
+
pickupPoint: {
|
|
13
|
+
id: "12345",
|
|
14
|
+
label: "Packeta Bratislava",
|
|
15
|
+
addressLines: ["Hlavná 1", "811 01 Bratislava"],
|
|
16
|
+
countryCode: "SK",
|
|
17
|
+
carrier: "packeta-zbox",
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
function selectedState() {
|
|
22
|
+
return checkoutSelectionReducer(
|
|
23
|
+
checkoutSelectionReducer(initialCheckoutSelectionState, {
|
|
24
|
+
type: "method-changed",
|
|
25
|
+
methodCode: "packeta-pickup",
|
|
26
|
+
}),
|
|
27
|
+
{ type: "accepted", revision: 1, selection: accepted },
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe("Packeta checkout selection state", () => {
|
|
32
|
+
it("locks checkout transitions while the widget or server validation is active", () => {
|
|
33
|
+
expect(
|
|
34
|
+
isCheckoutSelectionBusy({
|
|
35
|
+
...initialCheckoutSelectionState,
|
|
36
|
+
status: "opening",
|
|
37
|
+
}),
|
|
38
|
+
).toBe(true);
|
|
39
|
+
expect(
|
|
40
|
+
isCheckoutSelectionBusy({
|
|
41
|
+
...initialCheckoutSelectionState,
|
|
42
|
+
status: "saving",
|
|
43
|
+
}),
|
|
44
|
+
).toBe(true);
|
|
45
|
+
expect(isCheckoutSelectionBusy(initialCheckoutSelectionState)).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("keeps a server-accepted point when the widget is cancelled", () => {
|
|
49
|
+
const selected = selectedState();
|
|
50
|
+
const cancelled = checkoutSelectionReducer(selected, { type: "cancelled" });
|
|
51
|
+
|
|
52
|
+
expect(visiblePickupPoint(cancelled)).toEqual(accepted.pickupPoint);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("invalidates a point when the destination country changes", () => {
|
|
56
|
+
const selected = selectedState();
|
|
57
|
+
const changed = checkoutSelectionReducer(selected, {
|
|
58
|
+
type: "country-changed",
|
|
59
|
+
countryCode: "CZ",
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
expect(visiblePickupPoint(changed)).toBeNull();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("ignores the point on another method and reuses it on the same method", () => {
|
|
66
|
+
const selected = selectedState();
|
|
67
|
+
const courier = checkoutSelectionReducer(selected, {
|
|
68
|
+
type: "method-changed",
|
|
69
|
+
methodCode: "courier",
|
|
70
|
+
});
|
|
71
|
+
const returned = checkoutSelectionReducer(courier, {
|
|
72
|
+
type: "method-changed",
|
|
73
|
+
methodCode: "packeta-pickup",
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
expect(visiblePickupPoint(courier)).toBeNull();
|
|
77
|
+
expect(visiblePickupPoint(returned)).toEqual(accepted.pickupPoint);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("makes provider and validation failures retryable without inventing a point", () => {
|
|
81
|
+
const failed = checkoutSelectionReducer(initialCheckoutSelectionState, {
|
|
82
|
+
type: "failed",
|
|
83
|
+
message: "Výdajné miesto sa nepodarilo overiť. Skús to znova.",
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
expect(failed.status).toBe("error");
|
|
87
|
+
expect(failed.error).toContain("Skús to znova");
|
|
88
|
+
expect(visiblePickupPoint(failed)).toBeNull();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("ignores an in-flight result after its country was invalidated", () => {
|
|
92
|
+
const opening = checkoutSelectionReducer(
|
|
93
|
+
checkoutSelectionReducer(initialCheckoutSelectionState, {
|
|
94
|
+
type: "method-changed",
|
|
95
|
+
methodCode: "packeta-pickup",
|
|
96
|
+
}),
|
|
97
|
+
{ type: "opening" },
|
|
98
|
+
);
|
|
99
|
+
const changed = checkoutSelectionReducer(opening, {
|
|
100
|
+
type: "country-changed",
|
|
101
|
+
countryCode: "CZ",
|
|
102
|
+
});
|
|
103
|
+
const staleResponse = checkoutSelectionReducer(changed, {
|
|
104
|
+
type: "accepted",
|
|
105
|
+
revision: opening.revision,
|
|
106
|
+
selection: accepted,
|
|
107
|
+
});
|
|
108
|
+
const returned = checkoutSelectionReducer(staleResponse, {
|
|
109
|
+
type: "country-changed",
|
|
110
|
+
countryCode: "SK",
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
expect(visiblePickupPoint(staleResponse)).toBeNull();
|
|
114
|
+
expect(visiblePickupPoint(returned)).toBeNull();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("ignores an in-flight result after a country round trip", () => {
|
|
118
|
+
const opening = checkoutSelectionReducer(
|
|
119
|
+
checkoutSelectionReducer(initialCheckoutSelectionState, {
|
|
120
|
+
type: "method-changed",
|
|
121
|
+
methodCode: "packeta-pickup",
|
|
122
|
+
}),
|
|
123
|
+
{ type: "opening" },
|
|
124
|
+
);
|
|
125
|
+
const changed = checkoutSelectionReducer(opening, {
|
|
126
|
+
type: "country-changed",
|
|
127
|
+
countryCode: "CZ",
|
|
128
|
+
});
|
|
129
|
+
const returned = checkoutSelectionReducer(changed, {
|
|
130
|
+
type: "country-changed",
|
|
131
|
+
countryCode: "SK",
|
|
132
|
+
});
|
|
133
|
+
const staleResponse = checkoutSelectionReducer(returned, {
|
|
134
|
+
type: "accepted",
|
|
135
|
+
revision: opening.revision,
|
|
136
|
+
selection: accepted,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
expect(visiblePickupPoint(staleResponse)).toBeNull();
|
|
140
|
+
});
|
|
141
|
+
});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
PickupPoint,
|
|
3
|
+
ShippingSelection,
|
|
4
|
+
} from "@jarwizz/commerce-sdk/types";
|
|
5
|
+
|
|
6
|
+
export interface CheckoutSelectionState {
|
|
7
|
+
readonly countryCode: string;
|
|
8
|
+
readonly methodCode: string;
|
|
9
|
+
readonly revision: number;
|
|
10
|
+
readonly accepted: ShippingSelection | null;
|
|
11
|
+
readonly status: "idle" | "opening" | "saving" | "error";
|
|
12
|
+
readonly error: string | null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type CheckoutSelectionEvent =
|
|
16
|
+
| { readonly type: "country-changed"; readonly countryCode: string }
|
|
17
|
+
| { readonly type: "method-changed"; readonly methodCode: string }
|
|
18
|
+
| { readonly type: "opening" }
|
|
19
|
+
| { readonly type: "saving" }
|
|
20
|
+
| {
|
|
21
|
+
readonly type: "accepted";
|
|
22
|
+
readonly revision: number;
|
|
23
|
+
readonly selection: ShippingSelection;
|
|
24
|
+
}
|
|
25
|
+
| { readonly type: "cancelled" }
|
|
26
|
+
| { readonly type: "failed"; readonly message: string };
|
|
27
|
+
|
|
28
|
+
export const initialCheckoutSelectionState: CheckoutSelectionState = {
|
|
29
|
+
countryCode: "SK",
|
|
30
|
+
methodCode: "pickup",
|
|
31
|
+
revision: 0,
|
|
32
|
+
accepted: null,
|
|
33
|
+
status: "idle",
|
|
34
|
+
error: null,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export function isCheckoutSelectionBusy(
|
|
38
|
+
state: CheckoutSelectionState,
|
|
39
|
+
): boolean {
|
|
40
|
+
return state.status === "opening" || state.status === "saving";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function checkoutSelectionReducer(
|
|
44
|
+
state: CheckoutSelectionState,
|
|
45
|
+
event: CheckoutSelectionEvent,
|
|
46
|
+
): CheckoutSelectionState {
|
|
47
|
+
switch (event.type) {
|
|
48
|
+
case "country-changed":
|
|
49
|
+
return {
|
|
50
|
+
...state,
|
|
51
|
+
countryCode: event.countryCode,
|
|
52
|
+
revision: state.revision + 1,
|
|
53
|
+
accepted: null,
|
|
54
|
+
status: "idle",
|
|
55
|
+
error: null,
|
|
56
|
+
};
|
|
57
|
+
case "method-changed":
|
|
58
|
+
return {
|
|
59
|
+
...state,
|
|
60
|
+
methodCode: event.methodCode,
|
|
61
|
+
revision: state.revision + 1,
|
|
62
|
+
status: "idle",
|
|
63
|
+
error: null,
|
|
64
|
+
};
|
|
65
|
+
case "opening":
|
|
66
|
+
return { ...state, status: "opening", error: null };
|
|
67
|
+
case "saving":
|
|
68
|
+
return { ...state, status: "saving", error: null };
|
|
69
|
+
case "accepted":
|
|
70
|
+
if (
|
|
71
|
+
event.revision !== state.revision ||
|
|
72
|
+
event.selection.shippingMethodCode !== state.methodCode ||
|
|
73
|
+
event.selection.pickupPoint.countryCode !== state.countryCode
|
|
74
|
+
) {
|
|
75
|
+
return { ...state, status: "idle", error: null };
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
...state,
|
|
79
|
+
accepted: event.selection,
|
|
80
|
+
status: "idle",
|
|
81
|
+
error: null,
|
|
82
|
+
};
|
|
83
|
+
case "cancelled":
|
|
84
|
+
return { ...state, status: "idle", error: null };
|
|
85
|
+
case "failed":
|
|
86
|
+
return { ...state, status: "error", error: event.message };
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function visiblePickupPoint(
|
|
91
|
+
state: CheckoutSelectionState,
|
|
92
|
+
): PickupPoint | null {
|
|
93
|
+
const accepted = state.accepted;
|
|
94
|
+
return accepted?.shippingMethodCode === state.methodCode &&
|
|
95
|
+
accepted.pickupPoint.countryCode === state.countryCode
|
|
96
|
+
? accepted.pickupPoint
|
|
97
|
+
: null;
|
|
98
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
const commerce = vi.hoisted(() => ({
|
|
4
|
+
reconcileGuestPayment: vi.fn(),
|
|
5
|
+
getGuestOrderConfirmation: vi.fn(),
|
|
6
|
+
}));
|
|
7
|
+
|
|
8
|
+
vi.mock("./commerce", () => commerce);
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
readPaymentReturn,
|
|
12
|
+
refreshPaymentReturn,
|
|
13
|
+
} from "../app/[locale]/checkout/payment-return/actions";
|
|
14
|
+
|
|
15
|
+
describe("payment return actions", () => {
|
|
16
|
+
it("triggers reconciliation once, then reads the server-confirmed result", async () => {
|
|
17
|
+
commerce.reconcileGuestPayment.mockResolvedValue({
|
|
18
|
+
ok: true,
|
|
19
|
+
data: { status: "paid" },
|
|
20
|
+
});
|
|
21
|
+
commerce.getGuestOrderConfirmation.mockResolvedValue({
|
|
22
|
+
ok: true,
|
|
23
|
+
data: {
|
|
24
|
+
orderId: "42",
|
|
25
|
+
orderCode: "ORDER-42",
|
|
26
|
+
state: "PaymentSettled",
|
|
27
|
+
total: { amount: 2_590, currencyCode: "EUR" },
|
|
28
|
+
shippingAddress: null,
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
await expect(refreshPaymentReturn("sk", "ORDER-42")).resolves.toMatchObject(
|
|
33
|
+
{
|
|
34
|
+
kind: "paid",
|
|
35
|
+
},
|
|
36
|
+
);
|
|
37
|
+
await expect(readPaymentReturn("sk", "ORDER-42")).resolves.toMatchObject({
|
|
38
|
+
kind: "paid",
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
expect(commerce.reconcileGuestPayment).toHaveBeenCalledTimes(1);
|
|
42
|
+
expect(commerce.getGuestOrderConfirmation).toHaveBeenCalledTimes(2);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("treats an authorized redirect payment as a resolved outcome", async () => {
|
|
46
|
+
commerce.getGuestOrderConfirmation.mockResolvedValue({
|
|
47
|
+
ok: true,
|
|
48
|
+
data: {
|
|
49
|
+
orderId: "42",
|
|
50
|
+
orderCode: "ORDER-42",
|
|
51
|
+
state: "PaymentAuthorized",
|
|
52
|
+
total: { amount: 2_590, currencyCode: "EUR" },
|
|
53
|
+
shippingAddress: null,
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
await expect(readPaymentReturn("sk", "ORDER-42")).resolves.toMatchObject({
|
|
58
|
+
kind: "paid",
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createElement } from "react";
|
|
2
|
+
import { renderToStaticMarkup } from "react-dom/server";
|
|
3
|
+
import { describe, expect, it, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
vi.mock("next/headers", () => ({
|
|
6
|
+
headers: async () => new Headers({ host: "localhost:3001" }),
|
|
7
|
+
}));
|
|
8
|
+
vi.mock("../app/[locale]/checkout/payment-return/payment-return", () => ({
|
|
9
|
+
PaymentReturn: ({ orderCode }: { orderCode: string }) =>
|
|
10
|
+
createElement("p", undefined, `Checking ${orderCode}`),
|
|
11
|
+
}));
|
|
12
|
+
|
|
13
|
+
import PaymentReturnPage from "../app/[locale]/checkout/payment-return/page";
|
|
14
|
+
|
|
15
|
+
describe("payment return page", () => {
|
|
16
|
+
it("does not let a query string claim that an order was paid", async () => {
|
|
17
|
+
const withoutClaim = await PaymentReturnPage({
|
|
18
|
+
params: Promise.resolve({ locale: "sk" }),
|
|
19
|
+
searchParams: Promise.resolve({ order: "ORDER-42" }),
|
|
20
|
+
});
|
|
21
|
+
const forgedClaim = await PaymentReturnPage({
|
|
22
|
+
params: Promise.resolve({ locale: "sk" }),
|
|
23
|
+
searchParams: Promise.resolve({ order: "ORDER-42", paid: "true" }),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
expect(renderToStaticMarkup(forgedClaim)).toBe(
|
|
27
|
+
renderToStaticMarkup(withoutClaim),
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
nextPaymentReturnPollDelay,
|
|
5
|
+
paymentReturnPollingConfiguration,
|
|
6
|
+
} from "./payment-return-polling";
|
|
7
|
+
|
|
8
|
+
describe("payment return polling", () => {
|
|
9
|
+
it("uses the configured interval and never schedules beyond the configured window", () => {
|
|
10
|
+
const configuration = paymentReturnPollingConfiguration({
|
|
11
|
+
intervalMs: 1_000,
|
|
12
|
+
windowMs: 3_000,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
expect(
|
|
16
|
+
nextPaymentReturnPollDelay({
|
|
17
|
+
configuration,
|
|
18
|
+
startedAt: 10_000,
|
|
19
|
+
now: 10_000,
|
|
20
|
+
}),
|
|
21
|
+
).toBe(1_000);
|
|
22
|
+
expect(
|
|
23
|
+
nextPaymentReturnPollDelay({
|
|
24
|
+
configuration,
|
|
25
|
+
startedAt: 10_000,
|
|
26
|
+
now: 12_500,
|
|
27
|
+
}),
|
|
28
|
+
).toBe(500);
|
|
29
|
+
expect(
|
|
30
|
+
nextPaymentReturnPollDelay({
|
|
31
|
+
configuration,
|
|
32
|
+
startedAt: 10_000,
|
|
33
|
+
now: 13_000,
|
|
34
|
+
}),
|
|
35
|
+
).toBeNull();
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export interface PaymentReturnPollingConfiguration {
|
|
2
|
+
readonly intervalMs: number;
|
|
3
|
+
readonly windowMs: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const defaultPaymentReturnPollingConfiguration: PaymentReturnPollingConfiguration =
|
|
7
|
+
{
|
|
8
|
+
intervalMs: 1_000,
|
|
9
|
+
windowMs: 15_000,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
function positiveMilliseconds(
|
|
13
|
+
value: number | undefined,
|
|
14
|
+
fallback: number,
|
|
15
|
+
): number {
|
|
16
|
+
return value !== undefined && Number.isFinite(value) && value > 0
|
|
17
|
+
? value
|
|
18
|
+
: fallback;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Validates the public storefront configuration once, before a timer uses it. */
|
|
22
|
+
export function paymentReturnPollingConfiguration(
|
|
23
|
+
configured: Partial<PaymentReturnPollingConfiguration> = {},
|
|
24
|
+
): PaymentReturnPollingConfiguration {
|
|
25
|
+
const intervalMs = positiveMilliseconds(
|
|
26
|
+
configured.intervalMs,
|
|
27
|
+
defaultPaymentReturnPollingConfiguration.intervalMs,
|
|
28
|
+
);
|
|
29
|
+
const windowMs = positiveMilliseconds(
|
|
30
|
+
configured.windowMs,
|
|
31
|
+
defaultPaymentReturnPollingConfiguration.windowMs,
|
|
32
|
+
);
|
|
33
|
+
return { intervalMs, windowMs: Math.max(intervalMs, windowMs) };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function storefrontPaymentReturnPollingConfiguration(): PaymentReturnPollingConfiguration {
|
|
37
|
+
return paymentReturnPollingConfiguration({
|
|
38
|
+
intervalMs: Number(
|
|
39
|
+
process.env.NEXT_PUBLIC_JARSHOP_PAYMENT_RETURN_POLL_INTERVAL_MS,
|
|
40
|
+
),
|
|
41
|
+
windowMs: Number(
|
|
42
|
+
process.env.NEXT_PUBLIC_JARSHOP_PAYMENT_RETURN_POLL_WINDOW_MS,
|
|
43
|
+
),
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The next delay is clipped to the window, and null means polling is finished. */
|
|
48
|
+
export function nextPaymentReturnPollDelay(input: {
|
|
49
|
+
readonly configuration: PaymentReturnPollingConfiguration;
|
|
50
|
+
readonly startedAt: number;
|
|
51
|
+
readonly now: number;
|
|
52
|
+
}): number | null {
|
|
53
|
+
const remaining =
|
|
54
|
+
input.configuration.windowMs - (input.now - input.startedAt);
|
|
55
|
+
return remaining <= 0
|
|
56
|
+
? null
|
|
57
|
+
: Math.min(input.configuration.intervalMs, remaining);
|
|
58
|
+
}
|
|
@@ -28,21 +28,33 @@
|
|
|
28
28
|
"checkout.phone": "Phone",
|
|
29
29
|
"checkout.terms": "I accept the terms and conditions",
|
|
30
30
|
"checkout.shippingMethod": "Shipping method",
|
|
31
|
+
"checkout.paymentMethod": "Payment method",
|
|
31
32
|
"checkout.pickup": "Personal pickup — free",
|
|
32
33
|
"checkout.courier": "Courier — 4.90 EUR",
|
|
33
34
|
"checkout.street": "Street and number",
|
|
34
35
|
"checkout.city": "City",
|
|
35
36
|
"checkout.postalCode": "Postal code",
|
|
36
37
|
"checkout.country": "Country",
|
|
37
|
-
"checkout.slovakia": "Slovakia",
|
|
38
38
|
"checkout.pickupSummary": "Personal pickup: 0 EUR. Payment: cash on delivery.",
|
|
39
39
|
"checkout.courierSummary": "Courier: 4.90 EUR. Payment: cash on delivery.",
|
|
40
|
+
"checkout.packetaSummary": "Packeta: selected pickup point. Payment: cash on delivery.",
|
|
40
41
|
"checkout.submitPickup": "Place pickup COD order",
|
|
41
42
|
"checkout.submitCourier": "Place courier COD order",
|
|
43
|
+
"checkout.submitPacketa": "Place Packeta COD order",
|
|
44
|
+
"checkout.acceptedPickupPoint": "Verified pickup point",
|
|
45
|
+
"checkout.packetaConfigurationFailure": "Packeta is temporarily unavailable.",
|
|
46
|
+
"checkout.packetaRetry": "Try again",
|
|
42
47
|
"checkout.priceChanged": "The cart price changed. Review the refreshed totals and try again.",
|
|
43
48
|
"checkout.insufficientStock": "Some items are no longer available in that quantity. Adjust the cart and try again.",
|
|
44
49
|
"success.title": "Order placed",
|
|
45
50
|
"success.orderCode": "Order code",
|
|
51
|
+
"success.shippingAddress": "Shipping address",
|
|
52
|
+
"paymentReturn.checking": "Checking your payment…",
|
|
53
|
+
"paymentReturn.paid": "Your payment is confirmed.",
|
|
54
|
+
"paymentReturn.failed": "Your payment was not completed.",
|
|
55
|
+
"paymentReturn.retry": "Return to checkout",
|
|
56
|
+
"paymentReturn.pending": "Your payment is still being confirmed.",
|
|
57
|
+
"paymentReturn.unavailable": "Order confirmation is not available.",
|
|
46
58
|
"account.title": "My account",
|
|
47
59
|
"account.name": "Name",
|
|
48
60
|
"account.email": "Email",
|