Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.

15 St Margarets, NY 10033
(+381) 11 123 4567



How to Integrate Google Pay Service and Why You Should

Until recently, the implementation of a payment method into your business software solution was a matter of your clients’ convenience and an important factor of success in e-commerce. Today, in the condition of the COVID-19 pandemic, contactless payments have also become an important aspect of safety for both buyers and sellers.

When speaking about business mobile apps, the statistics say you may lose up to 80% of your clients just because the payment service is inconvenient or untrustworthy. Just think of all those attempts to enter all data with the help of a touchscreen — not to mention multiple shifts and the fear of data leaks. In this situation, the Google Pay payment gateway integration is a win-win solution for all parties. Besides contactless payments with Android phones and wearable devices, GPAY guarantees secure online payments with quick and safe checkout in apps and websites. Although it’s simple to integrate Google Pay in the Android app, this solution is mighty and reveals access to hundreds of cards saved in Google account and opens up your business for more opportunities. What’s more important is that it is free for both entrepreneurs and customers.

Business Advantages of Google Pay

Google Pay (previously known as Android Pay) enables mobile payments in just two steps. The users register in Google, and when it comes to the payment, they tap the button «Buy with GPAY», choose one of their payment cards, and submit. Shipping mode being attached once, they dispose of the necessity to enter the same contact data again and again. No wonder that such an easy payment method is really popular among the users: as of September 2018, the Google Pay app was downloaded more than 100 million times already. So, what are the business benefits? Airbnb, for instance, declared an 11 times increase in the number of online payments after the service integration. Google does not charge for the GPAY usage, so everything you get from integration is the growth in conversions and income. Let’s summarize:

  • G-Pay is free.
  • It’s easy to integrate.
  • It reveals access to hundreds of cards.
  • It skyrockets your conversions.
  • It boosts your income.

Any e-commerce software solution dealing with a retail or service provider can benefit a lot from Google Pay implementation in Android applications offered to the users.

Google Pay is available for all goods and services providers in the countries where the service is supported. Implementing Google Pay in Android solution is a perfect way to connect the user with several dozens of payment systems all over the world through a unified interface. It is easily integrated into the Android app.

Read further to learn how in detail.

Google Pay Integration

Import GPAY Service Library

The first thing you need to do for Google Pay integration in the Android app is to import a library. In the project file Gradle, you need to add a dependency: 

implementation ’com.google.android.gms:play-services-wallet:$play_service_wallet_version’ where $play_service_wallet_version is a version of the library we are going to use.

The current version is 18.0.0. The more detailed instruction for the library data and information about Google Pay Services libraries can be found here.

Initialize the Payment Client

Assuming the product or the service has already been formed and everything is ready for it to pass into your ownership, what we still need is to adjust the purchase process itself. For this, let’s initialize the payment client like so:

private lateinit var mPaymentsClient: PaymentsClient
private lateinit var mPaymentsClient: PaymentsClient
mPaymentsClient = GooglePayUtil.createPaymentsClient(this)
fun createPaymentsClient(activity: Activity): PaymentsClient {
val walletOptions = Wallet.WalletOptions.Builder()
return Wallet.getPaymentsClient(activity, walletOptions)
For PAYMENTS_ENVIRONMENT, we will specify the environment type WalletConstants.ENVIRONMENT_TEST.

Check the Possibility of Purchasing

Next, I propose to first check the possibility of purchasing with Google Pay like so:

GooglePayUtil.isReadyToPay(mPaymentsClient).addOnCompleteListener { task ->
try {
} catch (exception: ApiException) {
mGooglePayStatusText.text = «Payment status: Error init»
Log.d("isReadyToPay failed«, exception.statusCode.toString())
fun isReadyToPay(client: PaymentsClient): Task<Boolean> {
val request = IsReadyToPayRequest.newBuilder()
for (allowedMethod in&nbsp;SUPPORTED_METHODS) {
return client.isReadyToPay(request.build())

Display or Hide the Button

Depending upon the result, we need either to display or to hide the button like so:

private fun setGooglePayButtonAvailable(available: Boolean) {
if&nbsp;(available) {
mGooglePayStatusText.text = &laquo;Payment status: Supported&raquo;
mGooglePayButton.visibility = View.VISIBLE
} else {
mGooglePayStatusText.text = &laquo;Payment status: Not supported&raquo;

Specify the Payment Methods

Payment methods are specified in advance like so:

private val SUPPORTED_METHODS = Arrays.asList(

Google Pay in the Android App: Further Steps of Integration

We have gone through all preparatory stages related to our payment systems usage. Now, we have come to the most complicated stage; payment committing. See below:


An important note: most payment systems use the minimal possible currency value. Consequently, in the case of a kopeck or penny, for example, when indicating the write-off sum equal to 17852, we confirm that we want to write off 178.52.

The currency type is seen below:

private fun requestPayment() {
   mGooglePayButton.isClickable = false
   val price = GooglePayUtil.microsToString(17852)
   val transaction = GooglePayUtil.createTransaction(price)
   val request = GooglePayUtil.createPaymentDataRequest(transaction)
   val futurePaymentData = mPaymentsClient.loadPaymentData(request)
   AutoResolveHelper.resolveTask(futurePaymentData, this, LOAD_PAYMENT_DATA_REQUEST_CODE)

Next, prepare the transaction. Don’t forget to indicate the currency type like so:

fun createTransaction(price: String): TransactionInfo {
   return TransactionInfo.newBuilder()

Form the request. Right away, add the specifications for the merchant and the website link like so:

fun createPaymentDataRequest(transactionInfo: TransactionInfo): PaymentDataRequest {
   val paramsBuilder = PaymentMethodTokenizationParameters.newBuilder()
       .addParameter("gateway", “http://www.example.com”)
       .addParameter("gatewayMerchantId", "Example Merchant Name")
   return createPaymentDataRequest(transactionInfo, paramsBuilder.build())

Next, the request itself. Here, specify all the necessary fields and ordering parameters. For example, we may specify if we need to request a phone number and e-mail and whether we need a delivery option at all. Here and now, we also specify countries where the goods can be delivered, payment options, and payment systems (VISA, MASTERCARD) like so:

private fun createPaymentDataRequest(
   transactionInfo: TransactionInfo,
   params: PaymentMethodTokenizationParameters
): PaymentDataRequest {
   return PaymentDataRequest.newBuilder()
private val SHIPPING_SUPPORTED_COUNTRIES = Arrays.asList("UA, US, DE, GB")
private val SUPPORTED_NETWORKS = Arrays.asList(

So, the request is formed. Next, we need to process the result of this request like so:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
   when (requestCode) {
           when (resultCode) {
               Activity.RESULT_OK -> {
                   data?.let {
               Activity.RESULT_CANCELED -> {
               AutoResolveHelper.RESULT_ERROR -> {
           mGooglePayButton.isClickable = true

Then goes the transaction processing. Here, a lot of various logic can be added, including screen shifts congratulations of just service-based data, but in our version, we will just check the result and show it. This is just a trial version, so we will not dwell on the business logic. See below: 

private fun onPaymentSuccess(paymentData: PaymentData?) {
     Toast.makeText(this, “Payment Success”, Toast.LENGTH_LONG).show()
private fun onError(statusCode: Int?) {
   Log.w("loadPaymentData failed", String.format("Error code: %d", statusCode))

Android Google Pay Integration Outcome: What the User Sees

As a result, the users at the checkout stage will see the Google Pay button. After tapping it, they will see the checkout form.

Google Pay Update 2020

Time flies, and to keep this small Android Google Pay integration tutorial up-to-date, we decided to refresh it. Google has made a great tutorial already, and I just try to explain some points from the practical point of view.

Library Versions

First, over time, the version of the payment plug-in library has been upgraded.


The current version of the library is 18.0.0. The changes mainly affected security and bank interaction protocols.

Payment Providers

As to the banks and payment systems that work with Google Pay, you need to look at the up-to-date list. The payments can be realized only through the enumerated payment providers. By the way, the list is quite extensive and the most popular payment systems support GPAY payments as well. Here, you can find global payment systems such as BrainTree, Stripe, Square. There are also several options for the Russian and Ukrainian markets: LiqPay, Portmone, WayForPay, iPay, Sberbank, Yandex, and Tinkoff.


To be able to use payments, you should also study the design guides. It’s quite possible that during a review, you get a blocked payment before all problems are eliminated. All the guide requirements are, and are not complicated and are more than logical.


The first step to making successful payments is to register your business.

Then, you need to configure the payment client itself. To do this, you need to form a basic request at least, where the minimum version of API payment will be indicated. This part should be used during every request.

private val baseRequest = JSONObject().apply {
    put(&quot;apiVersion&quot;, 2)
    put(&quot;apiVersionMinor&quot;, 0)

We immediately form a request to tokenize your provider like the following:

private fun gatewayTokenizationSpecification(): JSONObject {
    return JSONObject().apply {
    put(&quot;type&laquo;, &laquo;PAYMENT_GATEWAY&raquo;)
    put(&quot;parameters&laquo;, JSONObject(mapOf(
    &laquo;gateway&raquo; to&nbsp;&laquo;example&raquo;,
    &laquo;gatewayMerchantId&raquo; to&nbsp;&laquo;exampleGatewayMerchantId&raquo;)))

Next, we indicate what types of cards we will work with and the possible authentication options. See below:

private val allowedCardNetworks = JSONArray(listOf(
private val allowedCardAuthMethods = JSONArray(listOf(

As for authorization, there is an option to specify just PAN_ONLY, which will allow you to use GPAY only with those cards that are already linked to your account. Further, there is nothing difficult. It remains that you only have to create the client for payment (PaymentClient), check the possibility of payment using GPAY, and display the button like it was described above. All code examples are provided on GitHub, where you can find an almost complete sample from Google itself.

Our Experience

Let’s speak about sad things a little. Based on our experience, many companies that need to develop a mobile application have their own flow of work with money already. It means that the companies have open bank accounts and established systems of reporting, so why do they need all this? 

In such cases, there is often a strict requirement from the business to use local payment providers without opening additional accounts and registering companies in consoles. It would be nice to do it, but it is almost impossible to realize it is a native medium. Most of the local payment systems that we have dealt with simply failed to have a working SDK or it was awfully outdated. The way out can be very simple. Although there is no native SDK, you can always use web plugins for payments. It may look not so beautiful, but it works! 

In most cases, we form a page with a ready-made payment plugin from the bank, in which the client is served, and we display it on the built-in browser page. It should also be noted that using an ordinary WebView will not do. Until recently, it was possible to create a simple WebClient, where you could write the logic of redirects, and thus, it was possible to handle callbacks from the payment system, thereby implementing further application logic. 

However, recently they’ve pulled the plug on this because а the poor security. Although most banking widgets will not see any changes in operation, Google Pay will not launch. The button on the widget will appear and will even be clickable, but nothing will happen after a click. The current implementation is CustomTabs usage. It is necessary to generate the URL for the web plugin in the same way, but the web client processing should not be manual; you should give it to the Chrome engine processor. 

Apparently, the cons are obvious: we cannot influence what is going on inside. We can no longer handle redirects and continue the main flow of the application after payment. In such an implementation, you need to trust the user and request the status of payment for the order from the server.

Let’s Sum It Up

Google Pay is a handy and safe payment service for the mobile app with the possibility of the integration of numerous payment systems. It makes purchases easier for clients and boosts online sales for the merchants. With the help of the algorithm described above, you can easily add Google Pay to the Android app you develop for your business and, besides, now you have some idea how to cope with difficulties in its integration.

Credit: Source link

Previous Next
Test Caption
Test Description goes like this