Save PayPal with the Payment Method Tokens API

CurrentLast updated: November 7th 2023, @ 9:47:43 am


No transaction is required when payment methods are saved with the Payment Method Tokens API. You can charge payers after a set amount of time. Payers don't need to be present when charged. A common use case is offering a free trial and charging payers after the trial expires.

Customers with a PayPal Wallet can:

  • Review PayPal transactions and transaction history
  • Review, add, or remove funding sources
  • Review and cancel recurring payments
  • Hold a balance in their PayPal account
  • Use PayPal to send and receive money
  • Withdraw money to a linked bank account
  • Use PayPal to transact with merchants

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

Know before you code

  • This server-side integration uses the Payment Method Tokens API.
  • The Payment Method Tokens API supports saving cards and PayPal Wallets.
  • 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
  • You must be approved and have your account configured for billing agreements to set up a reference transaction. Contact your account manager for details.

Check eligibility

  1. Go to paypal.com and sign in with your business account.
  2. Go to Account Settings > Payment Preferences > Save PayPal and Venmo payment methods.
  3. In the Save PayPal and Venmo payment methods section, select Get Started.
  4. When you submit profile details, PayPal reviews your eligibility to save PayPal Wallets and Venmo accounts.
  5. After PayPal reviews your eligibility, you'll see a status of Success, Need more information, or Denied.

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.

2. Create setup token to save PayPal Wallet

The payer must authenticate and approve the creation of a billing agreement. Then you can create a setup token to save PayPal as a payment method.

The initial POST on setup-tokens completes the following actions:

  • Returns a PAYER_ACTION_REQUIRED status
  • Creates a temporary setup token
  • Redirects to the approve URL

When saving a payer's PayPal Wallet for first time, the response to the setup-token request returns the PayPal-generated customer.id and the setup_token_id.

Tip: For a payer with previously-stored payment_sources, pass the customer.id in the setup-token request. This links additional payment_sources to the payer.

You can store a Merchant Customer ID aligned with your system to simplify the mapping of customer information within your system and PayPal when creating a setup token. This is an optional field that will return the value shared in the response.

To create a setup token for PayPal that triggers a payer action, copy and modify the following code:

Sample API request

1curl -v -k -X POST 'https://api-m.sandbox.paypal.com/v3/vault/setup-tokens' \
2 -H "Content-Type: application/json" \
3 -H "Authorization: Bearer ACCESS-TOKEN" \
4 -H "PayPal-Request-Id: REQUEST-ID" \
5 -d '{
6 "payment_source": {
7 "paypal": {
8 "description": "Description for PayPal to be shown to PayPal payer",
9 "shipping": {
10 "name": {
11 "full_name": "Firstname Lastname"
12 },
13 "address": {
14 "address_line_1": "2211 N First Street",
15 "address_line_2": "Building 17",
16 "admin_area_2": "San Jose",
17 "admin_area_1": "CA",
18 "postal_code": "95131",
19 "country_code": "US"
20 }
21 },
22 "permit_multiple_payment_tokens": false,
23 "usage_pattern": "IMMEDIATE",
24 "usage_type": "MERCHANT",
25 "customer_type": "CONSUMER",
26 "experience_context": {
27 "shipping_preference": "SET_PROVIDED_ADDRESS",
28 "payment_method_preference": "IMMEDIATE_PAYMENT_REQUIRED",
29 "brand_name": "EXAMPLE INC",
30 "locale": "en-US",
31 "return_url": "https://example.com/returnUrl",
32 "cancel_url": "https://example.com/cancelUrl"
33 }
34 }
35 }
36 }'

Modify the code

  1. Copy the code sample.
  2. Change ACCESS-TOKEN to your sandbox access token.
  3. Change REQUEST-ID to a set of unique alphanumeric characters such as a timestamp.
  4. Use PayPal as the payment_source. Complete the rest of the source object for your use case and business.
  5. Update the return_url value with the URL where the payer is redirected after they approve the flow.
  6. Update the cancel_url value with the URL where the payer is redirected after they cancel the flow.

Step result

A successful request results in the following:

  • An HTTP response code of 200 or 201. Returns 200 for an idempotent request.
  • A status of PAYER_ACTION_REQUIRED.
  • HATEOAS links:

RelMethodDescription
approveGETUse this link to take your payer through a PayPal-hosted approval flow.
confirmPOSTMake a POST request to use an approved setup token to save the PayPal Wallet and generate a payment token.
selfGETMake a GET request to view the state of your setup token and payment method details.

Sample API response

1{
2 "id": "4G4976650J0948357",
3 "customer": {
4 "id": "customer_4029352051"
5 },
6 "status": "PAYER_ACTION_REQUIRED",
7 "payment_source": {
8 "paypal": {
9 "description": "Description for PayPal to be shown to PayPal payer",
10 "usage_pattern": "IMMEDIATE",
11 "shipping": {
12 "name": {
13 "full_name": "Firstname Lastname"
14 },
15 "address": {
16 "address_line_1": "2211 N First Street",
17 "address_line_2": "Building 17",
18 "admin_area_2": "San Jose",
19 "admin_area_1": "CA",
20 "postal_code": "95131",
21 "country_code": "US"
22 }
23 },
24 "permit_multiple_payment_tokens": false,
25 "usage_type": "MERCHANT",
26 "customer_type": "CONSUMER"
27 }
28 },
29 "links": [
30 {
31 "href": "https://api-m.sandbox.paypal.com/v3/vault/setup-tokens/4G4976650J0948357",
32 "rel": "self",
33 "method": "GET",
34 "encType": "application/json"
35 },
36 {
37 "href": "https://sandbox.paypal.com/agreements/approve?approval_session_id=4G4976650J0948357",
38 "rel": "approve",
39 "method": "GET",
40 "encType": "application/json"
41 }
42 ]
43}

By default, the setup token expires after 3 days. After the payer completes the approval flow, you can upgrade the setup token to a full payment method token by calling create-payment-tokens.

3. Create payment token

You can store a Merchant Customer ID aligned with your system to simplify the mapping of customer information within your system and PayPal when creating a payment token. This is an optional field that will return the value shared in the response.

Use an approved setup token to save the payer's payment method to the vault. Then, copy the sample request code to generate a payment token:

Sample API request

1curl -v -k -X POST 'https://api-m.sandbox.paypal.com/v3/vault/payment-tokens' \
2 -H "Content-Type: application/json" \
3 -H "Authorization: Bearer ACCESS-TOKEN" \
4 -H "PayPal-Request-Id: REQUEST-ID" \
5 -d '{
6 "payment_source": {
7 "token": {
8 "id": "4G4976650J0948357",
9 "type": "SETUP_TOKEN"
10 }
11 }
12 }'

Modify the code

  1. Copy the code sample.
  2. Change ACCESS-TOKEN to your sandbox access token.
  3. Change REQUEST-ID to a unique alphanumeric set of characters such as a time stamp.
  4. Use token as the payment source and complete the rest of the source object for your use case and business.
  5. Use your setup token ID to pass in the payment source parameter and type as the SETUP_TOKEN.

Step result

A successful request results in the following:

  • An HTTP response code of 200 or 201. Returns 200 for an idempotent request.
  • ID of the payment token and associated payment method information.
  • HATEOAS links:
RelMethodDescription
selfGETMake a GET request to this link to retrieve data about the saved payment method.
deleteDELETEMake a DELETE request to delete the payment token.

Sample API response

1{
2 "id": "jwgvx42",
3 "customer": {
4 "id": "customer_4029352051"
5 },
6 "payment_source": {
7 "paypal": {
8 "description": "Description for PayPal to be shown to PayPal payer",
9 "usage_pattern": "IMMEDIATE",
10 "shipping": {
11 "name": {
12 "full_name": "Firstname Lastname"
13 },
14 "address": {
15 "address_line_1": "2211 N First Street",
16 "address_line_2": "Building 17",
17 "admin_area_2": "San Jose",
18 "admin_area_1": "CA",
19 "postal_code": "95131",
20 "country_code": "US"
21 }
22 },
23 "permit_multiple_payment_tokens": false,
24 "usage_type": "MERCHANT",
25 "customer_type": "CONSUMER",
26 "email_address": "email@example.com",
27 "payer_id": "AJM9JTWQJCFTA"
28 }
29 },
30 "links": [
31 {
32 "rel": "self",
33 "href": "https://api-m.sandbox.paypal.com/v3/vault/payment-tokens/jwgvx42",
34 "method": "GET",
35 "encType": "application/json"
36 },
37 {
38 "rel": "delete",
39 "href": "https://api-m.sandbox.paypal.com/v3/vault/payment-tokens/jwgvx42",
40 "method": "DELETE",
41 "encType": "application/json"
42 }
43 ]
44}

4. Use saved payment token

After you create a payment method token, use the token instead of the payment method to create a purchase and capture the payment with the Orders API.

You can store a Merchant Customer ID aligned with your system to simplify the mapping of customer information within your system and PayPal. This is an optional field that will return the value shared in the response.

Set the payment_source to specify the payment source type. Set the vault_id to the payment method token you received.

Sample API request with payment token associated with PayPal account

Copy the following code sample and modify it.

1curl -v -k -X POST 'https://api-m.sandbox.paypal.com/v2/checkout/orders' \
2 -H "PayPal-Request-Id: REQUEST-ID" \
3 -H "Authorization: Bearer ACCESS-TOKEN" \
4 -H "Content-Type: application/json" \
5 -d '{
6 "intent": "CAPTURE",
7 "purchase_units": [
8 {
9 "amount": {
10 "currency_code": "USD",
11 "value": "100.00"
12 }
13 }
14 ],
15 "payment_source": {
16 "paypal": {
17 "vault_id":"jwgvx42"
18 }
19 }
20 }'

Modify the code

  1. Copy the code sample.
  2. Change ACCESS-TOKEN to your sandbox access token.
  3. Change REQUEST-ID to a set of unique alphanumeric characters such as a time stamp.
  4. Use the ID of your payment method token as the vault_id.

Sample API response

1{
2 "id": "4TH21426N05692944",
3 "status": "COMPLETED",
4 "payment_source": {
5 "paypal": {
6 "email_address": "email@example.com",
7 "account_id": "AJM9JTWQJCFTA",
8 "name": {
9 "given_name": "Firstname",
10 "surname": "Lastname"
11 },
12 "address": {
13 "country_code": "US"
14 }
15 }
16 },
17 "purchase_units": [
18 {
19 "reference_id": "default",
20 "payments": {
21 "captures": [
22 {
23 "id": "3B017991HX624902V",
24 "status": "COMPLETED",
25 "amount": {
26 "currency_code": "USD",
27 "value": "100.00"
28 },
29 "final_capture": true,
30 "seller_protection": {
31 "status": "ELIGIBLE",
32 "dispute_categories": [
33 "ITEM_NOT_RECEIVED",
34 "UNAUTHORIZED_TRANSACTION"
35 ]
36 },
37 "seller_receivable_breakdown": {
38 "gross_amount": {
39 "currency_code": "USD",
40 "value": "100.00"
41 },
42 "paypal_fee": {
43 "currency_code": "USD",
44 "value": "3.98"
45 },
46 "net_amount": {
47 "currency_code": "USD",
48 "value": "96.02"
49 }
50 },
51 "links": [
52 {
53 "href": "https://api-m.sandbox.paypal.com/v2/payments/captures/3B017991HX624902V",
54 "rel": "self",
55 "method": "GET"
56 },
57 {
58 "href": "https://api-m.sandbox.paypal.com/v2/payments/captures/3B017991HX624902V/refund",
59 "rel": "refund",
60 "method": "POST"
61 },
62 {
63 "href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/4TH21426N05692944",
64 "rel": "up",
65 "method": "GET"
66 }
67 ],
68 "create_time": "2022-08-08T23:13:35Z",
69 "update_time": "2022-08-08T23:13:35Z"
70 }
71 ]
72 }
73 }
74 ],
75 "payer": {
76 "name": {
77 "given_name": "Firstname",
78 "surname": "Lastname"
79 },
80 "email_address": "email@example.com",
81 "payer_id": "AJM9JTWQJCFTA",
82 "address": {
83 "country_code": "US"
84 }
85 },
86 "links": [
87 {
88 "href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/4TH21426N05692944",
89 "rel": "self",
90 "method": "GET"
91 }
92 ]
93}

Use payment token on behalf of payer

When the payer isn't present to check out, you can use the payment method token to create an order on behalf of the payer.

1. Retrieve a payer's payment method token

If you stored the payment token the payer created on your site, skip this step.

To make a payment on behalf of the payer, retrieve the payment token they created. You'll need the customer ID that you assigned to this payer when saving the payment method.

Sample API request

API endpoint used: Payment tokens

1curl -v -k -X GET 'https://api-m.sandbox.paypal.com/v3/vault/payment-tokens?customer_id=customer_4029352051' \
2 -H 'Authorization: Bearer ACCESS-TOKEN' \
3 -H 'Content-Type: application/json'

Modify the code

  1. Copy the code sample.
  2. Change ACCESS-TOKEN to your sandbox access token.
  3. Pass the PayPal-generated customer.id to retrieve the payment token details associated with the payer.
  4. If stored in the payment token, the response will return the Merchant Customer ID.

Sample response

1{
2 "customer": {
3 "id": "customer_4029352051"
4 },
5 "payment_tokens": [
6 {
7 "id": "jwgvx42",
8 "customer": {
9 "id": "customer_4029352051"
10 },
11 "payment_source": {
12 "paypal": {
13 "description": "Description for PayPal to be shown to PayPal payer",
14 "shipping": {
15 "name": {
16 "full_name": "Firstname Lastname"
17 },
18 "address": {
19 "address_line_1": "2211 N First Street",
20 "address_line_2": "Building 17",
21 "admin_area_2": "San Jose",
22 "admin_area_1": "CA",
23 "postal_code": "95131",
24 "country_code": "US"
25 }
26 },
27 "usage_type": "MERCHANT",
28 "customer_type": "CONSUMER",
29 "name": {
30 "given_name": "Firstname",
31 "surname": "Lastname",
32 "full_name": "Firstname Lastname"
33 },
34 "email_address": "email@example.com",
35 "payer_id": "AJM9JTWQJCFTA",
36 "phone": {
37 "phone_number": {
38 "country_code": "US",
39 "national_number": "408-208-9263"
40 }
41 }
42 }
43 },
44 "links": [
45 {
46 "rel": "self",
47 "href": "https://api-m.sandbox.paypal.com/v3/vault/payment-tokens/jwgvx42",
48 "method": "GET",
49 "encType": "application/json"
50 },
51 {
52 "rel": "delete",
53 "href": "https://api-m.sandbox.paypal.com/v3/vault/payment-tokens/jwgvx42",
54 "method": "DELETE",
55 "encType": "application/json"
56 }
57 ]
58 }
59 ],
60 "links": [
61 {
62 "rel": "self",
63 "href": "https://api-m.sandbox.paypal.com/v3/vault/payment-tokens?customer_id=customer_4029352051&page=1&page_size=5&total_required=false",
64 "method": "GET",
65 "encType": "application/json"
66 },
67 {
68 "rel": "first",
69 "href": "https://api-m.sandbox.paypal.com/v3/vault/payment-tokens?customer_id=customer_4029352051&page=1&page_size=5&total_required=false",
70 "method": "GET",
71 "encType": "application/json"
72 },
73 {
74 "rel": "last",
75 "href": "https://api-m.sandbox.paypal.com/v3/vault/payment-tokens?customer_id=customer_4029352051&page=1&page_size=5&total_required=false",
76 "method": "GET",
77 "encType": "application/json"
78 }
79 ]
80}

Step result

A successful request results in the following:

  1. An HTTP response code of 200 OK.
  2. Payment method details and status for the provided payment token.
  3. HATEOAS links:
RelMethodDescription
selfGETMake a GET request to this link to retrieve data about the saved payment method.
deleteDELETEMake a DELETE request to delete the payment token from the vault.

2. Use payment method token with checkout

After you get the payment method token ID, you can use a payment method token with checkout to create your order.

Webhooks

EventTriggerPayment methods
VAULT.PAYMENT-TOKEN.CREATEDA payment token is created to save a payment method.Cards and PayPal
VAULT.PAYMENT-TOKEN.DELETEDA payment token is deleted. The payer's payment method is no longer saved to the PayPal vault.Cards and PayPal
VAULT.PAYMENT-TOKEN.DELETION-INITIATEDA request to delete a payment token has been submitted to the Payment Method Tokens API.PayPal

For more information on webhooks, see webhooks.

Next steps

See also