@economist/web-apple-pay 1.15.1-beta.1 → 1.15.2

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 CHANGED
@@ -151,6 +151,10 @@ Consumers should listen for this event and continue payment orchestration.
151
151
  - `SUBSCRIBE`: same always-on-after-exclusions gate for `/subscribe` (sma-checkout); no Amplitude. Wallet impression `entry_point` is `subscribe`.
152
152
  - `PAYWALL`: always-on after offer exclusions + Safari/Apple placement eligibility. No Amplitude experiment / `$exposure`.
153
153
  - `REGWALL`: inert — always returns `null`.
154
+ - **Where the user returns after purchase.** Each entry point sets a `ReturnUrlPolicy` in `ENTRY_POINT_CONFIG` (`src/apple-pay-button.ts`), which decides what is written to `localStorage['checkout-redirect']` (read by the onboarding app):
155
+ - `CURRENT_PAGE`: the page the button is on (banners, popup, paywall, regwall), e.g. back to the article.
156
+ - `HOST_MANAGED`: nothing is written, so the host's own value is kept (`SUBSCRIBE`: sma-checkout stores the article the user arrived from; with none stored, onboarding returns them to the home page).
157
+ - A new entry point must declare its policy, or it will not compile.
154
158
  - `ApplePayModal`: modal component class.
155
159
  - `ensureApplePayLogoSymbol()`: logo symbol injection helper.
156
160
  - `Offer`: offer contract type used by component props.
@@ -220,4 +224,3 @@ Story coverage matrix:
220
224
  - `ApplePayButton` behavior and state variants are available as individual canvas stories in Storybook.
221
225
  - `ApplePayModal` docs page shows the stable default example.
222
226
  - `ApplePayModal` behavior and state variants are available as individual canvas stories in Storybook.
223
-
package/dist/index.js CHANGED
@@ -569,15 +569,21 @@ var configureApplePay = (config) => {
569
569
  var getApplePayConfig = () => runtimeConfig;
570
570
 
571
571
  // src/utils/recaptcha.ts
572
+ var findScriptForKey = (siteKey) => Array.from(
573
+ document.querySelectorAll(
574
+ 'script[src*="recaptcha/api.js"]'
575
+ )
576
+ ).find((script) => script.src.includes(`render=${siteKey}`));
577
+ var pendingLoads = /* @__PURE__ */ new Map();
572
578
  var loadRecaptchaScript = (siteKey) => {
573
- return new Promise((resolve, reject) => {
574
- if (typeof grecaptcha !== "undefined") return resolve();
575
- const existingScript = document.querySelector(
576
- 'script[src*="recaptcha/api.js"]'
577
- );
578
- if (existingScript) {
579
- existingScript.addEventListener("load", () => resolve());
580
- existingScript.addEventListener("error", () => {
579
+ const pending = pendingLoads.get(siteKey);
580
+ if (pending) return pending;
581
+ const ownScript = findScriptForKey(siteKey);
582
+ if (ownScript && typeof grecaptcha !== "undefined") return Promise.resolve();
583
+ const load = new Promise((resolve, reject) => {
584
+ if (ownScript) {
585
+ ownScript.addEventListener("load", () => resolve());
586
+ ownScript.addEventListener("error", () => {
581
587
  reject(new Error("Failed to load existing reCAPTCHA script"));
582
588
  });
583
589
  return;
@@ -592,6 +598,9 @@ var loadRecaptchaScript = (siteKey) => {
592
598
  };
593
599
  document.head.appendChild(script);
594
600
  });
601
+ pendingLoads.set(siteKey, load);
602
+ load.catch(() => pendingLoads.delete(siteKey));
603
+ return load;
595
604
  };
596
605
  var getRecaptchaToken = async (action) => {
597
606
  const siteKey = getApplePayConfig()?.recaptchaSiteKey;
@@ -1303,8 +1312,13 @@ function isAllowedRedirectUrl(url) {
1303
1312
  return false;
1304
1313
  }
1305
1314
  }
1315
+ var CHECKOUT_REDIRECT_BY_POLICY = {
1316
+ ["current_page" /* CURRENT_PAGE */]: (currentPageUrl) => currentPageUrl,
1317
+ ["host_managed" /* HOST_MANAGED */]: () => null
1318
+ };
1319
+ var resolveCheckoutRedirectUrl = (policy, currentPageUrl) => CHECKOUT_REDIRECT_BY_POLICY[policy](currentPageUrl);
1306
1320
  var writeCheckoutRedirect = (originUrl) => {
1307
- if (!isAllowedRedirectUrl(originUrl)) return;
1321
+ if (originUrl === null || !isAllowedRedirectUrl(originUrl)) return;
1308
1322
  try {
1309
1323
  localStorage.setItem(CHECKOUT_REDIRECT_KEY, originUrl);
1310
1324
  } catch {
@@ -2010,26 +2024,34 @@ var isEligibleForApplePayExpressPaywall = () => isAppleDeviceOrSafari();
2010
2024
  // src/apple-pay-button.ts
2011
2025
  var ENTRY_POINT_CONFIG = {
2012
2026
  ["dual_offer_banner" /* DUAL_OFFER_BANNER */]: {
2013
- isEligible: () => true
2027
+ isEligible: () => true,
2028
+ returnUrl: "current_page" /* CURRENT_PAGE */
2014
2029
  },
2015
2030
  ["free_trial_banner" /* FREE_TRIAL_BANNER */]: {
2016
- isEligible: () => true
2031
+ isEligible: () => true,
2032
+ returnUrl: "current_page" /* CURRENT_PAGE */
2017
2033
  },
2018
2034
  ["article_paywall" /* PAYWALL */]: {
2019
- isEligible: isEligibleForApplePayExpressPaywall
2035
+ isEligible: isEligibleForApplePayExpressPaywall,
2036
+ returnUrl: "current_page" /* CURRENT_PAGE */
2020
2037
  },
2021
2038
  ["article_regwall" /* REGWALL */]: {
2022
2039
  isEligible: () => true,
2023
- enabled: false
2040
+ enabled: false,
2041
+ returnUrl: "current_page" /* CURRENT_PAGE */
2024
2042
  },
2025
2043
  ["free_trial_popup" /* FREE_TRIAL_POPUP */]: {
2026
- isEligible: () => true
2044
+ isEligible: () => true,
2045
+ returnUrl: "current_page" /* CURRENT_PAGE */
2027
2046
  },
2028
2047
  ["subscribe" /* SUBSCRIBE */]: {
2029
- isEligible: () => true
2048
+ isEligible: () => true,
2030
2049
  // Always-on after offer exclusions — host (sma-checkout) owns rollout toggle
2050
+ // The offers page is not a place to return to: the host keeps the article the user arrived from (or home).
2051
+ returnUrl: "host_managed" /* HOST_MANAGED */
2031
2052
  }
2032
2053
  };
2054
+ var DEFAULT_ENTRY_POINT = "dual_offer_banner" /* DUAL_OFFER_BANNER */;
2033
2055
  var TOTAL_LABEL = "The Economist";
2034
2056
  var DEFAULT_CURRENCY = "USD";
2035
2057
  var DEFAULT_TOTAL = "0.00";
@@ -2072,7 +2094,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2072
2094
  if (APPLE_PAY_EXCLUDED_VARIANTS.includes(offer.product?.variant)) {
2073
2095
  return null;
2074
2096
  }
2075
- const resolvedEntryPoint = entryPoint || "dual_offer_banner" /* DUAL_OFFER_BANNER */;
2097
+ const resolvedEntryPoint = entryPoint || DEFAULT_ENTRY_POINT;
2076
2098
  const config = ENTRY_POINT_CONFIG[resolvedEntryPoint];
2077
2099
  if (!config) {
2078
2100
  return null;
@@ -2110,7 +2132,10 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2110
2132
  #readyPromise = null;
2111
2133
  #sessionData = null;
2112
2134
  #paymentOptions = null;
2135
+ /** URL of the page the button is on (sent to payment options). */
2113
2136
  #returnUrl = "";
2137
+ /** `checkout-redirect` to persist after purchase, per the entry point's `ReturnUrlPolicy`; `null` = leave it. */
2138
+ #checkoutRedirectUrl = null;
2114
2139
  #supportedBillingCountries = [];
2115
2140
  #entryPoint = "";
2116
2141
  // ─── Public API ─────────────────────────────────────────────────────────────
@@ -2176,6 +2201,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2176
2201
  this.#sessionData = null;
2177
2202
  this.#paymentOptions = null;
2178
2203
  this.#returnUrl = "";
2204
+ this.#checkoutRedirectUrl = null;
2179
2205
  this.#supportedBillingCountries = [];
2180
2206
  this.querySelector(SEL_CONTAINER)?.remove();
2181
2207
  }
@@ -2244,6 +2270,11 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2244
2270
  /** Captures the current page URL once at mount for reuse across the session lifetime. */
2245
2271
  #captureReturnUrl() {
2246
2272
  this.#returnUrl = captureOriginUrl();
2273
+ const config = ENTRY_POINT_CONFIG[this.#entryPoint || DEFAULT_ENTRY_POINT] ?? ENTRY_POINT_CONFIG[DEFAULT_ENTRY_POINT];
2274
+ this.#checkoutRedirectUrl = resolveCheckoutRedirectUrl(
2275
+ config.returnUrl,
2276
+ this.#returnUrl
2277
+ );
2247
2278
  }
2248
2279
  /**
2249
2280
  * Computes supported billing countries once at mount.
@@ -2370,7 +2401,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2370
2401
  totalAmount,
2371
2402
  totalLabel: TOTAL_LABEL,
2372
2403
  skuId,
2373
- returnUrl: this.#returnUrl,
2404
+ returnUrl: this.#checkoutRedirectUrl,
2374
2405
  userLoggedIn: sessionData.loggedIn,
2375
2406
  onPaymentAuthorized: async (event, session) => {
2376
2407
  const recaptchaToken = await recaptchaTokenPromise;
@@ -2378,7 +2409,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2378
2409
  skuId,
2379
2410
  country,
2380
2411
  currency: offer.price?.currency_code ?? DEFAULT_CURRENCY,
2381
- returnUrl: this.#returnUrl,
2412
+ returnUrl: this.#checkoutRedirectUrl,
2382
2413
  supportedBillingCountries: this.#supportedBillingCountries,
2383
2414
  recaptchaToken,
2384
2415
  entryPoint: this.#entryPoint
@@ -2404,7 +2435,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2404
2435
  );
2405
2436
  } catch {
2406
2437
  redirectToFallbackCheckout({
2407
- returnUrl: this.#returnUrl,
2438
+ returnUrl: this.#checkoutRedirectUrl,
2408
2439
  skuId,
2409
2440
  countryCode: country
2410
2441
  });
@@ -564,15 +564,21 @@ var runtimeConfig = null;
564
564
  var getApplePayConfig = () => runtimeConfig;
565
565
 
566
566
  // src/utils/recaptcha.ts
567
+ var findScriptForKey = (siteKey) => Array.from(
568
+ document.querySelectorAll(
569
+ 'script[src*="recaptcha/api.js"]'
570
+ )
571
+ ).find((script) => script.src.includes(`render=${siteKey}`));
572
+ var pendingLoads = /* @__PURE__ */ new Map();
567
573
  var loadRecaptchaScript = (siteKey) => {
568
- return new Promise((resolve, reject) => {
569
- if (typeof grecaptcha !== "undefined") return resolve();
570
- const existingScript = document.querySelector(
571
- 'script[src*="recaptcha/api.js"]'
572
- );
573
- if (existingScript) {
574
- existingScript.addEventListener("load", () => resolve());
575
- existingScript.addEventListener("error", () => {
574
+ const pending = pendingLoads.get(siteKey);
575
+ if (pending) return pending;
576
+ const ownScript = findScriptForKey(siteKey);
577
+ if (ownScript && typeof grecaptcha !== "undefined") return Promise.resolve();
578
+ const load = new Promise((resolve, reject) => {
579
+ if (ownScript) {
580
+ ownScript.addEventListener("load", () => resolve());
581
+ ownScript.addEventListener("error", () => {
576
582
  reject(new Error("Failed to load existing reCAPTCHA script"));
577
583
  });
578
584
  return;
@@ -587,6 +593,9 @@ var loadRecaptchaScript = (siteKey) => {
587
593
  };
588
594
  document.head.appendChild(script);
589
595
  });
596
+ pendingLoads.set(siteKey, load);
597
+ load.catch(() => pendingLoads.delete(siteKey));
598
+ return load;
590
599
  };
591
600
  var getRecaptchaToken = async (action) => {
592
601
  const siteKey = getApplePayConfig()?.recaptchaSiteKey;
@@ -1298,8 +1307,13 @@ function isAllowedRedirectUrl(url) {
1298
1307
  return false;
1299
1308
  }
1300
1309
  }
1310
+ var CHECKOUT_REDIRECT_BY_POLICY = {
1311
+ ["current_page" /* CURRENT_PAGE */]: (currentPageUrl) => currentPageUrl,
1312
+ ["host_managed" /* HOST_MANAGED */]: () => null
1313
+ };
1314
+ var resolveCheckoutRedirectUrl = (policy, currentPageUrl) => CHECKOUT_REDIRECT_BY_POLICY[policy](currentPageUrl);
1301
1315
  var writeCheckoutRedirect = (originUrl) => {
1302
- if (!isAllowedRedirectUrl(originUrl)) return;
1316
+ if (originUrl === null || !isAllowedRedirectUrl(originUrl)) return;
1303
1317
  try {
1304
1318
  localStorage.setItem(CHECKOUT_REDIRECT_KEY, originUrl);
1305
1319
  } catch {
@@ -1994,26 +2008,34 @@ var isEligibleForApplePayExpressPaywall = () => isAppleDeviceOrSafari();
1994
2008
  // src/apple-pay-button.ts
1995
2009
  var ENTRY_POINT_CONFIG = {
1996
2010
  ["dual_offer_banner" /* DUAL_OFFER_BANNER */]: {
1997
- isEligible: () => true
2011
+ isEligible: () => true,
2012
+ returnUrl: "current_page" /* CURRENT_PAGE */
1998
2013
  },
1999
2014
  ["free_trial_banner" /* FREE_TRIAL_BANNER */]: {
2000
- isEligible: () => true
2015
+ isEligible: () => true,
2016
+ returnUrl: "current_page" /* CURRENT_PAGE */
2001
2017
  },
2002
2018
  ["article_paywall" /* PAYWALL */]: {
2003
- isEligible: isEligibleForApplePayExpressPaywall
2019
+ isEligible: isEligibleForApplePayExpressPaywall,
2020
+ returnUrl: "current_page" /* CURRENT_PAGE */
2004
2021
  },
2005
2022
  ["article_regwall" /* REGWALL */]: {
2006
2023
  isEligible: () => true,
2007
- enabled: false
2024
+ enabled: false,
2025
+ returnUrl: "current_page" /* CURRENT_PAGE */
2008
2026
  },
2009
2027
  ["free_trial_popup" /* FREE_TRIAL_POPUP */]: {
2010
- isEligible: () => true
2028
+ isEligible: () => true,
2029
+ returnUrl: "current_page" /* CURRENT_PAGE */
2011
2030
  },
2012
2031
  ["subscribe" /* SUBSCRIBE */]: {
2013
- isEligible: () => true
2032
+ isEligible: () => true,
2014
2033
  // Always-on after offer exclusions — host (sma-checkout) owns rollout toggle
2034
+ // The offers page is not a place to return to: the host keeps the article the user arrived from (or home).
2035
+ returnUrl: "host_managed" /* HOST_MANAGED */
2015
2036
  }
2016
2037
  };
2038
+ var DEFAULT_ENTRY_POINT = "dual_offer_banner" /* DUAL_OFFER_BANNER */;
2017
2039
  var TOTAL_LABEL = "The Economist";
2018
2040
  var DEFAULT_CURRENCY = "USD";
2019
2041
  var DEFAULT_TOTAL = "0.00";
@@ -2056,7 +2078,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2056
2078
  if (APPLE_PAY_EXCLUDED_VARIANTS.includes(offer.product?.variant)) {
2057
2079
  return null;
2058
2080
  }
2059
- const resolvedEntryPoint = entryPoint || "dual_offer_banner" /* DUAL_OFFER_BANNER */;
2081
+ const resolvedEntryPoint = entryPoint || DEFAULT_ENTRY_POINT;
2060
2082
  const config = ENTRY_POINT_CONFIG[resolvedEntryPoint];
2061
2083
  if (!config) {
2062
2084
  return null;
@@ -2094,7 +2116,10 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2094
2116
  #readyPromise = null;
2095
2117
  #sessionData = null;
2096
2118
  #paymentOptions = null;
2119
+ /** URL of the page the button is on (sent to payment options). */
2097
2120
  #returnUrl = "";
2121
+ /** `checkout-redirect` to persist after purchase, per the entry point's `ReturnUrlPolicy`; `null` = leave it. */
2122
+ #checkoutRedirectUrl = null;
2098
2123
  #supportedBillingCountries = [];
2099
2124
  #entryPoint = "";
2100
2125
  // ─── Public API ─────────────────────────────────────────────────────────────
@@ -2160,6 +2185,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2160
2185
  this.#sessionData = null;
2161
2186
  this.#paymentOptions = null;
2162
2187
  this.#returnUrl = "";
2188
+ this.#checkoutRedirectUrl = null;
2163
2189
  this.#supportedBillingCountries = [];
2164
2190
  this.querySelector(SEL_CONTAINER)?.remove();
2165
2191
  }
@@ -2228,6 +2254,11 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2228
2254
  /** Captures the current page URL once at mount for reuse across the session lifetime. */
2229
2255
  #captureReturnUrl() {
2230
2256
  this.#returnUrl = captureOriginUrl();
2257
+ const config = ENTRY_POINT_CONFIG[this.#entryPoint || DEFAULT_ENTRY_POINT] ?? ENTRY_POINT_CONFIG[DEFAULT_ENTRY_POINT];
2258
+ this.#checkoutRedirectUrl = resolveCheckoutRedirectUrl(
2259
+ config.returnUrl,
2260
+ this.#returnUrl
2261
+ );
2231
2262
  }
2232
2263
  /**
2233
2264
  * Computes supported billing countries once at mount.
@@ -2354,7 +2385,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2354
2385
  totalAmount,
2355
2386
  totalLabel: TOTAL_LABEL,
2356
2387
  skuId,
2357
- returnUrl: this.#returnUrl,
2388
+ returnUrl: this.#checkoutRedirectUrl,
2358
2389
  userLoggedIn: sessionData.loggedIn,
2359
2390
  onPaymentAuthorized: async (event, session) => {
2360
2391
  const recaptchaToken = await recaptchaTokenPromise;
@@ -2362,7 +2393,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2362
2393
  skuId,
2363
2394
  country,
2364
2395
  currency: offer.price?.currency_code ?? DEFAULT_CURRENCY,
2365
- returnUrl: this.#returnUrl,
2396
+ returnUrl: this.#checkoutRedirectUrl,
2366
2397
  supportedBillingCountries: this.#supportedBillingCountries,
2367
2398
  recaptchaToken,
2368
2399
  entryPoint: this.#entryPoint
@@ -2388,7 +2419,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2388
2419
  );
2389
2420
  } catch {
2390
2421
  redirectToFallbackCheckout({
2391
- returnUrl: this.#returnUrl,
2422
+ returnUrl: this.#checkoutRedirectUrl,
2392
2423
  skuId,
2393
2424
  countryCode: country
2394
2425
  });
@@ -572,15 +572,21 @@ var resetApplePayConfig = () => {
572
572
  };
573
573
 
574
574
  // src/utils/recaptcha.ts
575
+ var findScriptForKey = (siteKey) => Array.from(
576
+ document.querySelectorAll(
577
+ 'script[src*="recaptcha/api.js"]'
578
+ )
579
+ ).find((script) => script.src.includes(`render=${siteKey}`));
580
+ var pendingLoads = /* @__PURE__ */ new Map();
575
581
  var loadRecaptchaScript = (siteKey) => {
576
- return new Promise((resolve, reject) => {
577
- if (typeof grecaptcha !== "undefined") return resolve();
578
- const existingScript = document.querySelector(
579
- 'script[src*="recaptcha/api.js"]'
580
- );
581
- if (existingScript) {
582
- existingScript.addEventListener("load", () => resolve());
583
- existingScript.addEventListener("error", () => {
582
+ const pending = pendingLoads.get(siteKey);
583
+ if (pending) return pending;
584
+ const ownScript = findScriptForKey(siteKey);
585
+ if (ownScript && typeof grecaptcha !== "undefined") return Promise.resolve();
586
+ const load = new Promise((resolve, reject) => {
587
+ if (ownScript) {
588
+ ownScript.addEventListener("load", () => resolve());
589
+ ownScript.addEventListener("error", () => {
584
590
  reject(new Error("Failed to load existing reCAPTCHA script"));
585
591
  });
586
592
  return;
@@ -595,6 +601,9 @@ var loadRecaptchaScript = (siteKey) => {
595
601
  };
596
602
  document.head.appendChild(script);
597
603
  });
604
+ pendingLoads.set(siteKey, load);
605
+ load.catch(() => pendingLoads.delete(siteKey));
606
+ return load;
598
607
  };
599
608
  var getRecaptchaToken = async (action) => {
600
609
  const siteKey = getApplePayConfig()?.recaptchaSiteKey;
@@ -1306,8 +1315,13 @@ function isAllowedRedirectUrl(url) {
1306
1315
  return false;
1307
1316
  }
1308
1317
  }
1318
+ var CHECKOUT_REDIRECT_BY_POLICY = {
1319
+ ["current_page" /* CURRENT_PAGE */]: (currentPageUrl) => currentPageUrl,
1320
+ ["host_managed" /* HOST_MANAGED */]: () => null
1321
+ };
1322
+ var resolveCheckoutRedirectUrl = (policy, currentPageUrl) => CHECKOUT_REDIRECT_BY_POLICY[policy](currentPageUrl);
1309
1323
  var writeCheckoutRedirect = (originUrl) => {
1310
- if (!isAllowedRedirectUrl(originUrl)) return;
1324
+ if (originUrl === null || !isAllowedRedirectUrl(originUrl)) return;
1311
1325
  try {
1312
1326
  localStorage.setItem(CHECKOUT_REDIRECT_KEY, originUrl);
1313
1327
  } catch {
@@ -2002,26 +2016,34 @@ var isEligibleForApplePayExpressPaywall = () => isAppleDeviceOrSafari();
2002
2016
  // src/apple-pay-button.ts
2003
2017
  var ENTRY_POINT_CONFIG = {
2004
2018
  ["dual_offer_banner" /* DUAL_OFFER_BANNER */]: {
2005
- isEligible: () => true
2019
+ isEligible: () => true,
2020
+ returnUrl: "current_page" /* CURRENT_PAGE */
2006
2021
  },
2007
2022
  ["free_trial_banner" /* FREE_TRIAL_BANNER */]: {
2008
- isEligible: () => true
2023
+ isEligible: () => true,
2024
+ returnUrl: "current_page" /* CURRENT_PAGE */
2009
2025
  },
2010
2026
  ["article_paywall" /* PAYWALL */]: {
2011
- isEligible: isEligibleForApplePayExpressPaywall
2027
+ isEligible: isEligibleForApplePayExpressPaywall,
2028
+ returnUrl: "current_page" /* CURRENT_PAGE */
2012
2029
  },
2013
2030
  ["article_regwall" /* REGWALL */]: {
2014
2031
  isEligible: () => true,
2015
- enabled: false
2032
+ enabled: false,
2033
+ returnUrl: "current_page" /* CURRENT_PAGE */
2016
2034
  },
2017
2035
  ["free_trial_popup" /* FREE_TRIAL_POPUP */]: {
2018
- isEligible: () => true
2036
+ isEligible: () => true,
2037
+ returnUrl: "current_page" /* CURRENT_PAGE */
2019
2038
  },
2020
2039
  ["subscribe" /* SUBSCRIBE */]: {
2021
- isEligible: () => true
2040
+ isEligible: () => true,
2022
2041
  // Always-on after offer exclusions — host (sma-checkout) owns rollout toggle
2042
+ // The offers page is not a place to return to: the host keeps the article the user arrived from (or home).
2043
+ returnUrl: "host_managed" /* HOST_MANAGED */
2023
2044
  }
2024
2045
  };
2046
+ var DEFAULT_ENTRY_POINT = "dual_offer_banner" /* DUAL_OFFER_BANNER */;
2025
2047
  var TOTAL_LABEL = "The Economist";
2026
2048
  var DEFAULT_CURRENCY = "USD";
2027
2049
  var DEFAULT_TOTAL = "0.00";
@@ -2064,7 +2086,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2064
2086
  if (APPLE_PAY_EXCLUDED_VARIANTS.includes(offer.product?.variant)) {
2065
2087
  return null;
2066
2088
  }
2067
- const resolvedEntryPoint = entryPoint || "dual_offer_banner" /* DUAL_OFFER_BANNER */;
2089
+ const resolvedEntryPoint = entryPoint || DEFAULT_ENTRY_POINT;
2068
2090
  const config = ENTRY_POINT_CONFIG[resolvedEntryPoint];
2069
2091
  if (!config) {
2070
2092
  return null;
@@ -2102,7 +2124,10 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2102
2124
  #readyPromise = null;
2103
2125
  #sessionData = null;
2104
2126
  #paymentOptions = null;
2127
+ /** URL of the page the button is on (sent to payment options). */
2105
2128
  #returnUrl = "";
2129
+ /** `checkout-redirect` to persist after purchase, per the entry point's `ReturnUrlPolicy`; `null` = leave it. */
2130
+ #checkoutRedirectUrl = null;
2106
2131
  #supportedBillingCountries = [];
2107
2132
  #entryPoint = "";
2108
2133
  // ─── Public API ─────────────────────────────────────────────────────────────
@@ -2168,6 +2193,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2168
2193
  this.#sessionData = null;
2169
2194
  this.#paymentOptions = null;
2170
2195
  this.#returnUrl = "";
2196
+ this.#checkoutRedirectUrl = null;
2171
2197
  this.#supportedBillingCountries = [];
2172
2198
  this.querySelector(SEL_CONTAINER)?.remove();
2173
2199
  }
@@ -2236,6 +2262,11 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2236
2262
  /** Captures the current page URL once at mount for reuse across the session lifetime. */
2237
2263
  #captureReturnUrl() {
2238
2264
  this.#returnUrl = captureOriginUrl();
2265
+ const config = ENTRY_POINT_CONFIG[this.#entryPoint || DEFAULT_ENTRY_POINT] ?? ENTRY_POINT_CONFIG[DEFAULT_ENTRY_POINT];
2266
+ this.#checkoutRedirectUrl = resolveCheckoutRedirectUrl(
2267
+ config.returnUrl,
2268
+ this.#returnUrl
2269
+ );
2239
2270
  }
2240
2271
  /**
2241
2272
  * Computes supported billing countries once at mount.
@@ -2362,7 +2393,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2362
2393
  totalAmount,
2363
2394
  totalLabel: TOTAL_LABEL,
2364
2395
  skuId,
2365
- returnUrl: this.#returnUrl,
2396
+ returnUrl: this.#checkoutRedirectUrl,
2366
2397
  userLoggedIn: sessionData.loggedIn,
2367
2398
  onPaymentAuthorized: async (event, session) => {
2368
2399
  const recaptchaToken = await recaptchaTokenPromise;
@@ -2370,7 +2401,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2370
2401
  skuId,
2371
2402
  country,
2372
2403
  currency: offer.price?.currency_code ?? DEFAULT_CURRENCY,
2373
- returnUrl: this.#returnUrl,
2404
+ returnUrl: this.#checkoutRedirectUrl,
2374
2405
  supportedBillingCountries: this.#supportedBillingCountries,
2375
2406
  recaptchaToken,
2376
2407
  entryPoint: this.#entryPoint
@@ -2396,7 +2427,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2396
2427
  );
2397
2428
  } catch {
2398
2429
  redirectToFallbackCheckout({
2399
- returnUrl: this.#returnUrl,
2430
+ returnUrl: this.#checkoutRedirectUrl,
2400
2431
  skuId,
2401
2432
  countryCode: country
2402
2433
  });
@@ -564,15 +564,21 @@ var runtimeConfig = null;
564
564
  var getApplePayConfig = () => runtimeConfig;
565
565
 
566
566
  // src/utils/recaptcha.ts
567
+ var findScriptForKey = (siteKey) => Array.from(
568
+ document.querySelectorAll(
569
+ 'script[src*="recaptcha/api.js"]'
570
+ )
571
+ ).find((script) => script.src.includes(`render=${siteKey}`));
572
+ var pendingLoads = /* @__PURE__ */ new Map();
567
573
  var loadRecaptchaScript = (siteKey) => {
568
- return new Promise((resolve, reject) => {
569
- if (typeof grecaptcha !== "undefined") return resolve();
570
- const existingScript = document.querySelector(
571
- 'script[src*="recaptcha/api.js"]'
572
- );
573
- if (existingScript) {
574
- existingScript.addEventListener("load", () => resolve());
575
- existingScript.addEventListener("error", () => {
574
+ const pending = pendingLoads.get(siteKey);
575
+ if (pending) return pending;
576
+ const ownScript = findScriptForKey(siteKey);
577
+ if (ownScript && typeof grecaptcha !== "undefined") return Promise.resolve();
578
+ const load = new Promise((resolve, reject) => {
579
+ if (ownScript) {
580
+ ownScript.addEventListener("load", () => resolve());
581
+ ownScript.addEventListener("error", () => {
576
582
  reject(new Error("Failed to load existing reCAPTCHA script"));
577
583
  });
578
584
  return;
@@ -587,6 +593,9 @@ var loadRecaptchaScript = (siteKey) => {
587
593
  };
588
594
  document.head.appendChild(script);
589
595
  });
596
+ pendingLoads.set(siteKey, load);
597
+ load.catch(() => pendingLoads.delete(siteKey));
598
+ return load;
590
599
  };
591
600
  var getRecaptchaToken = async (action) => {
592
601
  const siteKey = getApplePayConfig()?.recaptchaSiteKey;
@@ -564,15 +564,21 @@ var runtimeConfig = null;
564
564
  var getApplePayConfig = () => runtimeConfig;
565
565
 
566
566
  // src/utils/recaptcha.ts
567
+ var findScriptForKey = (siteKey) => Array.from(
568
+ document.querySelectorAll(
569
+ 'script[src*="recaptcha/api.js"]'
570
+ )
571
+ ).find((script) => script.src.includes(`render=${siteKey}`));
572
+ var pendingLoads = /* @__PURE__ */ new Map();
567
573
  var loadRecaptchaScript = (siteKey) => {
568
- return new Promise((resolve, reject) => {
569
- if (typeof grecaptcha !== "undefined") return resolve();
570
- const existingScript = document.querySelector(
571
- 'script[src*="recaptcha/api.js"]'
572
- );
573
- if (existingScript) {
574
- existingScript.addEventListener("load", () => resolve());
575
- existingScript.addEventListener("error", () => {
574
+ const pending = pendingLoads.get(siteKey);
575
+ if (pending) return pending;
576
+ const ownScript = findScriptForKey(siteKey);
577
+ if (ownScript && typeof grecaptcha !== "undefined") return Promise.resolve();
578
+ const load = new Promise((resolve, reject) => {
579
+ if (ownScript) {
580
+ ownScript.addEventListener("load", () => resolve());
581
+ ownScript.addEventListener("error", () => {
576
582
  reject(new Error("Failed to load existing reCAPTCHA script"));
577
583
  });
578
584
  return;
@@ -587,6 +593,9 @@ var loadRecaptchaScript = (siteKey) => {
587
593
  };
588
594
  document.head.appendChild(script);
589
595
  });
596
+ pendingLoads.set(siteKey, load);
597
+ load.catch(() => pendingLoads.delete(siteKey));
598
+ return load;
590
599
  };
591
600
  var getRecaptchaToken = async (action) => {
592
601
  const siteKey = getApplePayConfig()?.recaptchaSiteKey;
@@ -1298,8 +1307,13 @@ function isAllowedRedirectUrl(url) {
1298
1307
  return false;
1299
1308
  }
1300
1309
  }
1310
+ var CHECKOUT_REDIRECT_BY_POLICY = {
1311
+ ["current_page" /* CURRENT_PAGE */]: (currentPageUrl) => currentPageUrl,
1312
+ ["host_managed" /* HOST_MANAGED */]: () => null
1313
+ };
1314
+ var resolveCheckoutRedirectUrl = (policy, currentPageUrl) => CHECKOUT_REDIRECT_BY_POLICY[policy](currentPageUrl);
1301
1315
  var writeCheckoutRedirect = (originUrl) => {
1302
- if (!isAllowedRedirectUrl(originUrl)) return;
1316
+ if (originUrl === null || !isAllowedRedirectUrl(originUrl)) return;
1303
1317
  try {
1304
1318
  localStorage.setItem(CHECKOUT_REDIRECT_KEY, originUrl);
1305
1319
  } catch {
@@ -1994,26 +2008,34 @@ var isEligibleForApplePayExpressPaywall = () => isAppleDeviceOrSafari();
1994
2008
  // src/apple-pay-button.ts
1995
2009
  var ENTRY_POINT_CONFIG = {
1996
2010
  ["dual_offer_banner" /* DUAL_OFFER_BANNER */]: {
1997
- isEligible: () => true
2011
+ isEligible: () => true,
2012
+ returnUrl: "current_page" /* CURRENT_PAGE */
1998
2013
  },
1999
2014
  ["free_trial_banner" /* FREE_TRIAL_BANNER */]: {
2000
- isEligible: () => true
2015
+ isEligible: () => true,
2016
+ returnUrl: "current_page" /* CURRENT_PAGE */
2001
2017
  },
2002
2018
  ["article_paywall" /* PAYWALL */]: {
2003
- isEligible: isEligibleForApplePayExpressPaywall
2019
+ isEligible: isEligibleForApplePayExpressPaywall,
2020
+ returnUrl: "current_page" /* CURRENT_PAGE */
2004
2021
  },
2005
2022
  ["article_regwall" /* REGWALL */]: {
2006
2023
  isEligible: () => true,
2007
- enabled: false
2024
+ enabled: false,
2025
+ returnUrl: "current_page" /* CURRENT_PAGE */
2008
2026
  },
2009
2027
  ["free_trial_popup" /* FREE_TRIAL_POPUP */]: {
2010
- isEligible: () => true
2028
+ isEligible: () => true,
2029
+ returnUrl: "current_page" /* CURRENT_PAGE */
2011
2030
  },
2012
2031
  ["subscribe" /* SUBSCRIBE */]: {
2013
- isEligible: () => true
2032
+ isEligible: () => true,
2014
2033
  // Always-on after offer exclusions — host (sma-checkout) owns rollout toggle
2034
+ // The offers page is not a place to return to: the host keeps the article the user arrived from (or home).
2035
+ returnUrl: "host_managed" /* HOST_MANAGED */
2015
2036
  }
2016
2037
  };
2038
+ var DEFAULT_ENTRY_POINT = "dual_offer_banner" /* DUAL_OFFER_BANNER */;
2017
2039
  var TOTAL_LABEL = "The Economist";
2018
2040
  var DEFAULT_CURRENCY = "USD";
2019
2041
  var DEFAULT_TOTAL = "0.00";
@@ -2056,7 +2078,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2056
2078
  if (APPLE_PAY_EXCLUDED_VARIANTS.includes(offer.product?.variant)) {
2057
2079
  return null;
2058
2080
  }
2059
- const resolvedEntryPoint = entryPoint || "dual_offer_banner" /* DUAL_OFFER_BANNER */;
2081
+ const resolvedEntryPoint = entryPoint || DEFAULT_ENTRY_POINT;
2060
2082
  const config = ENTRY_POINT_CONFIG[resolvedEntryPoint];
2061
2083
  if (!config) {
2062
2084
  return null;
@@ -2094,7 +2116,10 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2094
2116
  #readyPromise = null;
2095
2117
  #sessionData = null;
2096
2118
  #paymentOptions = null;
2119
+ /** URL of the page the button is on (sent to payment options). */
2097
2120
  #returnUrl = "";
2121
+ /** `checkout-redirect` to persist after purchase, per the entry point's `ReturnUrlPolicy`; `null` = leave it. */
2122
+ #checkoutRedirectUrl = null;
2098
2123
  #supportedBillingCountries = [];
2099
2124
  #entryPoint = "";
2100
2125
  // ─── Public API ─────────────────────────────────────────────────────────────
@@ -2160,6 +2185,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2160
2185
  this.#sessionData = null;
2161
2186
  this.#paymentOptions = null;
2162
2187
  this.#returnUrl = "";
2188
+ this.#checkoutRedirectUrl = null;
2163
2189
  this.#supportedBillingCountries = [];
2164
2190
  this.querySelector(SEL_CONTAINER)?.remove();
2165
2191
  }
@@ -2228,6 +2254,11 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2228
2254
  /** Captures the current page URL once at mount for reuse across the session lifetime. */
2229
2255
  #captureReturnUrl() {
2230
2256
  this.#returnUrl = captureOriginUrl();
2257
+ const config = ENTRY_POINT_CONFIG[this.#entryPoint || DEFAULT_ENTRY_POINT] ?? ENTRY_POINT_CONFIG[DEFAULT_ENTRY_POINT];
2258
+ this.#checkoutRedirectUrl = resolveCheckoutRedirectUrl(
2259
+ config.returnUrl,
2260
+ this.#returnUrl
2261
+ );
2231
2262
  }
2232
2263
  /**
2233
2264
  * Computes supported billing countries once at mount.
@@ -2354,7 +2385,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2354
2385
  totalAmount,
2355
2386
  totalLabel: TOTAL_LABEL,
2356
2387
  skuId,
2357
- returnUrl: this.#returnUrl,
2388
+ returnUrl: this.#checkoutRedirectUrl,
2358
2389
  userLoggedIn: sessionData.loggedIn,
2359
2390
  onPaymentAuthorized: async (event, session) => {
2360
2391
  const recaptchaToken = await recaptchaTokenPromise;
@@ -2362,7 +2393,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2362
2393
  skuId,
2363
2394
  country,
2364
2395
  currency: offer.price?.currency_code ?? DEFAULT_CURRENCY,
2365
- returnUrl: this.#returnUrl,
2396
+ returnUrl: this.#checkoutRedirectUrl,
2366
2397
  supportedBillingCountries: this.#supportedBillingCountries,
2367
2398
  recaptchaToken,
2368
2399
  entryPoint: this.#entryPoint
@@ -2388,7 +2419,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2388
2419
  );
2389
2420
  } catch {
2390
2421
  redirectToFallbackCheckout({
2391
- returnUrl: this.#returnUrl,
2422
+ returnUrl: this.#checkoutRedirectUrl,
2392
2423
  skuId,
2393
2424
  countryCode: country
2394
2425
  });
@@ -10,7 +10,8 @@ export type ApplePaySessionConfig = {
10
10
  totalAmount: string;
11
11
  totalLabel: string;
12
12
  skuId: string;
13
- returnUrl: string;
13
+ /** `checkout-redirect` to persist, or `null` to leave the host's value untouched. */
14
+ returnUrl: string | null;
14
15
  userLoggedIn: boolean;
15
16
  onPaymentAuthorized: (event: ApplePayJS.ApplePayPaymentAuthorizedEvent, session: ApplePaySession) => Promise<void>;
16
17
  onCancel?: () => void;
@@ -74,7 +74,7 @@ function isAllowedRedirectUrl(url) {
74
74
  }
75
75
  }
76
76
  var writeCheckoutRedirect = (originUrl) => {
77
- if (!isAllowedRedirectUrl(originUrl)) return;
77
+ if (originUrl === null || !isAllowedRedirectUrl(originUrl)) return;
78
78
  try {
79
79
  localStorage.setItem(CHECKOUT_REDIRECT_KEY, originUrl);
80
80
  } catch {
@@ -100,15 +100,21 @@ var redirectToCheckoutFallback = (params) => {
100
100
  };
101
101
 
102
102
  // src/utils/recaptcha.ts
103
+ var findScriptForKey = (siteKey) => Array.from(
104
+ document.querySelectorAll(
105
+ 'script[src*="recaptcha/api.js"]'
106
+ )
107
+ ).find((script) => script.src.includes(`render=${siteKey}`));
108
+ var pendingLoads = /* @__PURE__ */ new Map();
103
109
  var loadRecaptchaScript = (siteKey) => {
104
- return new Promise((resolve, reject) => {
105
- if (typeof grecaptcha !== "undefined") return resolve();
106
- const existingScript = document.querySelector(
107
- 'script[src*="recaptcha/api.js"]'
108
- );
109
- if (existingScript) {
110
- existingScript.addEventListener("load", () => resolve());
111
- existingScript.addEventListener("error", () => {
110
+ const pending = pendingLoads.get(siteKey);
111
+ if (pending) return pending;
112
+ const ownScript = findScriptForKey(siteKey);
113
+ if (ownScript && typeof grecaptcha !== "undefined") return Promise.resolve();
114
+ const load = new Promise((resolve, reject) => {
115
+ if (ownScript) {
116
+ ownScript.addEventListener("load", () => resolve());
117
+ ownScript.addEventListener("error", () => {
112
118
  reject(new Error("Failed to load existing reCAPTCHA script"));
113
119
  });
114
120
  return;
@@ -123,6 +129,9 @@ var loadRecaptchaScript = (siteKey) => {
123
129
  };
124
130
  document.head.appendChild(script);
125
131
  });
132
+ pendingLoads.set(siteKey, load);
133
+ load.catch(() => pendingLoads.delete(siteKey));
134
+ return load;
126
135
  };
127
136
  var getRecaptchaToken = async (action) => {
128
137
  const siteKey = getApplePayConfig()?.recaptchaSiteKey;
@@ -19,7 +19,8 @@ export type PaymentHandlerConfig = {
19
19
  skuId: string;
20
20
  country: string;
21
21
  currency: string;
22
- returnUrl: string;
22
+ /** `checkout-redirect` to persist, or `null` to leave the host's value untouched. */
23
+ returnUrl: string | null;
23
24
  supportedBillingCountries: string[];
24
25
  recaptchaToken: string;
25
26
  /** When FREE_TRIAL_POPUP and the session is logged in, complete is opened with status=nspc. */
@@ -249,7 +249,7 @@ function isAllowedRedirectUrl(url) {
249
249
  }
250
250
  }
251
251
  var writeCheckoutRedirect = (originUrl) => {
252
- if (!isAllowedRedirectUrl(originUrl)) return;
252
+ if (originUrl === null || !isAllowedRedirectUrl(originUrl)) return;
253
253
  try {
254
254
  localStorage.setItem(CHECKOUT_REDIRECT_KEY, originUrl);
255
255
  } catch {
package/dist/src/index.js CHANGED
@@ -569,15 +569,21 @@ var configureApplePay = (config) => {
569
569
  var getApplePayConfig = () => runtimeConfig;
570
570
 
571
571
  // src/utils/recaptcha.ts
572
+ var findScriptForKey = (siteKey) => Array.from(
573
+ document.querySelectorAll(
574
+ 'script[src*="recaptcha/api.js"]'
575
+ )
576
+ ).find((script) => script.src.includes(`render=${siteKey}`));
577
+ var pendingLoads = /* @__PURE__ */ new Map();
572
578
  var loadRecaptchaScript = (siteKey) => {
573
- return new Promise((resolve, reject) => {
574
- if (typeof grecaptcha !== "undefined") return resolve();
575
- const existingScript = document.querySelector(
576
- 'script[src*="recaptcha/api.js"]'
577
- );
578
- if (existingScript) {
579
- existingScript.addEventListener("load", () => resolve());
580
- existingScript.addEventListener("error", () => {
579
+ const pending = pendingLoads.get(siteKey);
580
+ if (pending) return pending;
581
+ const ownScript = findScriptForKey(siteKey);
582
+ if (ownScript && typeof grecaptcha !== "undefined") return Promise.resolve();
583
+ const load = new Promise((resolve, reject) => {
584
+ if (ownScript) {
585
+ ownScript.addEventListener("load", () => resolve());
586
+ ownScript.addEventListener("error", () => {
581
587
  reject(new Error("Failed to load existing reCAPTCHA script"));
582
588
  });
583
589
  return;
@@ -592,6 +598,9 @@ var loadRecaptchaScript = (siteKey) => {
592
598
  };
593
599
  document.head.appendChild(script);
594
600
  });
601
+ pendingLoads.set(siteKey, load);
602
+ load.catch(() => pendingLoads.delete(siteKey));
603
+ return load;
595
604
  };
596
605
  var getRecaptchaToken = async (action) => {
597
606
  const siteKey = getApplePayConfig()?.recaptchaSiteKey;
@@ -1303,8 +1312,13 @@ function isAllowedRedirectUrl(url) {
1303
1312
  return false;
1304
1313
  }
1305
1314
  }
1315
+ var CHECKOUT_REDIRECT_BY_POLICY = {
1316
+ ["current_page" /* CURRENT_PAGE */]: (currentPageUrl) => currentPageUrl,
1317
+ ["host_managed" /* HOST_MANAGED */]: () => null
1318
+ };
1319
+ var resolveCheckoutRedirectUrl = (policy, currentPageUrl) => CHECKOUT_REDIRECT_BY_POLICY[policy](currentPageUrl);
1306
1320
  var writeCheckoutRedirect = (originUrl) => {
1307
- if (!isAllowedRedirectUrl(originUrl)) return;
1321
+ if (originUrl === null || !isAllowedRedirectUrl(originUrl)) return;
1308
1322
  try {
1309
1323
  localStorage.setItem(CHECKOUT_REDIRECT_KEY, originUrl);
1310
1324
  } catch {
@@ -2010,26 +2024,34 @@ var isEligibleForApplePayExpressPaywall = () => isAppleDeviceOrSafari();
2010
2024
  // src/apple-pay-button.ts
2011
2025
  var ENTRY_POINT_CONFIG = {
2012
2026
  ["dual_offer_banner" /* DUAL_OFFER_BANNER */]: {
2013
- isEligible: () => true
2027
+ isEligible: () => true,
2028
+ returnUrl: "current_page" /* CURRENT_PAGE */
2014
2029
  },
2015
2030
  ["free_trial_banner" /* FREE_TRIAL_BANNER */]: {
2016
- isEligible: () => true
2031
+ isEligible: () => true,
2032
+ returnUrl: "current_page" /* CURRENT_PAGE */
2017
2033
  },
2018
2034
  ["article_paywall" /* PAYWALL */]: {
2019
- isEligible: isEligibleForApplePayExpressPaywall
2035
+ isEligible: isEligibleForApplePayExpressPaywall,
2036
+ returnUrl: "current_page" /* CURRENT_PAGE */
2020
2037
  },
2021
2038
  ["article_regwall" /* REGWALL */]: {
2022
2039
  isEligible: () => true,
2023
- enabled: false
2040
+ enabled: false,
2041
+ returnUrl: "current_page" /* CURRENT_PAGE */
2024
2042
  },
2025
2043
  ["free_trial_popup" /* FREE_TRIAL_POPUP */]: {
2026
- isEligible: () => true
2044
+ isEligible: () => true,
2045
+ returnUrl: "current_page" /* CURRENT_PAGE */
2027
2046
  },
2028
2047
  ["subscribe" /* SUBSCRIBE */]: {
2029
- isEligible: () => true
2048
+ isEligible: () => true,
2030
2049
  // Always-on after offer exclusions — host (sma-checkout) owns rollout toggle
2050
+ // The offers page is not a place to return to: the host keeps the article the user arrived from (or home).
2051
+ returnUrl: "host_managed" /* HOST_MANAGED */
2031
2052
  }
2032
2053
  };
2054
+ var DEFAULT_ENTRY_POINT = "dual_offer_banner" /* DUAL_OFFER_BANNER */;
2033
2055
  var TOTAL_LABEL = "The Economist";
2034
2056
  var DEFAULT_CURRENCY = "USD";
2035
2057
  var DEFAULT_TOTAL = "0.00";
@@ -2072,7 +2094,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2072
2094
  if (APPLE_PAY_EXCLUDED_VARIANTS.includes(offer.product?.variant)) {
2073
2095
  return null;
2074
2096
  }
2075
- const resolvedEntryPoint = entryPoint || "dual_offer_banner" /* DUAL_OFFER_BANNER */;
2097
+ const resolvedEntryPoint = entryPoint || DEFAULT_ENTRY_POINT;
2076
2098
  const config = ENTRY_POINT_CONFIG[resolvedEntryPoint];
2077
2099
  if (!config) {
2078
2100
  return null;
@@ -2110,7 +2132,10 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2110
2132
  #readyPromise = null;
2111
2133
  #sessionData = null;
2112
2134
  #paymentOptions = null;
2135
+ /** URL of the page the button is on (sent to payment options). */
2113
2136
  #returnUrl = "";
2137
+ /** `checkout-redirect` to persist after purchase, per the entry point's `ReturnUrlPolicy`; `null` = leave it. */
2138
+ #checkoutRedirectUrl = null;
2114
2139
  #supportedBillingCountries = [];
2115
2140
  #entryPoint = "";
2116
2141
  // ─── Public API ─────────────────────────────────────────────────────────────
@@ -2176,6 +2201,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2176
2201
  this.#sessionData = null;
2177
2202
  this.#paymentOptions = null;
2178
2203
  this.#returnUrl = "";
2204
+ this.#checkoutRedirectUrl = null;
2179
2205
  this.#supportedBillingCountries = [];
2180
2206
  this.querySelector(SEL_CONTAINER)?.remove();
2181
2207
  }
@@ -2244,6 +2270,11 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2244
2270
  /** Captures the current page URL once at mount for reuse across the session lifetime. */
2245
2271
  #captureReturnUrl() {
2246
2272
  this.#returnUrl = captureOriginUrl();
2273
+ const config = ENTRY_POINT_CONFIG[this.#entryPoint || DEFAULT_ENTRY_POINT] ?? ENTRY_POINT_CONFIG[DEFAULT_ENTRY_POINT];
2274
+ this.#checkoutRedirectUrl = resolveCheckoutRedirectUrl(
2275
+ config.returnUrl,
2276
+ this.#returnUrl
2277
+ );
2247
2278
  }
2248
2279
  /**
2249
2280
  * Computes supported billing countries once at mount.
@@ -2370,7 +2401,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2370
2401
  totalAmount,
2371
2402
  totalLabel: TOTAL_LABEL,
2372
2403
  skuId,
2373
- returnUrl: this.#returnUrl,
2404
+ returnUrl: this.#checkoutRedirectUrl,
2374
2405
  userLoggedIn: sessionData.loggedIn,
2375
2406
  onPaymentAuthorized: async (event, session) => {
2376
2407
  const recaptchaToken = await recaptchaTokenPromise;
@@ -2378,7 +2409,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2378
2409
  skuId,
2379
2410
  country,
2380
2411
  currency: offer.price?.currency_code ?? DEFAULT_CURRENCY,
2381
- returnUrl: this.#returnUrl,
2412
+ returnUrl: this.#checkoutRedirectUrl,
2382
2413
  supportedBillingCountries: this.#supportedBillingCountries,
2383
2414
  recaptchaToken,
2384
2415
  entryPoint: this.#entryPoint
@@ -2404,7 +2435,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
2404
2435
  );
2405
2436
  } catch {
2406
2437
  redirectToFallbackCheckout({
2407
- returnUrl: this.#returnUrl,
2438
+ returnUrl: this.#checkoutRedirectUrl,
2408
2439
  skuId,
2409
2440
  countryCode: country
2410
2441
  });
@@ -7,7 +7,23 @@
7
7
  *
8
8
  * Matches sma-checkout: src/lib/navigate-to-checkout/index.tsx
9
9
  */
10
- export declare const writeCheckoutRedirect: (originUrl: string) => void;
10
+ /**
11
+ * What the post-checkout "return to" target (`checkout-redirect`, read by the onboarding app once the user has
12
+ * finished onboarding) should be. Chosen per entry point in `ENTRY_POINT_CONFIG` (apple-pay-button.ts).
13
+ */
14
+ export declare enum ReturnUrlPolicy {
15
+ /** Return the user to the page the button is on, e.g. the article behind a paywall. */
16
+ CURRENT_PAGE = "current_page",
17
+ /**
18
+ * Leave `checkout-redirect` untouched: the host owns it. sma-checkout's /subscribe pages store the article the
19
+ * user arrived from (and never themselves), so overwriting it would send the user back to the offers page.
20
+ * With nothing stored, onboarding returns the user to the home page.
21
+ */
22
+ HOST_MANAGED = "host_managed"
23
+ }
24
+ export declare const resolveCheckoutRedirectUrl: (policy: ReturnUrlPolicy, currentPageUrl: string) => string | null;
25
+ /** Persists the return target. `null` (see `ReturnUrlPolicy.HOST_MANAGED`) leaves the stored value as it is. */
26
+ export declare const writeCheckoutRedirect: (originUrl: string | null) => void;
11
27
  /**
12
28
  * Captures the current page URL (the article the user is reading).
13
29
  * Must be called before any redirect occurs.
@@ -10,8 +10,18 @@ function isAllowedRedirectUrl(url) {
10
10
  return false;
11
11
  }
12
12
  }
13
+ var ReturnUrlPolicy = /* @__PURE__ */ ((ReturnUrlPolicy2) => {
14
+ ReturnUrlPolicy2["CURRENT_PAGE"] = "current_page";
15
+ ReturnUrlPolicy2["HOST_MANAGED"] = "host_managed";
16
+ return ReturnUrlPolicy2;
17
+ })(ReturnUrlPolicy || {});
18
+ var CHECKOUT_REDIRECT_BY_POLICY = {
19
+ ["current_page" /* CURRENT_PAGE */]: (currentPageUrl) => currentPageUrl,
20
+ ["host_managed" /* HOST_MANAGED */]: () => null
21
+ };
22
+ var resolveCheckoutRedirectUrl = (policy, currentPageUrl) => CHECKOUT_REDIRECT_BY_POLICY[policy](currentPageUrl);
13
23
  var writeCheckoutRedirect = (originUrl) => {
14
- if (!isAllowedRedirectUrl(originUrl)) return;
24
+ if (originUrl === null || !isAllowedRedirectUrl(originUrl)) return;
15
25
  try {
16
26
  localStorage.setItem(CHECKOUT_REDIRECT_KEY, originUrl);
17
27
  } catch {
@@ -53,10 +63,12 @@ var redirectToCheckoutFallback = (params) => {
53
63
  return url.pathname + url.search;
54
64
  };
55
65
  export {
66
+ ReturnUrlPolicy,
56
67
  buildQueryString,
57
68
  captureOriginUrl,
58
69
  decodeSku,
59
70
  redirectToCheckoutComplete,
60
71
  redirectToCheckoutFallback,
72
+ resolveCheckoutRedirectUrl,
61
73
  writeCheckoutRedirect
62
74
  };
@@ -1,8 +1,9 @@
1
1
  /**
2
2
  * reCAPTCHA v3 token acquisition.
3
3
  *
4
- * Dynamically injects the Google reCAPTCHA script loader into the DOM
5
- * because it is not loaded globally on article/paywall pages.
4
+ * Dynamically injects the Google reCAPTCHA script loader for this package's
5
+ * site key. Another reCAPTCHA script on the page (e.g. the host's own key) is
6
+ * not reused, because it does not register our key.
6
7
  *
7
8
  * The site key is provided at runtime via configureApplePay({ recaptchaSiteKey }).
8
9
  */
@@ -3,15 +3,21 @@ var runtimeConfig = null;
3
3
  var getApplePayConfig = () => runtimeConfig;
4
4
 
5
5
  // src/utils/recaptcha.ts
6
+ var findScriptForKey = (siteKey) => Array.from(
7
+ document.querySelectorAll(
8
+ 'script[src*="recaptcha/api.js"]'
9
+ )
10
+ ).find((script) => script.src.includes(`render=${siteKey}`));
11
+ var pendingLoads = /* @__PURE__ */ new Map();
6
12
  var loadRecaptchaScript = (siteKey) => {
7
- return new Promise((resolve, reject) => {
8
- if (typeof grecaptcha !== "undefined") return resolve();
9
- const existingScript = document.querySelector(
10
- 'script[src*="recaptcha/api.js"]'
11
- );
12
- if (existingScript) {
13
- existingScript.addEventListener("load", () => resolve());
14
- existingScript.addEventListener("error", () => {
13
+ const pending = pendingLoads.get(siteKey);
14
+ if (pending) return pending;
15
+ const ownScript = findScriptForKey(siteKey);
16
+ if (ownScript && typeof grecaptcha !== "undefined") return Promise.resolve();
17
+ const load = new Promise((resolve, reject) => {
18
+ if (ownScript) {
19
+ ownScript.addEventListener("load", () => resolve());
20
+ ownScript.addEventListener("error", () => {
15
21
  reject(new Error("Failed to load existing reCAPTCHA script"));
16
22
  });
17
23
  return;
@@ -26,6 +32,9 @@ var loadRecaptchaScript = (siteKey) => {
26
32
  };
27
33
  document.head.appendChild(script);
28
34
  });
35
+ pendingLoads.set(siteKey, load);
36
+ load.catch(() => pendingLoads.delete(siteKey));
37
+ return load;
29
38
  };
30
39
  var getRecaptchaToken = async (action) => {
31
40
  const siteKey = getApplePayConfig()?.recaptchaSiteKey;
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.15.1-beta.1",
4
+ "version": "1.15.2",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",