@economist/web-apple-pay 1.7.5 → 1.7.7

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.
@@ -1,11 +1,13 @@
1
1
  /**
2
2
  * Centralized redirect URL builders for the Express Checkout payment handler.
3
3
  *
4
- * All redirects include a `returnUrl` parameter so downstream pages
5
- * (/checkout/complete, onboarding) can return the user to the original article.
4
+ * Before redirecting, the origin article URL is persisted to localStorage under
5
+ * the `checkout-redirect` key so that sma-checkout / the onboarding app can
6
+ * return the user to the article after checkout and onboarding complete.
6
7
  *
7
8
  * Matches sma-checkout: src/lib/navigate-to-checkout/index.tsx
8
9
  */
10
+ export declare const writeCheckoutRedirect: (originUrl: string) => void;
9
11
  /**
10
12
  * Captures the current page URL (the article the user is reading).
11
13
  * Must be called before any redirect occurs.
@@ -29,31 +31,51 @@ export declare const buildQueryString: () => string;
29
31
  export declare const decodeSku: (sku: string) => string;
30
32
  /**
31
33
  * Builds the success redirect URL.
32
- * /checkout/complete?basketId={id}&lmo_offer={sku}&status={userStatus}&redirectToCheckout=true&returnUrl=...
34
+ * /checkout/complete?basketId={id}&lmo_offer={sku}&status={userStatus}&redirectToCheckout=true
35
+ * &email={email}&country={country}
36
+ *
37
+ * `email` — the user's email address (from session for lex users, from Apple Pay
38
+ * shipping contact for nex users). Always appended when non-empty so checkout
39
+ * can pre-fill the account field.
40
+ *
41
+ * `country` — the offer country code forwarded from the payment handler config.
42
+ * Always appended when non-empty to preserve geo-context across the redirect.
43
+ *
44
+ * The origin article URL is written to localStorage by the caller via
45
+ * writeCheckoutRedirect() before this redirect fires.
33
46
  */
34
47
  export declare const redirectToCheckoutComplete: (params: {
35
48
  basketId: string;
36
49
  status: "nex" | "lex";
37
50
  skuId: string;
38
- returnUrl: string;
39
- queryString?: string;
51
+ email?: string;
52
+ country?: string;
40
53
  }) => string;
41
54
  /**
42
55
  * Builds the fallback redirect URL for error scenarios.
43
- * /checkout?lmo_offer={offerPath}&inlineError=...&returnUrl=...
56
+ * /checkout?lmo_offer={offerPath}&inlineError=...&email={email}&country={country}
44
57
  *
45
58
  * The skuId is decoded via decodeSku() before being set as lmo_offer, producing
46
59
  * a Limio offer path (e.g. /offers2/UK-UK.DIGITAL.1YAR.X.DISC.30.NYT) as
47
60
  * checkout expects. If decoding fails (malformed SKU), the raw skuId is used.
48
61
  *
62
+ * `email` — always appended when non-empty so checkout can pre-fill the email
63
+ * field on the fallback page (e.g. for EMAIL_COLLISION flows).
64
+ *
65
+ * `country` — the offer country code, always appended when non-empty to preserve
66
+ * geo-context across the redirect.
67
+ *
49
68
  * Builds on top of the current page URL (window.location.href) so that any
50
69
  * existing query params (UTM tags, campaign flags) are preserved — matching
51
70
  * sma-checkout: src/lib/navigate-to-checkout/index.tsx
71
+ *
72
+ * The origin article URL is written to localStorage by the caller via
73
+ * writeCheckoutRedirect() before this redirect fires.
52
74
  */
53
75
  export declare const redirectToCheckoutFallback: (params: {
54
76
  skuId: string;
55
- returnUrl: string;
56
77
  inlineError?: string;
57
78
  emailError?: string;
58
79
  email?: string;
80
+ country?: string;
59
81
  }) => string;
@@ -1,4 +1,22 @@
1
1
  // src/utils/expressCheckoutRedirect.ts
2
+ var CHECKOUT_REDIRECT_KEY = "checkout-redirect";
3
+ var ALLOWED_REDIRECT_HOSTNAME = "economist.com";
4
+ function isAllowedRedirectUrl(url) {
5
+ if (url.startsWith("/")) return !url.startsWith("//");
6
+ try {
7
+ const { hostname } = new URL(url);
8
+ return hostname === ALLOWED_REDIRECT_HOSTNAME || hostname.endsWith(`.${ALLOWED_REDIRECT_HOSTNAME}`);
9
+ } catch {
10
+ return false;
11
+ }
12
+ }
13
+ var writeCheckoutRedirect = (originUrl) => {
14
+ if (!isAllowedRedirectUrl(originUrl)) return;
15
+ try {
16
+ localStorage.setItem(CHECKOUT_REDIRECT_KEY, originUrl);
17
+ } catch {
18
+ }
19
+ };
2
20
  var captureOriginUrl = () => window.location.href;
3
21
  var buildQueryString = () => {
4
22
  const countryCode = new URLSearchParams(window.location.search).get(
@@ -16,14 +34,11 @@ var decodeSku = (sku) => {
16
34
  var redirectToCheckoutComplete = (params) => {
17
35
  const url = new URL("/checkout/complete", window.location.origin);
18
36
  url.searchParams.set("basketId", params.basketId);
19
- if (params.queryString) {
20
- const qs = params.queryString.startsWith("?") ? params.queryString.slice(1) : params.queryString;
21
- new URLSearchParams(qs).forEach((v, k) => url.searchParams.set(k, v));
22
- }
23
37
  url.searchParams.set("lmo_offer", params.skuId);
24
38
  url.searchParams.set("status", params.status);
25
39
  url.searchParams.set("redirectToCheckout", "true");
26
- url.searchParams.set("returnUrl", params.returnUrl);
40
+ if (params.email) url.searchParams.set("email", params.email);
41
+ if (params.country) url.searchParams.set("country", params.country);
27
42
  return url.pathname + url.search;
28
43
  };
29
44
  var redirectToCheckoutFallback = (params) => {
@@ -34,7 +49,7 @@ var redirectToCheckoutFallback = (params) => {
34
49
  url.searchParams.set("inlineError", params.inlineError);
35
50
  if (params.email) url.searchParams.set("email", params.email);
36
51
  if (params.emailError) url.searchParams.set("emailError", params.emailError);
37
- url.searchParams.set("returnUrl", params.returnUrl);
52
+ if (params.country) url.searchParams.set("country", params.country);
38
53
  return url.pathname + url.search;
39
54
  };
40
55
  export {
@@ -42,5 +57,6 @@ export {
42
57
  captureOriginUrl,
43
58
  decodeSku,
44
59
  redirectToCheckoutComplete,
45
- redirectToCheckoutFallback
60
+ redirectToCheckoutFallback,
61
+ writeCheckoutRedirect
46
62
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@economist/web-apple-pay",
3
3
  "description": "Web component package for Apple Pay checkout UI",
4
- "version": "1.7.5",
4
+ "version": "1.7.7",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",