Collecting Card Details on Android

    Learn how to build a Stripe integration for Android that uses Charges and tokenization.

    Accepting a card payment with Charges and the Android SDK is a three-step process, with server-side and client-side actions.

    1. Securely collect payment information
    2. Tokenize the card information

    Step 1: Securely collect payment information

    Stripe’s Android SDK provides a set of standard user interface components that you can use to securely collect card information from customers. The following example demonstrates how to use the CardMultilineWidget, a custom view which collects the customer’s card number, CVC, and expiration date.

    Define a CardMultilineWidget in your XML layout.

    <com.stripe.android.view.CardMultilineWidget
        android:id="@+id/card_widget"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    

    If the customer’s input is valid, CardMultilineWidget#getCard() will return a Card instance; otherwise, it will return null.

    private void onCardSaved() {
        final Card cardToSave = cardWidget.getCard();
        if (cardToSave != null) {
            // tokenize card (see below)
            tokenizeCard(cardToSave);
        }
    }
    
    private fun onCardSaved() {
        val cardToSave = cardWidget.card
        if (cardToSave != null) {
            // tokenize card (see below)
            tokenizeCard(cardToSave)
        }
    }
    

    If you prefer to build your own custom form for collecting and validating card information, you can manually instantiate the Card class and pass in raw card information. You also have the option of using Google Pay to collect payment information, allowing customers to use credit or debit cards that are already associated with their Google account.

    Step 2: Tokenize the card information

    The next step is sending the customer’s payment information to Stripe and exchanging it for a token, which you can send to your server and use to create a charge. Tokenization ensures that no sensitive card data ever needs to touch your server, so that your integration remains PCI-compliant.

    The following code shows how to use Stripe#createToken(). The method takes a Card instance as the first parameter. The second parameter is a TokenCallback instance that the client invokes on success or failure. When tokenization completes successfully, send the returned token id to your server.

    private void tokenizeCard(@NonNull Card card) {
        stripe.createToken(
            card,
            new ApiResultCallback<Token>() {
                public void onSuccess(@NonNull Token token) {
                    // send token id to your server for charge creation
                    createCharge(token.id);
                }
                public void onError(@NonNull Exception error) {
                    // Show error message to customer
                }
            }
        );
    }
    
    private fun tokenizeCard(card: Card) {
        stripe.createToken(
            card,
            object : ApiResultCallback<Token> {
                override fun onSuccess(token: Token) {
                    // send token id to your server for charge creation
                    createCharge(token.id)
                }
    
                override fun onError(e: Exception) {
                    // Show error message to customer
                }
            }
        )
    }
    

    Next steps

    Now you have a complete Android integration built with the Charges API and tokenization. Continue reading for more details.

    Was this page helpful? Yes No

    Send

    Thank you for helping improve Stripe's documentation. If you need help or have any questions, please consider contacting support.

    Questions?

    We're always happy to help with code or other questions you might have. Search our documentation, contact support, or connect with our sales team. You can also chat live with other developers in #stripe on freenode.

    On this page