Braintree Java SDK

repository·master·Indexed 41 days ago

https://github.com/braintree/braintree_java

A Java library for integrating with the Braintree Gateway to process payments, manage transactions, and handle payment methods. Supports version 3.x.x and requires TLS 1.2. Provides a BraintreeGateway class for executing transaction sales and managing payment flows.

Tokens
1.5K
Snippets
5
Records
7
Agent score
1%

What's inside Braintree

  1. Run unit tests

    master

    Unit tests can be run on any system. You can use either rake or Maven to execute them. Note that you should explicitly skip integration tests (ITs) to ensure they do not fail, as integration tests require a local development gateway server.

    # Using Maven (recommended to skip integration tests)
    mvn verify -DskipITs
    
    # Using rake
    rake test:unit
  2. Install the Braintree Java SDK via Maven

    master

    To use the Braintree Java library in your project, add the following dependency to your pom.xml. The library is available in Maven Central, so no additional repositories are required.

    Note: Ensure you replace PUT VERSION NUMBER HERE with the latest version (e.g., 3.54.0).

    <dependency>
      <groupId>com.braintreepayments.gateway</groupId>
      <artifactId>braintree-java</artifactId>
      <version>PUT VERSION NUMBER HERE</version>
    </dependency>
  3. Run linting with Checkstyle

    master

    The project uses Checkstyle with the Google Java Style configuration. You can run the linting process using Maven. After the command completes, you can view the detailed linting report in your browser by opening the generated HTML file located at target/site/checkstyle.html.

    mvn site
  4. Important: TLS and SSL Certificate Requirements

    master

    TLS Requirements

    Braintree requires TLS 1.2 for all HTTPS connections and HTTP/1.1 for all connections. Early versions of TLS are retired and will not work.

    SSL Certificate Expiration Warning

    Critical: SSL certificates for Java SDK versions older than 2.75.0 expire on March 30, 2026. If you do not update your SDK to a version with updated certificates by this date, 100% of your impacted traffic will fail.

  5. Quick Start: Create a transaction sale

    master

    To process a transaction, initialize a BraintreeGateway with your credentials and environment, construct a TransactionRequest, and call gateway.transaction().sale(request).

    Always check result.isSuccess() to determine if the transaction was processed correctly. If it fails, you can inspect result.getTransaction() for processor errors or result.getErrors() for validation errors.

    import java.math.BigDecimal;
    import com.braintreegateway.*;
    
    public class BraintreeExample {
        public static void main(String[] args) {
            BraintreeGateway gateway = new BraintreeGateway(
                Environment.SANDBOX,
                "the_merchant_id",
                "the_public_key",
                "the_private_key"
            );
    
            TransactionRequest request = new TransactionRequest()
                .amount(new BigDecimal("1000.00"))
                .paymentMethodNonce(nonceFromTheClient)
                .options()
                    .submitForSettlement(true)
                    .done();
    
            Result<Transaction> result = gateway.transaction().sale(request);
    
            if (result.isSuccess()) {
                Transaction transaction = result.getTarget();
                System.out.println("Success!: " + transaction.getId());
            } else if (result.getTransaction() != null) {
                Transaction transaction = result.getTransaction();
                System.out.println("Error processing transaction:");
                System.out.println("  Status: " + transaction.getStatus());
                System.out.println("  Code: " + transaction.getProcessorResponseCode());
                System.out.println("  Text: " + transaction.getProcessorResponseText());
            } else {
                for (ValidationError error : result.getErrors().getAllDeepValidationErrors()) {
                   System.out.println("Attribute: " + error.getAttribute());
                   System.out.println("  Code: " + error.getCode());
                   System.out.println("  Message: " + error.getMessage());
                }
            }
        }
    }