Using Payment Intents on Android

    Learn how to build an Android application that uses the Payment Intents API to accept card payments.

    Accepting a card payment with the Payment Intents APIThe Payment Intents API is a new way to build dynamic payment flows. It tracks the lifecycle of a customer checkout flow and triggers additional authentication steps when required by regulatory mandates, custom Radar fraud rules, or redirect-based payment methods. using automatic confirmation and the Android SDK is a four-step process, with server-side and client-side actions:

    1. Create a PaymentIntent and make its client secret accessible to your application
    2. Collect card information and create a PaymentMethodCreateParams object
    3. Confirm the PaymentIntent
    4. Redirect and authenticate the payment if necessary

    Step 1: Create a PaymentIntent and make its client secret accessible to your application

    When a customer begins the checkout process, create a PaymentIntent on your server. To use the PaymentIntent, you need to make its client secretThe client secret is a unique key associated with a single PaymentIntent. It is passed to the client side where it can be used in Stripe.js to complete the payment process. accessible to your mobile application. Typically, the best approach is to serve it from an HTTP endpoint on your server and then retrieve it on the client side.

    Step 2: Collect card information

    Collect Card details

    In your application, collect card details from the customer. There are a few ways to do this:

    • You can use CardInputWidget or the CardMultilineWidget, calling the getCard method to retrieve a Card object.
    • You can collect payment information through your own form and pass the collected information into a new Card instance.
    • You can use the token returned from Google Pay.

    Create a PaymentMethodCreateParams object

    Once you have the Card object, convert it to a PaymentMethodCreateParams.Card using Card#toPaymentMethodParamsCard and pass it to PaymentMethodCreateParams.create to create a new PaymentMethodCreateParams object. If you already have a PaymentMethod that you would like to associate with the PaymentIntent, you may skip this step. For more information on creating a PaymentMethod, see the Payment Methods API guide for Android.

    The following code demonstrates how to create the PaymentMethodCreateParams object from a Card:

    PaymentMethodCreateParams.Card paymentMethodParamsCard =
        mCardInputWidget.getCard().toPaymentMethodParamsCard();
    PaymentMethodCreateParams cardPaymentMethodCreateParams =
        PaymentMethodCreateParams.create(paymentMethodParamsCard, null);
    

    Step 3: Confirm the PaymentIntent

    To initiate payment collection, you must confirmConfirming a PaymentIntent indicates that the customer intends to pay with the current or provided payment method. Upon confirmation, the PaymentIntent attempts to initiate a payment. that your customer intends to pay with the provided payment details.

    Create PaymentIntentParams with a PaymentMethod

    Create a PaymentIntentParams object from the desired PaymentMethod. If you are using a new PaymentMethod, you can accomplish this with the createConfirmPaymentIntentWithPaymentMethodCreateParams method. If you are attaching an existing PaymentMethod, use the createConfirmPaymentIntentWithPaymentMethodId method instead.

    The first parameter is the PaymentMethodCreateParams object created in the previous step. The second parameter is your client secret. The last parameter is the return URL, indicating where the PaymentIntent should return if it is redirected as part of the authentication process. You can associate the desired Activity with the return URL in your application’s AndroidManifest.xml.

    PaymentMethodCreateParams paymentMethodCreateParams =
        PaymentMethodCreateParams.create(card, null);
    PaymentIntentParams paymentIntentParams =
        PaymentIntentParams.createConfirmPaymentIntentWithPaymentMethodCreateParams(
            paymentMethodCreateParams, clientSecret,
            "yourapp://post-authentication-return-url");
    

    Confirm the PaymentIntent

    To finish confirming the payment, pass the PaymentIntentParams object to the confirmPaymentIntentSynchronous method:

    Stripe stripe = new Stripe();
    // Do not call on the UI thread or your app will crash
    PaymentIntent paymentIntent = stripe.confirmPaymentIntentSynchronous(
        paymentIntentParams,
        "pk_test_TYooMQauvdEDq54NiTphI7jx"
    );
    

    The parameter constructors are provided as convenience methods for the most common fields. If you’d like to send additional data, you can use PaymentIntentParams#setExtraParams. Any extra parameters that you add are included in the request to Stripe. To send an e-mail to the customer on payment confirmation, for example, you can use the receipt_email parameter:

    Map<String, Object> extraParams = new HashMap();
    extraParams.put("receipt_email", "{{YOUR_RECEIPT_EMAIL}}");
    paymentIntentParams.setExtraParams(extraParams);
    

    Step 4: Redirect and authenticate the payment if necessary

    Some payment methods may require additional authentication steps in order to complete a payment. To determine whether further action is required from your customer, check the status of the PaymentIntent object returned from the confirmation function in the previous step.

    If the PaymentIntent’s status property is requires_action, further action is required from your customer in order to complete the payment. The required next action is determined by inspecting the next_action property. If the required action is redirect_to_url, obtain the URL by calling PaymentIntent#getRedirectUrl. You can open the URL in a browser to accommodate the customer’s completion of the authentication step:

    PaymentIntent.Status status = PaymentIntent.Status
        .fromCode(paymentIntent.getStatus());
    if (PaymentIntent.Status.RequiresAction == status) {
        Uri redirectUrl = paymentIntent.getRedirectUrl();
        if (redirectUrl != null) {
            startActivity(new Intent(Intent.ACTION_VIEW, redirectUrl));
        }
    } else {
      // Show success message
    }
    

    After the user has taken action, they are returned to the your application via the return URL specified in step 3. When declaring an activity that must be able to perform redirects, list an intent-filter item in your AndroidManifest.xml file. This allows you to accept links into your application. Your activity must include android:launchMode="singleTask" or else a new copy of it is opened when your customer comes back from the browser.

    <activity
        android:name=".activity.PollingActivity"
        android:launchMode="singleTask"
        android:theme="@style/SampleTheme">
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data
                android:host="yourapp"
                android:scheme="post-authentication-return-url"/>
        </intent-filter>
    </activity>
    

    To receive information from this event, listen for your activity getting started back up with a new Intent using the onNewIntent lifecycle method. The redirect adds payment_intent and payment_intent_client_secret URL query parameters that your application can use to identify the PaymentIntent associated with the customer’s purchase. You can use PaymentIntentParams.createRetrievePaymentIntentParams to retrieve the status of the PaymentIntent to see if it has succeeded.

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent.getData() != null && intent.getData().getQuery() != null) {
            final String host = intent.getData().getHost();
            final String clientSecret = intent.getData().getQueryParameter(
                "payment_intent_client_secret")
    
            final PaymentIntentParams retrievePaymentIntentParams =
                PaymentIntentParams.createRetrievePaymentIntentParams(clientSecret);
    
            AsyncTask.execute(new Runnable() {
                @Override
                public void run() {
                    // retrieve the PaymentIntent on a background thread
                    final PaymentIntent paymentIntent =
                        mStripe.retrievePaymentIntentSynchronous(
                            retrievePaymentIntentParams,
                            "pk_test_TYooMQauvdEDq54NiTphI7jx");
                }
            });
    
            // If you had a dialog open when your user went elsewhere, remember to close it here.
            mRedirectDialogController.dismissDialog();
        }
    }
    

    Using Google Pay with the Payment Intents API

    To integrate with Google Pay, use the token returned by your Google Pay integration to confirm the PaymentIntent. Create a PaymentMethodCreateParams object with PaymentMethodCreateParams.create(), passing in the ID of the token. After that, you can confirm the PaymentIntent as normal.

    PaymentMethodCreateParams paymentMethodCreateParams =
       PaymentMethodCreateParams.create(
           new PaymentMethodCreateParams.Card.Builder()
               .setToken("tok_visa")
               .build(),
           null);
    
    PaymentIntentParams paymentIntentParams =
       PaymentIntentParams.createConfirmPaymentIntentWithPaymentMethodDataParams(
           paymentMethodCreateParams, clientSecret, "yourapp://post-authentication-return-url");
    

    Next steps

    You now have an Android integration that can accept card payments with the Payment Intents API, prompting customers for authentication as needed.

    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.

    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.

    On this page