Skip to content

Commit 002218e

Browse files
authored
Merge pull request #856 from paypal/feature/5849-react-readme-cardfields
Added Card Fields documentation to React README file
2 parents 1b44c19 + 6aa4863 commit 002218e

File tree

2 files changed

+267
-0
lines changed

2 files changed

+267
-0
lines changed

.changeset/cute-numbers-fail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@paypal/react-paypal-js": patch
3+
---
4+
5+
Updated React README.md file to contain V6 React Card Fields documentation.

packages/react-paypal-js/README.md

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Integrating PayPal into React applications requires careful handling of SDK scri
4444
- **Venmo** - Venmo payments
4545
- **Pay Later** - PayPal's buy now, pay later option
4646
- **PayPal Basic Card** - Guest card payments without a PayPal account
47+
- **PayPal Advanced Card** - Card payments with enhanced features and customization options
4748
- **PayPal Subscriptions** - Recurring billing subscriptions
4849
- **PayPal Save** - Vault payment methods without purchase
4950
- **PayPal Credit** - PayPal Credit one-time and save payments
@@ -138,6 +139,7 @@ The `components` prop accepts an array of the following values:
138139
- `"venmo-payments"` - Venmo button
139140
- `"paypal-guest-payments"` - Guest checkout (card payments)
140141
- `"paypal-subscriptions"` - Subscription payments
142+
- `"card-fields"` - Card Fields (advanced card payment UI)
141143

142144
### With Promise-based Client ID
143145

@@ -489,6 +491,121 @@ import { PayPalCreditSavePaymentButton } from "@paypal/react-paypal-js/sdk-v6";
489491
5. On approval, `onApprove` callback captures the order via the backend
490492
6. Success/error handling displays the result to the user
491493

494+
## Card Fields Components
495+
496+
Card Fields components provide customizable card input fields for collecting payment details directly on your page.
497+
498+
Requires `"card-fields"` in the provider's `components` array.
499+
500+
### PayPalCardFieldsProvider
501+
502+
Wraps card field components and manages the Card Fields session.
503+
504+
```tsx
505+
import {
506+
PayPalProvider,
507+
PayPalCardFieldsProvider,
508+
} from "@paypal/react-paypal-js/sdk-v6";
509+
510+
function App() {
511+
return (
512+
<PayPalProvider
513+
clientToken="your-client-token"
514+
components={["card-fields"]}
515+
pageType="checkout"
516+
>
517+
<CheckoutForm />
518+
</PayPalProvider>
519+
);
520+
}
521+
522+
function CheckoutForm() {
523+
return (
524+
<PayPalCardFieldsProvider>
525+
<CardPaymentForm />
526+
</PayPalCardFieldsProvider>
527+
);
528+
}
529+
```
530+
531+
**Props:**
532+
533+
| Prop | Type | Required | Description |
534+
| --------------------- | ----------------- | -------- | --------------------------------------------------------------------------------- |
535+
| `amount` | `OrderAmount` | No | Amount for the card transaction (e.g., `{ value: "10.00", currencyCode: "USD" }`) |
536+
| `isCobrandedEligible` | `boolean` | No | Enable co-branded card eligibility |
537+
| `blur` | `(event) => void` | No | Callback when a field loses focus |
538+
| `change` | `(event) => void` | No | Callback when field value changes |
539+
| `focus` | `(event) => void` | No | Callback when field receives focus |
540+
| `empty` | `(event) => void` | No | Callback when field becomes empty |
541+
| `notempty` | `(event) => void` | No | Callback when field becomes non-empty |
542+
| `validitychange` | `(event) => void` | No | Callback when field validity changes |
543+
| `cardtypechange` | `(event) => void` | No | Callback when detected card type changes |
544+
| `inputsubmit` | `(event) => void` | No | Callback when submit key is pressed in field |
545+
546+
### PayPalCardNumberField
547+
548+
Renders a card number input field. Must be used within a [PayPalCardFieldsProvider](#paypalcardfieldsprovider) component.
549+
550+
```tsx
551+
import { PayPalCardNumberField } from "@paypal/react-paypal-js/sdk-v6";
552+
553+
<PayPalCardNumberField
554+
placeholder="Card number"
555+
containerStyles={{ height: "3rem", marginBottom: "1rem" }}
556+
/>;
557+
```
558+
559+
### PayPalCardExpiryField
560+
561+
Renders a card expiry input field. Must be used within a [PayPalCardFieldsProvider](#paypalcardfieldsprovider) component.
562+
563+
```tsx
564+
import { PayPalCardExpiryField } from "@paypal/react-paypal-js/sdk-v6";
565+
566+
<PayPalCardExpiryField
567+
placeholder="MM/YY"
568+
containerStyles={{ height: "3rem", marginBottom: "1rem" }}
569+
/>;
570+
```
571+
572+
### PayPalCardCvvField
573+
574+
Renders a CVV input field. Must be used within a [PayPalCardFieldsProvider](#paypalcardfieldsprovider) component.
575+
576+
```tsx
577+
import { PayPalCardCvvField } from "@paypal/react-paypal-js/sdk-v6";
578+
579+
<PayPalCardCvvField
580+
placeholder="CVV"
581+
containerStyles={{ height: "3rem", marginBottom: "1rem" }}
582+
/>;
583+
```
584+
585+
### Field Component Props
586+
587+
All field components ([`PayPalCardNumberField`](#paypalcardnumberfield), [`PayPalCardExpiryField`](#paypalcardexpiryfield), [`PayPalCardCvvField`](#paypalcardcvvfield)) accept the same set of props. They combine container styling properties with CardField-specific configuration options.
588+
589+
| Prop | Type | Required | Description |
590+
| ------------------------- | --------------------- | -------- | ---------------------------------------------- |
591+
| `placeholder` | `string` | No | Placeholder text for the field |
592+
| `label` | `string` | No | Label text for the field |
593+
| `style` | `MerchantStyleObject` | No | Style object for the field |
594+
| `ariaLabel` | `string` | No | ARIA label for accessibility |
595+
| `ariaDescription` | `string` | No | ARIA description for accessibility |
596+
| `ariaInvalidErrorMessage` | `string` | No | ARIA error message when field is invalid |
597+
| `containerStyles` | `React.CSSProperties` | No | CSS styles for the field container wrapper |
598+
| `containerClassName` | `string` | No | CSS class name for the field container wrapper |
599+
600+
## Payment Flow: Card Fields
601+
602+
1. User enters card number, expiry, and CVV in the card fields
603+
2. User clicks your submit button
604+
3. `createOrder` creates an order via your backend API
605+
4. `submit(orderId)` processes the card payment with the order ID
606+
5. `submitResponse` object gets updated with the payment result
607+
6. Handle submit response based on payment result
608+
492609
## Hooks API
493610

494611
### usePayPal
@@ -561,6 +678,27 @@ function PayLaterMessage() {
561678
}
562679
```
563680

681+
### usePayPalCardFields
682+
683+
Returns the Card Fields instance initialization errors. Must be used within a [PayPalCardFieldsProvider](#paypalcardfieldsprovider) component.
684+
685+
```tsx
686+
import { usePayPalCardFields } from "@paypal/react-paypal-js/sdk-v6";
687+
688+
function CardFields() {
689+
const { error } = usePayPalCardFields();
690+
691+
useEffect(() => {
692+
if (error) {
693+
// Handle error logic
694+
console.error("Error initializing PayPal Card Fields: ", error);
695+
}
696+
}, [error]);
697+
698+
return <CardPaymentForm />;
699+
}
700+
```
701+
564702
### Payment Session Hooks
565703

566704
For advanced use cases where you need full control over the payment flow, use the session hooks directly with web components.
@@ -792,6 +930,130 @@ function CustomPayPalCreditSaveButton() {
792930
}
793931
```
794932

933+
### Card Fields Session Hooks
934+
935+
Use these hooks to handle the Card Fields payment flows. The hooks must be used within a [`PayPalCardFieldsProvider`](#paypalcardfieldsprovider) component.
936+
937+
| Hook | Payment Type |
938+
| ------------------------------------------ | ------------ |
939+
| `usePayPalCardFieldsOneTimePaymentSession` | One-time |
940+
| `usePayPalCardFieldsSavePaymentSession` | Save/Vault |
941+
942+
#### usePayPalCardFieldsOneTimePaymentSession
943+
944+
Hook for managing one-time payment Card Fields sessions.
945+
946+
```tsx
947+
import { usePayPalCardFieldsOneTimePaymentSession } from "@paypal/react-paypal-js/sdk-v6";
948+
949+
function CardPaymentForm() {
950+
const { submit, submitResponse, error } =
951+
usePayPalCardFieldsOneTimePaymentSession();
952+
953+
useEffect(() => {
954+
if (error) {
955+
// Handle submit error logic
956+
console.error("Error submitting PayPal Card Fields payment", error);
957+
}
958+
}, [error]);
959+
960+
useEffect(() => {
961+
if (!submitResponse) {
962+
return;
963+
}
964+
965+
const { orderId, message } = submitResponse.data;
966+
967+
switch (submitResponse.state) {
968+
case "succeeded":
969+
// Handle submit success logic
970+
console.log(`One time payment succeeded: orderId: ${orderId}`);
971+
break;
972+
case "failed":
973+
// Handle submit failed response logic
974+
console.error(
975+
`One time payment failed: orderId: ${orderId}, message: ${message}`,
976+
);
977+
break;
978+
}
979+
}, [submitResponse]);
980+
981+
const handleSubmit = async () => {
982+
const orderId = await createOrder();
983+
await submit(orderId);
984+
};
985+
986+
return (
987+
<div>
988+
<PayPalCardNumberField />
989+
<PayPalCardExpiryField />
990+
<PayPalCardCvvField />
991+
<button onClick={handleSubmit}>Pay</button>
992+
</div>
993+
);
994+
}
995+
```
996+
997+
#### usePayPalCardFieldsSavePaymentSession
998+
999+
Hook for managing save payment Card Fields sessions.
1000+
1001+
```tsx
1002+
import { usePayPalCardFieldsSavePaymentSession } from "@paypal/react-paypal-js/sdk-v6";
1003+
1004+
function CardPaymentForm() {
1005+
const { submit, submitResponse, error } =
1006+
usePayPalCardFieldsSavePaymentSession();
1007+
1008+
useEffect(() => {
1009+
if (error) {
1010+
// Handle submit error logic
1011+
console.error(
1012+
"Error submitting PayPal Card Fields payment method",
1013+
error,
1014+
);
1015+
}
1016+
}, [error]);
1017+
1018+
useEffect(() => {
1019+
if (!submitResponse) {
1020+
return;
1021+
}
1022+
1023+
const { vaultSetupToken, message } = submitResponse.data;
1024+
1025+
switch (submitResponse.state) {
1026+
case "succeeded":
1027+
// Handle submit success logic
1028+
console.log(
1029+
`Save payment method succeeded: vaultSetupToken: ${vaultSetupToken}`,
1030+
);
1031+
break;
1032+
case "failed":
1033+
// Handle submit failed response logic
1034+
console.error(
1035+
`Save payment method failed: vaultSetupToken: ${vaultSetupToken}, message: ${message}`,
1036+
);
1037+
break;
1038+
}
1039+
}, [submitResponse]);
1040+
1041+
const handleSubmit = async () => {
1042+
const { vaultSetupToken } = await createCardVaultToken();
1043+
await submit(vaultSetupToken);
1044+
};
1045+
1046+
return (
1047+
<div>
1048+
<PayPalCardNumberField />
1049+
<PayPalCardExpiryField />
1050+
<PayPalCardCvvField />
1051+
<button onClick={handleSubmit}>Save Payment Method</button>
1052+
</div>
1053+
);
1054+
}
1055+
```
1056+
7951057
## Web Components
7961058

7971059
The V6 SDK uses web components for rendering buttons. These are automatically typed when you import from `@paypal/react-paypal-js/sdk-v6`.

0 commit comments

Comments
 (0)