SDKs overview

Braintree offers a suite of Software Development Kits (SDKs) designed to facilitate the integration of its payment gateway services into various applications. These SDKs are categorized into client-side and server-side components, addressing different aspects of the payment flow. Client-side SDKs, such as those for JavaScript, Android, and iOS, primarily handle the collection and tokenization of sensitive payment information directly from the user's device, helping to maintain PCI DSS compliance by reducing the scope of direct card data handling on merchant servers. Server-side SDKs, available in multiple programming languages, manage transactions, subscriptions, and other backend operations by interacting with the Braintree API. The SDKs aim to simplify complex payment processing tasks, allowing developers to focus on core application logic rather than low-level API interactions or security protocols.

The availability of SDKs across popular languages and platforms supports a broad range of integration scenarios, from e-commerce websites to mobile applications and subscription services. Braintree's SDKs abstract the underlying RESTful API, providing language-specific methods and objects for common operations such as creating transactions, managing customers, and handling webhooks. For detailed information, developers can consult the Braintree developer documentation.

Official SDKs by language

Braintree maintains official SDKs for both client-side and server-side integration. These SDKs are developed and supported by Braintree to ensure compatibility with the latest API versions and security standards. The client-side SDKs often include features like a pre-built Drop-in UI or customizable Hosted Fields, which simplify the user interface for payment collection and enhance security by isolating sensitive data entry. Server-side SDKs provide robust functionality for managing the full lifecycle of payments, including transaction processing, refunds, and recurring billing. Below is a summary of the key official SDKs:

Language/Platform Package/Module Install Command Example Maturity
JavaScript (Web) braintree-web, braintree-web-drop-in npm install braintree-web braintree-web-drop-in Stable, actively maintained
Android com.braintreepayments.api:braintree Add to build.gradle dependencies Stable, actively maintained
iOS/Swift Braintree (CocoaPods/Carthage) pod 'Braintree' Stable, actively maintained
Ruby braintree (Gem) gem install braintree Stable, actively maintained
Python braintree (PyPI) pip install braintree Stable, actively maintained
PHP braintree/braintree_php (Composer) composer require braintree/braintree_php Stable, actively maintained
Java com.braintreepayments.java:braintree (Maven/Gradle) Add to pom.xml or build.gradle dependencies Stable, actively maintained
.NET (C#) Braintree (NuGet) Install-Package Braintree Stable, actively maintained
Node.js braintree (npm) npm install braintree Stable, actively maintained
Go github.com/braintree/braintree_go go get github.com/braintree/braintree_go Stable, actively maintained

Installation

Installing Braintree SDKs typically involves using package managers native to each programming environment. The process generally includes adding the SDK as a dependency to your project and then configuring it with your Braintree credentials. For client-side SDKs like JavaScript, the integration often involves embedding a script or using a package manager like npm to include the library. For server-side languages, package managers such as RubyGems, pip, Composer, Maven/Gradle, npm, or NuGet are commonly used.

Client-Side SDKs:

  • JavaScript: For web applications, you can add braintree-web-drop-in via npm:
    npm install braintree-web-drop-in --save
    Alternatively, you can include it directly via CDN for quick testing, though package managers are recommended for production environments.
  • Android: Add the Braintree SDK dependency to your app's build.gradle file:
    dependencies {
        implementation 'com.braintreepayments.api:braintree:X.Y.Z' // Replace X.Y.Z with the latest version
    }
  • iOS (Swift/Objective-C): Use CocoaPods by adding to your Podfile:
    pod 'Braintree'
    Then run pod install. For Carthage, add github "braintree/braintree_ios" to your Cartfile.

Server-Side SDKs:

  • Ruby: Add to your Gemfile:
    gem 'braintree'
    Then run bundle install.
  • Python: Install using pip:
    pip install braintree
  • PHP: Install using Composer:
    composer require braintree/braintree_php
  • Java: If using Maven, add the dependency to your pom.xml:
    <dependency>
        <groupId>com.braintreepayments.java</groupId>
        <artifactId>braintree</artifactId&n>
        <version>X.Y.Z</version> 
    </dependency>
  • Node.js: Install using npm:
    npm install braintree
  • .NET: Install via NuGet Package Manager Console:
    Install-Package Braintree
  • Go: Fetch the package using go get:
    go get github.com/braintree/braintree_go

After installation, you will need to configure the SDK with your Braintree API credentials (Merchant ID, Public Key, Private Key) to authenticate your requests. These credentials can be found in your Braintree Control Panel by following the instructions for retrieving Braintree API credentials.

Quickstart example

This quickstart example demonstrates a basic server-side transaction using the Node.js SDK. The client-side part would involve using the Braintree Drop-in UI or Hosted Fields to collect payment information and obtain a paymentMethodNonce, which is then sent to your server. The server then uses this nonce to create a transaction.

Client-Side (HTML & JavaScript with Drop-in UI):

First, include the Braintree Drop-in UI on your checkout page:

<html>
<head>
  <meta charset="utf-8">
  <title>Checkout</title>
  <script src="https://js.braintreegateway.com/web/drop-in/1.33.0/js/drop-in.min.js"></script>
</head>
<body>
  <div id="dropin-container"></div>
  <button id="submit-button">Purchase</button>

  <script>
    var button = document.querySelector('#submit-button');

    braintree.dropin.create({
      authorization: '<YOUR_CLIENT_TOKEN_FROM_SERVER>', // Replace with actual client token
      container: '#dropin-container'
    }, function (createErr, instance) {
      button.addEventListener('click', function () {
        instance.requestPaymentMethod(function (requestPaymentMethodErr, payload) {
          // Send payload.nonce to your server
          fetch('/checkout', {
            method: 'POST',
            headers: {
              'Content-Type': 'application/json',
            },
            body: JSON.stringify({ paymentMethodNonce: payload.nonce })
          })
          .then(response => response.json())
          .then(data => {
            console.log(data); // Handle server response (success/failure)
            alert('Transaction complete!');
          })
          .catch(error => console.error('Error:', error));
        });
      });
    });
  </script>
</body>
</html>

Server-Side (Node.js):

This Node.js example assumes you have an Express server and have installed the Braintree Node.js SDK (npm install braintree express body-parser).

const braintree = require('braintree');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());

const gateway = new braintree.BraintreeGateway({
  environment: braintree.Environment.Sandbox, // Use Environment.Production for live transactions
  merchantId: 'YOUR_MERCHANT_ID',
  publicKey: 'YOUR_PUBLIC_KEY',
  privateKey: 'YOUR_PRIVATE_KEY'
});

// Route to generate a client token for client-side SDK initialization
app.get('/client_token', (req, res) => {
  gateway.clientToken.generate({}, (err, response) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.send(response.clientToken); // Send the client token to your client-side
    }
  });
});

// Route to process a payment (receive nonce from client-side)
app.post('/checkout', (req, res) => {
  const paymentMethodNonce = req.body.paymentMethodNonce;

  gateway.transaction.sale({
    amount: '10.00',
    paymentMethodNonce: paymentMethodNonce,
    options: {
      submitForSettlement: true
    }
  }, (err, result) => {
    if (result.success) {
      res.send({ success: true, transactionId: result.transaction.id });
    } else {
      res.status(500).send({ success: false, message: result.message });
    }
  });
});

app.listen(3000, () => {
  console.log('Server listening on port 3000');
});

This example demonstrates how to generate a client token for client-side initialization and then process a transaction using a nonce received from the client. Remember to replace placeholder values like <YOUR_CLIENT_TOKEN_FROM_SERVER>, YOUR_MERCHANT_ID, YOUR_PUBLIC_KEY, and YOUR_PRIVATE_KEY with your actual Braintree credentials. For security, never expose your private key on the client side.

Community libraries

While Braintree provides a robust set of official SDKs, the developer community sometimes contributes additional libraries or integrations that extend functionality or offer alternative approaches. These might include wrappers for less common languages, specific framework integrations (e.g., Django, Laravel plugins), or tools for local development and testing. However, when considering community-contributed libraries, it is important to evaluate their maintenance status, security practices, and compatibility with the latest Braintree API versions. Official Braintree documentation generally recommends using the official client-side SDKs and server-side SDKs for production applications due to their adherence to Braintree's security and performance standards.

For example, while Braintree officially supports a Go SDK, a community might develop a more opinionated client for specific use cases. Similarly, while the core SDKs handle payment processing, community tools could emerge for niche integrations, such as connecting Braintree to a custom reporting dashboard or a specific CRM. Developers should consult Braintree's official forums or repositories (like those on GitHub) to discover and assess community-maintained projects. Stripe, a direct competitor, also emphasizes the use of official SDKs but maintains an active community repository that can be a model for exploring various Stripe community libraries.