Android Integration

Our Android libraries let you easily accept mobile payments inside any Android app. If you need help, check out our answers to common questions or chat live with our developers in #stripe on freenode.

Stripe has created a Java library for Android allowing you to easily submit payments from the platform. We support Android back to version 4, and the library has no external dependencies.

Installation

  1. First download the stripe-android libraries.
  2. Be sure you've installed the Android SDK with API Level 17 and android-support-v4
  3. Import the stripe folder into Eclipse.
  4. In your project settings, add the stripe project under the "Libraries" section of the "Android" category.

Creating & validating cards

You'll need to import the Stripe classes before you can use them.

import com.stripe.android.*;

There are two main classes, Card and Stripe. The Card class contains a bunch of useful helpers for validating card input on the client-side, before we even try to create a charge.

Let's construct a Card instance with your customers's payment information, perhaps retrived from a form.

Card card = new Card(
    cardNumber,
    cardExpMonth,
    cardExpYear,
    cardCVC
);

card.validateNumber();
card.validateCVC();

As you can see in the example above, the Card instance contains some helpers to validate that the card number passes the Luhn check, that the expiry is the future, and that the CVC looks valid. You'll probably want to validate these three things at once, so we've included a validateCard function to help out:

Card card = new Card("4242-4242-4242-4242", 12, 2014, "123");

if ( !card.validateCard() ) {
    // Show errors
}

Creating tokens

With our mobile library, we shoulder the burden of PCI compliance by helping you avoid the need to send card data directly to your server. Instead, our libraries send the card data directly to our servers, where we can convert them to tokens. You can charge these tokens later in your server-side code.

The next step is to pass off that sensitive payment information securely to Stripe, where we'll exchange it for a token. We can create tokens using the Stripe instance method createToken, passing in a Card instance, and completion callbacks. An asynchronous network request will be executed, and the appropriate callback invoked when it completes.

Card card = new Card("4242424242424242", "12", "2014", "123");

Stripe stripe = new Stripe("pk_test_6pRNASCoBOKtIshFeQd4XMUh");
stripe.createToken(
    card,
    new TokenCallback() {
        public void onSuccess(Token token) {
            // Send token to your server
        }
        public void onError(Exception error) {
            // Show localized error message
            Toast.makeText(getContext(),
              error.getLocalizedString(getContext()),
              Toast.LENGTH_LONG
            ).show();
        }
    }
)

We've placed your test publishable API key as the first argument to Stripe. You'll need to swap it out with your live publishable key in production. You can see all your keys in your dashboard.

Set up a endpoint on your server that can receive a HTTP POST call for the token. In the onSuccess callback you'll need to POST the supplied token off to your server. Make sure any communication with your server is SSL secured to prevent eavesdropping.

Now we have a Stripe token representing a card on our server we can go ahead and charge it, save it for charging later, or sign the user up for a subscription. For more information, take a look at our tutorial on creating charges.

Take a look at the full example application to see everything put together.