Change payment methods

DOCS

Last updated: Sept 23rd, 9:31pm

Give your buyers the option to change the payment method for a specific transaction. For example, buyers might choose a credit card with more points or a bank account with more funds.

This page is for merchants who have a PayPal v1/billing agreements REST API billing agreement with their buyer. When changing the payment method, the buyer can select an existing payment method or add a new one.

Know before you code

  • The buyer must already have a billing agreement with the merchant before they can change their payment method. The billing agreement ID should be in your records.
  • The buyer can change the payment option only if they're present to complete the transaction.
  • This integration works for intent capture and authorize transactions.
  • This integration uses the v2/orders REST API.

How it works

The following is a sample workflow:

  1. Create an order by calling the v2/orders API and passing the Billing Agreement ID in the application_context object.
  2. Get the HATEOAS URL from the response.
  3. On your website, display a hyperlink using the HATEOAS URL. Give the hyperlink a name, such as Options. You can use JavaScript to create a pop-up window when the user selects the hyperlink.
    options
    Figure 1: Checkout page with the option to change the payment method.
  4. Your buyer selects the hyperlink and changes the payment method.
  5. Your buyer places the order.

Step 1: Create an order

Copy the following code and modify it.

Sample request

API endpoint used: Create order

    1curl -v -X POST https://api-m.sandbox.paypal.com/v2/checkout/orders \
    2-H "Content-Type: application/json" \
    3-H "Authorization: Bearer Access-Token" \
    4-d '{
    5 "intent": "CAPTURE",
    6 "payer": {
    7 "address": {
    8 "address_line_1": "billing",
    9 "address_line_2": "address",
    10 "admin_area_2": "fremont",
    11 "admin_area_1": "CA",
    12 "postal_code": "94536",
    13 "country_code": "US"
    14 }
    15 },
    16 "application_context": {
    17 "return_url": "https://example.com/return",
    18 "cancel_url": "https://example.com/cancel",
    19 "preferred_payment_source": {
    20 "token": {
    21 "type": "BILLING_AGREEMENT",
    22 "id": "B-2SV62607B0766021W"
    23 }
    24 }
    25 },
    26 "purchase_units": [
    27 {
    28 "description": "Sporting Goods",
    29 "custom_id": "CUST-HighFashions",
    30 "soft_descriptor": "HighFashions",
    31 "amount": {
    32 "currency_code": "USD",
    33 "value": "220.00",
    34 "breakdown": {
    35 "item_total": {
    36 "currency_code": "USD",
    37 "value": "180.00"
    38 },
    39 "shipping": {
    40 "currency_code": "USD",
    41 "value": "20.00"
    42 },
    43 "handling": {
    44 "currency_code": "USD",
    45 "value": "10.00"
    46 },
    47 "tax_total": {
    48 "currency_code": "USD",
    49 "value": "20.00"
    50 },
    51 "gift_wrap": {
    52 "currency_code": "USD",
    53 "value": "0.00"
    54 },
    55 "shipping_discount": {
    56 "currency_code": "USD",
    57 "value": "10.00"
    58 }
    59 }
    60 },
    61 "items": [
    62 {
    63 "name": "T-Shirt",
    64 "description": "Green XL",
    65 "sku": "sku01",
    66 "unit_amount": {
    67 "currency_code": "USD",
    68 "value": "90.00"
    69 },
    70 "tax": {
    71 "currency_code": "USD",
    72 "value": "10.00"
    73 },
    74 "quantity": "1",
    75 "category": "PHYSICAL_GOODS"
    76 },
    77 {
    78 "name": "Shoes",
    79 "description": "Running, Size 10.5",
    80 "sku": "sku02",
    81 "unit_amount": {
    82 "currency_code": "USD",
    83 "value": "45.00"
    84 },
    85 "tax": {
    86 "currency_code": "USD",
    87 "value": "5.00"
    88 },
    89 "quantity": "2",
    90 "category": "PHYSICAL_GOODS"
    91 }
    92 ],
    93 "shipping": {
    94 "method": "United States Postal Service",
    95 "address": {
    96 "address_line_1": "123 Townsend St",
    97 "address_line_2": "Floor 6",
    98 "admin_area_2": "San Francisco",
    99 "admin_area_1": "CA",
    100 "postal_code": "94107",
    101 "country_code": "US"
    102 }
    103 }
    104 }
    105 ]
    106}'

    Modify the code

    After you copy the code in the sample request, modify the following:

    • Change Access-Token to your access token.
    • Change return_url to the URL of the webpage where buyers land after completing a successful form submission.
    • Change cancel_url to the URL of the webpage where buyers land after cancelling a form submission.

    Step result

    A successful request returns:

    • A return status code of HTTP 201 Created.
    • An order ID. In the sample response, the ID is 38F59383Y76396014.
    • Multiple HATEOAS links.

    Sample response

      1{
      2 "id": "38F59383Y76396014",
      3 "intent": "CAPTURE",
      4 "purchase_units": [
      5 {
      6 "reference_id": "default",
      7 "amount": {
      8 "currency_code": "USD",
      9 "value": "220.00",
      10 "breakdown": {
      11 "item_total": {
      12 "currency_code": "USD",
      13 "value": "180.00"
      14 },
      15 "shipping": {
      16 "currency_code": "USD",
      17 "value": "20.00"
      18 },
      19 "handling": {
      20 "currency_code": "USD",
      21 "value": "10.00"
      22 },
      23 "tax_total": {
      24 "currency_code": "USD",
      25 "value": "20.00"
      26 },
      27 "shipping_discount": {
      28 "currency_code": "USD",
      29 "value": "10.00"
      30 }
      31 }
      32 },
      33 "payee": {
      34 "email_address": "hjohari-facilitator@paypal.com",
      35 "merchant_id": "N4XE6SG5NQP6N"
      36 },
      37 "description": "Sporting Goods",
      38 "custom_id": "CUST-HighFashions",
      39 "soft_descriptor": "HighFashions",
      40 "items": [
      41 {
      42 "name": "T-Shirt",
      43 "unit_amount": {
      44 "currency_code": "USD",
      45 "value": "90.00"
      46 },
      47 "tax": {
      48 "currency_code": "USD",
      49 "value": "10.00"
      50 },
      51 "quantity": "1",
      52 "description": "Green XL",
      53 "sku": "sku01",
      54 "category": "PHYSICAL_GOODS"
      55 },
      56 {
      57 "name": "Shoes",
      58 "unit_amount": {
      59 "currency_code": "USD",
      60 "value": "45.00"
      61 },
      62 "tax": {
      63 "currency_code": "USD",
      64 "value": "5.00"
      65 },
      66 "quantity": "2",
      67 "description": "Running, Size 10.5",
      68 "sku": "sku02",
      69 "category": "PHYSICAL_GOODS"
      70 }
      71 ],
      72 "shipping": {
      73 "method": "United States Postal Service",
      74 "address": {
      75 "address_line_1": "123 Townsend St",
      76 "address_line_2": "Floor 6",
      77 "admin_area_2": "San Francisco",
      78 "admin_area_1": "CA",
      79 "postal_code": "94107",
      80 "country_code": "US"
      81 }
      82 }
      83 }
      84 ],
      85 "payer": {
      86 "address": {
      87 "address_line_1": "billing",
      88 "address_line_2": "address",
      89 "admin_area_1": "CA",
      90 "postal_code": "94536",
      91 "country_code": "US"
      92 }
      93 },
      94 "create_time": "2020-01-09T03:20:53Z",
      95 "links": [
      96 {
      97 "href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/38F59383Y76396014",
      98 "rel": "self",
      99 "method": "GET"
      100 },
      101 {
      102 "href": "https://www.sandbox.paypal.com/checkoutnow?token=38F59383Y76396014",
      103 "rel": "approve",
      104 "method": "GET"
      105 },
      106 {
      107 "href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/38F59383Y76396014",
      108 "rel": "update",
      109 "method": "PATCH"
      110 },
      111 {
      112 "href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/38F59383Y76396014/capture",
      113 "rel": "capture",
      114 "method": "POST"
      115 }
      116 ],
      117 "status": "CREATED"
      118}

      Step 2: Redirect the buyer

      Use the HATEOAS URL returned in step 1 to redirect the buyer so they can login to their PayPal account, choose the payment method, and approve the transaction.

        1{
        2"href": "https://www.sandbox.paypal.com/checkoutnow?token={id}",
        3"rel": "approve",
        4"method": "GET"
        5}

        Replace id with the order ID returned from step 1.

        Step 3: Capture the order

        After the buyer chooses the payment method and approves the transaction, the PayPal window closes. The API then redirects the buyer to the return_url from the Create order API call.

        Once the buyer confirms their order, capture the order by calling the HATEOAS link with rel:capture to complete the transaction.

        Copy the following code and replace id with the order ID.

        Sample request

        API endpoint used: Capture the payment

          1{
          2"href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/{id}/capture",
          3"rel": "capture",
          4"method": "POST"
          5}

          After you copy the code in the sample request, replace id with the order ID.

          Step result

          A successful request returns:

          • The HTTP 201 Created status code.
          • A JSON response body that shows the captured payment details.

          Sample response

            1{
            2 "id": "38F59383Y76396014",
            3 "purchase_units": [
            4 {
            5 "reference_id": "default",
            6 "shipping": {
            7 "name": {
            8 "full_name": "test buyer"
            9 },
            10 "address": {
            11 "address_line_1": "123 Townsend St",
            12 "address_line_2": "Floor 6",
            13 "admin_area_2": "San Francisco",
            14 "admin_area_1": "CA",
            15 "postal_code": "94107",
            16 "country_code": "US"
            17 }
            18 },
            19 "payments": {
            20 "captures": [
            21 {
            22 "id": "7R598468U3262991D",
            23 "status": "COMPLETED",
            24 "amount": {
            25 "currency_code": "USD",
            26 "value": "220.00"
            27 },
            28 "final_capture": true,
            29 "seller_protection": {
            30 "status": "ELIGIBLE",
            31 "dispute_categories": [
            32 "ITEM_NOT_RECEIVED",
            33 "UNAUTHORIZED_TRANSACTION"
            34 ]
            35 },
            36 "seller_receivable_breakdown": {
            37 "gross_amount": {
            38 "currency_code": "USD",
            39 "value": "220.00"
            40 },
            41 "paypal_fee": {
            42 "currency_code": "USD",
            43 "value": "6.68"
            44 },
            45 "net_amount": {
            46 "currency_code": "USD",
            47 "value": "213.32"
            48 }
            49 },
            50 "custom_id": "CUST-HighFashions",
            51 "links": [
            52 {
            53 "href": "https://api-m.sandbox.paypal.com/v2/payments/captures/7R598468U3262991D",
            54 "rel": "self",
            55 "method": "GET"
            56 },
            57 {
            58 "href": "https://api-m.sandbox.paypal.com/v2/payments/captures/7R598468U3262991D/refund",
            59 "rel": "refund",
            60 "method": "POST"
            61 },
            62 {
            63 "href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/38F59383Y76396014",
            64 "rel": "up",
            65 "method": "GET"
            66 }
            67 ],
            68 "create_time": "2020-01-09T03:42:24Z",
            69 "update_time": "2020-01-09T03:42:24Z"
            70 }
            71 ]
            72 }
            73 }
            74 ],
            75 "payer": {
            76 "name": {
            77 "given_name": "test",
            78 "surname": "buyer"
            79 },
            80 "email_address": "hjohari-buyer@paypal.com",
            81 "payer_id": "84BRDHAWM4VE8",
            82 "phone": {
            83 "phone_number": {
            84 "national_number": "4087991221"
            85 }
            86 },
            87 "address": {
            88 "country_code": "US"
            89 }
            90 },
            91 "links": [
            92 {
            93 "href": "https://api-m.sandbox.paypal.com/v2/checkout/orders/38F59383Y76396014",
            94 "rel": "self",
            95 "method": "GET"
            96 }
            97 ],
            98 "status": "COMPLETED"
            99}

            See also

            Billing Agreements and Reference Transactions

            We use cookies to improve your experience on our site. May we use marketing cookies to show you personalized ads? Manage all cookies