@paypercut/checkout-js 1.1.0 → 1.1.8

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.mjs CHANGED
@@ -87,7 +87,7 @@ class Emitter {
87
87
  * Production configuration (for merchants)
88
88
  */
89
89
  const productionConfig = {
90
- version: "1.1.0",
90
+ version: "1.1.8",
91
91
  defaultCheckoutOrigin: 'https://buy.paypercut.io',
92
92
  defaultApiOrigin: 'https://api.paypercut.io',
93
93
  defaultVaultApiUrl: 'https://api.basistheory.com',
@@ -484,6 +484,9 @@ class ExpressCheckoutImpl {
484
484
  // 3DS modal — same pattern as CheckoutImpl
485
485
  this.threeDSModal = null;
486
486
  this.threeDSIframe = null;
487
+ // Pending 3DS data captured during THREEDS_START_FLOW while GPay sheet is still closing.
488
+ // Shown after loadPaymentData() resolves
489
+ this.pending3DSAfterGpay = null;
487
490
  // Apple Pay native session state — session created directly in the SDK window
488
491
  // on the merchant's registered domain.
489
492
  this.applePaySession = null;
@@ -503,6 +506,10 @@ class ExpressCheckoutImpl {
503
506
  // Pending Google Pay payment data waiting for tokenization response
504
507
  this.pendingGpayResolve = null;
505
508
  this.pendingGpayReject = null;
509
+ // Stored GPay metadata for use when TOKENIZE_RESPONSE arrives after 3DS (sheet already closed)
510
+ this.pendingGpayBillingDetails = null;
511
+ this.pendingGpayShippingDetails = null;
512
+ this.pendingGpayShippingOption = undefined;
506
513
  this.currentShippingOptions = options.shippingOptions ?? [];
507
514
  this.messageHandler = this.onMessage.bind(this);
508
515
  window.addEventListener('message', this.messageHandler);
@@ -544,10 +551,10 @@ class ExpressCheckoutImpl {
544
551
  googlePayStatus,
545
552
  applePayStatus: data.payment_domain?.wallets?.apple_pay?.status ?? 'inactive',
546
553
  displayName: data.merchant?.name ?? '',
547
- merchantMid: data.account_settings?.mid ?? data.merchant?.mid ?? '',
548
- merchantMcc: data.account_settings?.mcc ?? data.merchant?.mcc ?? '',
549
- merchantCountry: data.account_settings?.country ?? data.merchant?.country ?? '',
550
- merchantUrl: data.account_settings?.business_url ?? data.merchant?.url ?? '',
554
+ merchantMid: data.merchant?.mid ?? '',
555
+ merchantMcc: data.merchant?.mcc ?? '',
556
+ merchantCountry: data.merchant?.country ?? '',
557
+ merchantUrl: data.merchant?.business_url ?? '',
551
558
  };
552
559
  }
553
560
  catch (err) {
@@ -558,7 +565,7 @@ class ExpressCheckoutImpl {
558
565
  // postMessage handler
559
566
  onMessage(evt) {
560
567
  if (!isOriginAllowed(evt.origin)) {
561
- if (evt.origin.includes('localhost') || evt.origin.includes('paypercut')) {
568
+ if (evt.origin.includes('localhost')) {
562
569
  console.warn('[PaypercutExpressCheckout] Message blocked — origin not allowed:', evt.origin);
563
570
  }
564
571
  return;
@@ -613,20 +620,82 @@ class ExpressCheckoutImpl {
613
620
  }
614
621
  }
615
622
  else {
616
- // Google Pay — resolve the pending promise; handleGooglePayAuthorized calls createPaymentMethod
623
+ // Google Pay
617
624
  if (data.success) {
618
- this.pendingGpayResolve?.(data);
625
+ if (this.pendingGpayResolve) {
626
+ // Normal path (no 3DS): resolve and let handleGooglePayAuthorized create PM
627
+ this.pendingGpayResolve(data);
628
+ this.pendingGpayResolve = null;
629
+ this.pendingGpayReject = null;
630
+ }
631
+ else {
632
+ // Post-3DS path: sheet was already closed — create PM directly, same as Apple Pay
633
+ this.createPaymentMethod({
634
+ btTokenId: data.btTokenId,
635
+ cardPayload: data.cardPayload,
636
+ billingDetails: data.billingDetails,
637
+ csessId: data.csessId,
638
+ walletType: 'google_pay',
639
+ }).then((paymentMethodId) => {
640
+ this.emitter.emit(ExpressSdkEvent.PaymentMethodCreated, {
641
+ paymentMethodId,
642
+ wallet: 'google_pay',
643
+ billingDetails: this.pendingGpayBillingDetails ?? undefined,
644
+ shippingDetails: this.pendingGpayShippingDetails ?? undefined,
645
+ selectedShippingOption: this.pendingGpayShippingOption,
646
+ });
647
+ }).catch((err) => {
648
+ this.emitter.emit(ExpressSdkEvent.Error, new Error(err?.message ?? 'Payment method creation failed'));
649
+ this.emitter.emit(ExpressSdkEvent.PaymentMethodFailed, { error: err?.message ?? 'Payment method creation failed' });
650
+ }).finally(() => {
651
+ this.pendingGpayBillingDetails = null;
652
+ this.pendingGpayShippingDetails = null;
653
+ this.pendingGpayShippingOption = undefined;
654
+ });
655
+ }
619
656
  }
620
657
  else {
621
- this.pendingGpayReject?.(new Error(data.error ?? 'Tokenization failed'));
658
+ if (this.pendingGpayReject) {
659
+ // Normal path (no 3DS): reject the pending promise in handleGooglePayAuthorized
660
+ this.pendingGpayReject(new Error(data.error ?? 'Tokenization failed'));
661
+ this.pendingGpayResolve = null;
662
+ this.pendingGpayReject = null;
663
+ }
664
+ else {
665
+ // Post-3DS path: sheet already closed — emit events directly.
666
+ // Distinguish user cancellation from an actual error so integrators
667
+ // can handle them differently (e.g. "cancel" = re-show checkout,
668
+ // "error" = show an error message).
669
+ const errMsg = data.error ?? 'Google Pay payment failed';
670
+ const isCanceled = errMsg.toLowerCase().includes('cancel');
671
+ if (isCanceled) {
672
+ this.emitter.emit(ExpressSdkEvent.Cancel);
673
+ }
674
+ else {
675
+ this.emitter.emit(ExpressSdkEvent.Error, new Error(errMsg));
676
+ this.emitter.emit(ExpressSdkEvent.PaymentMethodFailed, { error: errMsg });
677
+ }
678
+ this.pendingGpayBillingDetails = null;
679
+ this.pendingGpayShippingDetails = null;
680
+ this.pendingGpayShippingOption = undefined;
681
+ }
622
682
  }
623
- this.pendingGpayResolve = null;
624
- this.pendingGpayReject = null;
625
683
  }
626
684
  break;
627
- // 3DS flow — same mechanism as PaypercutCheckout
685
+ // 3DS flow
628
686
  case 'THREEDS_START_FLOW':
629
- this.show3DSModal(data);
687
+ // Google Pay sheet is a native browser UI that cannot be overlaid by DOM elements.
688
+ // Resolve the pending GPay promise so GPay starts closing its sheet.
689
+ if (this.pendingGpayResolve) {
690
+ this.pending3DSAfterGpay = data;
691
+ this.pendingGpayResolve({ __closeSheet: true });
692
+ this.pendingGpayResolve = null;
693
+ this.pendingGpayReject = null;
694
+ }
695
+ else {
696
+ // Apple Pay or any path without a pending GPay sheet — show immediately
697
+ this.show3DSModal(data);
698
+ }
630
699
  break;
631
700
  case 'THREEDS_READY':
632
701
  this.handle3DSReady();
@@ -721,6 +790,10 @@ class ExpressCheckoutImpl {
721
790
  if (showApplePay && this.applePayContainer && config?.applePayStatus === 'active') {
722
791
  await this.mountApplePayButton(this.applePayContainer);
723
792
  }
793
+ else if (this.applePayContainer) {
794
+ // Apple Pay can't mount — remove its container so the remaining button fills full width
795
+ this.removeApplePayContainer();
796
+ }
724
797
  if (showGooglePay && config) {
725
798
  this.mountGooglePayFromConfig();
726
799
  }
@@ -1137,6 +1210,15 @@ class ExpressCheckoutImpl {
1137
1210
  * Called after config is fetched. Waits for the GPay script to finish loading,
1138
1211
  * then mounts the button.
1139
1212
  */
1213
+ removeGpayContainer() {
1214
+ this.gpayContainer?.remove();
1215
+ this.gpayContainer = null;
1216
+ }
1217
+ removeApplePayContainer() {
1218
+ this.applePayContainer?.remove();
1219
+ this.applePayContainer = null;
1220
+ this.applePayButtonEl = null;
1221
+ }
1140
1222
  mountGooglePayFromConfig() {
1141
1223
  const config = this.expressConfig;
1142
1224
  if (!config)
@@ -1147,10 +1229,18 @@ class ExpressCheckoutImpl {
1147
1229
  const merchantName = config.displayName || 'Checkout';
1148
1230
  if (!tenantId) {
1149
1231
  console.error('[PaypercutExpressCheckout] tenantId is empty — Google Pay cannot be mounted without a valid gatewayMerchantId.');
1232
+ this.removeGpayContainer();
1150
1233
  return;
1151
1234
  }
1152
1235
  if (!googlePayMerchantId) {
1153
1236
  console.warn('[PaypercutExpressCheckout] googlePayMerchantId is empty — Google Pay will not be mounted (merchant may not have Google Pay active).');
1237
+ this.removeGpayContainer();
1238
+ return;
1239
+ }
1240
+ console.log({ liveMode });
1241
+ if (liveMode && (!config.merchantMcc || !config.merchantUrl)) {
1242
+ console.warn('[PaypercutExpressCheckout] Google Pay disabled!');
1243
+ this.removeGpayContainer();
1154
1244
  return;
1155
1245
  }
1156
1246
  loadGooglePayScript().then(() => {
@@ -1313,6 +1403,12 @@ class ExpressCheckoutImpl {
1313
1403
  }
1314
1404
  try {
1315
1405
  await this.gpayClient.loadPaymentData(paymentRequest);
1406
+ // If 3DS was triggered, the GPay sheet is now fully dismissed — show the challenge modal.
1407
+ if (this.pending3DSAfterGpay) {
1408
+ const threeDSData = this.pending3DSAfterGpay;
1409
+ this.pending3DSAfterGpay = null;
1410
+ this.show3DSModal(threeDSData);
1411
+ }
1316
1412
  }
1317
1413
  catch (e) {
1318
1414
  if (e?.statusCode === 'CANCELED' || e?.message?.toLowerCase?.().includes('cancel')) {
@@ -1329,6 +1425,7 @@ class ExpressCheckoutImpl {
1329
1425
  async handleGooglePayAuthorized(paymentData) {
1330
1426
  try {
1331
1427
  const encryptedToken = paymentData.paymentMethodData.tokenizationData.token;
1428
+ const cardNetwork = paymentData.paymentMethodData?.info?.cardNetwork ?? '';
1332
1429
  const billingAddr = paymentData.paymentMethodData?.info?.billingAddress;
1333
1430
  const shippingAddr = paymentData.shippingAddress;
1334
1431
  const shippingOptionData = paymentData.shippingOptionData;
@@ -1342,6 +1439,27 @@ class ExpressCheckoutImpl {
1342
1439
  country: billingAddr.countryCode,
1343
1440
  },
1344
1441
  } : null;
1442
+ const shippingDetails = shippingAddr ? {
1443
+ name: shippingAddr.name || undefined,
1444
+ address: {
1445
+ line1: shippingAddr.address1,
1446
+ city: shippingAddr.locality,
1447
+ state: shippingAddr.administrativeArea,
1448
+ postalCode: shippingAddr.postalCode,
1449
+ country: shippingAddr.countryCode,
1450
+ },
1451
+ } : undefined;
1452
+ const selectedShippingOption = shippingOptionData
1453
+ ? (this.currentShippingOptions.find((so) => so.id === shippingOptionData.id) ?? {
1454
+ id: shippingOptionData.id,
1455
+ label: shippingOptionData.label ?? shippingOptionData.id,
1456
+ amount: 0,
1457
+ })
1458
+ : undefined;
1459
+ // Store metadata upfront — needed if 3DS triggers and the sheet closes before TOKENIZE_RESPONSE
1460
+ this.pendingGpayBillingDetails = billingDetails;
1461
+ this.pendingGpayShippingDetails = shippingDetails;
1462
+ this.pendingGpayShippingOption = selectedShippingOption;
1345
1463
  // Send to hidden tokenize iframe — it will BT-tokenize + create the payment method
1346
1464
  const tokenResult = await new Promise((resolve, reject) => {
1347
1465
  const timeoutId = setTimeout(() => {
@@ -1356,6 +1474,7 @@ class ExpressCheckoutImpl {
1356
1474
  instanceId: this.instanceId,
1357
1475
  wallet: 'google_pay',
1358
1476
  encryptedToken,
1477
+ cardNetwork,
1359
1478
  idemKey: this.idempotencyKey,
1360
1479
  publishableKey: this.options.publishableKey,
1361
1480
  billingDetails,
@@ -1370,6 +1489,10 @@ class ExpressCheckoutImpl {
1370
1489
  },
1371
1490
  });
1372
1491
  });
1492
+ // 3DS was triggered — sheet already closed, payment method creation handled in message handler
1493
+ if (tokenResult.__closeSheet) {
1494
+ return { transactionState: 'SUCCESS' };
1495
+ }
1373
1496
  if (!tokenResult.success) {
1374
1497
  const errMsg = tokenResult.error ?? 'Tokenization failed';
1375
1498
  this.emitter.emit(ExpressSdkEvent.Error, new Error(errMsg));
@@ -1383,23 +1506,6 @@ class ExpressCheckoutImpl {
1383
1506
  csessId: tokenResult.csessId,
1384
1507
  walletType: 'google_pay',
1385
1508
  });
1386
- const selectedShippingOption = shippingOptionData
1387
- ? (this.currentShippingOptions.find((so) => so.id === shippingOptionData.id) ?? {
1388
- id: shippingOptionData.id,
1389
- label: shippingOptionData.label ?? shippingOptionData.id,
1390
- amount: 0,
1391
- })
1392
- : undefined;
1393
- const shippingDetails = shippingAddr ? {
1394
- name: shippingAddr.name || undefined,
1395
- address: {
1396
- line1: shippingAddr.address1,
1397
- city: shippingAddr.locality,
1398
- state: shippingAddr.administrativeArea,
1399
- postalCode: shippingAddr.postalCode,
1400
- country: shippingAddr.countryCode,
1401
- },
1402
- } : undefined;
1403
1509
  this.emitter.emit(ExpressSdkEvent.PaymentMethodCreated, {
1404
1510
  paymentMethodId,
1405
1511
  wallet: 'google_pay',
@@ -1407,6 +1513,9 @@ class ExpressCheckoutImpl {
1407
1513
  shippingDetails,
1408
1514
  selectedShippingOption,
1409
1515
  });
1516
+ this.pendingGpayBillingDetails = null;
1517
+ this.pendingGpayShippingDetails = null;
1518
+ this.pendingGpayShippingOption = undefined;
1410
1519
  return { transactionState: 'SUCCESS' };
1411
1520
  }
1412
1521
  catch (err) {
@@ -1501,7 +1610,12 @@ class ExpressCheckoutImpl {
1501
1610
  catch { }
1502
1611
  this.gpayContainer = null;
1503
1612
  }
1504
- this.applePayButtonEl = null;
1613
+ if (this.applePayContainer) {
1614
+ try {
1615
+ this.removeApplePayContainer();
1616
+ }
1617
+ catch { }
1618
+ }
1505
1619
  this.gpayPaymentRequest = null;
1506
1620
  if (this.layoutResizeObserver) {
1507
1621
  this.layoutResizeObserver.disconnect();