You can collect customer payment information via the client SDK in a number of ways:

Client side payment token flow Diagram demonstrating the required interaction between the client, Braintree servers and your server.

Android SDK setup

In your build.gradle, add the following:

Groovy
Click to copy
Copied
dependencies {
  compile 'com.braintreepayments.api:drop-in:3.+'
}

For other installation techniques and notes, see the Android Client SDK Guide.

Get a client token

Your server is responsible for generating a client token, which contains the authorization and configuration details that your client needs to initialize the client SDK.

Request the client token from your server, then initialize Braintree and present drop-in UI (this example uses Android Async Http Client to request a client token from your server - adapt to your own setup):

Java
Click to copy
Copied
AsyncHttpClient client = new AsyncHttpClient();
client.get("https://your-server/client_token", new TextHttpResponseHandler() {
    @Override
    public void onSuccess(int statusCode, Header[] headers, String clientToken) {
        this.clientToken = clientToken;
    }
});

You should obtain a new client token often, at least as often as your app restarts. For the best experience, you should kick off this network operation before it would block a user interaction.

important

You must generate a client token on your server once per user checkout session. The endpoint we provide in this example is for demonstration purposes only.

Try it now

We generated a demo client token for you so you can jump right in. This is for testing purposes only!

The above demo client token is for temporary use. You must change this client token in order to process payments with your Braintree sandbox or production account.

So you can jump right in we generated a client token for you. You can copy and paste the below code to try out Braintree right away:

Java
Click to copy
Copied
public void onBraintreeSubmit(View v) {
  DropInRequest dropInRequest = new DropInRequest()
      .clientToken("eyJ2ZXJzaW9uIjoyLCJhdXRob3JpemF0aW9uRmluZ2VycHJpbnQiOiJlZjIyNzdlMzRiODVmZmQ4ZGM1MmQ1MzE4NGZiODE3MzM0YTJmZjRmNTMzM2M1YzI1N2JhMzRhMWNjODJkMjE1fGNyZWF0ZWRfYXQ9MjAxNy0wOS0xOVQxNTowNDoyNi4wNjUzMjU1NzMrMDAwMFx1MDAyNm1lcmNoYW50X2lkPTM0OHBrOWNnZjNiZ3l3MmJcdTAwMjZwdWJsaWNfa2V5PTJuMjQ3ZHY4OWJxOXZtcHIiLCJjb25maWdVcmwiOiJodHRwczovL2FwaS5zYW5kYm94LmJyYWludHJlZWdhdGV3YXkuY29tOjQ0My9tZXJjaGFudHMvMzQ4c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QiOiJtYXN0ZXJjbGllbnQzIiwiYmlsbGluZ0FncmVlbWVudHNFbmFibGVkIjp0cnVlLCJtZXJjaGFudEFjY291bnRJZCI6ImFjbWV3aWRnZXRzbHRkc2FuZGJveCIsImN1cnJlbmN5SXNvQ29kZSI6IlVTRCJ9LCJtZXJjaGFudElkIjoiMzQ4cGs5Y2dmM2JneXcyYiIsInZlbm1vIjoib2ZmIn0=");
  startActivityForResult(dropInRequest.getIntent(this), REQUEST_CODE);
}

Present Drop-in UI

At this point, you are ready to collect payment details from your customer.

Drop-in is the easiest way to get started. It provides a fully fledged payments experience out of the box. You can also choose to create a custom UI and then tokenize the payment method details directly.

When your user provides payment information, your app receives a paymentMethodNonce in your calling Activity's onActivityResult, which you will override to get the response or any possible errors:

Java
Click to copy
Copied
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == REQUEST_CODE) {
    if (resultCode == Activity.RESULT_OK) {
      DropInResult result = data.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT);
      // use the result to update your UI and send the payment method nonce to your server
    } else if (resultCode == Activity.RESULT_CANCELED) {
      // the user canceled
    } else {
      // handle errors here, an exception may be available in
      Exception error = (Exception) data.getSerializableExtra(DropInActivity.EXTRA_ERROR);
    }
  }
}

Test your integration

Create a sandbox account

If you haven't already, sign up for a free Braintree sandbox account:

Sign Up for a Braintree Sandbox Account

Log in to obtain your sandbox API credentials. You'll need your:

  • Sandbox merchant ID
  • Public key
  • Private key

Use these credentials for your development and testing.

important

When you go live, you will need to replace your sandbox API credentials with production API credentials.

Test values

When testing in the sandbox, be sure to use our test card numbers (e.g. 4111111111111111) and nonces (e.g. fake-valid-nonce). Real payment method data will not work in the sandbox. See our Testing page for more details.

Send payment method nonce to server

Send the resulting payment method nonce to your server (again, this example uses Android Async Http Client - adapt to your own setup):

Java
Click to copy
Copied
void postNonceToServer(String nonce) {
  AsyncHttpClient client = new AsyncHttpClient();
  RequestParams params = new RequestParams();
  params.put("payment_method_nonce", nonce);
  client.post("http://your-server/checkout", params,
    new AsyncHttpResponseHandler() {
      // Your implementation here
    }
  );
}

world.greeted = true

At this point, you should have a working client-side checkout flow. When your user provides payment details, you receive a payment method nonce and send it to your server.

Next, your server closes the loop by using the payment method nonce to create a transaction.

Next: Simple Server →

Still have questions?

If you can’t find an answer, contact our Support team.