JS SDK Reference for Payment Fields

DocsCurrentLast updated: November 17th 2023, @ 4:51:27 pm


Query paramDefaultDescription
client-idn/aYour PayPal REST client ID. This identifies your PayPal account and determines where transactions are paid.
componentsbuttonsA comma-separated list of components to enable. The buttons, payment-fields, marks, and funding-eligibility components are required for payment fields components.
enable-fundingnoneFunding sources to allow from showing in the buttons and marks.

By default, PayPal JavaScript SDK provides smart logic to display only appropriate marks and buttons for the current buyer. This optional parameter bypasses the buyer country check for desired payment methods.

For example:

https://www.paypal.com/sdk/js?client-id=YOUR_CLIENT_ID&enable-funding=ideal,bancontact

currencyUSDCurrency of the transaction.
localeautomaticBy default, PayPal detects the preferred locale for the buyer based on their geolocation and browser preferences. It is recommended to pass this parameter with a supported locale if you would like the payment fields components to render in the same language as the rest of your site.
intentcaptureThe funds are captured immediately, while the buyer is present on your site.
committrueThis indicates that the final amount won't change after the buyer returns from PayPal to your site.
vaultfalseDisplays all funding sources including those that don’t support vaulting.

paypal.Buttons(options)

style

Customize your buttons by passing in the style option.

paypal.Buttons({
  style: {
    layout: 'vertical',
    label:  'paypal'
  }
}).render('#paypal-button-container');

Note: Alternative payment methods supports only vertical layout.

See additional, optional parameters.

createOrder

The createOrder parameter sets up the details of the transaction. It's called when the buyer clicks the PayPal button, which launches the PayPal Checkout window where the buyer logs in and approves the transaction on the paypal.com website.

<script>
  paypal.Buttons({
    // Order is created on the server and the order id is returned
    createOrder() {
      return fetch("/my-server/create-paypal-order", {
        method: "post",
        headers: {
          "Content-Type": "application/json",
        }, 
        // use the "body" param to optionally pass additional order information
        // like product skus and quantities
        body: JSON.stringify({
          cart: [
            {
              sku: "YOUR_PRODUCT_STOCK_KEEPING_UNIT",
              quantity: "YOUR_PRODUCT_QUANTITY",
            },
          ],
        }),
      })
      .then((response) => response.json())
      .then((order) => order.id);
    },
  }).render('#paypal-button-container');
</script>

onApprove

The onApprove function is called after the buyer approves the transaction.

Make the capture call from your server to capture the funds from the transaction and show a message to the buyer to let them know the transaction is successful.

onCancel

When a buyer cancels a payment, they typically return to the parent page. You can instead use the onCancel function to show a cancellation page or return to the shopping cart.

Data attributes

orderId ID of the order.

paypal.Buttons({
  onCancel: function (data) {
    // Show a cancel page, or return to cart
  }
}).render('#paypal-button-container');

onError

If an error prevents buyer checkout, alert the user that an error has occurred with the buttons using the onError callback:

paypal.Buttons({
  onError: function (err) {
    // For example, redirect to a specific error page
    window.location.href = "/your-error-page-here";
  }
}).render('#paypal-button-container');

Note: This error handler is a catch-all. Errors at this point are not expected to be handled beyond showing a generic error message or page.

paypal.Buttons().isEligible

Before rendering marks and payment fields, you can use paypal.Buttons().isEligible to check if the funding source is eligible.

// Loop over each funding source / payment method
paypal.getFundingSources().forEach(function(fundingSource) {

  // Initialize the buttons
  var button = paypal.Buttons({
    fundingSource: fundingSource
  });

  // Check if the button is eligible
  if (button.isEligible()) {

    // Render the standalone button for that funding source
    button.render('#paypal-button-container');
  }
});

Funding

This table includes the available alternative payment methods.

Funding sourcePayment button
paypal.FUNDING.BANCONTACTBancontact
paypal.FUNDING.EPSeps
paypal.FUNDING.GIROPAYgiropay
paypal.FUNDING.IDEALiDEAL
paypal.FUNDING.BLIKBLIK
paypal.FUNDING.MYBANKMyBank
paypal.FUNDING.P24Przelewy24
paypal.FUNDING.SOFORTSOFORT (Legacy) *
paypal.FUNDING.APPLEPAYApplePay

* Important: The SOFORT payment method will be deprecated by the end of September 2024. New integrations can't accept SOFORT payments as of October 20th, 2023.