Set Up Your Serveranchor

Install and configureanchor

Install the Braintree node package:

npm install braintree
note

See the Braintree Node Version Changelog.

Construct your gateway objectanchor

In your code, you must first construct a gateway object with the environment-specific (sandbox or production) access token provided in your PayPal dashboard:

  1. Node
var gateway = new braintree.BraintreeGateway({
  accessToken: useYourAccessToken
});

This must be called before performing any method (e.g. client token generation, authorizations, captures).

note

Keep in mind that the step to initialize the gateway using the Express Checkout integration is different than that used for a direct integration with Braintree.

Generate a client tokenanchor

Your server is responsible for generating a client token, which contains all the authorization and configuration information your client needs to initialize the client SDK to communicate with Braintree. The client-side page covers the client side of the exchange.

Send a client token to your clientanchor

Here is an example of how your server would generate and expose a client token:

  1. Node
app.get("/client_token", function (req, res) {
  gateway.clientToken.generate({}, function (err, response) {
    res.send(response.clientToken);
  });
});

How the token is used by the client may vary. In JavaScript integrations the client token is often included in the generated HTML/JS, although you could load the client token from an AJAX call instead. These methods are discussed in the client token setup section.

Receive a payment method nonce from your clientanchor

Once your customer has successfully completed the PayPal checkout flow your client will receive a payment_method_nonce representing customer payment authorization, which it then sends to your server.

Your server implementation is then responsible for receiving the payment_method_nonce and creating a transaction.

  1. Node
app.post("/checkout", function (req, res) {
  var nonce = req.body.payment_method_nonce;
  // Use payment method nonce here
});

Create a transactionanchor

You can create a transaction using a payment_method_nonce and an amount.

Collect device data from the client and include the device_data_from_the_client in the transaction.

Below is an example of all relevant PayPal parameters you can pass in your transaction call:

  1. Node
var gateway = new braintree.BraintreeGateway({
  accessToken: useYourAccessToken
});

var saleRequest = {
  amount: req.body.amount,
  merchantAccountId: "USD",
  paymentMethodNonce: req.body.nonce,
  deviceData: req.body.device_data,
  orderId: "Mapped to PayPal Invoice Number",
  descriptor: {
    name: "Descriptor displayed in customer CC statements. 22 char max"
  },
  shipping: {
    firstName: "Jen",
    lastName: "Smith",
    company: "Braintree",
    streetAddress: "1 E 1st St",
    extendedAddress: "5th Floor",
    locality: "Bartlett",
    region: "IL",
    postalCode: "60103",
    countryCodeAlpha2: "US"
  },
  options: {
    paypal: {
      customField: "PayPal custom field",
      description: "Description for PayPal email receipt"
    },
    submitForSettlement: true
  }
};

gateway.transaction.sale(saleRequest, function (err, result) {
  if (err) {
    res.send("<h1>Error:  " + err + "</h1>");
  } else if (result.success) {
    res.send("<h1>Success! Transaction ID: " + result.transaction.id + "</h1>");
  } else {
    res.send("<h1>Error:  " + result.message + "</h1>");
  }
});

Specify currencyanchor

The customer will be charged in the currency you specify in the merchant_account_id field on the transaction call. You should pass the same currency here that you specified on the client side.

important

If you don't pass the merchant_account_id field, the customer will be charged in the default currency for the country associated with your account.

The merchant_account_id field accepts the 3-letter currency code of each of the currencies currently enabled for your account. You can see these currencies on the My Apps & Credentials page in your PayPal Developer Dashboard.

Shipping addressesanchor

PayPal validates the shipping address, so it must follow the PayPal address conventions.

Seller Protectionanchor

By passing a shipping address, you may also be eligible for PayPal Seller Protection. You can check the status of Seller Protection as follows:

  1. Node
gateway.transaction.find("theTransactionId", function (err, transaction) {
  transaction.paypalAccount.sellerProtectionStatus;
  // "ELIGIBLE"
});

See alsoanchor

The below are links to reference pages used by merchants performing direct integrations with Braintree. If you have questions about your Express Checkout integration, please be sure to contact PayPal Support rather than Braintree Support.


Next Page: Testing and Go Live