Will send you an activation email --> Click on the activation link and activate your account.
Notedown following important information
- merchantid
- public key
- private key
And configure in your project. In my case I have configured them as environment variable. In production ensure its encrypted.
Add following dependency in your project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>com.braintreepayments.gateway</groupId> | |
<artifactId>braintree-java</artifactId> | |
<version>2.77.0</version> | |
</dependency> |
Create a controller class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.clouddemo.kjoshi.restep.controller; | |
import com.braintreegateway.Result; | |
import com.clouddemo.kjoshi.restep.service.BrainTreeService; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestParam; | |
import org.springframework.web.bind.annotation.RestController; | |
@RestController | |
@RequestMapping("/v1/api/braintree") | |
public class BrainTreeController { | |
private static final Logger logger = LoggerFactory.getLogger(BrainTreeController.class); | |
@Autowired | |
BrainTreeService svc; | |
@RequestMapping("/token") | |
public void getBrainTreeToken(){ | |
svc.getClientTocken(); | |
} | |
@RequestMapping("/transaction") | |
public Result bookTransaction(@RequestParam("amt")Double amt,@RequestParam("nonce")String nonce){ | |
logger.info("Booking transaction for amount:{} and nounce:{}",amt,nonce); | |
return svc.performTransaction(amt,nonce); | |
} | |
} |
And a service class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.clouddemo.kjoshi.restep.service; | |
import com.braintreegateway.*; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.stereotype.Service; | |
import org.springframework.util.StringUtils; | |
import javax.validation.ValidationException; | |
import java.math.BigDecimal; | |
@Service | |
public class BrainTreeService { | |
private static String MERCHANT_ID = System.getenv("MERCHANT_ID"); | |
private static String PRIVATE_KEY = System.getenv("PRIVATE_KEY"); | |
private static String PUBLIC_KEY = System.getenv("PUBLIC_KEY"); | |
private static final Logger logger = LoggerFactory.getLogger(BrainTreeService.class); | |
private static BraintreeGateway gateway = new BraintreeGateway(Environment.SANDBOX,MERCHANT_ID,PUBLIC_KEY,PRIVATE_KEY); | |
public String getClientTocken(){ | |
if(StringUtils.isEmpty(MERCHANT_ID)||StringUtils.isEmpty(PRIVATE_KEY)||StringUtils.isEmpty(PUBLIC_KEY)){ | |
logger.error("Setup issue.."); | |
throw new ValidationException("Setup not correct"); | |
}else{ | |
String clientToken = gateway.clientToken().generate(); | |
logger.info("Client token generated:{}",clientToken); | |
return clientToken; | |
} | |
} | |
public Result performTransaction(Double amt,String nonce){ | |
logger.info("Submitting transaction"); | |
TransactionRequest req = new TransactionRequest(); | |
req.amount(new BigDecimal(amt)).paymentMethodNonce(nonce).options().submitForSettlement(true).done(); | |
Result<Transaction> txn = gateway.transaction().sale(req); | |
return txn; | |
} | |
} |
List of nonce are available here
Perform few transactions like following
http://localhost:8000/v1/api/braintree/transaction?amt=130&nonce=fake-valid-visa-nonce
http://localhost:8000/v1/api/braintree/transaction?amt=34&nonce=fake-valid-dinersclub-nonce
On Braintree Transaction Summary page you can see these transactions reflected.
Or you can check transactions