@payment-kit-js/vanilla 0.5.13 → 0.5.15

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.
Files changed (80) hide show
  1. package/dist/{airwallex-apple-pay-adapter-BFsoDoSf.mjs → airwallex-apple-pay-adapter-Bs8AdQKY.mjs} +8 -2
  2. package/dist/airwallex-apple-pay-adapter-Bs8AdQKY.mjs.map +1 -0
  3. package/dist/{airwallex-apple-pay-adapter-BCYt7Jzc.d.mts → airwallex-apple-pay-adapter-D8enI9Vq.d.mts} +3 -1
  4. package/dist/airwallex-apple-pay-adapter-D8enI9Vq.d.mts.map +1 -0
  5. package/dist/{airwallex-google-pay-adapter-B5xmg7b8.mjs → airwallex-google-pay-adapter-C8JTp01q.mjs} +1 -1
  6. package/dist/{airwallex-google-pay-adapter-B5xmg7b8.mjs.map → airwallex-google-pay-adapter-C8JTp01q.mjs.map} +1 -1
  7. package/dist/{airwallex-google-pay-adapter-DvEB6t5i.d.mts → airwallex-google-pay-adapter-adaDktQM.d.mts} +1 -1
  8. package/dist/{airwallex-google-pay-adapter-DvEB6t5i.d.mts.map → airwallex-google-pay-adapter-adaDktQM.d.mts.map} +1 -1
  9. package/dist/bnpl-shared-B-en2sPr.d.mts +64 -0
  10. package/dist/bnpl-shared-B-en2sPr.d.mts.map +1 -0
  11. package/dist/bnpl-shared-CEAXTL3Q.mjs +107 -0
  12. package/dist/bnpl-shared-CEAXTL3Q.mjs.map +1 -0
  13. package/dist/cdn/paymentkit.js +362 -20
  14. package/dist/cdn/paymentkit.js.map +4 -4
  15. package/dist/cdn/paymentkit.min.js +6 -6
  16. package/dist/cdn/paymentkit.min.js.map +4 -4
  17. package/dist/{connect-card-Cxy51W6t.d.mts → connect-card-Cra_F1cC.d.mts} +1 -1
  18. package/dist/{connect-card-Cxy51W6t.d.mts.map → connect-card-Cra_F1cC.d.mts.map} +1 -1
  19. package/dist/{connect-card-CMZkeppE.mjs → connect-card-D3Je4V7p.mjs} +1 -1
  20. package/dist/{connect-card-CMZkeppE.mjs.map → connect-card-D3Je4V7p.mjs.map} +1 -1
  21. package/dist/{connect-tunnel-x-lv6Wtdme.d.mts → connect-tunnel-x-Wh-JG1O5.d.mts} +1 -1
  22. package/dist/{connect-tunnel-x-lv6Wtdme.d.mts.map → connect-tunnel-x-Wh-JG1O5.d.mts.map} +1 -1
  23. package/dist/index.d.mts +3 -3
  24. package/dist/index.mjs +1 -1
  25. package/dist/index.mjs.map +1 -1
  26. package/dist/{next-action-handlers-CTx8tRt0.mjs → next-action-handlers-BZs04hYb.mjs} +1 -1
  27. package/dist/{next-action-handlers-CTx8tRt0.mjs.map → next-action-handlers-BZs04hYb.mjs.map} +1 -1
  28. package/dist/payment-methods/affirm.d.mts +13 -0
  29. package/dist/payment-methods/affirm.d.mts.map +1 -0
  30. package/dist/payment-methods/affirm.mjs +14 -0
  31. package/dist/payment-methods/affirm.mjs.map +1 -0
  32. package/dist/payment-methods/afterpay.d.mts +13 -0
  33. package/dist/payment-methods/afterpay.d.mts.map +1 -0
  34. package/dist/payment-methods/afterpay.mjs +14 -0
  35. package/dist/payment-methods/afterpay.mjs.map +1 -0
  36. package/dist/payment-methods/airwallex-apple-pay-adapter.d.mts +1 -1
  37. package/dist/payment-methods/airwallex-apple-pay-adapter.mjs +1 -1
  38. package/dist/payment-methods/airwallex-google-pay-adapter.d.mts +1 -1
  39. package/dist/payment-methods/airwallex-google-pay-adapter.mjs +1 -1
  40. package/dist/payment-methods/apple-pay.d.mts +38 -7
  41. package/dist/payment-methods/apple-pay.d.mts.map +1 -1
  42. package/dist/payment-methods/apple-pay.mjs +159 -14
  43. package/dist/payment-methods/apple-pay.mjs.map +1 -1
  44. package/dist/payment-methods/bnpl-shared.d.mts +5 -0
  45. package/dist/payment-methods/bnpl-shared.mjs +4 -0
  46. package/dist/payment-methods/card.d.mts +3 -3
  47. package/dist/payment-methods/card.mjs +3 -3
  48. package/dist/payment-methods/google-pay.d.mts +5 -5
  49. package/dist/payment-methods/google-pay.mjs +3 -3
  50. package/dist/payment-methods/klarna.d.mts +13 -0
  51. package/dist/payment-methods/klarna.d.mts.map +1 -0
  52. package/dist/payment-methods/klarna.mjs +14 -0
  53. package/dist/payment-methods/klarna.mjs.map +1 -0
  54. package/dist/payment-methods/next-action-handlers.mjs +1 -1
  55. package/dist/payment-methods/paypal.d.mts +9 -3
  56. package/dist/payment-methods/paypal.d.mts.map +1 -1
  57. package/dist/payment-methods/paypal.mjs.map +1 -1
  58. package/dist/payment-methods/stripe-apple-pay-adapter.d.mts +1 -1
  59. package/dist/payment-methods/stripe-apple-pay-adapter.mjs +1 -1
  60. package/dist/payment-methods/stripe-google-pay-adapter.d.mts +1 -1
  61. package/dist/payment-methods/stripe-google-pay-adapter.mjs +1 -1
  62. package/dist/payment-methods/vgs-collect-loader.mjs +1 -1
  63. package/dist/penpal/connect-card.d.mts +1 -1
  64. package/dist/penpal/connect-card.mjs +1 -1
  65. package/dist/penpal/connect-tunnel-x.d.mts +1 -1
  66. package/dist/{stripe-apple-pay-adapter-Cd9LVNnp.mjs → stripe-apple-pay-adapter-BLRjqgDf.mjs} +1 -1
  67. package/dist/{stripe-apple-pay-adapter-Cd9LVNnp.mjs.map → stripe-apple-pay-adapter-BLRjqgDf.mjs.map} +1 -1
  68. package/dist/{stripe-apple-pay-adapter-DhpGclW9.d.mts → stripe-apple-pay-adapter-DcuGlQqQ.d.mts} +1 -1
  69. package/dist/{stripe-apple-pay-adapter-DhpGclW9.d.mts.map → stripe-apple-pay-adapter-DcuGlQqQ.d.mts.map} +1 -1
  70. package/dist/{stripe-google-pay-adapter-Bdox4xBq.d.mts → stripe-google-pay-adapter-BlQ2jVpE.d.mts} +1 -1
  71. package/dist/{stripe-google-pay-adapter-Bdox4xBq.d.mts.map → stripe-google-pay-adapter-BlQ2jVpE.d.mts.map} +1 -1
  72. package/dist/{stripe-google-pay-adapter-3cx0KNjK.mjs → stripe-google-pay-adapter-CIkgjhw8.mjs} +1 -1
  73. package/dist/{stripe-google-pay-adapter-3cx0KNjK.mjs.map → stripe-google-pay-adapter-CIkgjhw8.mjs.map} +1 -1
  74. package/dist/{types-D88-nhWu.d.mts → types-CQ8xbgoh.d.mts} +3 -3
  75. package/dist/{types-D88-nhWu.d.mts.map → types-CQ8xbgoh.d.mts.map} +1 -1
  76. package/dist/{vgs-collect-loader-BZj36d8r.mjs → vgs-collect-loader-l5_Pnmhz.mjs} +1 -1
  77. package/dist/{vgs-collect-loader-BZj36d8r.mjs.map → vgs-collect-loader-l5_Pnmhz.mjs.map} +1 -1
  78. package/package.json +6 -2
  79. package/dist/airwallex-apple-pay-adapter-BCYt7Jzc.d.mts.map +0 -1
  80. package/dist/airwallex-apple-pay-adapter-BFsoDoSf.mjs.map +0 -1
@@ -1,5 +1,5 @@
1
1
  /**
2
- * PaymentKit.js v0.5.13
2
+ * PaymentKit.js v0.5.15
3
3
  * https://paymentkit.com
4
4
  *
5
5
  * @license MIT
@@ -42,7 +42,7 @@ var PaymentKit = (() => {
42
42
  });
43
43
 
44
44
  // package.json
45
- var version = "0.5.13";
45
+ var version = "0.5.15";
46
46
 
47
47
  // src/analytics/mock-adapter.ts
48
48
  var MockAnalyticsAdapter = class {
@@ -6598,6 +6598,175 @@ var PaymentKit = (() => {
6598
6598
  };
6599
6599
  var index_default = PaymentKit;
6600
6600
 
6601
+ // src/payment-methods/bnpl-shared.ts
6602
+ var createSubmitPayment = (states, config) => {
6603
+ const { name, endpointPath, supportsSetupFutureUsage } = config;
6604
+ const capitalizedName = name.charAt(0).toUpperCase() + name.slice(1);
6605
+ const submitPayment = async (_fields, options) => {
6606
+ const { apiBaseUrl, secureToken, environment } = states;
6607
+ const bnplOptions = options;
6608
+ if (!bnplOptions?.processorId) {
6609
+ return { errors: { processor_id: "Processor ID is required" } };
6610
+ }
6611
+ if (!bnplOptions?.customerInfo?.first_name || !bnplOptions?.customerInfo?.last_name) {
6612
+ return {
6613
+ errors: {
6614
+ customer_name: "Customer first and last name are required"
6615
+ }
6616
+ };
6617
+ }
6618
+ try {
6619
+ const checkoutRequestId = getOrCreateCheckoutRequestId(environment);
6620
+ const requestBody = {
6621
+ processor_id: bnplOptions.processorId,
6622
+ customer_info: bnplOptions.customerInfo,
6623
+ fraud_metadata: collectFraudMetadata()
6624
+ };
6625
+ if (supportsSetupFutureUsage && bnplOptions.setupFutureUsage !== void 0) {
6626
+ requestBody.setup_future_usage = bnplOptions.setupFutureUsage;
6627
+ }
6628
+ const startResponse = await fetch(`${apiBaseUrl}/api/checkout/${secureToken}/stripe/${endpointPath}/start`, {
6629
+ method: "POST",
6630
+ headers: {
6631
+ "Content-Type": "application/json",
6632
+ "x-request-id": checkoutRequestId
6633
+ },
6634
+ body: JSON.stringify(requestBody)
6635
+ });
6636
+ if (!startResponse.ok) {
6637
+ let errorMessage = `Failed to start ${capitalizedName} checkout (${startResponse.status})`;
6638
+ try {
6639
+ const errorData = await startResponse.json();
6640
+ errorMessage = errorData.detail || errorMessage;
6641
+ } catch {
6642
+ errorMessage = startResponse.statusText || errorMessage;
6643
+ }
6644
+ return {
6645
+ errors: {
6646
+ [name]: errorMessage
6647
+ }
6648
+ };
6649
+ }
6650
+ const { approval_url, popup_token, checkout_attempt_id } = await startResponse.json();
6651
+ const popup = window.open(approval_url, capitalizedName, "width=600,height=700");
6652
+ if (!popup) {
6653
+ return {
6654
+ errors: {
6655
+ [name]: `Failed to open ${capitalizedName} popup. Please allow popups for this site.`
6656
+ }
6657
+ };
6658
+ }
6659
+ states.popup = popup;
6660
+ return new Promise((resolve) => {
6661
+ let popupClosedGracePolls = 0;
6662
+ const MAX_GRACE_POLLS = 5;
6663
+ const pollInterval = setInterval(async () => {
6664
+ try {
6665
+ const statusResponse = await fetch(
6666
+ `${apiBaseUrl}/api/checkout/${secureToken}/popup/status?popup_token=${popup_token}`
6667
+ );
6668
+ if (!statusResponse.ok) {
6669
+ clearInterval(pollInterval);
6670
+ popup.close();
6671
+ resolve({
6672
+ errors: {
6673
+ [name]: `Failed to check ${capitalizedName} status`
6674
+ }
6675
+ });
6676
+ return;
6677
+ }
6678
+ const statusResult = await statusResponse.json();
6679
+ if (statusResult.completed) {
6680
+ clearInterval(pollInterval);
6681
+ popup.close();
6682
+ if (statusResult.status === "success") {
6683
+ const result = statusResult.result || {
6684
+ id: checkout_attempt_id,
6685
+ checkoutAttemptId: checkout_attempt_id,
6686
+ checkoutSessionId: secureToken,
6687
+ state: "checkout_succeeded"
6688
+ };
6689
+ resolve({ data: result });
6690
+ } else {
6691
+ resolve({
6692
+ errors: {
6693
+ [name]: statusResult.error || `${capitalizedName} checkout ${statusResult.status}`
6694
+ }
6695
+ });
6696
+ }
6697
+ return;
6698
+ }
6699
+ if (popup.closed) {
6700
+ popupClosedGracePolls++;
6701
+ if (popupClosedGracePolls >= MAX_GRACE_POLLS) {
6702
+ clearInterval(pollInterval);
6703
+ resolve({
6704
+ errors: {
6705
+ [name]: `${capitalizedName} popup closed by user`
6706
+ }
6707
+ });
6708
+ }
6709
+ return;
6710
+ }
6711
+ } catch (error) {
6712
+ clearInterval(pollInterval);
6713
+ popup.close();
6714
+ resolve({
6715
+ errors: {
6716
+ [name]: `Polling error: ${error}`
6717
+ }
6718
+ });
6719
+ }
6720
+ }, 2e3);
6721
+ states.pollInterval = pollInterval;
6722
+ });
6723
+ } catch (error) {
6724
+ return {
6725
+ errors: {
6726
+ [name]: `${capitalizedName} checkout error: ${error}`
6727
+ }
6728
+ };
6729
+ }
6730
+ };
6731
+ return submitPayment;
6732
+ };
6733
+ function createBnplPaymentMethod(config) {
6734
+ return definePaymentMethod((paymentKitStates) => {
6735
+ const localStates = { ...paymentKitStates };
6736
+ return {
6737
+ name: config.name,
6738
+ externalFuncs: {},
6739
+ internalFuncs: {
6740
+ submitPayment: createSubmitPayment(localStates, config),
6741
+ cleanup: () => {
6742
+ if (localStates.pollInterval) {
6743
+ clearInterval(localStates.pollInterval);
6744
+ }
6745
+ if (localStates.popup && !localStates.popup.closed) {
6746
+ localStates.popup.close();
6747
+ }
6748
+ }
6749
+ }
6750
+ };
6751
+ });
6752
+ }
6753
+
6754
+ // src/payment-methods/affirm.ts
6755
+ var AffirmPaymentMethod = createBnplPaymentMethod({
6756
+ name: "affirm",
6757
+ endpointPath: "affirm",
6758
+ supportsSetupFutureUsage: false
6759
+ });
6760
+ var affirm_default = AffirmPaymentMethod;
6761
+
6762
+ // src/payment-methods/afterpay.ts
6763
+ var AfterpayPaymentMethod = createBnplPaymentMethod({
6764
+ name: "afterpay",
6765
+ endpointPath: "afterpay",
6766
+ supportsSetupFutureUsage: false
6767
+ });
6768
+ var afterpay_default = AfterpayPaymentMethod;
6769
+
6601
6770
  // src/payment-methods/airwallex-apple-pay-adapter.ts
6602
6771
  var AirwallexApplePayAdapter = class {
6603
6772
  constructor(mockScenario) {
@@ -6709,6 +6878,8 @@ var PaymentKit = (() => {
6709
6878
  success: true,
6710
6879
  token: mockToken,
6711
6880
  payerEmail: "mock@example.com",
6881
+ payerFirstName: "Mock",
6882
+ payerLastName: "User",
6712
6883
  complete: (status) => console.log(`[MockApplePay:Airwallex] complete: ${status}`)
6713
6884
  };
6714
6885
  }
@@ -6734,8 +6905,8 @@ var PaymentKit = (() => {
6734
6905
  amount,
6735
6906
  type: "final"
6736
6907
  },
6737
- // Surface the payer's email so the backend can fill missing customer email.
6738
- requiredBillingContactFields: ["email"]
6908
+ // Surface the payer's email and name so the backend can fill missing customer info.
6909
+ requiredBillingContactFields: ["email", "name"]
6739
6910
  };
6740
6911
  return new Promise((resolve) => {
6741
6912
  try {
@@ -6779,6 +6950,8 @@ var PaymentKit = (() => {
6779
6950
  transactionIdentifier: payment.token.transactionIdentifier
6780
6951
  };
6781
6952
  const payerEmail = payment.billingContact?.emailAddress ?? payment.shippingContact?.emailAddress ?? void 0;
6953
+ const payerFirstName = payment.billingContact?.givenName ?? payment.shippingContact?.givenName ?? void 0;
6954
+ const payerLastName = payment.billingContact?.familyName ?? payment.shippingContact?.familyName ?? void 0;
6782
6955
  const complete = (status) => {
6783
6956
  console.log("[ApplePay:Airwallex] completing session with status:", status);
6784
6957
  const appleStatus = status === "success" ? STATUS_SUCCESS : STATUS_FAILURE;
@@ -6788,6 +6961,8 @@ var PaymentKit = (() => {
6788
6961
  success: true,
6789
6962
  token,
6790
6963
  payerEmail,
6964
+ payerFirstName,
6965
+ payerLastName,
6791
6966
  complete
6792
6967
  });
6793
6968
  };
@@ -7300,6 +7475,28 @@ var PaymentKit = (() => {
7300
7475
  // src/payment-methods/apple-pay.ts
7301
7476
  var preparedStripeState = null;
7302
7477
  var preparedAirwallexState = null;
7478
+ var applePayScriptLoadPromise = null;
7479
+ async function loadApplePayScript() {
7480
+ if (typeof window === "undefined") return;
7481
+ if (window.ApplePaySession) return;
7482
+ if (document.getElementById("__pk_apple_pay_sdk")) {
7483
+ return applePayScriptLoadPromise ?? Promise.resolve();
7484
+ }
7485
+ applePayScriptLoadPromise = new Promise((resolve, reject) => {
7486
+ const script = document.createElement("script");
7487
+ script.id = "__pk_apple_pay_sdk";
7488
+ script.src = "https://applepay.cdn-apple.com/jsapi/1.latest/apple-pay-sdk.js";
7489
+ script.crossOrigin = "anonymous";
7490
+ script.onload = () => resolve();
7491
+ script.onerror = () => {
7492
+ applePayScriptLoadPromise = null;
7493
+ document.getElementById("__pk_apple_pay_sdk")?.remove();
7494
+ reject(new Error("Failed to load Apple Pay SDK"));
7495
+ };
7496
+ document.head.appendChild(script);
7497
+ });
7498
+ return applePayScriptLoadPromise;
7499
+ }
7303
7500
  async function apiCall(url, options, checkoutRequestId) {
7304
7501
  const headers = new Headers(options.headers);
7305
7502
  if (checkoutRequestId) {
@@ -7409,7 +7606,25 @@ var PaymentKit = (() => {
7409
7606
  const checkoutRequestId = getOrCreateCheckoutRequestId(environment);
7410
7607
  console.log(`[ApplePay] Using checkout_request_id: ${checkoutRequestId}`);
7411
7608
  if (options.processorType === "airwallex") {
7412
- console.log("[ApplePay] Airwallex processor - fetching session info for amount");
7609
+ console.log("[ApplePay] Airwallex processor - loading Apple Pay SDK, fetching session info for amount");
7610
+ if (!mockScenarioStr) {
7611
+ try {
7612
+ await loadApplePayScript();
7613
+ } catch (err) {
7614
+ console.error("[ApplePay] Failed to load Apple Pay SDK:", err);
7615
+ return { success: false, error: "Failed to load Apple Pay" };
7616
+ }
7617
+ let canMake = false;
7618
+ try {
7619
+ canMake = !!window.ApplePaySession?.canMakePayments();
7620
+ } catch {
7621
+ canMake = false;
7622
+ }
7623
+ if (!canMake) {
7624
+ console.log("[ApplePay] Apple Pay not available on this device/browser");
7625
+ return { success: false, error: "Apple Pay not available on this device" };
7626
+ }
7627
+ }
7413
7628
  const sessionInfoResult = await callAirwallexSessionInfoEndpoint(apiBaseUrl, secureToken, checkoutRequestId);
7414
7629
  if (sessionInfoResult.error || !sessionInfoResult.data) {
7415
7630
  console.error("[ApplePay] Failed to fetch session info:", sessionInfoResult.error);
@@ -7418,7 +7633,7 @@ var PaymentKit = (() => {
7418
7633
  console.log("[ApplePay] Got session info:", sessionInfoResult.data);
7419
7634
  const resolvedAmount = sessionInfoResult.data.amount;
7420
7635
  const resolvedCurrency = sessionInfoResult.data.currency;
7421
- const resolvedCountry = options.country || sessionInfoResult.data.country;
7636
+ const resolvedCountry = sessionInfoResult.data.country || options.country;
7422
7637
  preparedAirwallexState = {
7423
7638
  country: resolvedCountry,
7424
7639
  currency: resolvedCurrency,
@@ -7513,7 +7728,7 @@ var PaymentKit = (() => {
7513
7728
  checkoutRequestId
7514
7729
  );
7515
7730
  }
7516
- async function callAirwallexConfirmEndpoint(apiBaseUrl, secureToken, applePayToken, mockScenarioStr, checkoutRequestId, payerEmail) {
7731
+ async function callAirwallexConfirmEndpoint(apiBaseUrl, secureToken, applePayToken, mockScenarioStr, checkoutRequestId, payerEmail, payerFirstName, payerLastName) {
7517
7732
  const result = await apiCall(
7518
7733
  `${apiBaseUrl}/api/checkout/${secureToken}/airwallex/apple-pay/confirm`,
7519
7734
  {
@@ -7522,6 +7737,8 @@ var PaymentKit = (() => {
7522
7737
  body: JSON.stringify({
7523
7738
  apple_pay_token: applePayToken,
7524
7739
  payer_email: payerEmail,
7740
+ payer_first_name: payerFirstName,
7741
+ payer_last_name: payerLastName,
7525
7742
  mock_scenario: mockScenarioStr
7526
7743
  })
7527
7744
  },
@@ -7613,7 +7830,7 @@ var PaymentKit = (() => {
7613
7830
  merchantName: options.merchantName || "Total"
7614
7831
  };
7615
7832
  if (!adapter.initialize(config)) {
7616
- return { errors: { apple_pay: "Apple Pay not available on this device (requires Safari)" } };
7833
+ return { errors: { apple_pay: "Apple Pay not available on this device" } };
7617
7834
  }
7618
7835
  const canPay = await adapter.canMakePayment();
7619
7836
  if (!canPay) {
@@ -7658,14 +7875,16 @@ var PaymentKit = (() => {
7658
7875
  }
7659
7876
  return { errors: { apple_pay: paymentResult.error || "Apple Pay failed" } };
7660
7877
  }
7661
- const { token, complete, payerEmail } = paymentResult;
7878
+ const { token, complete, payerEmail, payerFirstName, payerLastName } = paymentResult;
7662
7879
  let response = await callAirwallexConfirmEndpoint(
7663
7880
  apiBaseUrl,
7664
7881
  secureToken,
7665
7882
  token,
7666
7883
  mockScenarioStr,
7667
7884
  checkoutRequestId,
7668
- payerEmail
7885
+ payerEmail,
7886
+ payerFirstName,
7887
+ payerLastName
7669
7888
  );
7670
7889
  let userActionCount = 0;
7671
7890
  while (response.charge_status === "pending" && response.next_action && userActionCount < MAX_USER_ACTIONS) {
@@ -7698,10 +7917,17 @@ var PaymentKit = (() => {
7698
7917
  complete(response.charge_status === "success" ? "success" : "fail");
7699
7918
  return toApplePayResult(response, secureToken);
7700
7919
  }
7701
- var defSubmitPayment = (states) => {
7920
+ var defSubmitPayment = (states, getAutoPrepareConfig, onClearPrepared) => {
7702
7921
  const submitPayment = async (_fields, options) => {
7703
7922
  const { apiBaseUrl, secureToken, environment } = states;
7704
7923
  const applePayOptions = options;
7924
+ const autoPrepareConfig = getAutoPrepareConfig();
7925
+ if (!applePayOptions.processorId && autoPrepareConfig?.processorId) {
7926
+ applePayOptions.processorId = autoPrepareConfig.processorId;
7927
+ }
7928
+ if (!applePayOptions.processorType && autoPrepareConfig?.processorType) {
7929
+ applePayOptions.processorType = autoPrepareConfig.processorType;
7930
+ }
7705
7931
  const validationError = validateOptions(applePayOptions);
7706
7932
  if (validationError) {
7707
7933
  return { errors: validationError };
@@ -7810,26 +8036,131 @@ var PaymentKit = (() => {
7810
8036
  return confirmResult;
7811
8037
  }
7812
8038
  } catch (error) {
7813
- clearPreparedApplePay();
8039
+ onClearPrepared();
7814
8040
  return { errors: { apple_pay: `Apple Pay error: ${error}` } };
7815
8041
  }
7816
8042
  };
7817
8043
  return submitPayment;
7818
8044
  };
7819
8045
  var ApplePayPaymentMethod = definePaymentMethod((paymentKitStates) => {
8046
+ const { apiBaseUrl, secureToken, environment } = paymentKitStates;
8047
+ let autoPrepareConfig = null;
8048
+ let onApplePayReadyCallbacks = [];
8049
+ let isApplePayReadyState = false;
8050
+ let autoPrepareInProgress = false;
8051
+ let cleanupGeneration = 0;
8052
+ let pendingAutoPrepareArgs = null;
8053
+ let pendingPrepareResolvers = [];
8054
+ function instanceSetApplePayReady(ready) {
8055
+ isApplePayReadyState = ready;
8056
+ for (const cb of onApplePayReadyCallbacks) cb(ready);
8057
+ }
8058
+ function instanceClearPreparedApplePay() {
8059
+ clearPreparedApplePay();
8060
+ instanceSetApplePayReady(false);
8061
+ }
8062
+ async function instanceAutoPrepare(apBase, sToken, env) {
8063
+ if (autoPrepareInProgress) {
8064
+ pendingAutoPrepareArgs = { apiBaseUrl: apBase, secureToken: sToken, environment: env };
8065
+ return new Promise((resolve) => pendingPrepareResolvers.push(resolve));
8066
+ }
8067
+ autoPrepareInProgress = true;
8068
+ const capturedGeneration = cleanupGeneration;
8069
+ instanceSetApplePayReady(false);
8070
+ try {
8071
+ const result = await apiCall(`${apBase}/api/checkout-sessions/token/${sToken}`, {
8072
+ method: "GET"
8073
+ });
8074
+ if (cleanupGeneration !== capturedGeneration) return;
8075
+ if (result.error || !result.data) {
8076
+ console.warn("[ApplePay] Auto-prepare: failed to fetch session config:", result.error);
8077
+ return;
8078
+ }
8079
+ const processorId = result.data.express_checkout_processor_id;
8080
+ if (!processorId) {
8081
+ console.log("[ApplePay] Auto-prepare: no express checkout processor configured");
8082
+ return;
8083
+ }
8084
+ const processorTypeRaw = result.data.express_checkout_processor_type;
8085
+ const processorType = processorTypeRaw === "airwallex" || processorTypeRaw === "stripe" ? processorTypeRaw : void 0;
8086
+ autoPrepareConfig = { processorId, processorType };
8087
+ const prepareResult = await prepareApplePay(
8088
+ apBase,
8089
+ sToken,
8090
+ { processorId, processorType, customerInfo: { first_name: "", last_name: "" } },
8091
+ env
8092
+ );
8093
+ if (cleanupGeneration !== capturedGeneration) return;
8094
+ instanceSetApplePayReady(prepareResult.success && prepareResult.applePay === true);
8095
+ } catch (err) {
8096
+ if (cleanupGeneration === capturedGeneration) {
8097
+ console.error("[ApplePay] Auto-prepare failed:", err);
8098
+ }
8099
+ } finally {
8100
+ autoPrepareInProgress = false;
8101
+ const stale = cleanupGeneration !== capturedGeneration;
8102
+ const pending = pendingAutoPrepareArgs;
8103
+ const resolvers = pendingPrepareResolvers.splice(0);
8104
+ if (!stale && pending) {
8105
+ pendingAutoPrepareArgs = null;
8106
+ instanceAutoPrepare(pending.apiBaseUrl, pending.secureToken, pending.environment).then(() => {
8107
+ for (const r2 of resolvers) r2();
8108
+ });
8109
+ } else {
8110
+ pendingAutoPrepareArgs = null;
8111
+ for (const r2 of resolvers) r2();
8112
+ }
8113
+ }
8114
+ }
8115
+ instanceAutoPrepare(apiBaseUrl, secureToken, environment).catch(
8116
+ (err) => console.error("[ApplePay] Unhandled init error:", err)
8117
+ );
7820
8118
  return {
7821
8119
  name: "apple_pay",
7822
8120
  externalFuncs: {
7823
- prepareApplePay: (options) => prepareApplePay(
7824
- paymentKitStates.apiBaseUrl,
7825
- paymentKitStates.secureToken,
7826
- options,
7827
- paymentKitStates.environment
7828
- )
8121
+ /**
8122
+ * @deprecated The SDK now prepares automatically on init. Use `onApplePayReady` to observe
8123
+ * readiness and `notifyAmountChanged` to trigger re-prepare after amount changes.
8124
+ */
8125
+ prepareApplePay: (options) => prepareApplePay(apiBaseUrl, secureToken, options, environment),
8126
+ /**
8127
+ * Register a callback to be notified when Apple Pay becomes ready (or stops being ready).
8128
+ * Fires immediately with the current state, then on every state change.
8129
+ * Multiple registrations are all notified — none are silently overwritten.
8130
+ *
8131
+ * @example
8132
+ * paymentKit.apple_pay.onApplePayReady((isReady) => {
8133
+ * applePayButton.disabled = !isReady;
8134
+ * });
8135
+ */
8136
+ onApplePayReady: (callback) => {
8137
+ onApplePayReadyCallbacks.push(callback);
8138
+ callback(isApplePayReadyState);
8139
+ },
8140
+ /**
8141
+ * Notify the SDK that the payment amount has changed (e.g. coupon applied).
8142
+ * The SDK will disable Apple Pay, re-prepare with the new amount, then re-enable.
8143
+ * The returned promise resolves when re-prepare is fully complete.
8144
+ *
8145
+ * @example
8146
+ * await paymentKit.apple_pay.notifyAmountChanged();
8147
+ */
8148
+ notifyAmountChanged: () => {
8149
+ instanceClearPreparedApplePay();
8150
+ autoPrepareConfig = null;
8151
+ return instanceAutoPrepare(apiBaseUrl, secureToken, environment);
8152
+ }
7829
8153
  },
7830
8154
  internalFuncs: {
7831
- submitPayment: defSubmitPayment(paymentKitStates),
8155
+ submitPayment: defSubmitPayment(paymentKitStates, () => autoPrepareConfig, instanceClearPreparedApplePay),
7832
8156
  cleanup: () => {
8157
+ cleanupGeneration++;
8158
+ onApplePayReadyCallbacks = [];
8159
+ isApplePayReadyState = false;
8160
+ autoPrepareConfig = null;
8161
+ pendingAutoPrepareArgs = null;
8162
+ pendingPrepareResolvers = [];
8163
+ autoPrepareInProgress = false;
7833
8164
  clearPreparedApplePay();
7834
8165
  }
7835
8166
  }
@@ -9143,6 +9474,14 @@ var PaymentKit = (() => {
9143
9474
  });
9144
9475
  var google_pay_default = GooglePayPaymentMethod;
9145
9476
 
9477
+ // src/payment-methods/klarna.ts
9478
+ var KlarnaPaymentMethod = createBnplPaymentMethod({
9479
+ name: "klarna",
9480
+ endpointPath: "klarna",
9481
+ supportsSetupFutureUsage: true
9482
+ });
9483
+ var klarna_default = KlarnaPaymentMethod;
9484
+
9146
9485
  // src/payment-methods/paypal.ts
9147
9486
  var defSubmitPayment4 = (states) => {
9148
9487
  const submitPayment = async (_fields, options) => {
@@ -9293,7 +9632,10 @@ var PaymentKit = (() => {
9293
9632
  applePay: apple_pay_default,
9294
9633
  card: card_default,
9295
9634
  googlePay: google_pay_default,
9296
- paypal: paypal_default
9635
+ paypal: paypal_default,
9636
+ klarna: klarna_default,
9637
+ afterpay: afterpay_default,
9638
+ affirm: affirm_default
9297
9639
  };
9298
9640
  return __toCommonJS(cdn_exports);
9299
9641
  })();