@paypercut/checkout-js 1.2.5 → 1.2.9

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.cjs CHANGED
@@ -91,7 +91,7 @@ class Emitter {
91
91
  * Production configuration (for merchants)
92
92
  */
93
93
  const productionConfig = {
94
- version: "1.2.5",
94
+ version: "1.2.9",
95
95
  defaultCheckoutOrigin: 'https://buy.paypercut.io',
96
96
  defaultApiOrigin: 'https://api.paypercut.io',
97
97
  defaultVaultApiUrl: 'https://api.basistheory.com',
@@ -532,6 +532,19 @@ exports.SdkEvent = void 0;
532
532
  SdkEvent["Cancel"] = "cancel";
533
533
  SdkEvent["FormValidation"] = "form_validation";
534
534
  })(exports.SdkEvent || (exports.SdkEvent = {}));
535
+ // Apple Pay bridge message names — wire format shared with hosted-checkout's CheckoutMessageType enum.
536
+ // Defined here as a const so the switch in index.ts is compile-checked within this repo.
537
+ // If a message name changes in hosted-checkout, update this map and the switch together.
538
+ const ApplePayBridgeMsg = {
539
+ START: 'APPLE_PAY_START',
540
+ VALIDATE_MERCHANT: 'APPLE_PAY_VALIDATE_MERCHANT',
541
+ MERCHANT_SESSION: 'APPLE_PAY_MERCHANT_SESSION',
542
+ MERCHANT_ERROR: 'APPLE_PAY_MERCHANT_ERROR',
543
+ PAYMENT_DATA: 'APPLE_PAY_PAYMENT_DATA',
544
+ PAYMENT_RESULT: 'APPLE_PAY_PAYMENT_RESULT',
545
+ CANCELLED: 'APPLE_PAY_CANCELLED',
546
+ SESSION_ERROR: 'APPLE_PAY_SESSION_ERROR',
547
+ };
535
548
 
536
549
  // ─── Locale ───────────────────────────────────────────────────────────────────
537
550
  /**
@@ -2013,6 +2026,30 @@ const PaypercutExpressCheckout = function (options) {
2013
2026
  return new ExpressCheckoutImpl(options);
2014
2027
  };
2015
2028
 
2029
+ // ── Apple Pay SDK loader ──────────────────────────────────────────────────────
2030
+ // Tracks whether the Apple Pay CDN script has already been injected so multiple
2031
+ // CheckoutImpl instances don't race to add it more than once.
2032
+ let _applePaySdkInjected = false;
2033
+ /**
2034
+ * Injects Apple's cross-browser Apple Pay JS SDK into the parent page so that
2035
+ * window.ApplePaySession is available when the embedded bridge delegates session
2036
+ * creation here. On Safari, ApplePaySession is a browser built-in and the script
2037
+ * is a no-op; on Chrome it provides the iCloud Keychain integration.
2038
+ */
2039
+ function ensureApplePaySdk() {
2040
+ if (typeof window === 'undefined')
2041
+ return;
2042
+ if (window.ApplePaySession)
2043
+ return;
2044
+ if (_applePaySdkInjected)
2045
+ return;
2046
+ _applePaySdkInjected = true;
2047
+ const s = document.createElement('script');
2048
+ s.src = 'https://applepay.cdn-apple.com/jsapi/1.latest/apple-pay-sdk.js';
2049
+ s.crossOrigin = 'anonymous';
2050
+ s.async = true;
2051
+ document.head.appendChild(s);
2052
+ }
2016
2053
  // ── Appearance validation ─────────────────────────────────────────────────────
2017
2054
  const _HEX_RE = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
2018
2055
  const _CSS_LEN_RE = /^(0|(\d+(\.\d+)?(px|rem|em)))$/;
@@ -2047,6 +2084,9 @@ class CheckoutImpl {
2047
2084
  this.mounted = false;
2048
2085
  this.destroyed = false;
2049
2086
  this.iframe = null;
2087
+ // Apple Pay bridge state (embedded mode only)
2088
+ this.applePaySession = null;
2089
+ this.applePayBridgeTimeout = null;
2050
2090
  // Bind message handler
2051
2091
  this.messageHandler = this.onMessage.bind(this);
2052
2092
  this.threeDSModal = new ThreeDSModalController({
@@ -2056,6 +2096,10 @@ class CheckoutImpl {
2056
2096
  onReady: (iframe) => this.postThreeDSInit(iframe),
2057
2097
  });
2058
2098
  window.addEventListener('message', this.messageHandler);
2099
+ // In embedded mode the Apple Pay bridge delegates ApplePaySession creation to this parent window.
2100
+ if (validateUIMode(options.ui_mode ?? exports.UIMode.EMBEDDED) === exports.UIMode.EMBEDDED) {
2101
+ ensureApplePaySdk();
2102
+ }
2059
2103
  }
2060
2104
  /**
2061
2105
  * Build the iframe source URL with query parameters
@@ -2214,6 +2258,18 @@ class CheckoutImpl {
2214
2258
  wallet: data.wallet,
2215
2259
  });
2216
2260
  break;
2261
+ case ApplePayBridgeMsg.START:
2262
+ this.handleApplePayStart(data);
2263
+ break;
2264
+ case ApplePayBridgeMsg.MERCHANT_SESSION:
2265
+ this.handleApplePayMerchantSession(data.merchantSession);
2266
+ break;
2267
+ case ApplePayBridgeMsg.MERCHANT_ERROR:
2268
+ this.abortApplePaySession();
2269
+ break;
2270
+ case ApplePayBridgeMsg.PAYMENT_RESULT:
2271
+ this.handleApplePayPaymentResult(data.success === true);
2272
+ break;
2217
2273
  }
2218
2274
  }
2219
2275
  /**
@@ -2337,6 +2393,95 @@ class CheckoutImpl {
2337
2393
  checkoutId: this.options.id,
2338
2394
  }, checkoutOrigin);
2339
2395
  }
2396
+ // ---------------------------------------------------------------------------
2397
+ // Apple Pay bridge
2398
+ // ---------------------------------------------------------------------------
2399
+ /**
2400
+ * Called when the embedded iframe requests that we create an ApplePaySession at the
2401
+ * top-level window so the payment sheet / QR popup is not clipped by the container.
2402
+ */
2403
+ handleApplePayStart(data) {
2404
+ this.abortApplePaySession();
2405
+ const ApplePayAPI = window.ApplePaySession;
2406
+ if (!ApplePayAPI) {
2407
+ this.postToIframe({ type: ApplePayBridgeMsg.SESSION_ERROR, checkoutId: this.options.id });
2408
+ return;
2409
+ }
2410
+ const version = ApplePayAPI.supportsVersion?.(14) ? 14 : 3;
2411
+ let session;
2412
+ try {
2413
+ session = new ApplePayAPI(version, data.request);
2414
+ }
2415
+ catch (e) {
2416
+ console.error(e?.name === 'SecurityError'
2417
+ ? '[PaypercutCheckout] Apple Pay blocked — parent page missing allow="payment" on iframe'
2418
+ : '[PaypercutCheckout] Failed to create ApplePaySession', e);
2419
+ this.postToIframe({ type: ApplePayBridgeMsg.SESSION_ERROR, checkoutId: this.options.id });
2420
+ return;
2421
+ }
2422
+ this.applePaySession = session;
2423
+ session.onvalidatemerchant = (event) => {
2424
+ // If the iframe doesn't respond within 15 s, abort — Apple's own timeout is ~30 s
2425
+ // and we need to clean up the ref before that fires.
2426
+ this.applePayBridgeTimeout = setTimeout(() => this.abortApplePaySession(), 15000);
2427
+ this.postToIframe({
2428
+ type: ApplePayBridgeMsg.VALIDATE_MERCHANT,
2429
+ checkoutId: this.options.id,
2430
+ validationURL: event.validationURL,
2431
+ });
2432
+ };
2433
+ session.onpaymentauthorized = (event) => {
2434
+ this.postToIframe({
2435
+ type: ApplePayBridgeMsg.PAYMENT_DATA,
2436
+ checkoutId: this.options.id,
2437
+ payment: event.payment,
2438
+ });
2439
+ };
2440
+ session.oncancel = () => {
2441
+ this.applePaySession = null;
2442
+ this.postToIframe({ type: ApplePayBridgeMsg.CANCELLED, checkoutId: this.options.id });
2443
+ };
2444
+ session.begin();
2445
+ }
2446
+ handleApplePayMerchantSession(merchantSession) {
2447
+ if (this.applePayBridgeTimeout) {
2448
+ clearTimeout(this.applePayBridgeTimeout);
2449
+ this.applePayBridgeTimeout = null;
2450
+ }
2451
+ if (!this.applePaySession)
2452
+ return;
2453
+ try {
2454
+ this.applePaySession.completeMerchantValidation(merchantSession);
2455
+ }
2456
+ catch (e) {
2457
+ console.error('[PaypercutCheckout] completeMerchantValidation failed', e);
2458
+ this.abortApplePaySession();
2459
+ this.postToIframe({ type: ApplePayBridgeMsg.SESSION_ERROR, checkoutId: this.options.id });
2460
+ }
2461
+ }
2462
+ handleApplePayPaymentResult(success) {
2463
+ if (!this.applePaySession)
2464
+ return;
2465
+ const ApplePayAPI = window.ApplePaySession;
2466
+ try {
2467
+ this.applePaySession.completePayment(success ? ApplePayAPI.STATUS_SUCCESS : ApplePayAPI.STATUS_FAILURE);
2468
+ }
2469
+ catch (e) {
2470
+ console.error('[PaypercutCheckout] completePayment failed', e);
2471
+ }
2472
+ this.applePaySession = null;
2473
+ }
2474
+ abortApplePaySession() {
2475
+ if (this.applePayBridgeTimeout) {
2476
+ clearTimeout(this.applePayBridgeTimeout);
2477
+ this.applePayBridgeTimeout = null;
2478
+ }
2479
+ try {
2480
+ this.applePaySession?.abort();
2481
+ }
2482
+ catch { }
2483
+ this.applePaySession = null;
2484
+ }
2340
2485
  /**
2341
2486
  * Destroy the checkout instance and cleanup (idempotent)
2342
2487
  */
@@ -2357,6 +2502,8 @@ class CheckoutImpl {
2357
2502
  }
2358
2503
  // Cleanup 3DS modal if open
2359
2504
  this.threeDSModal.close();
2505
+ // Abort Apple Pay session if active
2506
+ this.abortApplePaySession();
2360
2507
  // Only set mounted = false after successful DOM removal
2361
2508
  this.mounted = false;
2362
2509
  this.destroyed = true;