PayPal

Migrating from checkout.js to the PayPal JS SDK

Availability

This page is only relevant for JavaScript v3 PayPal custom integrations (not Drop-in UI).

This guide won't be relevant to you if:

Previously, Braintree documented how to integrate with version 4 of PayPal's JavaScript SDK, called checkout.js. While that integration is still supported, Braintree now offers support for version 5 of the PayPal JavaScript SDK.

This guide walks through how to upgrade from the old version to the new version.

Script tagAnchorIcon

Using checkout.js, it was necessary to load the script tag separately from the Braintree SDKs:

  1. HTML
Code snippet NOT FOUND

With the PayPal JS SDK, all you need to do is load the Braintree SDK and then call loadPayPalSDK on the paypalCheckoutInstance that is created.

  1. HTML
Code snippet NOT FOUND
  1. Callback
  2. Promise
Code snippet NOT FOUND

A configuration object can be passed into loadPayPalSDK that will overwrite the default properties for configuring the PayPal JS SDK:

  1. Callback
  2. Promise
Code snippet NOT FOUND

For all possible values, review the documentation for supported query params in the PayPal JS SDK.

Alternatively, you can load the PayPal JS SDK directly on the page:

  1. HTML
Code snippet NOT FOUND

Replace your-sandbox-or-prod-client-id with the PayPal client ID found in the Braintree Control Panel under Settings > Account Settings > PayPal > Options > PayPal Client ID.

This ID will be different for your sandbox account and your production account.

There are many other query parameters that can be used in addition to the client-id param.

Setting up the SDKAnchorIcon

How to initialize the PayPal SDKAnchorIcon

The biggest difference between checkout.js and the PayPal JS SDK is in how PayPal button(s) are rendered.

In checkout.js, paypal.Button.render was called with your PayPal JS SDK config and selector for the PayPal button.

  1. JavaScript
Code snippet NOT FOUND

For the PayPal JS SDK, call paypal.Buttons with your PayPal JS SDK config, and then call render on the result with your selector for your PayPal button.

  1. JavaScript
Code snippet NOT FOUND

How to render a PayPal buttonAnchorIcon

In checkout.js, it was necessary to pass a style parameter to configure the display of the button.

  1. JavaScript
Code snippet NOT FOUND

For the PayPal JS SDK, use fundingSource: paypal.FUNDING.PAYPAL in your configuration to load only a PayPal button (by default, the integration will try to render all eligible payment methods). In addition, the PayPal JS SDK supports the style parameter:

  1. JavaScript
Code snippet NOT FOUND

Creating a payment resourceAnchorIcon

In checkout.js, a payment function was used to create the payment resource. This could be used for either the Vault or Checkout integration flows.

  1. JavaScript
Code snippet NOT FOUND

The payment function has been renamed to createOrder for the Checkout flow or createBillingAgreement for the Vault flow.

  1. JavaScript
Code snippet NOT FOUND
  1. JavaScript
Code snippet NOT FOUND

Tokenizing the PayPal accountAnchorIcon

In checkout.js, an onAuthorize function was used to finish the flow.

  1. Callback
  2. Promise
Code snippet NOT FOUND

The onAuthorize function in the PayPal JS SDK config has been renamed to onApprove.

  1. JavaScript
  2. JavaScript
Code snippet NOT FOUND
Code snippet NOT FOUND

Other considerationsAnchorIcon

onCancel and onError functions are unchanged.

env no longer needs to be passed as the environment is pulled from the PayPal client-id.

Checkout flow vs Vault flowAnchorIcon

With checkout.js, you could dynamically choose whether the Checkout flow or the Vault flow would be used. With the PayPal JS SDK, this decision needs to be made during the setup of the SDK.

With the Checkout flow, you will use a createOrder function in your PayPal JS SDK config to initialize the login. With the Vault flow, you will pass vault: true when calling loadPayPalSDK and use a createBillingAgreement function to initialize the login.

Checkout flowAnchorIcon

A typical PayPal integration using the Checkout flow with checkout.js would look something like:

  1. Callback
  2. Promise
Code snippet NOT FOUND

A full PayPal JS SDK integration with currency and intent set may look like:

  1. Callback
  2. Promise
Code snippet NOT FOUND

Vault flowAnchorIcon

vault: true must be passed when calling loadPayPalSDK.

  1. Callback
  2. Promise
Code snippet NOT FOUND