@economist/web-apple-pay 0.1.1-beta.0 → 0.1.1-beta.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/dist/index.js CHANGED
@@ -307,7 +307,9 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
307
307
  this.#country = value;
308
308
  }
309
309
  connectedCallback() {
310
+ console.log("[web-apple-pay] ApplePayModal connectedCallback");
310
311
  if (this.querySelector(".apm-overlay")) {
312
+ console.log("[web-apple-pay] ApplePayModal connectedCallback: already rendered, skipping");
311
313
  return;
312
314
  }
313
315
  ensureApplePayLogoSymbol();
@@ -321,6 +323,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
321
323
  this.remove();
322
324
  return;
323
325
  }
326
+ console.log("[web-apple-pay] ApplePayModal: rendering for offer", { skuId: offer.sku_id, country });
324
327
  const offerNameElement = this.querySelector(
325
328
  "#apple-pay-modal-offer-name"
326
329
  );
@@ -393,11 +396,14 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
393
396
  });
394
397
  }
395
398
  cta?.addEventListener("click", () => {
399
+ console.log("[web-apple-pay] ApplePayModal: CTA clicked", { skuId: offer.sku_id, country, isUS });
396
400
  if (isUS && checkbox && !checkbox.checked) {
401
+ console.log("[web-apple-pay] ApplePayModal: T&C checkbox not accepted, showing error");
397
402
  errorElement?.classList.add("apm__error--visible");
398
403
  checkboxLabel?.classList.add("apm__checkbox-label--error");
399
404
  return;
400
405
  }
406
+ console.log("[web-apple-pay] ApplePayModal: dispatching apple-pay-confirmed", { skuId: offer.sku_id, country });
401
407
  this.dispatchEvent(
402
408
  new CustomEvent("apple-pay-confirmed", {
403
409
  bubbles: true,
@@ -405,7 +411,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
405
411
  detail: { skuId: offer.sku_id, country }
406
412
  })
407
413
  );
408
- this.close();
414
+ console.log("[web-apple-pay] ApplePayModal: dispatching apple-pay-confirmed-final", { skuId: offer.sku_id, country });
409
415
  });
410
416
  this.querySelector("#apple-pay-modal-close")?.addEventListener(
411
417
  "click",
@@ -413,6 +419,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
413
419
  );
414
420
  }
415
421
  open() {
422
+ console.log("[web-apple-pay] ApplePayModal: open()");
416
423
  this.#triggerElement = document.activeElement;
417
424
  document.body.appendChild(this);
418
425
  this.#keyHandler = (event) => {
@@ -427,6 +434,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
427
434
  this.querySelector("#apple-pay-modal")?.focus();
428
435
  }
429
436
  close() {
437
+ console.log("[web-apple-pay] ApplePayModal: close()");
430
438
  if (this.#keyHandler) {
431
439
  document.removeEventListener("keydown", this.#keyHandler, {
432
440
  capture: true
@@ -1351,11 +1359,19 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1351
1359
  * Creates and configures an ApplePayButton if the feature flag is present.
1352
1360
  */
1353
1361
  static createIfEnabled(offer, country, entryPoint) {
1354
- if (!offer) return null;
1355
- if (APPLE_PAY_EXCLUDED_VARIANTS.includes(offer.product?.variant))
1362
+ if (!offer) {
1363
+ console.log("[web-apple-pay] createIfEnabled: no offer provided \u2192 null");
1356
1364
  return null;
1357
- if (!new URLSearchParams(window.location.search).has(FEATURE_APPLE_PAY_EXPRESS_CHECKOUT))
1365
+ }
1366
+ if (APPLE_PAY_EXCLUDED_VARIANTS.includes(offer.product?.variant)) {
1367
+ console.log("[web-apple-pay] createIfEnabled: excluded variant \u2192 null", { variant: offer.product?.variant });
1358
1368
  return null;
1369
+ }
1370
+ if (!new URLSearchParams(window.location.search).has(FEATURE_APPLE_PAY_EXPRESS_CHECKOUT)) {
1371
+ console.log("[web-apple-pay] createIfEnabled: feature flag absent \u2192 null");
1372
+ return null;
1373
+ }
1374
+ console.log("[web-apple-pay] createIfEnabled: creating button", { skuId: offer.sku_id, country, entryPoint });
1359
1375
  _ApplePayButton.define();
1360
1376
  const button = document.createElement(_ApplePayButton.tag);
1361
1377
  button.offer = offer;
@@ -1423,7 +1439,9 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1423
1439
  }
1424
1440
  // ─── Lifecycle ──────────────────────────────────────────────────────────────
1425
1441
  async connectedCallback() {
1442
+ console.log("[web-apple-pay] connectedCallback: element connected");
1426
1443
  if (this.#rendering || this.querySelector(SEL_CONTAINER)) {
1444
+ console.log("[web-apple-pay] connectedCallback: already rendering or rendered, skipping");
1427
1445
  return;
1428
1446
  }
1429
1447
  this.#rendering = true;
@@ -1450,6 +1468,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1450
1468
  }
1451
1469
  // ─── Render pipeline ────────────────────────────────────────────────────────
1452
1470
  async #run() {
1471
+ console.log("[web-apple-pay] #run: starting render pipeline");
1453
1472
  try {
1454
1473
  if (await this.#checkAvailability()) return;
1455
1474
  if (await this.#checkSession()) return;
@@ -1468,9 +1487,11 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1468
1487
  async #checkAvailability() {
1469
1488
  const available = await isApplePayAvailable();
1470
1489
  if (!available) {
1490
+ console.log("[web-apple-pay] #checkAvailability: Apple Pay unavailable \u2192 hiding");
1471
1491
  this.style.display = "none";
1472
1492
  return true;
1473
1493
  }
1494
+ console.log("[web-apple-pay] #checkAvailability: Apple Pay available");
1474
1495
  return false;
1475
1496
  }
1476
1497
  /**
@@ -1479,7 +1500,9 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1479
1500
  */
1480
1501
  async #checkSession() {
1481
1502
  this.#sessionData = await this.#fetchSession();
1503
+ console.log("[web-apple-pay] #checkSession: session fetched", { loggedIn: this.#sessionData?.loggedIn, userType: this.#sessionData?.userType });
1482
1504
  if (this.#sessionData?.loggedIn && (this.#sessionData.userType === "active-subscription" || this.#sessionData.userType === "b2b-user")) {
1505
+ console.log("[web-apple-pay] #checkSession: ineligible user \u2192 hiding", { userType: this.#sessionData.userType });
1483
1506
  this.style.display = "none";
1484
1507
  return true;
1485
1508
  }
@@ -1488,14 +1511,17 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1488
1511
  /** Returns `true` (hide) when no offer has been set before mount. */
1489
1512
  #checkOffer() {
1490
1513
  if (!this.#offer) {
1514
+ console.log("[web-apple-pay] #checkOffer: no offer set \u2192 hiding");
1491
1515
  this.style.display = "none";
1492
1516
  return true;
1493
1517
  }
1518
+ console.log("[web-apple-pay] #checkOffer: offer present", { skuId: this.#offer.sku_id });
1494
1519
  return false;
1495
1520
  }
1496
1521
  /** Captures the current page URL once at mount for reuse across the session lifetime. */
1497
1522
  #captureReturnUrl() {
1498
1523
  this.#returnUrl = captureOriginUrl();
1524
+ console.log("[web-apple-pay] #captureReturnUrl:", this.#returnUrl);
1499
1525
  }
1500
1526
  /**
1501
1527
  * Computes supported billing countries once at mount.
@@ -1507,6 +1533,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1507
1533
  this.#country,
1508
1534
  this.#offer.product?.variant ?? ""
1509
1535
  );
1536
+ console.log("[web-apple-pay] #computeBillingCountries:", this.#supportedBillingCountries);
1510
1537
  }
1511
1538
  /**
1512
1539
  * Returns `true` (hide) when the wallet BFF reports Apple Pay is unavailable
@@ -1516,6 +1543,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1516
1543
  this.#paymentOptions = await this.#fetchPaymentOptions();
1517
1544
  console.log("[web-apple-pay] #fetchAndCacheOptions: fetched payment options", { paymentOptions: this.#paymentOptions });
1518
1545
  if (!this.#paymentOptions?.options?.apple_pay) {
1546
+ console.log("[web-apple-pay] #fetchAndCacheOptions: apple_pay not in options \u2192 hiding");
1519
1547
  this.style.display = "none";
1520
1548
  return true;
1521
1549
  }
@@ -1523,6 +1551,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1523
1551
  }
1524
1552
  /** Injects the button template and wires click and confirm event listeners. */
1525
1553
  #renderTemplate() {
1554
+ console.log("[web-apple-pay] #renderTemplate: rendering Apple Pay button", { skuId: this.#offer?.sku_id, country: this.#country });
1526
1555
  this.style.removeProperty("display");
1527
1556
  ensureApplePayLogoSymbol();
1528
1557
  const template = document.createElement("template");
@@ -1537,10 +1566,11 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1537
1566
  button.addEventListener("click", () => {
1538
1567
  this.#openModal();
1539
1568
  });
1540
- this.addEventListener("apple-pay-confirmed", (event) => {
1569
+ document.addEventListener("apple-pay-confirmed", (event) => {
1541
1570
  const { skuId, country } = event.detail;
1571
+ console.log("[web-apple-pay] apple-pay-confirmed received on document", { skuId, country });
1542
1572
  this.#initiateApplePay(skuId, country);
1543
- });
1573
+ }, { once: true });
1544
1574
  }
1545
1575
  // ─── Private helpers ────────────────────────────────────────────────────────
1546
1576
  #updateDisabledState() {
@@ -1555,6 +1585,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1555
1585
  button.setAttribute("aria-disabled", "true");
1556
1586
  }
1557
1587
  #openModal() {
1588
+ console.log("[web-apple-pay] #openModal: button clicked", { skuId: this.#offer?.sku_id });
1558
1589
  if (!this.#offer || document.querySelector(ApplePayModal.tag)) return;
1559
1590
  trackExpressWalletClick(this.#offer.sku_id);
1560
1591
  ApplePayModal.define();
@@ -1568,24 +1599,31 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1568
1599
  }
1569
1600
  async #fetchSession() {
1570
1601
  try {
1571
- return await getExpressCheckoutSession();
1572
- } catch {
1602
+ const session = await getExpressCheckoutSession();
1603
+ console.log("[web-apple-pay] #fetchSession: success", { loggedIn: session.loggedIn, userType: session.userType });
1604
+ return session;
1605
+ } catch (err) {
1606
+ console.warn("[web-apple-pay] #fetchSession: failed, falling back to anonymous session", err);
1573
1607
  return null;
1574
1608
  }
1575
1609
  }
1576
1610
  async #fetchPaymentOptions() {
1577
1611
  try {
1578
- return await getPaymentOptions({
1612
+ const options = await getPaymentOptions({
1579
1613
  sku: this.#offer.sku_id,
1580
1614
  countryCode: this.#country,
1581
1615
  currencyCode: this.#offer.price?.currency_code ?? DEFAULT_CURRENCY,
1582
1616
  checkoutUrl: this.#returnUrl
1583
1617
  });
1584
- } catch {
1618
+ console.log("[web-apple-pay] #fetchPaymentOptions: success", { hasApplePay: !!options?.options?.apple_pay });
1619
+ return options;
1620
+ } catch (err) {
1621
+ console.warn("[web-apple-pay] #fetchPaymentOptions: failed", err);
1585
1622
  return null;
1586
1623
  }
1587
1624
  }
1588
1625
  #buildSessionParams(skuId, country, sessionData, paymentOptions, recaptchaTokens) {
1626
+ console.log("[web-apple-pay] #buildSessionParams", { skuId, country, userLoggedIn: sessionData.loggedIn });
1589
1627
  const offer = this.#offer;
1590
1628
  const totalAmount = offer.price?.purchase ? `${offer.price.purchase}` : DEFAULT_TOTAL;
1591
1629
  return {
@@ -1608,6 +1646,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1608
1646
  };
1609
1647
  }
1610
1648
  async #initiateApplePay(skuId, country) {
1649
+ console.log("[web-apple-pay] #initiateApplePay: starting", { skuId, country });
1611
1650
  try {
1612
1651
  const sessionData = this.#sessionData ?? _ApplePayButton.#ANONYMOUS_SESSION;
1613
1652
  const paymentOptions = this.#paymentOptions;
@@ -307,7 +307,9 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
307
307
  this.#country = value;
308
308
  }
309
309
  connectedCallback() {
310
+ console.log("[web-apple-pay] ApplePayModal connectedCallback");
310
311
  if (this.querySelector(".apm-overlay")) {
312
+ console.log("[web-apple-pay] ApplePayModal connectedCallback: already rendered, skipping");
311
313
  return;
312
314
  }
313
315
  ensureApplePayLogoSymbol();
@@ -321,6 +323,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
321
323
  this.remove();
322
324
  return;
323
325
  }
326
+ console.log("[web-apple-pay] ApplePayModal: rendering for offer", { skuId: offer.sku_id, country });
324
327
  const offerNameElement = this.querySelector(
325
328
  "#apple-pay-modal-offer-name"
326
329
  );
@@ -393,11 +396,14 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
393
396
  });
394
397
  }
395
398
  cta?.addEventListener("click", () => {
399
+ console.log("[web-apple-pay] ApplePayModal: CTA clicked", { skuId: offer.sku_id, country, isUS });
396
400
  if (isUS && checkbox && !checkbox.checked) {
401
+ console.log("[web-apple-pay] ApplePayModal: T&C checkbox not accepted, showing error");
397
402
  errorElement?.classList.add("apm__error--visible");
398
403
  checkboxLabel?.classList.add("apm__checkbox-label--error");
399
404
  return;
400
405
  }
406
+ console.log("[web-apple-pay] ApplePayModal: dispatching apple-pay-confirmed", { skuId: offer.sku_id, country });
401
407
  this.dispatchEvent(
402
408
  new CustomEvent("apple-pay-confirmed", {
403
409
  bubbles: true,
@@ -405,7 +411,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
405
411
  detail: { skuId: offer.sku_id, country }
406
412
  })
407
413
  );
408
- this.close();
414
+ console.log("[web-apple-pay] ApplePayModal: dispatching apple-pay-confirmed-final", { skuId: offer.sku_id, country });
409
415
  });
410
416
  this.querySelector("#apple-pay-modal-close")?.addEventListener(
411
417
  "click",
@@ -413,6 +419,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
413
419
  );
414
420
  }
415
421
  open() {
422
+ console.log("[web-apple-pay] ApplePayModal: open()");
416
423
  this.#triggerElement = document.activeElement;
417
424
  document.body.appendChild(this);
418
425
  this.#keyHandler = (event) => {
@@ -427,6 +434,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
427
434
  this.querySelector("#apple-pay-modal")?.focus();
428
435
  }
429
436
  close() {
437
+ console.log("[web-apple-pay] ApplePayModal: close()");
430
438
  if (this.#keyHandler) {
431
439
  document.removeEventListener("keydown", this.#keyHandler, {
432
440
  capture: true
@@ -1340,11 +1348,19 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1340
1348
  * Creates and configures an ApplePayButton if the feature flag is present.
1341
1349
  */
1342
1350
  static createIfEnabled(offer, country, entryPoint) {
1343
- if (!offer) return null;
1344
- if (APPLE_PAY_EXCLUDED_VARIANTS.includes(offer.product?.variant))
1351
+ if (!offer) {
1352
+ console.log("[web-apple-pay] createIfEnabled: no offer provided \u2192 null");
1345
1353
  return null;
1346
- if (!new URLSearchParams(window.location.search).has(FEATURE_APPLE_PAY_EXPRESS_CHECKOUT))
1354
+ }
1355
+ if (APPLE_PAY_EXCLUDED_VARIANTS.includes(offer.product?.variant)) {
1356
+ console.log("[web-apple-pay] createIfEnabled: excluded variant \u2192 null", { variant: offer.product?.variant });
1347
1357
  return null;
1358
+ }
1359
+ if (!new URLSearchParams(window.location.search).has(FEATURE_APPLE_PAY_EXPRESS_CHECKOUT)) {
1360
+ console.log("[web-apple-pay] createIfEnabled: feature flag absent \u2192 null");
1361
+ return null;
1362
+ }
1363
+ console.log("[web-apple-pay] createIfEnabled: creating button", { skuId: offer.sku_id, country, entryPoint });
1348
1364
  _ApplePayButton.define();
1349
1365
  const button = document.createElement(_ApplePayButton.tag);
1350
1366
  button.offer = offer;
@@ -1412,7 +1428,9 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1412
1428
  }
1413
1429
  // ─── Lifecycle ──────────────────────────────────────────────────────────────
1414
1430
  async connectedCallback() {
1431
+ console.log("[web-apple-pay] connectedCallback: element connected");
1415
1432
  if (this.#rendering || this.querySelector(SEL_CONTAINER)) {
1433
+ console.log("[web-apple-pay] connectedCallback: already rendering or rendered, skipping");
1416
1434
  return;
1417
1435
  }
1418
1436
  this.#rendering = true;
@@ -1439,6 +1457,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1439
1457
  }
1440
1458
  // ─── Render pipeline ────────────────────────────────────────────────────────
1441
1459
  async #run() {
1460
+ console.log("[web-apple-pay] #run: starting render pipeline");
1442
1461
  try {
1443
1462
  if (await this.#checkAvailability()) return;
1444
1463
  if (await this.#checkSession()) return;
@@ -1457,9 +1476,11 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1457
1476
  async #checkAvailability() {
1458
1477
  const available = await isApplePayAvailable();
1459
1478
  if (!available) {
1479
+ console.log("[web-apple-pay] #checkAvailability: Apple Pay unavailable \u2192 hiding");
1460
1480
  this.style.display = "none";
1461
1481
  return true;
1462
1482
  }
1483
+ console.log("[web-apple-pay] #checkAvailability: Apple Pay available");
1463
1484
  return false;
1464
1485
  }
1465
1486
  /**
@@ -1468,7 +1489,9 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1468
1489
  */
1469
1490
  async #checkSession() {
1470
1491
  this.#sessionData = await this.#fetchSession();
1492
+ console.log("[web-apple-pay] #checkSession: session fetched", { loggedIn: this.#sessionData?.loggedIn, userType: this.#sessionData?.userType });
1471
1493
  if (this.#sessionData?.loggedIn && (this.#sessionData.userType === "active-subscription" || this.#sessionData.userType === "b2b-user")) {
1494
+ console.log("[web-apple-pay] #checkSession: ineligible user \u2192 hiding", { userType: this.#sessionData.userType });
1472
1495
  this.style.display = "none";
1473
1496
  return true;
1474
1497
  }
@@ -1477,14 +1500,17 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1477
1500
  /** Returns `true` (hide) when no offer has been set before mount. */
1478
1501
  #checkOffer() {
1479
1502
  if (!this.#offer) {
1503
+ console.log("[web-apple-pay] #checkOffer: no offer set \u2192 hiding");
1480
1504
  this.style.display = "none";
1481
1505
  return true;
1482
1506
  }
1507
+ console.log("[web-apple-pay] #checkOffer: offer present", { skuId: this.#offer.sku_id });
1483
1508
  return false;
1484
1509
  }
1485
1510
  /** Captures the current page URL once at mount for reuse across the session lifetime. */
1486
1511
  #captureReturnUrl() {
1487
1512
  this.#returnUrl = captureOriginUrl();
1513
+ console.log("[web-apple-pay] #captureReturnUrl:", this.#returnUrl);
1488
1514
  }
1489
1515
  /**
1490
1516
  * Computes supported billing countries once at mount.
@@ -1496,6 +1522,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1496
1522
  this.#country,
1497
1523
  this.#offer.product?.variant ?? ""
1498
1524
  );
1525
+ console.log("[web-apple-pay] #computeBillingCountries:", this.#supportedBillingCountries);
1499
1526
  }
1500
1527
  /**
1501
1528
  * Returns `true` (hide) when the wallet BFF reports Apple Pay is unavailable
@@ -1505,6 +1532,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1505
1532
  this.#paymentOptions = await this.#fetchPaymentOptions();
1506
1533
  console.log("[web-apple-pay] #fetchAndCacheOptions: fetched payment options", { paymentOptions: this.#paymentOptions });
1507
1534
  if (!this.#paymentOptions?.options?.apple_pay) {
1535
+ console.log("[web-apple-pay] #fetchAndCacheOptions: apple_pay not in options \u2192 hiding");
1508
1536
  this.style.display = "none";
1509
1537
  return true;
1510
1538
  }
@@ -1512,6 +1540,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1512
1540
  }
1513
1541
  /** Injects the button template and wires click and confirm event listeners. */
1514
1542
  #renderTemplate() {
1543
+ console.log("[web-apple-pay] #renderTemplate: rendering Apple Pay button", { skuId: this.#offer?.sku_id, country: this.#country });
1515
1544
  this.style.removeProperty("display");
1516
1545
  ensureApplePayLogoSymbol();
1517
1546
  const template = document.createElement("template");
@@ -1526,10 +1555,11 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1526
1555
  button.addEventListener("click", () => {
1527
1556
  this.#openModal();
1528
1557
  });
1529
- this.addEventListener("apple-pay-confirmed", (event) => {
1558
+ document.addEventListener("apple-pay-confirmed", (event) => {
1530
1559
  const { skuId, country } = event.detail;
1560
+ console.log("[web-apple-pay] apple-pay-confirmed received on document", { skuId, country });
1531
1561
  this.#initiateApplePay(skuId, country);
1532
- });
1562
+ }, { once: true });
1533
1563
  }
1534
1564
  // ─── Private helpers ────────────────────────────────────────────────────────
1535
1565
  #updateDisabledState() {
@@ -1544,6 +1574,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1544
1574
  button.setAttribute("aria-disabled", "true");
1545
1575
  }
1546
1576
  #openModal() {
1577
+ console.log("[web-apple-pay] #openModal: button clicked", { skuId: this.#offer?.sku_id });
1547
1578
  if (!this.#offer || document.querySelector(ApplePayModal.tag)) return;
1548
1579
  trackExpressWalletClick(this.#offer.sku_id);
1549
1580
  ApplePayModal.define();
@@ -1557,24 +1588,31 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1557
1588
  }
1558
1589
  async #fetchSession() {
1559
1590
  try {
1560
- return await getExpressCheckoutSession();
1561
- } catch {
1591
+ const session = await getExpressCheckoutSession();
1592
+ console.log("[web-apple-pay] #fetchSession: success", { loggedIn: session.loggedIn, userType: session.userType });
1593
+ return session;
1594
+ } catch (err) {
1595
+ console.warn("[web-apple-pay] #fetchSession: failed, falling back to anonymous session", err);
1562
1596
  return null;
1563
1597
  }
1564
1598
  }
1565
1599
  async #fetchPaymentOptions() {
1566
1600
  try {
1567
- return await getPaymentOptions({
1601
+ const options = await getPaymentOptions({
1568
1602
  sku: this.#offer.sku_id,
1569
1603
  countryCode: this.#country,
1570
1604
  currencyCode: this.#offer.price?.currency_code ?? DEFAULT_CURRENCY,
1571
1605
  checkoutUrl: this.#returnUrl
1572
1606
  });
1573
- } catch {
1607
+ console.log("[web-apple-pay] #fetchPaymentOptions: success", { hasApplePay: !!options?.options?.apple_pay });
1608
+ return options;
1609
+ } catch (err) {
1610
+ console.warn("[web-apple-pay] #fetchPaymentOptions: failed", err);
1574
1611
  return null;
1575
1612
  }
1576
1613
  }
1577
1614
  #buildSessionParams(skuId, country, sessionData, paymentOptions, recaptchaTokens) {
1615
+ console.log("[web-apple-pay] #buildSessionParams", { skuId, country, userLoggedIn: sessionData.loggedIn });
1578
1616
  const offer = this.#offer;
1579
1617
  const totalAmount = offer.price?.purchase ? `${offer.price.purchase}` : DEFAULT_TOTAL;
1580
1618
  return {
@@ -1597,6 +1635,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1597
1635
  };
1598
1636
  }
1599
1637
  async #initiateApplePay(skuId, country) {
1638
+ console.log("[web-apple-pay] #initiateApplePay: starting", { skuId, country });
1600
1639
  try {
1601
1640
  const sessionData = this.#sessionData ?? _ApplePayButton.#ANONYMOUS_SESSION;
1602
1641
  const paymentOptions = this.#paymentOptions;
@@ -307,7 +307,9 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
307
307
  this.#country = value;
308
308
  }
309
309
  connectedCallback() {
310
+ console.log("[web-apple-pay] ApplePayModal connectedCallback");
310
311
  if (this.querySelector(".apm-overlay")) {
312
+ console.log("[web-apple-pay] ApplePayModal connectedCallback: already rendered, skipping");
311
313
  return;
312
314
  }
313
315
  ensureApplePayLogoSymbol();
@@ -321,6 +323,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
321
323
  this.remove();
322
324
  return;
323
325
  }
326
+ console.log("[web-apple-pay] ApplePayModal: rendering for offer", { skuId: offer.sku_id, country });
324
327
  const offerNameElement = this.querySelector(
325
328
  "#apple-pay-modal-offer-name"
326
329
  );
@@ -393,11 +396,14 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
393
396
  });
394
397
  }
395
398
  cta?.addEventListener("click", () => {
399
+ console.log("[web-apple-pay] ApplePayModal: CTA clicked", { skuId: offer.sku_id, country, isUS });
396
400
  if (isUS && checkbox && !checkbox.checked) {
401
+ console.log("[web-apple-pay] ApplePayModal: T&C checkbox not accepted, showing error");
397
402
  errorElement?.classList.add("apm__error--visible");
398
403
  checkboxLabel?.classList.add("apm__checkbox-label--error");
399
404
  return;
400
405
  }
406
+ console.log("[web-apple-pay] ApplePayModal: dispatching apple-pay-confirmed", { skuId: offer.sku_id, country });
401
407
  this.dispatchEvent(
402
408
  new CustomEvent("apple-pay-confirmed", {
403
409
  bubbles: true,
@@ -405,7 +411,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
405
411
  detail: { skuId: offer.sku_id, country }
406
412
  })
407
413
  );
408
- this.close();
414
+ console.log("[web-apple-pay] ApplePayModal: dispatching apple-pay-confirmed-final", { skuId: offer.sku_id, country });
409
415
  });
410
416
  this.querySelector("#apple-pay-modal-close")?.addEventListener(
411
417
  "click",
@@ -413,6 +419,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
413
419
  );
414
420
  }
415
421
  open() {
422
+ console.log("[web-apple-pay] ApplePayModal: open()");
416
423
  this.#triggerElement = document.activeElement;
417
424
  document.body.appendChild(this);
418
425
  this.#keyHandler = (event) => {
@@ -427,6 +434,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
427
434
  this.querySelector("#apple-pay-modal")?.focus();
428
435
  }
429
436
  close() {
437
+ console.log("[web-apple-pay] ApplePayModal: close()");
430
438
  if (this.#keyHandler) {
431
439
  document.removeEventListener("keydown", this.#keyHandler, {
432
440
  capture: true
@@ -1355,11 +1363,19 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1355
1363
  * Creates and configures an ApplePayButton if the feature flag is present.
1356
1364
  */
1357
1365
  static createIfEnabled(offer, country, entryPoint) {
1358
- if (!offer) return null;
1359
- if (APPLE_PAY_EXCLUDED_VARIANTS.includes(offer.product?.variant))
1366
+ if (!offer) {
1367
+ console.log("[web-apple-pay] createIfEnabled: no offer provided \u2192 null");
1360
1368
  return null;
1361
- if (!new URLSearchParams(window.location.search).has(FEATURE_APPLE_PAY_EXPRESS_CHECKOUT))
1369
+ }
1370
+ if (APPLE_PAY_EXCLUDED_VARIANTS.includes(offer.product?.variant)) {
1371
+ console.log("[web-apple-pay] createIfEnabled: excluded variant \u2192 null", { variant: offer.product?.variant });
1362
1372
  return null;
1373
+ }
1374
+ if (!new URLSearchParams(window.location.search).has(FEATURE_APPLE_PAY_EXPRESS_CHECKOUT)) {
1375
+ console.log("[web-apple-pay] createIfEnabled: feature flag absent \u2192 null");
1376
+ return null;
1377
+ }
1378
+ console.log("[web-apple-pay] createIfEnabled: creating button", { skuId: offer.sku_id, country, entryPoint });
1363
1379
  _ApplePayButton.define();
1364
1380
  const button = document.createElement(_ApplePayButton.tag);
1365
1381
  button.offer = offer;
@@ -1427,7 +1443,9 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1427
1443
  }
1428
1444
  // ─── Lifecycle ──────────────────────────────────────────────────────────────
1429
1445
  async connectedCallback() {
1446
+ console.log("[web-apple-pay] connectedCallback: element connected");
1430
1447
  if (this.#rendering || this.querySelector(SEL_CONTAINER)) {
1448
+ console.log("[web-apple-pay] connectedCallback: already rendering or rendered, skipping");
1431
1449
  return;
1432
1450
  }
1433
1451
  this.#rendering = true;
@@ -1454,6 +1472,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1454
1472
  }
1455
1473
  // ─── Render pipeline ────────────────────────────────────────────────────────
1456
1474
  async #run() {
1475
+ console.log("[web-apple-pay] #run: starting render pipeline");
1457
1476
  try {
1458
1477
  if (await this.#checkAvailability()) return;
1459
1478
  if (await this.#checkSession()) return;
@@ -1472,9 +1491,11 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1472
1491
  async #checkAvailability() {
1473
1492
  const available = await isApplePayAvailable();
1474
1493
  if (!available) {
1494
+ console.log("[web-apple-pay] #checkAvailability: Apple Pay unavailable \u2192 hiding");
1475
1495
  this.style.display = "none";
1476
1496
  return true;
1477
1497
  }
1498
+ console.log("[web-apple-pay] #checkAvailability: Apple Pay available");
1478
1499
  return false;
1479
1500
  }
1480
1501
  /**
@@ -1483,7 +1504,9 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1483
1504
  */
1484
1505
  async #checkSession() {
1485
1506
  this.#sessionData = await this.#fetchSession();
1507
+ console.log("[web-apple-pay] #checkSession: session fetched", { loggedIn: this.#sessionData?.loggedIn, userType: this.#sessionData?.userType });
1486
1508
  if (this.#sessionData?.loggedIn && (this.#sessionData.userType === "active-subscription" || this.#sessionData.userType === "b2b-user")) {
1509
+ console.log("[web-apple-pay] #checkSession: ineligible user \u2192 hiding", { userType: this.#sessionData.userType });
1487
1510
  this.style.display = "none";
1488
1511
  return true;
1489
1512
  }
@@ -1492,14 +1515,17 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1492
1515
  /** Returns `true` (hide) when no offer has been set before mount. */
1493
1516
  #checkOffer() {
1494
1517
  if (!this.#offer) {
1518
+ console.log("[web-apple-pay] #checkOffer: no offer set \u2192 hiding");
1495
1519
  this.style.display = "none";
1496
1520
  return true;
1497
1521
  }
1522
+ console.log("[web-apple-pay] #checkOffer: offer present", { skuId: this.#offer.sku_id });
1498
1523
  return false;
1499
1524
  }
1500
1525
  /** Captures the current page URL once at mount for reuse across the session lifetime. */
1501
1526
  #captureReturnUrl() {
1502
1527
  this.#returnUrl = captureOriginUrl();
1528
+ console.log("[web-apple-pay] #captureReturnUrl:", this.#returnUrl);
1503
1529
  }
1504
1530
  /**
1505
1531
  * Computes supported billing countries once at mount.
@@ -1511,6 +1537,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1511
1537
  this.#country,
1512
1538
  this.#offer.product?.variant ?? ""
1513
1539
  );
1540
+ console.log("[web-apple-pay] #computeBillingCountries:", this.#supportedBillingCountries);
1514
1541
  }
1515
1542
  /**
1516
1543
  * Returns `true` (hide) when the wallet BFF reports Apple Pay is unavailable
@@ -1520,6 +1547,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1520
1547
  this.#paymentOptions = await this.#fetchPaymentOptions();
1521
1548
  console.log("[web-apple-pay] #fetchAndCacheOptions: fetched payment options", { paymentOptions: this.#paymentOptions });
1522
1549
  if (!this.#paymentOptions?.options?.apple_pay) {
1550
+ console.log("[web-apple-pay] #fetchAndCacheOptions: apple_pay not in options \u2192 hiding");
1523
1551
  this.style.display = "none";
1524
1552
  return true;
1525
1553
  }
@@ -1527,6 +1555,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1527
1555
  }
1528
1556
  /** Injects the button template and wires click and confirm event listeners. */
1529
1557
  #renderTemplate() {
1558
+ console.log("[web-apple-pay] #renderTemplate: rendering Apple Pay button", { skuId: this.#offer?.sku_id, country: this.#country });
1530
1559
  this.style.removeProperty("display");
1531
1560
  ensureApplePayLogoSymbol();
1532
1561
  const template = document.createElement("template");
@@ -1541,10 +1570,11 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1541
1570
  button.addEventListener("click", () => {
1542
1571
  this.#openModal();
1543
1572
  });
1544
- this.addEventListener("apple-pay-confirmed", (event) => {
1573
+ document.addEventListener("apple-pay-confirmed", (event) => {
1545
1574
  const { skuId, country } = event.detail;
1575
+ console.log("[web-apple-pay] apple-pay-confirmed received on document", { skuId, country });
1546
1576
  this.#initiateApplePay(skuId, country);
1547
- });
1577
+ }, { once: true });
1548
1578
  }
1549
1579
  // ─── Private helpers ────────────────────────────────────────────────────────
1550
1580
  #updateDisabledState() {
@@ -1559,6 +1589,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1559
1589
  button.setAttribute("aria-disabled", "true");
1560
1590
  }
1561
1591
  #openModal() {
1592
+ console.log("[web-apple-pay] #openModal: button clicked", { skuId: this.#offer?.sku_id });
1562
1593
  if (!this.#offer || document.querySelector(ApplePayModal.tag)) return;
1563
1594
  trackExpressWalletClick(this.#offer.sku_id);
1564
1595
  ApplePayModal.define();
@@ -1572,24 +1603,31 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1572
1603
  }
1573
1604
  async #fetchSession() {
1574
1605
  try {
1575
- return await getExpressCheckoutSession();
1576
- } catch {
1606
+ const session = await getExpressCheckoutSession();
1607
+ console.log("[web-apple-pay] #fetchSession: success", { loggedIn: session.loggedIn, userType: session.userType });
1608
+ return session;
1609
+ } catch (err) {
1610
+ console.warn("[web-apple-pay] #fetchSession: failed, falling back to anonymous session", err);
1577
1611
  return null;
1578
1612
  }
1579
1613
  }
1580
1614
  async #fetchPaymentOptions() {
1581
1615
  try {
1582
- return await getPaymentOptions({
1616
+ const options = await getPaymentOptions({
1583
1617
  sku: this.#offer.sku_id,
1584
1618
  countryCode: this.#country,
1585
1619
  currencyCode: this.#offer.price?.currency_code ?? DEFAULT_CURRENCY,
1586
1620
  checkoutUrl: this.#returnUrl
1587
1621
  });
1588
- } catch {
1622
+ console.log("[web-apple-pay] #fetchPaymentOptions: success", { hasApplePay: !!options?.options?.apple_pay });
1623
+ return options;
1624
+ } catch (err) {
1625
+ console.warn("[web-apple-pay] #fetchPaymentOptions: failed", err);
1589
1626
  return null;
1590
1627
  }
1591
1628
  }
1592
1629
  #buildSessionParams(skuId, country, sessionData, paymentOptions, recaptchaTokens) {
1630
+ console.log("[web-apple-pay] #buildSessionParams", { skuId, country, userLoggedIn: sessionData.loggedIn });
1593
1631
  const offer = this.#offer;
1594
1632
  const totalAmount = offer.price?.purchase ? `${offer.price.purchase}` : DEFAULT_TOTAL;
1595
1633
  return {
@@ -1612,6 +1650,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1612
1650
  };
1613
1651
  }
1614
1652
  async #initiateApplePay(skuId, country) {
1653
+ console.log("[web-apple-pay] #initiateApplePay: starting", { skuId, country });
1615
1654
  try {
1616
1655
  const sessionData = this.#sessionData ?? _ApplePayButton.#ANONYMOUS_SESSION;
1617
1656
  const paymentOptions = this.#paymentOptions;
@@ -307,7 +307,9 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
307
307
  this.#country = value;
308
308
  }
309
309
  connectedCallback() {
310
+ console.log("[web-apple-pay] ApplePayModal connectedCallback");
310
311
  if (this.querySelector(".apm-overlay")) {
312
+ console.log("[web-apple-pay] ApplePayModal connectedCallback: already rendered, skipping");
311
313
  return;
312
314
  }
313
315
  ensureApplePayLogoSymbol();
@@ -321,6 +323,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
321
323
  this.remove();
322
324
  return;
323
325
  }
326
+ console.log("[web-apple-pay] ApplePayModal: rendering for offer", { skuId: offer.sku_id, country });
324
327
  const offerNameElement = this.querySelector(
325
328
  "#apple-pay-modal-offer-name"
326
329
  );
@@ -393,11 +396,14 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
393
396
  });
394
397
  }
395
398
  cta?.addEventListener("click", () => {
399
+ console.log("[web-apple-pay] ApplePayModal: CTA clicked", { skuId: offer.sku_id, country, isUS });
396
400
  if (isUS && checkbox && !checkbox.checked) {
401
+ console.log("[web-apple-pay] ApplePayModal: T&C checkbox not accepted, showing error");
397
402
  errorElement?.classList.add("apm__error--visible");
398
403
  checkboxLabel?.classList.add("apm__checkbox-label--error");
399
404
  return;
400
405
  }
406
+ console.log("[web-apple-pay] ApplePayModal: dispatching apple-pay-confirmed", { skuId: offer.sku_id, country });
401
407
  this.dispatchEvent(
402
408
  new CustomEvent("apple-pay-confirmed", {
403
409
  bubbles: true,
@@ -405,7 +411,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
405
411
  detail: { skuId: offer.sku_id, country }
406
412
  })
407
413
  );
408
- this.close();
414
+ console.log("[web-apple-pay] ApplePayModal: dispatching apple-pay-confirmed-final", { skuId: offer.sku_id, country });
409
415
  });
410
416
  this.querySelector("#apple-pay-modal-close")?.addEventListener(
411
417
  "click",
@@ -413,6 +419,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
413
419
  );
414
420
  }
415
421
  open() {
422
+ console.log("[web-apple-pay] ApplePayModal: open()");
416
423
  this.#triggerElement = document.activeElement;
417
424
  document.body.appendChild(this);
418
425
  this.#keyHandler = (event) => {
@@ -427,6 +434,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
427
434
  this.querySelector("#apple-pay-modal")?.focus();
428
435
  }
429
436
  close() {
437
+ console.log("[web-apple-pay] ApplePayModal: close()");
430
438
  if (this.#keyHandler) {
431
439
  document.removeEventListener("keydown", this.#keyHandler, {
432
440
  capture: true
@@ -307,7 +307,9 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
307
307
  this.#country = value;
308
308
  }
309
309
  connectedCallback() {
310
+ console.log("[web-apple-pay] ApplePayModal connectedCallback");
310
311
  if (this.querySelector(".apm-overlay")) {
312
+ console.log("[web-apple-pay] ApplePayModal connectedCallback: already rendered, skipping");
311
313
  return;
312
314
  }
313
315
  ensureApplePayLogoSymbol();
@@ -321,6 +323,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
321
323
  this.remove();
322
324
  return;
323
325
  }
326
+ console.log("[web-apple-pay] ApplePayModal: rendering for offer", { skuId: offer.sku_id, country });
324
327
  const offerNameElement = this.querySelector(
325
328
  "#apple-pay-modal-offer-name"
326
329
  );
@@ -393,11 +396,14 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
393
396
  });
394
397
  }
395
398
  cta?.addEventListener("click", () => {
399
+ console.log("[web-apple-pay] ApplePayModal: CTA clicked", { skuId: offer.sku_id, country, isUS });
396
400
  if (isUS && checkbox && !checkbox.checked) {
401
+ console.log("[web-apple-pay] ApplePayModal: T&C checkbox not accepted, showing error");
397
402
  errorElement?.classList.add("apm__error--visible");
398
403
  checkboxLabel?.classList.add("apm__checkbox-label--error");
399
404
  return;
400
405
  }
406
+ console.log("[web-apple-pay] ApplePayModal: dispatching apple-pay-confirmed", { skuId: offer.sku_id, country });
401
407
  this.dispatchEvent(
402
408
  new CustomEvent("apple-pay-confirmed", {
403
409
  bubbles: true,
@@ -405,7 +411,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
405
411
  detail: { skuId: offer.sku_id, country }
406
412
  })
407
413
  );
408
- this.close();
414
+ console.log("[web-apple-pay] ApplePayModal: dispatching apple-pay-confirmed-final", { skuId: offer.sku_id, country });
409
415
  });
410
416
  this.querySelector("#apple-pay-modal-close")?.addEventListener(
411
417
  "click",
@@ -413,6 +419,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
413
419
  );
414
420
  }
415
421
  open() {
422
+ console.log("[web-apple-pay] ApplePayModal: open()");
416
423
  this.#triggerElement = document.activeElement;
417
424
  document.body.appendChild(this);
418
425
  this.#keyHandler = (event) => {
@@ -427,6 +434,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
427
434
  this.querySelector("#apple-pay-modal")?.focus();
428
435
  }
429
436
  close() {
437
+ console.log("[web-apple-pay] ApplePayModal: close()");
430
438
  if (this.#keyHandler) {
431
439
  document.removeEventListener("keydown", this.#keyHandler, {
432
440
  capture: true
@@ -1340,11 +1348,19 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1340
1348
  * Creates and configures an ApplePayButton if the feature flag is present.
1341
1349
  */
1342
1350
  static createIfEnabled(offer, country, entryPoint) {
1343
- if (!offer) return null;
1344
- if (APPLE_PAY_EXCLUDED_VARIANTS.includes(offer.product?.variant))
1351
+ if (!offer) {
1352
+ console.log("[web-apple-pay] createIfEnabled: no offer provided \u2192 null");
1345
1353
  return null;
1346
- if (!new URLSearchParams(window.location.search).has(FEATURE_APPLE_PAY_EXPRESS_CHECKOUT))
1354
+ }
1355
+ if (APPLE_PAY_EXCLUDED_VARIANTS.includes(offer.product?.variant)) {
1356
+ console.log("[web-apple-pay] createIfEnabled: excluded variant \u2192 null", { variant: offer.product?.variant });
1347
1357
  return null;
1358
+ }
1359
+ if (!new URLSearchParams(window.location.search).has(FEATURE_APPLE_PAY_EXPRESS_CHECKOUT)) {
1360
+ console.log("[web-apple-pay] createIfEnabled: feature flag absent \u2192 null");
1361
+ return null;
1362
+ }
1363
+ console.log("[web-apple-pay] createIfEnabled: creating button", { skuId: offer.sku_id, country, entryPoint });
1348
1364
  _ApplePayButton.define();
1349
1365
  const button = document.createElement(_ApplePayButton.tag);
1350
1366
  button.offer = offer;
@@ -1412,7 +1428,9 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1412
1428
  }
1413
1429
  // ─── Lifecycle ──────────────────────────────────────────────────────────────
1414
1430
  async connectedCallback() {
1431
+ console.log("[web-apple-pay] connectedCallback: element connected");
1415
1432
  if (this.#rendering || this.querySelector(SEL_CONTAINER)) {
1433
+ console.log("[web-apple-pay] connectedCallback: already rendering or rendered, skipping");
1416
1434
  return;
1417
1435
  }
1418
1436
  this.#rendering = true;
@@ -1439,6 +1457,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1439
1457
  }
1440
1458
  // ─── Render pipeline ────────────────────────────────────────────────────────
1441
1459
  async #run() {
1460
+ console.log("[web-apple-pay] #run: starting render pipeline");
1442
1461
  try {
1443
1462
  if (await this.#checkAvailability()) return;
1444
1463
  if (await this.#checkSession()) return;
@@ -1457,9 +1476,11 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1457
1476
  async #checkAvailability() {
1458
1477
  const available = await isApplePayAvailable();
1459
1478
  if (!available) {
1479
+ console.log("[web-apple-pay] #checkAvailability: Apple Pay unavailable \u2192 hiding");
1460
1480
  this.style.display = "none";
1461
1481
  return true;
1462
1482
  }
1483
+ console.log("[web-apple-pay] #checkAvailability: Apple Pay available");
1463
1484
  return false;
1464
1485
  }
1465
1486
  /**
@@ -1468,7 +1489,9 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1468
1489
  */
1469
1490
  async #checkSession() {
1470
1491
  this.#sessionData = await this.#fetchSession();
1492
+ console.log("[web-apple-pay] #checkSession: session fetched", { loggedIn: this.#sessionData?.loggedIn, userType: this.#sessionData?.userType });
1471
1493
  if (this.#sessionData?.loggedIn && (this.#sessionData.userType === "active-subscription" || this.#sessionData.userType === "b2b-user")) {
1494
+ console.log("[web-apple-pay] #checkSession: ineligible user \u2192 hiding", { userType: this.#sessionData.userType });
1472
1495
  this.style.display = "none";
1473
1496
  return true;
1474
1497
  }
@@ -1477,14 +1500,17 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1477
1500
  /** Returns `true` (hide) when no offer has been set before mount. */
1478
1501
  #checkOffer() {
1479
1502
  if (!this.#offer) {
1503
+ console.log("[web-apple-pay] #checkOffer: no offer set \u2192 hiding");
1480
1504
  this.style.display = "none";
1481
1505
  return true;
1482
1506
  }
1507
+ console.log("[web-apple-pay] #checkOffer: offer present", { skuId: this.#offer.sku_id });
1483
1508
  return false;
1484
1509
  }
1485
1510
  /** Captures the current page URL once at mount for reuse across the session lifetime. */
1486
1511
  #captureReturnUrl() {
1487
1512
  this.#returnUrl = captureOriginUrl();
1513
+ console.log("[web-apple-pay] #captureReturnUrl:", this.#returnUrl);
1488
1514
  }
1489
1515
  /**
1490
1516
  * Computes supported billing countries once at mount.
@@ -1496,6 +1522,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1496
1522
  this.#country,
1497
1523
  this.#offer.product?.variant ?? ""
1498
1524
  );
1525
+ console.log("[web-apple-pay] #computeBillingCountries:", this.#supportedBillingCountries);
1499
1526
  }
1500
1527
  /**
1501
1528
  * Returns `true` (hide) when the wallet BFF reports Apple Pay is unavailable
@@ -1505,6 +1532,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1505
1532
  this.#paymentOptions = await this.#fetchPaymentOptions();
1506
1533
  console.log("[web-apple-pay] #fetchAndCacheOptions: fetched payment options", { paymentOptions: this.#paymentOptions });
1507
1534
  if (!this.#paymentOptions?.options?.apple_pay) {
1535
+ console.log("[web-apple-pay] #fetchAndCacheOptions: apple_pay not in options \u2192 hiding");
1508
1536
  this.style.display = "none";
1509
1537
  return true;
1510
1538
  }
@@ -1512,6 +1540,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1512
1540
  }
1513
1541
  /** Injects the button template and wires click and confirm event listeners. */
1514
1542
  #renderTemplate() {
1543
+ console.log("[web-apple-pay] #renderTemplate: rendering Apple Pay button", { skuId: this.#offer?.sku_id, country: this.#country });
1515
1544
  this.style.removeProperty("display");
1516
1545
  ensureApplePayLogoSymbol();
1517
1546
  const template = document.createElement("template");
@@ -1526,10 +1555,11 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1526
1555
  button.addEventListener("click", () => {
1527
1556
  this.#openModal();
1528
1557
  });
1529
- this.addEventListener("apple-pay-confirmed", (event) => {
1558
+ document.addEventListener("apple-pay-confirmed", (event) => {
1530
1559
  const { skuId, country } = event.detail;
1560
+ console.log("[web-apple-pay] apple-pay-confirmed received on document", { skuId, country });
1531
1561
  this.#initiateApplePay(skuId, country);
1532
- });
1562
+ }, { once: true });
1533
1563
  }
1534
1564
  // ─── Private helpers ────────────────────────────────────────────────────────
1535
1565
  #updateDisabledState() {
@@ -1544,6 +1574,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1544
1574
  button.setAttribute("aria-disabled", "true");
1545
1575
  }
1546
1576
  #openModal() {
1577
+ console.log("[web-apple-pay] #openModal: button clicked", { skuId: this.#offer?.sku_id });
1547
1578
  if (!this.#offer || document.querySelector(ApplePayModal.tag)) return;
1548
1579
  trackExpressWalletClick(this.#offer.sku_id);
1549
1580
  ApplePayModal.define();
@@ -1557,24 +1588,31 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1557
1588
  }
1558
1589
  async #fetchSession() {
1559
1590
  try {
1560
- return await getExpressCheckoutSession();
1561
- } catch {
1591
+ const session = await getExpressCheckoutSession();
1592
+ console.log("[web-apple-pay] #fetchSession: success", { loggedIn: session.loggedIn, userType: session.userType });
1593
+ return session;
1594
+ } catch (err) {
1595
+ console.warn("[web-apple-pay] #fetchSession: failed, falling back to anonymous session", err);
1562
1596
  return null;
1563
1597
  }
1564
1598
  }
1565
1599
  async #fetchPaymentOptions() {
1566
1600
  try {
1567
- return await getPaymentOptions({
1601
+ const options = await getPaymentOptions({
1568
1602
  sku: this.#offer.sku_id,
1569
1603
  countryCode: this.#country,
1570
1604
  currencyCode: this.#offer.price?.currency_code ?? DEFAULT_CURRENCY,
1571
1605
  checkoutUrl: this.#returnUrl
1572
1606
  });
1573
- } catch {
1607
+ console.log("[web-apple-pay] #fetchPaymentOptions: success", { hasApplePay: !!options?.options?.apple_pay });
1608
+ return options;
1609
+ } catch (err) {
1610
+ console.warn("[web-apple-pay] #fetchPaymentOptions: failed", err);
1574
1611
  return null;
1575
1612
  }
1576
1613
  }
1577
1614
  #buildSessionParams(skuId, country, sessionData, paymentOptions, recaptchaTokens) {
1615
+ console.log("[web-apple-pay] #buildSessionParams", { skuId, country, userLoggedIn: sessionData.loggedIn });
1578
1616
  const offer = this.#offer;
1579
1617
  const totalAmount = offer.price?.purchase ? `${offer.price.purchase}` : DEFAULT_TOTAL;
1580
1618
  return {
@@ -1597,6 +1635,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1597
1635
  };
1598
1636
  }
1599
1637
  async #initiateApplePay(skuId, country) {
1638
+ console.log("[web-apple-pay] #initiateApplePay: starting", { skuId, country });
1600
1639
  try {
1601
1640
  const sessionData = this.#sessionData ?? _ApplePayButton.#ANONYMOUS_SESSION;
1602
1641
  const paymentOptions = this.#paymentOptions;
package/dist/src/index.js CHANGED
@@ -307,7 +307,9 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
307
307
  this.#country = value;
308
308
  }
309
309
  connectedCallback() {
310
+ console.log("[web-apple-pay] ApplePayModal connectedCallback");
310
311
  if (this.querySelector(".apm-overlay")) {
312
+ console.log("[web-apple-pay] ApplePayModal connectedCallback: already rendered, skipping");
311
313
  return;
312
314
  }
313
315
  ensureApplePayLogoSymbol();
@@ -321,6 +323,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
321
323
  this.remove();
322
324
  return;
323
325
  }
326
+ console.log("[web-apple-pay] ApplePayModal: rendering for offer", { skuId: offer.sku_id, country });
324
327
  const offerNameElement = this.querySelector(
325
328
  "#apple-pay-modal-offer-name"
326
329
  );
@@ -393,11 +396,14 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
393
396
  });
394
397
  }
395
398
  cta?.addEventListener("click", () => {
399
+ console.log("[web-apple-pay] ApplePayModal: CTA clicked", { skuId: offer.sku_id, country, isUS });
396
400
  if (isUS && checkbox && !checkbox.checked) {
401
+ console.log("[web-apple-pay] ApplePayModal: T&C checkbox not accepted, showing error");
397
402
  errorElement?.classList.add("apm__error--visible");
398
403
  checkboxLabel?.classList.add("apm__checkbox-label--error");
399
404
  return;
400
405
  }
406
+ console.log("[web-apple-pay] ApplePayModal: dispatching apple-pay-confirmed", { skuId: offer.sku_id, country });
401
407
  this.dispatchEvent(
402
408
  new CustomEvent("apple-pay-confirmed", {
403
409
  bubbles: true,
@@ -405,7 +411,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
405
411
  detail: { skuId: offer.sku_id, country }
406
412
  })
407
413
  );
408
- this.close();
414
+ console.log("[web-apple-pay] ApplePayModal: dispatching apple-pay-confirmed-final", { skuId: offer.sku_id, country });
409
415
  });
410
416
  this.querySelector("#apple-pay-modal-close")?.addEventListener(
411
417
  "click",
@@ -413,6 +419,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
413
419
  );
414
420
  }
415
421
  open() {
422
+ console.log("[web-apple-pay] ApplePayModal: open()");
416
423
  this.#triggerElement = document.activeElement;
417
424
  document.body.appendChild(this);
418
425
  this.#keyHandler = (event) => {
@@ -427,6 +434,7 @@ var ApplePayModal = class _ApplePayModal extends HTMLElement {
427
434
  this.querySelector("#apple-pay-modal")?.focus();
428
435
  }
429
436
  close() {
437
+ console.log("[web-apple-pay] ApplePayModal: close()");
430
438
  if (this.#keyHandler) {
431
439
  document.removeEventListener("keydown", this.#keyHandler, {
432
440
  capture: true
@@ -1351,11 +1359,19 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1351
1359
  * Creates and configures an ApplePayButton if the feature flag is present.
1352
1360
  */
1353
1361
  static createIfEnabled(offer, country, entryPoint) {
1354
- if (!offer) return null;
1355
- if (APPLE_PAY_EXCLUDED_VARIANTS.includes(offer.product?.variant))
1362
+ if (!offer) {
1363
+ console.log("[web-apple-pay] createIfEnabled: no offer provided \u2192 null");
1356
1364
  return null;
1357
- if (!new URLSearchParams(window.location.search).has(FEATURE_APPLE_PAY_EXPRESS_CHECKOUT))
1365
+ }
1366
+ if (APPLE_PAY_EXCLUDED_VARIANTS.includes(offer.product?.variant)) {
1367
+ console.log("[web-apple-pay] createIfEnabled: excluded variant \u2192 null", { variant: offer.product?.variant });
1358
1368
  return null;
1369
+ }
1370
+ if (!new URLSearchParams(window.location.search).has(FEATURE_APPLE_PAY_EXPRESS_CHECKOUT)) {
1371
+ console.log("[web-apple-pay] createIfEnabled: feature flag absent \u2192 null");
1372
+ return null;
1373
+ }
1374
+ console.log("[web-apple-pay] createIfEnabled: creating button", { skuId: offer.sku_id, country, entryPoint });
1359
1375
  _ApplePayButton.define();
1360
1376
  const button = document.createElement(_ApplePayButton.tag);
1361
1377
  button.offer = offer;
@@ -1423,7 +1439,9 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1423
1439
  }
1424
1440
  // ─── Lifecycle ──────────────────────────────────────────────────────────────
1425
1441
  async connectedCallback() {
1442
+ console.log("[web-apple-pay] connectedCallback: element connected");
1426
1443
  if (this.#rendering || this.querySelector(SEL_CONTAINER)) {
1444
+ console.log("[web-apple-pay] connectedCallback: already rendering or rendered, skipping");
1427
1445
  return;
1428
1446
  }
1429
1447
  this.#rendering = true;
@@ -1450,6 +1468,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1450
1468
  }
1451
1469
  // ─── Render pipeline ────────────────────────────────────────────────────────
1452
1470
  async #run() {
1471
+ console.log("[web-apple-pay] #run: starting render pipeline");
1453
1472
  try {
1454
1473
  if (await this.#checkAvailability()) return;
1455
1474
  if (await this.#checkSession()) return;
@@ -1468,9 +1487,11 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1468
1487
  async #checkAvailability() {
1469
1488
  const available = await isApplePayAvailable();
1470
1489
  if (!available) {
1490
+ console.log("[web-apple-pay] #checkAvailability: Apple Pay unavailable \u2192 hiding");
1471
1491
  this.style.display = "none";
1472
1492
  return true;
1473
1493
  }
1494
+ console.log("[web-apple-pay] #checkAvailability: Apple Pay available");
1474
1495
  return false;
1475
1496
  }
1476
1497
  /**
@@ -1479,7 +1500,9 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1479
1500
  */
1480
1501
  async #checkSession() {
1481
1502
  this.#sessionData = await this.#fetchSession();
1503
+ console.log("[web-apple-pay] #checkSession: session fetched", { loggedIn: this.#sessionData?.loggedIn, userType: this.#sessionData?.userType });
1482
1504
  if (this.#sessionData?.loggedIn && (this.#sessionData.userType === "active-subscription" || this.#sessionData.userType === "b2b-user")) {
1505
+ console.log("[web-apple-pay] #checkSession: ineligible user \u2192 hiding", { userType: this.#sessionData.userType });
1483
1506
  this.style.display = "none";
1484
1507
  return true;
1485
1508
  }
@@ -1488,14 +1511,17 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1488
1511
  /** Returns `true` (hide) when no offer has been set before mount. */
1489
1512
  #checkOffer() {
1490
1513
  if (!this.#offer) {
1514
+ console.log("[web-apple-pay] #checkOffer: no offer set \u2192 hiding");
1491
1515
  this.style.display = "none";
1492
1516
  return true;
1493
1517
  }
1518
+ console.log("[web-apple-pay] #checkOffer: offer present", { skuId: this.#offer.sku_id });
1494
1519
  return false;
1495
1520
  }
1496
1521
  /** Captures the current page URL once at mount for reuse across the session lifetime. */
1497
1522
  #captureReturnUrl() {
1498
1523
  this.#returnUrl = captureOriginUrl();
1524
+ console.log("[web-apple-pay] #captureReturnUrl:", this.#returnUrl);
1499
1525
  }
1500
1526
  /**
1501
1527
  * Computes supported billing countries once at mount.
@@ -1507,6 +1533,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1507
1533
  this.#country,
1508
1534
  this.#offer.product?.variant ?? ""
1509
1535
  );
1536
+ console.log("[web-apple-pay] #computeBillingCountries:", this.#supportedBillingCountries);
1510
1537
  }
1511
1538
  /**
1512
1539
  * Returns `true` (hide) when the wallet BFF reports Apple Pay is unavailable
@@ -1516,6 +1543,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1516
1543
  this.#paymentOptions = await this.#fetchPaymentOptions();
1517
1544
  console.log("[web-apple-pay] #fetchAndCacheOptions: fetched payment options", { paymentOptions: this.#paymentOptions });
1518
1545
  if (!this.#paymentOptions?.options?.apple_pay) {
1546
+ console.log("[web-apple-pay] #fetchAndCacheOptions: apple_pay not in options \u2192 hiding");
1519
1547
  this.style.display = "none";
1520
1548
  return true;
1521
1549
  }
@@ -1523,6 +1551,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1523
1551
  }
1524
1552
  /** Injects the button template and wires click and confirm event listeners. */
1525
1553
  #renderTemplate() {
1554
+ console.log("[web-apple-pay] #renderTemplate: rendering Apple Pay button", { skuId: this.#offer?.sku_id, country: this.#country });
1526
1555
  this.style.removeProperty("display");
1527
1556
  ensureApplePayLogoSymbol();
1528
1557
  const template = document.createElement("template");
@@ -1537,10 +1566,11 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1537
1566
  button.addEventListener("click", () => {
1538
1567
  this.#openModal();
1539
1568
  });
1540
- this.addEventListener("apple-pay-confirmed", (event) => {
1569
+ document.addEventListener("apple-pay-confirmed", (event) => {
1541
1570
  const { skuId, country } = event.detail;
1571
+ console.log("[web-apple-pay] apple-pay-confirmed received on document", { skuId, country });
1542
1572
  this.#initiateApplePay(skuId, country);
1543
- });
1573
+ }, { once: true });
1544
1574
  }
1545
1575
  // ─── Private helpers ────────────────────────────────────────────────────────
1546
1576
  #updateDisabledState() {
@@ -1555,6 +1585,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1555
1585
  button.setAttribute("aria-disabled", "true");
1556
1586
  }
1557
1587
  #openModal() {
1588
+ console.log("[web-apple-pay] #openModal: button clicked", { skuId: this.#offer?.sku_id });
1558
1589
  if (!this.#offer || document.querySelector(ApplePayModal.tag)) return;
1559
1590
  trackExpressWalletClick(this.#offer.sku_id);
1560
1591
  ApplePayModal.define();
@@ -1568,24 +1599,31 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1568
1599
  }
1569
1600
  async #fetchSession() {
1570
1601
  try {
1571
- return await getExpressCheckoutSession();
1572
- } catch {
1602
+ const session = await getExpressCheckoutSession();
1603
+ console.log("[web-apple-pay] #fetchSession: success", { loggedIn: session.loggedIn, userType: session.userType });
1604
+ return session;
1605
+ } catch (err) {
1606
+ console.warn("[web-apple-pay] #fetchSession: failed, falling back to anonymous session", err);
1573
1607
  return null;
1574
1608
  }
1575
1609
  }
1576
1610
  async #fetchPaymentOptions() {
1577
1611
  try {
1578
- return await getPaymentOptions({
1612
+ const options = await getPaymentOptions({
1579
1613
  sku: this.#offer.sku_id,
1580
1614
  countryCode: this.#country,
1581
1615
  currencyCode: this.#offer.price?.currency_code ?? DEFAULT_CURRENCY,
1582
1616
  checkoutUrl: this.#returnUrl
1583
1617
  });
1584
- } catch {
1618
+ console.log("[web-apple-pay] #fetchPaymentOptions: success", { hasApplePay: !!options?.options?.apple_pay });
1619
+ return options;
1620
+ } catch (err) {
1621
+ console.warn("[web-apple-pay] #fetchPaymentOptions: failed", err);
1585
1622
  return null;
1586
1623
  }
1587
1624
  }
1588
1625
  #buildSessionParams(skuId, country, sessionData, paymentOptions, recaptchaTokens) {
1626
+ console.log("[web-apple-pay] #buildSessionParams", { skuId, country, userLoggedIn: sessionData.loggedIn });
1589
1627
  const offer = this.#offer;
1590
1628
  const totalAmount = offer.price?.purchase ? `${offer.price.purchase}` : DEFAULT_TOTAL;
1591
1629
  return {
@@ -1608,6 +1646,7 @@ var ApplePayButton = class _ApplePayButton extends HTMLElement {
1608
1646
  };
1609
1647
  }
1610
1648
  async #initiateApplePay(skuId, country) {
1649
+ console.log("[web-apple-pay] #initiateApplePay: starting", { skuId, country });
1611
1650
  try {
1612
1651
  const sessionData = this.#sessionData ?? _ApplePayButton.#ANONYMOUS_SESSION;
1613
1652
  const paymentOptions = this.#paymentOptions;
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": "0.1.1-beta.0",
4
+ "version": "0.1.1-beta.1",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",