Skip to main content

Set up PayPal on Braintree v2.0

Zuora

Set up PayPal on Braintree v2.0

This article describes how to set up PayPal on the Braintree v2.0 gateway integration by using either the JavaScript SDK approach or the REST API approach.

Set up PayPal through the JavaScript SDK approach

You can add a PayPal button to your checkout flow by integrating with a JavaScript SDK provided by Zuora. Complete the following tasks to implement the integration:

  1. Complete prerequisite tasks.
  2. Implement the client-side SDK integration.
  3. Implement the server-side API integration.
  4. Perform integration testing.

Complete prerequisite tasks

  1. Turn on the Payment Form feature.

    Submit a request at Zuora Global Support to turn on the Payment Form feature on your tenant. The JavaScript SDK integration utilizes the publishable key from the Payment Form feature for authentication. This integration supports embedding the PayPal button within an iframe hosted by Zuora.

  2. Configure a Braintree v2.0 gateway instance.

    Ensure that a Braintree v2.0 gateway instance has been created and is in the active status on your Zuora tenant.

  3. Configure the Braintree gateway in Braintree's console:
    1. Log in to Braintree's console, such as https://sandbox.braintreegateway.com/login.
    2. Click the setting icon and then click Account Settings.

      Account setting in Braintree console

    3. On the Processing Options tab, link the PayPal test account.

      Link PayPal in Braintree console

Implement the client-side SDK integration

Step 1. Load zuora.js

Import the Zuora JavaScript client library to your page:

<script src="https://js.zuora.com/payment/v3/zuora.js"></script>

Step 2. Create a container

Create a container for the PayPal button on your page and place it where you want the button to be rendered. Replace paypal-button-container with the ID you want to use.

<div id="paypal-button-container">
<!-- The PayPal button will be inserted here. -->
</div>

Step 3. Render the PayPal button

  1. Copy the publishable key from Payment Form. In the Zuora UI, click your username in the upper right and navigate to Settings > Payments > Dynamic Hosted Payment Form. On the Publishable Keys tab, copy the key.
  2. Initialize an instance of the Zuora object with your publishable key.
  3. Populate the payment request parameters and generate a payment session when the end customers click the Pay button. The following table describes the payment request parameters:
    Parameter Type Description
    currency string

    Required.

    The ISO 4217 alphabetic currency code, such as "USD".

    amount string

    Required.

    The total amount for the payment. This field is required if intent is authorize.

    countryCode string

    The ISO 3166-1 alpha-2 code of the country where the transaction is processed, such as "US".

    If it is not specified, the default value US is used.

    locale string

    The locale code, which is the combination of ISO language code and ISO country code, such as "en_US".

    If it is not specified, the default value en_US is used.

    vault boolean To store the payment method, set this field to true.
    intent string The intent of the transaction. Currently, only authorize is supported.
    provider object

    The payment gateway instance that will process the payment. Specify the following nested fields:

    • name: The type of the payment gateway. To support the PayPal integration on Braintree v2.0, specify Braintree. This field is case-sensitive.
    • paymentGatewayId: The ID of the Braintree v2.0 payment gateway instance.

    For more information about payment flow implementation by using these parameters, see Payment flow use cases in the later section.

  4. Mount the button component to the container.
  5. Handle the payment result.  

Here is an example of the code implementation:

// The publishable key can be found on the Payment Form setting page.
const publishableKey = "pk_rO0AB..........";
const renderForm = () => {
  const zuora = Zuora(publishableKey);
  zuora.createPayPalButton({
    countryCode: "US",
    locale: "en_US",
    currency: "USD",
    // The amount to be paid. this is required when intent is "authorize".
    amount: "1.00", 
    // set it to true to enable vaulting.
    vault: true, 
    // The intent of the transaction. only "authorize" is supported
    intent: "authorize", 
    provider: {
      name: "Braintree",
      // This is the Braintree v2 gateway instance id.
      paymentGatewayId: "4028839390f481ae0190f491084f0004",
    },
    createPaymentSession: () => {
      // it MUST return a Promise here.
      return new Promise((resolve, reject) => {
        // Send request to your server to create a payment session.
        // The server should return a payment session id.
        // then resolve the promise with the payment session id.
        resolve("payment-session-id-from-server");
      });
    },
    onComplete: (result) => {
      // handle the transaction result
      // when result.success is true, the transaction is successful.
      // result.paymentMethodId is the created payment method id.
      // result.paymentId is the processed payment id.
      //
      // When result.success is false, the transaction is failed.
      // result.error will contain the error code and message.
    }
  }).then(function(paypalButton) {
    paypalButton.mount("#paypal-button-container")
  }).catch(function(error) {
    // failed to create the PayPal button
    console.error(error);
  });
}; 

Implement the server-side API integration to create payment session

In your server, make an API request to the Create a payment session endpoint. For details about the API request parameters, see Create a payment session in the API Reference.

For more information about payment flow implementation, see Payment flow use cases in the later section.

Payment flow use cases

To configure the payment flow mode, specify parameters for creating the server-side payment session and the client-side button, as outlined in the following table:

Payment Flow Create Payment Session Parameters Create PayPal Button Parameters
Create and save the PayPal payment method
  • amount: 0
  • processPayment: false
  • storePaymentMethod: true
  • amount: 0
  • vault: true
Process a one-time payment without saving the payment method
  • amount: Specify the amount number. It must match the amount in the button parameter.
  • processPayment: true
  • storePaymentMethod: false
  • amount: Specify the amount number.
  • vault: false
  • intent: authorize
Process the first payment and save the payment method for subsequent recurring payments
  • amount: Specify the amount number. It must match the amount in the button parameter.
  • processPayment: true
  • storePaymentMethod: true
  • amount: Specify the amount number.
  • vault: true
  • intent: authorize

Perform integration testing

In client code, make a call to your backend API to create a payment session.

Set up PayPal through the REST API approach

You can create a PayPal payment method on Braintree v2.0 through the Create a payment method REST API operation, with type set to PayPalCP. For details about the required and optional request fields, see the Create a payment method API Reference document.

To create a tokenized PayPal payment method, use one of the following options:

Option Description Example
Pass in the account information and the tokenization flag

Pass in the following fields:

  • type: PayPalCP
  • accountKey
  • BAID
  • email
  • tokenize: true

The token, which is the payment method ID, will be generated. The BAID, email, and token information will all be stored in Zuora, and token will be used in the subsequent recurring payments.

{
   "type": "PayPalCP",
   "accountKey": "8a90d6128d45df2b018d4b90681c05x0",
   "BAID": "I-1TJ3GAGG82Y9",
   "email": "customer@example.com",
   "tokenize": true
}
Pass in the existing token

Pass in the following fields:

  • type: PayPalCP
  • accountKey
  • tokens
    • gatewayType: Braintree
    • tokenId: paymentMethodId
  • tokenize: true

The token will be stored in Zuora and used in the subsequent recurring payments.

{
   "type": "PayPalCP",
   "accountKey": "402881868ab1b937018ab2314b91234a",
    "tokens": {
        "gatewayType": "Braintree",
        "tokenId": "testTokenId"
    },
   "tokenize": true
}

For tokenized payment methods, you must validate them in Zuora so that payments made with these payment methods are successful. To validate tokenized payment methods, turn on the following settings on the gateway instance configuration page:

  • Verify new payment method
  • Verify updated payment method

View and export payment method data

After a PayPal payment method is successfully created, you can retrieve this payment method through the UI, API, Data Source Export, and Data Query.