Stripe Payment Intents in Android

Posted By : Prince Bhardwaj | 31-Jan-2019

Introduction

 

The payment intent is the object that helps to represent your intent for collecting payment from the customer. It keeps the track of lifecycle for the payment through each stage of the process. Elements, strip.js, and payment intent are coupled together for accepting the payment methods that involve multiple steps.

 

For using this card payment with PaymentIntents and Android SDK. It involves a four-step process, with server-side and client-side actions:


 

1. Creating a paymentIntent and make its client secret accessible to your application

 

Create a paymentIntent on the server the moment customer begins the checkout process. For using the paymentIntent, we need to make the client secret accessible to our application. The best approach is to get it to serve from the HTTP endpoint on the server and then retrieve that on the client side.

 

2. Collect the card information and create the source param object

 

In the application, collect card details from customers. We can make use of CardInputWidget or CardMultilineWidget, call getCard function for receiving card object or just collect the payment information through your own form and then pass collected information in a new card instance.

 

when you have the card object, just pass it to the SourceParams.createcardParams for creating a new SourceParams object. If the source is already there just associate it with the PaymentIntent

 

Following is the code that demonstrates how to create SourceParams object from the card

 

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

 

 

3. Confirm PaymentIntent

 

For initiating the payment collection, it should be confirmed that the customer intended to pay with provided payment details. Start it by creating PaymentIntentParams object from the desired source. If we use a new resource, we can accomplish this with createConfirmPaymentIntentWithSourceDataParams function. If we attach an existing source, we should use createConfirmPaymentIntentWithSourceIdParams function instead.

 

The first parameter is the source param object that was created in the previous step. The second parameter is the client secret. The return URL that was indicating where PaymentIntent should return if it has been redirected as part of the authentication process is the last URL. We can associate the desired activity with this return URL in the application's AndroidManifest.xml


 

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


 

For finishing the payment, just pass PaymentIntentParams object to confirmPaymentIntentSychronous method

 

Stripe mStripe = Stripe();
PaymentIntent paymentIntent = mStripe.confirmPaymentIntentSynchronous(
 paymentIntentParams, "pk_test_TYooMQauvdEDq54NiTphI7jx");


 

The parameter constructors are provided as convenience methods for most common fields. If we want to send additional data, we should use PaymentintentParams#setextraParams. Any extra parameter we add is included in the request to stripe. For sending an email to  customer on payment confirmation, we can use receipt_email parameter


 

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

 

4. Redirect and authenticate payment if necessary

 

Some of the payment methods can require additional authentication steps for completeing the order. If we want to determine furthur action is required from customer, check status of PaymentIntent object returned from the confirmation function from previous step. If we see that PaymentIntent status is requires-source_action, more action is require from the customer in order for completing payment. But if the required source action is authorize_with_url, we can obtain authorization URL just by calling PaymentIntent#getAuthorizationUrl.We can just open the URL in browser for accomodating customer's completion of 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);
   }
}

 

Once the user has taken action, then they will be returned to the application via return URL that has been specified in the previous step. We can make use of PaymentIntentParams.createRetrievePaymentIntentParams for retrieving the status of PaymentIntent to see if it has been succeeded.

 

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


 

Use of PaymentIntents with google pay

 

For integrating it with google pay, We can use token returned by the google pay integration for confirming the paymentIntent. Just create the source param object with SourceParams.createSourceFromTokenParams and pass in the ID of the token

 

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

 

About Author

Author Image
Prince Bhardwaj

Prince Bhardwaj is having Talent as Application Developer, he is an enthusiastic team player in Oodles Technologies.

Request for Proposal

Name is required

Comment is required

Sending message..