We used cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it. What For?

« Back to Blogs

Integrate Braintree Payment Gateway Using Java SDK

Braintree is  very popular and full stack payments platform. It provides various features like recurring billing, credit card storage, support for mobile and international payments, and PCI compliance solutions along with simple pricing, security and support. It allows you to accept multiple payment methods like credit and debit cards, PayPal, Apple Pay, Android Pay, Venmo and Bitcoin. Recently I have integrated Braintree Gateway in one of our project. This article will help you to integrate Braintree payment acceptance system in your Java project.


Here is the sample Java PSVM program for quick and easy understanding. You can integrate this in your application according to your requirements.


package com.st.braintree;

import java.math.BigDecimal;
import com.braintreegateway.BraintreeGateway;
import com.braintreegateway.CustomerRequest;
import com.braintreegateway.Environment;
import com.braintreegateway.Result;
import com.braintreegateway.Transaction;
import com.braintreegateway.TransactionOptionsRequest;
import com.braintreegateway.TransactionRequest;
import com.braintreegateway.ValidationErrors;
public class SampleBrainttreeIntegration {
private static BraintreeGateway gateway = null;
private static String publicKey = "YOUR_PUBLIC_KEY";
private static String privateKey = "YOUR_PRIVATE_KEY";
private static String merchantId= "YOUR_MERCHANT_ID";
private public static void main(String[] args) {
      gateway = connectBraintreeGateway();
// Connect to Braintree Gateway.
public static BraintreeGateway connectBraintreeGateway() {
         BraintreeGateway braintreeGateway = new BraintreeGateway(
                 Environment.SANDBOX, merchantId, publicKey, privateKey);
         return braintreeGateway;
public static void braintreePaymentProcessing() {
        String clientToken = generateClientToken();
        String nonceFromTheClient = getPaymentMethodNonce();
      // Receive payment method nonce from client and do payment transactions
        BigDecimal amount = new BigDecimal("5.10");
        doPaymentTransaction(nonceFromTheClient, amount);
// Make an endpoint which return client token(Generate at server side) to client.
public static String generateClientToken() {
        String clientToken = gateway.clientToken().generate();
        return clientToken;
public static String getPaymentMethodNonce() {
        String nonceFromTheClient =  "fake-consumed-nonce";
        return nonceFromTheClient;
// Make an endpoint which receive payment method nonce from client and do payment transaction.
public static void doPaymentTransaction(String paymentMethodNonce, BigDecimal amount) {
        TransactionRequest request = new TransactionRequest();
private static void setCustomerInTransaction(TransactionRequest request) {
         CustomerRequest customerRequest = request.customer();
         customerRequest.email("[email protected]");
 private static TransactionOptionsRequest SetTransactionRequestOptions(TransactionRequest request) {
         TransactionOptionsRequest options = request.options();
         // Complete transaction request
         return options;
 private static void executePaymentTransaction(TransactionRequest request) {
          // Create transaction.
          Result<Transaction> result = gateway.transaction().sale(request);
          boolean isSuccess = result.isSuccess();
          if (isSuccess) {
                  Transaction transaction = result.getTarget();
                  // Further process on transaction
          } else {
                  ValidationErrors errors = result.getErrors();
                  // Error handling as per your requirement


We can help you if you want to integrate Braintree or any other payment gateways in your application.


contact-us Request a callback WhatsApp