# Google Pay
source: https://developer.mastercard.com/mastercard-gateway/documentation/integrations-types/mobile-integration/google-pay/index.md

You can use [Google Pay](https://developer.mastercard.com/mastercard-gateway/documentation/payment-methods/digital-wallets/google-pay/index.md) to collect payment details as a token representing a payment card that the payer has added to their Google Pay wallet.

The Mobile SDK includes a helper utility, called GooglePayHandler, for collecting a token from the payer's Google Pay wallet:

1. Enable Google Pay support within your app. For instructions, see the [Google Pay](https://developer.mastercard.com/mastercard-gateway/documentation/payment-methods/digital-wallets/google-pay/index.md) documentation. The instructions guide you to:

* Set up the Google Pay API.
* Request payment information from the Google wallet.
* Incorporate the Google Pay button in your checkout layout.
* Manage the response from the Google Pay API.

2. Since Google Pay integration is optional with the Mobile SDK, provide the appropriate play services dependency:

```gradle
implementation 'com.google.android.gms:play-services-wallet:X.X.X'
```

The gateway is integrated with Google Pay as a `PAYMENT_GATEWAY` type.

3. To request encrypted card information from Google Pay:

* Use the valid name mpgs in your request.
* Replace MERCHANT_ID with the merchant ID that you use on the gateway.

```kotlin
val tokenizationSpecification = JSONObject()
    .put("type", "PAYMENT_GATEWAY")
    .put("parameters", JSONObject()
        .put("gateway", "mpgs")
        .put("gatewayMerchantId", "MERCHANT_ID")
    )
```

4. Use the GooglePayHandler to launch the Google Pay wallet with your constructed payments client and request data.

```kotlin
// YourActivity.kt

fun googlePayButtonClicked() {
    try {
        val request = PaymentDataRequest.fromJson(paymentDataRequest.toString())

        // Use the Gateway convenience handler for launching the Google Pay flow
        GooglePayHandler.requestData(this, paymentsClient, request)

    } catch (e: JSONException) {
        Toast.makeText(this, "Could not request payment data", Toast.LENGTH_SHORT).show()
    }
}
```

5. To manage wallet responses, the Mobile SDK offers a Google Pay lifecycle handler. You can implement the provided Google Pay callback and use the result handler within your activity. This eliminates the need to manually manage the Google Pay activity results and delegates the important transaction steps to the callback methods.

```kotlin
// YourActivity.kt

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    // Handle the Google Pay response
    if (GooglePayHandler.handleActivityResult(requestCode, resultCode, data, callback)) {
        return
    }

    super.onActivityResult(requestCode, resultCode, data)
}

val googlePayCallback = object : GooglePayCallback {
    override fun onReceivedPaymentData(paymentData: JSONObject) {
        try {
            val description = paymentData
                .getJSONObject("paymentMethodData")
                .getString("description")

            Log.d(MyGooglePayCallback::class.java.simpleName, "ReceivedPaymentData: $description")
        } catch (e: Exception) {
            // Handle exception
        }

        handleGooglePayData(paymentData)
    }

    override fun onGooglePayCancelled() {
        // Handle cancelled
    }

    override fun onGooglePayError(status: Status) {
        // Handle error
    }
}
```

6. When you receive payment data from Google Pay, update the gateway session with the payment token.

```kotlin
fun handleGooglePayData(paymentData: JSONObject) {
    val token = paymentData
        .getJSONObject("paymentMethodData")
        .getJSONObject("tokenizationData")
        .getString("token")

    val request = GatewayMap()
        .set("sourceOfFunds.provided.card.devicePayment.paymentToken", token)

    GatewayAPI.updateSession(session, request, callback)
}
```

7. Complete a Google Pay transaction from your server just as any other payment, using the PAY or AUTHORIZE transaction. For more information, see [Making a Server API Request](https://developer.mastercard.com/mastercard-gateway/documentation/getting-started/make-server-api-req/index.md). When completing a payment on a session that is using Google Pay, set the `order.walletProvider` field to GOOGLE_PAY and include the session ID in the request.
