Braintree payment gateway integration in Spring Boot

Posted By : Amit Maurya | 28-Sep-2021

Braintree is a payment gateway provided by paypal. In this tutorial we will integrate Braintree Payment Gateway in Spring Boot. Braintree is a full-stack platform for accepting payment that makes it easy to accept payments in your app or website. Braintree replaces the traditional model of sourcing a payment gateway and merchant account from different providers.

 

1. First of all we need to add maven dependency =>

<dependency>

  <groupId>com.braintreepayments.gateway</groupId>

  <artifactId>braintree-java</artifactId>

  <version>3.12.0</version>

</dependency>

Note: Check maven central repository for latest version

 

2. Now create a braintree sandbox account from official website to get the following credencials:

* your_merchant_id

* your_public_key

* your_private_key

 

3. Now configure braintree with your credencials:

private static BraintreeGateway gateway = new BraintreeGateway(

  Environment.SANDBOX,

  "your_merchant_id",

  "your_public_key",

  "your_private_key"

);

 

 

4. Now create an API to generate client token:


ClientTokenRequest clientTokenRequest = new ClientTokenRequest()

  .customerId(aCustomerId);

// pass clientToken to your front-end

String clientToken = gateway.clientToken().generate(clientTokenRequest);

 

 

5. Now create an API for making payment which receives payment_method_nonce, deviceDataFromTheClient, and amount.

public String processTransaction(String chargeAmount, String nonce, String deviceData) {

        log.info("Processing transaction");

        TransactionRequest request = new TransactionRequest().amount(new BigDecimal(chargeAmount))

                .paymentMethodNonce(nonce).deviceData(deviceData).options()

                .submitForSettlement(true).done();

        Result<Transaction> transactionResult = gateway.transaction().sale(request);

        Transaction transaction;

        if (transactionResult.isSuccess()) {

            transaction = transactionResult.getTarget();

            return transaction.getId();

        }

}

 

6. Now the final step is to test the integration. We can find testing nonse from the Braintree official website. Here we will use "fake-valid-nonce" as the testing nonse.

    pass these values in API:

                              nonse         = 'fake-valid-nonce'

                              deviceDatea   = "any_string_value"

                              chargeAmount  = "20.0"

Note: If the API return a transaction id that will be sonething like "f19akzec", which means braintree integration is working fine. We can also verify the payments from the braintree dashboard.

 

About Author

Author Image
Amit Maurya

Amit Maurya is a highly skilled Backend Developer with more than 2 years of experience in developing RESTAPIs and Microservices. He has expertise in using Spring Boot framework, Hibernate, Java 8, and JavaEE, and he has worked with various databases such as MySQL, PostgreSQL, Oracle, Redis, and more.He has also worked on implementing payment gateways such as Stripe, Paypal, Cryptocurrency (Metamask), Android In-App Purchases, and Apple in App Purchases. Furthermore, he has experience in implementing and maintaining Streaming servers like Ant Media and Agora. He is proficient in using version control systems like GIT and source code management tools such as GitHub and GitLab, including command line applications.He has worked on several projects, including TutorX, Fabtrack, Toosi WhatsApp Chatbot Integration, and Virtuosica.

Request for Proposal

Name is required

Comment is required

Sending message..