Set Up Your Serveranchor

Server side payment token flowDiagram demonstrating the required interaction between the client, our servers, and your server.

Install and configureanchor

We provide an open-source JAR file to integrate with the Braintree gateway.

Download Java library
  • Version: 3.33.0
  • SHA256: 42c3cb9c196c2d7a867662b47944a41c073cc06805622a07e64d3daa8d694209

Or use Mavenanchor

Maven is a build manager for Java.

Edit the pom.xml for your project, and in dependencies add:

  1. XML
<dependency>
  <groupId>com.braintreepayments.gateway</groupId>
  <artifactId>braintree-java</artifactId>
  <version>3.33.0</version>
</dependency>

The Java Client Library is published to The Maven Central Repository , so no additional repositories are required.

In your code, configure the environment and API credentials:

  1. Java
private static BraintreeGateway gateway = new BraintreeGateway(
  Environment.SANDBOX,
  "your_merchant_id",
  "your_public_key",
  "your_private_key"
);
note

See the Braintree Java Version Changelog.

Generate a client tokenanchor

Your server is responsible for generating a client token, which contains all authorization and configuration information your client needs to initialize the client SDK to communicate with Braintree. Including a customerId() when generating the client token lets returning customers select from previously used payment method options, improving user experience over multiple checkouts.

  1. Java
ClientTokenRequest clientTokenRequest = new ClientTokenRequest()
  .customerId(aCustomerId);
// pass clientToken to your front-end
String clientToken = gateway.clientToken().generate(clientTokenRequest);

If the customer can't be found, it will return null.

Set Up Your Client covers the client side of the exchange.

The following Java examples use the Spark framework.

Send a client token to your clientanchor

Here is an example of how your server would generate and expose a client token:

  1. Java
get(new Route("/client_token") {
  @Override
  public Object handle(Request request, Response response) {
    return gateway.clientToken().generate();
  }
});

How the token is used by the client may vary. In JavaScript integrations the client token is often included in the generated HTML/JS, while in mobile apps the client token must be requested. These methods are discussed in the client token setup section.

Receive a payment method nonce from your clientanchor

Once your client successfully obtains a customer payment method, it receives a payment_method_nonce representing customer payment authorization, which it then sends to your server.

Your server implementation is then responsible for receiving the payment_method_nonce and using it appropriately.

  1. Java
post(new Route("/checkout") {
  @Override
  public Object handle(Request request, Response response) {
    String nonceFromTheClient = request.queryParams("payment_method_nonce");
    // Use payment method nonce here
  }
});

Create a transactionanchor

You can create a transaction using an amount and the nonceFromTheClient you received in the previous step.

Collect device data from the client and include the deviceDataFromTheClient in the transaction.

  1. Java
TransactionRequest request = new TransactionRequest()
  .amount(new BigDecimal("10.00"))
  .paymentMethodNonce(nonceFromTheClient)
  .deviceData(deviceDataFromTheClient)
  .options()
    .submitForSettlement(true)
    .done();

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

The sale call returns a Transaction Result Object which contains the transaction and information about the request.

Test your integrationanchor

See our Testing page for values you can use for nonceFromTheClient in your sandbox account. These nonces can be passed as strings through server-side calls to generate payment methods in the desired state. To verify your integration, you can check in the sandbox Control Panel, where transactions will immediately appear on success.

important

Always develop and test your code against your sandbox account before processing live transactions against a production account.

Transition to productionanchor

At this point, you should be able to accept a payment method nonce and create a transaction in our sandbox. When you're ready to start charging real money, transition over to our production environment. We'll explain that process next.

Further readinganchor


Next Page: Go Live