Availability
Supported countries
Supported countries
- Australia
- Austria
- Belgium
- Bulgaria
- Canada
- China
- Cyprus
- Czech Republic
- Denmark
- Estonia
- Finland
- France
- Germany
- Hong Kong
- Hungary
- Ireland
- Italy
- Japan
- Latvia
- Liechtenstein
- Lithuania
- Luxembourg
- Malta
- Netherlands
- Norway
- Poland
- Portugal
- Romania
- Singapore
- Slovakia
- Slovenia
- Spain
- Sweden
- United Kingdom
- United States
Prerequisites
- You are responsible for the front-end user experience. The JavaScript SDK provides back-end support.
- To save payment methods, you must be able to identify payers uniquely. For example, payers create an account and log in to your site.
- Complete the steps in Get started to get the following sandbox account information from the Developer Dashboard:
- Your sandbox account login information
- Your access token
- This client-side integration uses the
createCardFieldsSavePaymentSession()
method to save a card without a transaction. - The JavaScript SDK saves the following card types for purchase later:
- American Express
- Discover
- Mastercard
- Visa You’ll need an existing advanced credit and debit integration. PayPal must approve your business account for advanced credit and debit card payments.
How it works
PayPal encrypts payment method information and stores it in a digital vault for that customer.- The payer saves their payment method.
- For a first-time payer, PayPal creates a customer ID. Store this within your system for future use.
- When the customer returns to your website and is ready to check out, pass their PayPal-generated customer ID to your server to retrieve saved instruments.
- The payer completes any required verification (for example, 3DS) per your SCA policy.
- Your UI can display saved payment methods as a one-click button alongside other ways to pay.
1. Set up account to save payments
Set up your sandbox and live business accounts to save payment methods:- Log in to the Developer Dashboard.
- Under REST API apps, select your app name.
- Under Sandbox App Settings > App Feature Options, check Accept payments.
- Expand Advanced options. Confirm that Vault is selected.
2. Add SDK to HTML page
Pass your client ID to the SDK to identify yourself. ReplaceCLIENT-ID
with your app’s client ID in the following sample:
3. Create setup token
You request a setup token from your server. The setup token creation for v6 now supports optional 3D Secure verification per your SCA policy.Create a vault setup token on your server
This example for v6 includes optional 3DS or SCA behavior:- No verification - omit
verification_method
- 3D Secure verification -
"SCA_ALWAYS"
or"SCA_WHEN_REQUIRED"
Front-end sample
Back-end sample
Make this request from your server. This setup token is generated with an emptycard
in the payment_source
object. PayPal hosted fields use this token to securely update the setup token with card details.
Copy and modify the following code:
- Change
ACCESS-TOKEN
to your sandbox app’s access token. - Change
REQUEST-ID
to a set of unique alphanumeric characters such as a time stamp. - In the
createVaultSetupToken
, call the endpoint on your server to create a setup token with the Payment Method Tokens API.createVaultSetupToken
returns the setup token as a string.
4. Initialize card fields to save data
When you want to vault a card without taking a payment, use the Save Payment Session on the client:sdk.createCardFieldsSavePaymentSession()
.
Front-end sample
- No verification
- 3D Secure
Back-end sample
Make this request from your server. Copy and modify the following code.- Pass the
vaultSetupToken
returned byonApprove
to your server. - Change
ACCESS-TOKEN
to your sandbox app’s access token. - Change
REQUEST-ID
to a set of unique alphanumeric characters such as a time stamp. - Change
VAULT-SETUP-TOKEN
to the value passed from the client. - Save the resulting
payment token
returned from the API to use in future transactions.
Avoid validation errors
sdk.createCardFieldsSavePaymentSession()
cannot be used alongside sdk.createCardFieldsPaymentSession()
on the same page. Create separate sessions for different use cases:
5. Show error page
If an error prevents checkout, alert the payer that an error has occurred using theconsole.error()
method call.
Note: This script doesn’t handle specific errors. It shows a specified error page for all errors.
6. Show saved payment methods to returning payers
When a payer returns to your site, you can show the payer’s saved payment methods with the Payment Method Tokens API.List all saved payment methods
Make the server-side list all payment tokens API call to retrieve payment methods saved to a payer’s PayPal-generated customer ID. Based on this list, you can show all saved payment methods to a payer to select during checkout.Important: Don’t expose payment method token IDs on the client side. To protect your payers, create separate IDs for each token and use your server to correlate them.
Sample request: List all saved payment methods
Copy and modify the following code.- Change
CUSTOMER-ID
to a PayPal-generated customer ID. - Change
ACCESS-TOKEN
to your sandbox app’s access token. - Change
REQUEST-ID
to a set of unique alphanumeric characters such as a time stamp.
Show saved card to payer
Display the saved card to the payer and use the Orders API to make another transaction. Use the vault ID the payer selects as an input to theOrders API to capture the payment. Use supported CSS properties to style the card fields. We recommend showing the card brand and last 4 digits.
7. Integrate back end
The following sample shows a complete back-end integration to save cards for purchase later:8. Test saving cards
Use the following card numbers to test transactions in the sandbox:See test card numbers and types
See test card numbers and types
Test number | Card type |
---|---|
371449635398431 | American Express |
376680816376961 | American Express |
36259600000004 | Diners Club |
6304000000000000 | Maestro |
5063516945005047 | Maestro |
2223000048400011 | Mastercard |
4005519200000004 | Visa |
4012000033330026 | Visa |
4012000077777777 | Visa |
4012888888881881 | Visa |
4217651111111119 | Visa |
4500600000000061 | Visa |
4772129056533503 | Visa |
4915805038587737 | Visa |
console.error()
method call provided to the CardFields
component. Test 3DS success, cancel, and fail cases behave as expected.
- Render the card fields.
- Create a save button in your UI.
- When the save button is selected,
session.submit(setupToken)
returnssucceeded
and provides a reference you can store. Update the setup token with card details. - On your server, use a server-side call to swap your setup token for a payment token from the Payment Method Tokens API.
- For a first-time payer, save the PayPal-generated
customer.id
. - For a returning payer, use the PayPal-generated
customer.id
to swap thesetup-token
for apayment-token
.
- For a first-time payer, save the PayPal-generated
- Save the
payment-token
for future use. Test a purchase with the payment token. - Show saved payment methods:
- Make a server-side call to the list all payment tokens endpoint. Include the PayPal-generated
customer.id
. - Style the card fields.
- Make a server-side call to the list all payment tokens endpoint. Include the PayPal-generated
Troubleshooting
Issue | Resolution |
---|---|
Vault setup is failing | Check session type: Verify you’re using createCardFieldsSavePaymentSession() for vault-only flows, not createCardFieldsOneTimePaymentSession() . One-time sessions are for transactions, not vaulting. |
3D Secure verification is not completing properly | Check URLs: Ensure you’ve included return_url and cancel_url in your experience_context when requesting SCA verification. These are required for the 3DS challenge flow to redirect users back to your application. |
Cannot use vaulted payment methods for transactions | Check vault reference storage: Verify that your server is capturing and persisting the payment token returned in the API response after vaulting. Without storing this reference, you won’t be able to use the vaulted card later. |
Optional: Show saved payment methods
We recommend creating a page on your site where payers can see their saved payment methods as in the following example: