Skip to main content
Save payment methods to charge payers after a set amount of time. For example, you can offer a free trial and charge payers after the trial expires. Payers don’t need to be present when charged. No checkout required. Use the JavaScript SDK to save a payer’s card if you aren’t PCI Compliant - SAQ A but want to save credit or debit cards during checkout.

Availability

  • 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.
  1. The payer saves their payment method.
  2. For a first-time payer, PayPal creates a customer ID. Store this within your system for future use.
  3. 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.
  4. The payer completes any required verification (for example, 3DS) per your SCA policy.
  5. Your UI can display saved payment methods as a one-click button alongside other ways to pay.

1. Set up your account to save payments

Set up your sandbox and live business accounts to save payment methods:
  1. Log in to the Developer Dashboard.
  2. Under REST API apps, select your app name.
  3. Under Sandbox App Settings > App Feature Options, check Accept payments.
  4. Expand Advanced options. Confirm that Vault is selected.
PayPal requires Risk Data Acquisition (RDA) to reduce fraud. You must implement risk data collection for all customer-initiated transactions (CIT) that use PayPal and Venmo Payment Tokens. Payment attempts that are missing RDA data are likely to be declined due to insufficient risk identifiers. Use the PayPal FraudNet and Magnes libraries to collect and pass RDA data during payment processing.

2. Add the SDK to an HTML page

Pass your client ID to the SDK to identify yourself. Replace CLIENT-ID with your app’s client ID in the following sample:

3. Create a 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.
PayPal requires Risk Data Acquisition (RDA) to reduce fraud. You must implement risk data collection for all customer-initiated transactions (CIT) that use PayPal and Venmo Payment Tokens. Payment attempts that are missing RDA data are likely to be declined due to insufficient risk identifiers. Use the PayPal FraudNet and Magnes libraries to collect and pass RDA data during payment processing.

Create a vault setup token on your server

This example for v6 includes optional 3DS or SCA behavior:
Create a setup token for cards that have:
  • No verification - omit verification_method
  • 3D Secure verification - "SCA_ALWAYS" or "SCA_WHEN_REQUIRED"

Front-end sample

Use the setup token from your server to start the save payment session on the client.

Back-end sample

Make this request from your server. This setup token is generated with an empty card 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:
  1. Change ACCESS-TOKEN to your sandbox app’s access token.
  2. Change REQUEST-ID to a set of unique alphanumeric characters such as a time stamp.
  3. 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

Render the card fields and handle the submit flow based on your verification preference.

Back-end sample

Make this request from your server. Copy and modify the following code.
  1. Pass the vaultSetupToken returned by onApprove to your server.
  2. Change ACCESS-TOKEN to your sandbox app’s access token.
  3. Change REQUEST-ID to a set of unique alphanumeric characters such as a time stamp.
  4. Change VAULT-SETUP-TOKEN to the value passed from the client.
  5. 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 an error page

If an error prevents checkout, alert the payer that an error has occurred using the console.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 the saved card to the 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 the Orders API to capture the payment. Use supported CSS properties to style the card fields. We recommend showing the card brand and last 4 digits. Visa ending in 1234

7. Integrate the 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:
Test numberCard type
371449635398431American Express
376680816376961American Express
36259600000004Diners Club
6304000000000000Maestro
5063516945005047Maestro
2223000048400011Mastercard
4005519200000004Visa
4012000033330026Visa
4012000077777777Visa
4012888888881881Visa
4217651111111119Visa
4500600000000061Visa
4772129056533503Visa
4915805038587737Visa
Test your integration to see if it saves credit and debit cards as expected. Any errors that occur appear in the console.error() method call provided to the CardFields component. Test 3DS success, cancel, and fail cases behave as expected.
  1. Render the card fields.
  2. Create a save button in your UI.
  3. When the save button is selected, session.submit(setupToken) returns succeeded and provides a reference you can store. Update the setup token with card details.
  4. On your server, use a server-side call to swap your setup token for a payment token from the Payment Method Tokens API.
    1. For a first-time payer, save the PayPal-generated customer.id.
    2. For a returning payer, use the PayPal-generated customer.id to swap the setup-token for a payment-token.
  5. Save the payment-token for future use. Test a purchase with the payment token.
  6. Show saved payment methods:
    1. Make a server-side call to the list all payment tokens endpoint. Include the PayPal-generated customer.id.
    2. Style the card fields.

Troubleshooting

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: A website showing a payment methods page. The page shows the payer saved a PayPal Wallet and a credit card. The card option is highlighted.

Next step

Go live with your integration.