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
- This integration requires a PayPal Developer account.
- You’ll need to have an existing credit and debit card payments integration. PayPal must approve your account to process advanced credit and debit card payments.
- Complete the steps in Get started to get the following sandbox account information from the Developer Dashboard:
- The sandbox client ID and secret of your REST app.
- An access token to use the PayPal REST API server.
- This client-side and server-side integration uses the following:
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 the JavaScript SDK. The customer ID tells the JavaScript SDK to save or reuse a saved payment method.
- The payer completes a billing agreement.
- The JavaScript SDK populates the checkout page with each saved payment method. Each payment method appears as a one-click button next to other ways to pay.
Use cases
Businesses save payment methods if they want customers to:- Check out without re-entering a payment method
- Pay after use, for example, ride-sharing and food delivery
1. Set up sandbox to save payment methods
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 checkbox for payers to save card
Add acheckbox
element grouped with your card collection fields to give payers the option to save their card.
3. Pass checkbox value
Passdocument.getElementById("save").checked
to your server with the following details in the createOrder()
method:
- Value of the checkbox
- Optional: Card name
- Optional: Billing address
- No verification
- 3D Secure
4. Create order and save card
Server side
Set up your server to call the Orders API. The button that the payer selects determines thepayment_source
sent in the following sample.
This SDK uses the Orders v2 API to save payment methods in the background. Use the following request using the Orders API to add attributes needed to save a card.
- First-time payer
- Returning payer
Save payment method for first-time payers
This request is for payers who:- Don’t have a payment source saved into the vault.
- Selected the save checkbox. The
document.getElementById("save").checked
value istrue
.
payment_source.card.attributes.verification.method
to SCA_ALWAYS
or SCA_WHEN_REQUIRED
.SCA_ALWAYS
triggers an authentication for every transaction, while SCA_WHEN_REQUIRED
triggers an authentication only when a regional compliance mandate such as PSD2 is required. 3D Secure is supported only in countries with a PSD2 compliance mandate.Note: In the following requests, thepayment_source.attributes.vault.store_in_vault
with the valueON_SUCCESS
means the card is saved with a successful authorization or capture.
Response
Pass the orderid
to the JavaScript SDK. The SDK updates the order with the new card details. PayPal handles any PCI compliance issues.
After the SDK is updated, it triggers the onApprove()
method, which receives an object containing the orderID
. You can authorize or capture the order when you have the orderID
.
Note: The request to save the payment method is made when the order is created through payment_source.attributes.vault.store_in_vault
. Vault details are available only after an order is authorized or captured.
5. Authorize or capture order and save card
Server side
Set up your server to call the Orders v2 API:- Call the authorize order endpoint if the
intent
passed wasAUTHORIZE
. - Call the capture order endpoint if the
intent
passed wasCAPTURE
.
Request
Add apayment_source.card
instruction for vault. Use your desired amounts, URLs, and business policy.
Response
payment_source.card.attributes.vault
stores the card information as the vault.id
, which can be used for future payments when the vault.status
is VAULTED
.
Save approved payment source
If the payment has been authorized or captured, the payer doesn’t need to be present to save apayment_source
. To keep checkout times as short as possible, the Orders API responds as soon as payment is captured.
If the attributes.vault.status
returned after payment is APPROVED
, you won’t have a vault.id
yet. An example of the attributes
object from this scenario is in the following sample:
vault_id
when an APPROVED
status is returned, you’ll need to subscribe to the VAULT.PAYMENT-TOKEN.CREATED
webhook.
The Payment Method Tokens API sends a webhook after the payment source is saved. An example of the VAULT.PAYMENT-TOKEN.CREATED
webhook payload is shown in the following sample:
resource.id
field is the vault ID, and resource.customer.id
is the PayPal-generated customer ID.
You can now style your card fields and test a purchase.
Payment processor codes
Payment processors return the following codes when they receive a transaction request. For advanced card payments, the code displays in the authorization object under theresponse_code
field.
The following sample shows the processor response codes returned in an authorization (avs_code
) and capture call (cvv_code
) response:
response_code
object to get the processor response code for the non-PayPal payment processor errors.
6. Pay with saved payment methods
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.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 the Orders API. Use supported CSS properties to style the card fields.7. Test your integration
Test your vault integration in the PayPal sandbox.- Copy the sample request code.
- Change
'ACCESS_TOKEN'
to your access token.
Save payment method
- On the checkout page, enter the card information and select the option to save the card. You can use test card numbers for testing.
- Capture the transaction.
- Log in to sandbox with your merchant account and verify the transaction.
Pay with a saved payment method
- Use the list all payment tokens API to retrieve all the payment methods saved for the payer.
- Capture the payment by passing the payer-selected vault ID to the Orders API.
- Log in to the sandbox with your merchant account and verify the transaction.
8. Integrate front end
The following sample shows how a full script to save cards might appear in HTML:Testing
Use the following card numbers to test transactions in the sandbox: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 |
- Transaction succeeds and card is saved to the vault. Verify in the merchant dashboard.
- Transaction fails and the instrument is not saved with
ON_SUCCESS
. - 3DS success, cancellations, and failures behave as expected
Troubleshooting
- Make sure you’ve placed the vault directive under
payment_source.card.attributes
during order creation. - Verify currency and amount consistency between session context and server payloads.
- Add missing
break;
statements in clientswitch
handling ofstate
.
Next steps
- Go live with this integration.
- Complete production onboarding to be eligible to process cards with your live PayPal account.
- Be sure to swap the credentials and API URL from sandbox to production when going live with your integration.
- Follow Use payment method token with checkout for subsequent or recurring transactions.
- You can get a payment token, list all payment tokens, delete a payment token, and more with the Payment Method Tokens API.
- Keep saved cards up-to-date with the real-time account updater.