Using PaymentIntents on Android Beta

    Learn how to build an Android application that uses PaymentIntents to accept card payments.

    Accepting a card payment with PaymentIntents 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 an SourceParams 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 secret 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 and create a SourceParams object

    In your application, collect card details from the customer. You can use CardInputWidget or the CardMultilineWidget, calling the getCard method to retrieve a Card object. Or you can collect payment information through your own form and pass the collected information into a new Card instance.

    Once you have the Card object, pass it to SourceParams.createCardParams to create a new SourceParams object. If you already have a source that you would like to associate with the PaymentIntent, you may skip this step.

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

    Card card = mCardInputWidget.getCard();
    SourceParams cardSourceParams = SourceParams.createCardParams(card);
    

    Step 3: Confirm the PaymentIntent

    To initiate payment collection, you must confirm that your customer intends to pay with the provided payment details. Start by creating a PaymentIntentParams object from the desired source. If you are using a new source, you can accomplish this with the createConfirmPaymentIntentWithSourceDataParams method. If you are attaching an existing source, use the createConfirmPaymentIntentWithSourceIdParams method instead.

    The first parameter is the SourceParams 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.

    SourceParams cardSourceParams = SourceParams.createCardParams(card);
    PaymentIntentParams paymentIntentParams = PaymentIntentParams.createConfirmPaymentIntentWithSourceDataParams(
        cardSourceParams, clientSecret, "yourcompany://yourpath");
    

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

    Stripe mStripe = Stripe();
    // Do not call on the UI thread or your app will crash
    PaymentIntent paymentIntent = mStripe.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 status is requires_source_action, further action is required from your customer in order to complete the payment.

    If the required source action is authorize_with_url, obtain the authorization URL by calling PaymentIntent#getAuthorizationUrl. You can open the URL in a browser to accommodate the customer’s completion of the authentication step:

    if (paymentIntent.getStatus().equals("requires_source_action")) {
        Uri authorizationUrl = paymentIntent.getAuthorizationUrl();
        if (authorizationUrl != null) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, paymentIntent.getAuthorizationUrl());
            startActivity(browserIntent);
        }
    }
    

    After the user has taken action, they are returned to the your application via the return URL specified in the previous step. You can use PaymentIntentParams.createRetrievePaymentIntentParams to retrieve the status of the PaymentIntent to see if it has succeeded.

    PaymentIntentParams retrievePaymentIntentParams = PaymentIntentParams.createRetrievePaymentIntentParams(mClientSecret);
    PaymentIntent paymentIntent = mStripe.retrievePaymentIntentSynchronous(
      retrievePaymentIntentParams, "pk_test_TYooMQauvdEDq54NiTphI7jx");
    

    Using PaymentIntents with Google Pay

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

    SourceParams sourceParams = SourceParams.createSourceFromTokenParams("tok_visa");
    PaymentIntentParams paymentIntentParams = PaymentIntentParams.createConfirmPaymentIntentWithSourceDataParams(
      sourceParams, clientSecret, "hostvalue://schemevalue");

    Next steps

    You now have an Android integration that can accept card payments with PaymentIntents, 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.