Integrate Braintree Payment Gateway Using Java SDK - ST 2024
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("[email protected]"); 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 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.
For Your Business Requirements
2 - 4 October 2024
Hall: 10, Booth: #B8 Brussels, Belgium