8 Steps to Google Pay on Android

Within our Android Apps we can use the Google Pay API to plugin a convenient way for our users to checkout in our app, without the need for them to enter any of their payment information (after an initial setup). This brings the user a collection of advantages such as:
  • A centralised place to complete all their payments within your apps, just like they are able to do on the web or in stores
  • A streamlined process when completing the checkout stage of a purchase from within your app
  • A feeling of safety whilst paying due to the secure storage of payment information fro Google pay

The Google Pay API for android allows developers to easily integrate a payment method that provides this streamlined, secure and familiar experience for our users.

We all want to make things simpler and better or users, so in this article I want to show you can get Google Pay integrated into your Android Application with 8 individual steps.


Configure payment methods

Before we go ahead and start configuring the class used to handle payments within our app, we need to declare some fundamental data used in the requests that we are going to perform.

We begin by defining a list of accepted cards, this collection can currently include Amex, Discover, Mastercard, JCB and Visa cards.

private val allowedCardNetworks = JSONArray().apply {
    put("AMEX")
    put("MASTERCARD")
    put("VISA")
}

Next we need to define the accepted authentication methods that can be used for the transaction. The given choice can include the following options:

  • PAN_ONLY — Associated with cards that are stored alongside the Google Account of the current user. The data returned includes a corresponding account number alongside the expiration month and year.
  • CRYPTOGRAM_3DS — Associated with cards that are stored as Android device tokens. The data returned includes a 3DS cryptogram generated on the given device.
private val allowedCardAuthMethods = JSONArray().apply {
    put("PAN_ONLY")
    put("CRYPTOGRAM_3DS")
}

Now that we have the above details defined, we can go ahead and prepare this data for use with the Google Pay API. These need to go under the following properties:

  • allowedAuthMethods — Takes the accepted authentication methods that can be used when making payment requests
  • allowedCardNetworks — Takes the declared accepted cards that can be used to make payments
private val paymentMethodParameters = JSONObject().apply {
    put("allowedAuthMethods", allowedCardAuthMethods)
    put("allowedCardNetworks", allowedCardNetworks)
}

As well as the above we can also optionally declare:

  • allowPrepaidCards — States whether or not pre-paid cards can be used when making payment requests
  • billingAddressRequired — States whether or not a billing address is required to complete the transaction
  • billingAddressParameters — Takes data in the format of the BillingAddressParameters class

Finally, we put all of this together by declaring the accepted payment type as a CARD and pass over the payment parameters that we declared above:

val baseCardPaymentMethod = JSONObject().apply {
    put("type", "CARD")
    put("parameters", paymentMethodParameters)
}

Configure wallet options

Before we instantiate our payment client to handle our payment requests, we need to create an instance of the options used by this client — this is in the form of the WalletOptions class.

val options = Wallet.WalletOptions.Builder()
    .setEnvironment(WalletConstants.ENVIRONMENT_TEST)
    .setTheme(THEME_LIGHT)
    .build()

Using the builder we can configure two properties to be using by our payment client:


Initialise the Payments Client

Now that we have configured the options for our payments client, we can go ahead an instantiate a new instance of our client ready for use.

private val paymentsClient = Wallet.getPaymentsClient(this, options)

Here we pass in our current context and the WalletOptions instance that we built during the last step. At this point we now have a payments client reference within our application that can be triggered when the uses requests to make a payment.


Declare a Google Play Button

Using the assets provided over on the brand guidelines for Google Pay, you need to begin by adding a button to your application that uses one of the provided assets. This provides a way for the user to recognise the Google Pay payment method. Selecting this button will trigger the payment flow that we will look at over the next few sections.


Check if payments is ready for use

Now that we have a payments client, we want to check if the user is able to make payments using the Google Pay API — we may encounter errors when trying to access the API otherwise. So here we want to make sure that the button for Google Pay is not visible until the Google Pay API is ready for use.

We begin by building the request that is to be sent for our isReadyToPay call. Here we pass:

  • apiVersion — The major API version to be used (integer)
  • apiVersionMinor — The minor API version to be used (integer)
  • allowedPaymentMethods — A collection of PaymentMethod instances that declare the supported payment methods of the client.

https://gist.github.com/hitherejoe/7c68d36ea39ba6c59f2b6db5084670e1

We can also optionally provide a existingPaymentMethodRequired (boolean) property — setting this property to true will cause isReadyToPay() to return true if the user is ready to pay with at least one of the specified payment methods.

Now that we have these properties built, we can go ahead and pass them to our isReadyToPay() call:

https://gist.github.com/hitherejoe/80a5206f562a5ed00e7d5cc93273ced4


Setup Gateway Configuration

Next we need to setup the relevant gateway details — this gateway is what will receive the encrypted users information to process a given transaction.

  • gateway — Identify for a Google-supported gateway
  • gatewayMerchantId — Unique ID that links the merchant to the given gateway

You’ll notice below that we then bundle this all together (marked with the PAYMENT_GATEWAY identifier) as the tokenizationSpecification argument for use by the Google Pay API.

https://gist.github.com/hitherejoe/9b5fd907f2617bc4fd9cb22e550c2bee


Prepare transaction information

At this point we need to defined the transaction data that will be presented by the Google Pay widget. We begin with the TransactionInfo, this contains details such as:

  • currencyCode — The currency to be used for the transaction
  • totalPriceStatus — The status of the total price. This will be either NOT_CURRENTLY_KNOWN, ESTIMATED (the price may change based on shipping or tax calculations) or FINAL (the price will never change from the one currently displayed to the user)
  • totalPrice — The total price of the transaction
  • checkoutOption — Decides the behaviour used and text to be shown within the checkout button of the google pay sheet. This can be either DEFAULT (standard behaviour, the user will need to confirm transaction details) or COMPLETE_IMMEDIATE_PURCHASE (when the totalPriceStatus is final, the user will complete the purchase with no further action).

https://gist.github.com/hitherejoe/1013faf40f850e1ed40aa533e06d4ea9

We then need to build the payment request using our transaction information that we just prepared. For this we build a PaymentDataRequest instance and populate the required data:

  • apiVersion — The major API version to be used (integer)
  • apiVersionMinor — The minor API version to be used (integer)
  • merchantInfo — Information regarding the merchant in the form of a MerchantInfo class
  • allowedPaymentMethods — A collection of PaymentMethod instances that declare the supported payment methods of the client.
  • transactionInfo — Information about the transaction in the form of a TransactionInfo class
  • emailRequired — States whether an email address is required to complete the transaction
  • shippingAddressRequired — States whether a shipping address is required to complete the transaction
  • shippingAddressParameters — Information about the shipping address in the form of a ShippingAddressParameters class

Listen for Payments callback

Now that the payment request has been made we need to listen for the result. The result of this request is returned to the calling source via the onActivityResult method with the resultCode that was initially passed in with the request.

https://gist.github.com/hitherejoe/5078308488c8d1af23142192359f5f5e

Once we have the data back from the request we can make use of the PaymentData class from the Google Pay API to parse the result of the intent — giving us back an instance of this PaymentData class that contains billing information for the transaction.

Here this data is then converted into a JSON format so that it can be passed onto our payment processor. At this point we have carried out all of the required steps and our transaction is ready to be completed.

Conclusion

Hopefully from this quick post you can see just how little steps are required to integrate Google Pay into your Android application. Doing so allows us to implement a streamlined experience that is secure and gives a sense of familiarity to our users. Why not get started with Google Pay in your own Android applications today?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s