Search code examples
expresspaypal

How can I create a setup token or payment token using the orders API from PayPal in js?


I've been trying a couple days now to save the payment method of the user of my website, so that only have to sign in when they intend to make a purchase, without having to enter in financial information, but i've found no way to do that. in order to save a payment source, I would first need to create a setup token, and then use that setup token to exchange for a permanent payment token, that I can store in my database. I'm using the PayPal orders API, along with their advanced checkout system, and the way that advanced checkout works is that when the user enters their financial information to get charged, instead of most of the work being handled on my server side, most of it is handled on the client side by PayPal. In the PayPal documentation, it says that the capture order response will return a payment token, that I can store in my database, but it seems to only do so when im not using advanced checkout.

so im wondering is there some way for the capture order response to include a payment token even when using advanced checkout, because PayPal's documentation says I need to include this in the body of the request to capture:

"paypal": {
  "attributes": {
     "vault": {
        "store_in_vault": "ON_SUCCESS",
        "usage_type": "MERCHANT"
      }
   }
}

but when I insert this code into the capture order request body, it tells me I need a return URL or cancel URL, but then the advance checkout wouldn't work, because it handles validation on the client side! and the whole thing becomes a mess.

but basically im asking for an easy way to create a setup token or payment token using the orders and with advanced checkout, because otherwise the whole thing becomes a very big mess.

I've tried everything, and from my experience there is no way to make it so that I can create a setup token or payment token, AND have advanced checkout.


Solution

  • Have you tried following the Save Payment Methods documentation? For advanced, attributes goes in the card object.

    curl -v -X POST https://api-m.sandbox.paypal.com/v2/checkout/orders \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer 'ACCESS-TOKEN'" \
     -d '{
          "intent": "CAPTURE",
          "purchase_units": [{
            "reference_id": "d9f80740-38f0-11e8-b467-0ed5f89f718b",
            "amount": {
              "currency_code": "USD",
              "value": "100.00"
            }
          }],
          "payment_source": {
            "card": {
              "name": "Firstname Lastname",
              "billing_address": {
                "address_line_1": "123 Main St.",
                "address_line_2": "Unit B",
                "admin_area_2": "Anytown",
                "admin_area_1": "CA",
                "postal_code": "12345",
                "country_code": "US"
              },
              "attributes": {
                "vault": {
                  "store_in_vault": "ON_SUCCESS"
                },
                "verification": {
                  "method": "SCA_ALWAYS"
                }
              }
            }
          }
        }
    
    

    The PayPal checkout is something you should implement in parallel, since all Advanced integrations also need to have PayPal as a payment option as an equal choice for the buyer (this is in the user agreement).