Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/public-ears-stay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@paypal/paypal-js": minor
---

Added eligibility API typings for Card Fields and created card fields test file for its types
3 changes: 2 additions & 1 deletion packages/paypal-js/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { PayPalScriptOptions } from "../types/script-options";

interface PayPalScriptOptionsWithStringIndex
extends PayPalScriptOptions,
extends
PayPalScriptOptions,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Record<string, any> {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type EligiblePaymentMethods =
| "paypal"
| "venmo"
| "apple_pay"
| "advanced_cards"
| "google_pay";

export type PaymentFlow =
Expand All @@ -33,6 +34,7 @@ export type FundingSource =
| "paypal"
| "venmo"
| "card"
| "advanced_cards"
| "applepay"
| "googlepay";

Expand All @@ -49,6 +51,29 @@ type PayLaterEligiblePaymentMethodDetails = BaseEligiblePaymentMethodDetails & {
productCode: PayLaterProductCodes;
};

type CardFieldsEligiblePaymentMethodDetails = {
supportsInstallments: boolean;
cobrandedEnabled: boolean;
vendors: CardFieldsEligibleVendorDetails[];
};
Comment on lines +54 to +58
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see can_be_vaulted below, does this type require something similar?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can_be_vaulted is only part of the vendors array.


type CardFieldsCardNetwork =
| "AMEX"
| "CUP"
| "DINERS"
| "DISCOVER"
| "JCB"
| "MAESTRO"
| "MASTERCARD"
| "VISA";

type CardFieldsEligibleVendorDetails = {
network: CardFieldsCardNetwork;
eligible: boolean;
canBeVaulted: boolean;
branded: boolean;
};

type ApplePayEligiblePaymentMethodDetails = BaseEligiblePaymentMethodDetails & {
config: ApplePayConfig;
};
Expand All @@ -65,9 +90,11 @@ export type FindEligibleMethodsGetDetails<T extends FundingSource> =
? PayLaterEligiblePaymentMethodDetails
: T extends "applepay"
? ApplePayEligiblePaymentMethodDetails
: T extends "googlepay"
? GooglePayEligiblePaymentMethodDetails
: BaseEligiblePaymentMethodDetails;
: T extends "advanced_cards"
? CardFieldsEligiblePaymentMethodDetails
: T extends "googlepay"
? GooglePayEligiblePaymentMethodDetails
: BaseEligiblePaymentMethodDetails;

type EligiblePaymentMethodDetails = {
can_be_vaulted?: boolean;
Expand Down
80 changes: 80 additions & 0 deletions packages/paypal-js/types/v6/tests/card-fields.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { loadCoreSdkScript } from "../../../src/v6";
import type { PayPalV6Namespace } from "../index";

/**
* Type test for Card Fields integration
*
* @remarks
* This test verifies TypeScript compilation and type inference.
* The function is never executed — it's used for type-checking only.
*/

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function main() {
let paypal: PayPalV6Namespace | null;

try {
paypal = await loadCoreSdkScript({
environment: "sandbox",
debug: true,
});
} catch (err) {
throw new Error(`Failed to load the paypal sdk script: ${err}`);
}

if (!paypal?.createInstance) {
throw new Error("Invalid paypal object for v6");
}

if (!paypal?.version) {
throw new Error("PayPal v6 namespace missing version property");
}

const sdkInstance = await paypal.createInstance({
clientToken: "fakeValue",
components: ["card-fields"],
});

const paymentMethods = await sdkInstance.findEligibleMethods({
currencyCode: "USD",
});

if (!paymentMethods.isEligible("advanced_cards")) {
return;
}

const details = paymentMethods.getDetails("advanced_cards");
details.supportsInstallments;
details.cobrandedEnabled;
details.vendors[0].network;
details.vendors[0].eligible;
details.vendors[0].canBeVaulted;
details.vendors[0].branded;

const paypalCardFieldsOneTimePaymentSession =
sdkInstance.createCardFieldsOneTimePaymentSession();
paypalCardFieldsOneTimePaymentSession.createCardFieldsComponent({
type: "number",
placeholder: "Enter a number:",
});
paypalCardFieldsOneTimePaymentSession.createCardFieldsComponent({
type: "cvv",
placeholder: "Enter CVV:",
});
paypalCardFieldsOneTimePaymentSession.createCardFieldsComponent({
type: "expiry",
placeholder: "Enter Expiry:",
});

const createOrder = () => Promise.resolve({ orderId: "ABC123" });

const submitButton = document.createElement("button");
submitButton.addEventListener("click", async () => {
try {
const { orderId } = await createOrder();
await paypalCardFieldsOneTimePaymentSession.submit(orderId);
} catch (error) {
console.error(error);
}
});
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add more tests to confirm those added types are there? For eg

if (paymentMethods.isEligible("advanced_cards")) {
      const details = paymentMethods.getDetails("advanced_cards");
      console.log(details.supportsInstallments);
      console.log(details.cobrandedEnabled);
      console.log(details.vendors[0].network);
      console.log(details.vendors[0].eligible);
      console.log(details.vendors[0].can_be_vaulted);
      console.log(details.vendors[0].branded);
  }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great callout. Added this tests!

2 changes: 1 addition & 1 deletion packages/react-paypal-js/src/v6/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export function deepEqual(
}

// Non-object primitives (number, string, boolean, function, symbol, bigint) that
// Same typeof (verified above) but not ===, so unequal primitives
// Same typeof (verified above) but not ===, so unequal primitives
if (typeof obj1 !== "object") {
return false;
}
Expand Down
Loading