Recent Bloggers

Saurang Suthar
Posts: 5
Stars: 0
Date: 7/4/17
Jyoti Verma
Posts: 3
Stars: 0
Date: 6/28/17
Pankti Patel
Posts: 1
Stars: 0
Date: 5/26/17
Chandrika Baraiya
Posts: 5
Stars: 0
Date: 5/26/17
Atith Patel
Posts: 5
Stars: 0
Date: 5/22/17
Nisarg Pujara
Posts: 6
Stars: 0
Date: 5/10/17
Pranavsinh Parmar
Posts: 4
Stars: 0
Date: 5/3/17
Ankit Prajapati
Posts: 5
Stars: 0
Date: 5/3/17
Pratik Solanki
Posts: 5
Stars: 0
Date: 4/7/17
Chirag Patel
Posts: 4
Stars: 0
Date: 3/30/17
« Back

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();
      braintreePaymentProcessing();
 }
 
// 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();
        request.amount(amount);
        request.paymentMethodNonce(paymentMethodNonce);
       
        setCustomerInTransaction(request);
        SetTransactionRequestOptions(request);
        executePaymentTransaction(request);
 }
 
private static void setCustomerInTransaction(TransactionRequest request) {
         CustomerRequest customerRequest = request.customer();
         customerRequest.email("enquiry@surekhatech.com");
         customerRequest.firstName("Surekha");
         customerRequest.lastName("Technologies");
 }
 
 private static TransactionOptionsRequest SetTransactionRequestOptions(TransactionRequest request) {
         TransactionOptionsRequest options = request.options();
         options.submitForSettlement(true);
         
         // Complete transaction request
         options.done();
         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.

 

Comments
No comments yet. Be the first.

Contact Us

Loading

Get in touch

Headquarter :
302, Landmark,
Nr. Titanium City Center,
Prahlad Nagar Road,
Ahmedabad, India - 380015.