# Google Pay

* Web
* Native Android App

## Web Integration[​](#web-integration "Direct link to Web Integration")

You can start accepting Google Pay payments on the Web using [Hosted Payment Page](https://docs.monei.com/integrations/use-prebuilt-payment-page/.md) or [Payment Request Component](https://docs.monei.com/monei-js/reference/.md#paymentrequest-component). No additional configuration is required.

Our [Payment Request Component](https://docs.monei.com/monei-js/reference/.md#paymentrequest-component) gives you a single integration for Apple Pay and Google Pay. Customers see a Google Pay or an Apple Pay button, depending on what their device and browser combination supports.

If you prefer to integrate directly against the Google Pay API, follow the [direct API integration guide](#direct-api-integration) below.

### Before you begin[​](#before-you-begin "Direct link to Before you begin")

To accept Google Pay payments you need to have at least one configured card processor.

To configure card processors go to [MONEI Dashboard → Settings → Payment Methods → Card payments](https://dashboard.monei.com/settings/payment-methods/card).

Before you start, you need to:

* Make sure that you have Google Pay enabled in [MONEI Dashboard → Settings → Payment Methods](https://dashboard.monei.com/settings/payment-methods).
* Add a card in Chrome.
* Serve your application over HTTPS. This is a requirement both in development and in production. One way to get up and running is to use a service like [ngrok](https://ngrok.com/).

To test your integration:

* Use your [test mode](https://docs.monei.com/testing/.md) Account ID and API Key.
* You can use any real card details, you will not be charged in the test mode (card details are automatically replaced with the test card).
* You can check the status of the test payment in your [MONEI Dashboard → Payments](https://dashboard.monei.com/payments) (in test mode).

Google Pay Terms

By integrating Google Pay™, you adhere to the Google Pay APIs [Acceptable Use Policy](https://payments.developers.google.com/terms/aup) and accept the terms defined in the Google Pay API [Terms of Service](https://payments.developers.google.com/terms/sellertos).

### Express Checkout[​](#express-checkout "Direct link to Express Checkout")

Use express checkout to collect shipping and billing addresses directly in the Google Pay payment sheet. See the [Express Checkout guide](https://docs.monei.com/integrations/express-checkout/.md) for setup and examples.

### Direct API Integration[​](#direct-api-integration "Direct link to Direct API Integration")

If you prefer to integrate directly against the Google Pay API, follow [Google's instructions](https://developers.google.com/pay/api/web/guides/setup).

Specify following tokenization specification:

```
const tokenizationSpecification = {

  type: 'PAYMENT_GATEWAY',

  parameters: {

    gateway: 'monei',

    gatewayMerchantId: 'MONEI_ACCOUNT_ID'

  }

};
```

Use MONEI's `merchantId` from the [Get PaymentMethods](https://docs.monei.com/apis/rest/payment-methods-get/.md) API (`metadata.googlePay.merchantId`). No separate registration with Google is required.

```
paymentDataRequest.merchantInfo = {

  merchantName: 'Your Business Name',

  // Get merchantId from Get PaymentMethods API (metadata.googlePay.merchantId)

  // Test: 12345678901234567890, Production: BCR2DN6T37ENLJ3M

  merchantId: 'MONEI_GOOGLE_PAY_MERCHANT_ID'

};
```

Exchange Google Pay token for MONEI Payment Token that can be used to confirm payment:

```
async function processPayment(paymentData) {

  // Encode Google Pay token as a base64 string

  const token = window.btoa(paymentData.paymentMethodData.tokenizationData.token);

  const result = await monei.api.createToken({

    paymentId: '{{payment_id}}',

    paymentMethod: {

      googlePay: {

        token: token

      }

    }

  });

  return moneiTokenHandler(result.paymentToken);

}
```

### Confirm the Payment[​](#confirm-the-payment "Direct link to Confirm the Payment")

After you have obtained the MONEI Payment Token, you can confirm the payment using the token.

```
async function moneiTokenHandler(token) {

  try {

    const result = await monei.confirmPayment({

      paymentId: '{{payment_id}}',

      paymentToken: token

    });

    // At this moment you can show a customer the payment result

    // But you should always rely on the result passed to the callback endpoint

    // on your server to update the order status

    console.log(result);

  } catch (error) {

    console.error(error);

  }

}
```

## Native Android App Integration[​](#native-android-app-integration "Direct link to Native Android App Integration")

Google Pay works out of the box on Android with no additional certificate setup required. Simply integrate the Google Pay API and use MONEI as your payment gateway.

### Prerequisites[​](#prerequisites "Direct link to Prerequisites")

* Google Pay enabled in [MONEI Dashboard → Settings → Payment Methods](https://dashboard.monei.com/settings/payment-methods)
* At least one configured card processor in [MONEI Dashboard](https://dashboard.monei.com/settings/payment-methods/card)
* Android device or emulator with Google Play Services

### Add Dependencies[​](#add-dependencies "Direct link to Add Dependencies")

Add the Google Pay library to your app's `build.gradle`:

```
dependencies {

    implementation 'com.google.android.gms:play-services-wallet:19.2.1'

}
```

Add the Google Pay API meta-data to your `AndroidManifest.xml`:

```
<meta-data

    android:name="com.google.android.gms.wallet.api.enabled"

    android:value="true" />
```

### Check Google Pay Availability[​](#check-google-pay-availability "Direct link to Check Google Pay Availability")

Before showing the Google Pay button, verify it's available on the device:

```
val paymentsClient = Wallet.getPaymentsClient(

    this,

    Wallet.WalletOptions.Builder()

        .setEnvironment(WalletConstants.ENVIRONMENT_TEST) // Use ENVIRONMENT_PRODUCTION for live

        .build()

)



val isReadyToPayRequest = IsReadyToPayRequest.fromJson("""

    {

        "apiVersion": 2,

        "apiVersionMinor": 0,

        "allowedPaymentMethods": [{

            "type": "CARD",

            "parameters": {

                "allowedAuthMethods": ["PAN_ONLY", "CRYPTOGRAM_3DS"],

                "allowedCardNetworks": ["VISA", "MASTERCARD", "AMEX", "DISCOVER", "JCB"]

            }

        }]

    }

""")



paymentsClient.isReadyToPay(isReadyToPayRequest)

    .addOnCompleteListener { task ->

        if (task.isSuccessful) {

            // Show Google Pay button

        }

    }
```

### Configure Payment Request[​](#configure-payment-request "Direct link to Configure Payment Request")

Create a payment request with MONEI as the gateway. Use the `merchantId` from the [Get PaymentMethods](https://docs.monei.com/apis/rest/payment-methods-get/.md) API (`metadata.googlePay.merchantId`). No separate registration with Google is required:

```
val paymentDataRequest = PaymentDataRequest.fromJson("""

    {

        "apiVersion": 2,

        "apiVersionMinor": 0,

        "allowedPaymentMethods": [{

            "type": "CARD",

            "parameters": {

                "allowedAuthMethods": ["PAN_ONLY", "CRYPTOGRAM_3DS"],

                "allowedCardNetworks": ["VISA", "MASTERCARD", "AMEX", "DISCOVER", "JCB"]

            },

            "tokenizationSpecification": {

                "type": "PAYMENT_GATEWAY",

                "parameters": {

                    "gateway": "monei",

                    "gatewayMerchantId": "YOUR_MONEI_ACCOUNT_ID"

                }

            }

        }],

        "merchantInfo": {

            "merchantName": "Your Store Name",

            "merchantId": "MONEI_GOOGLE_PAY_MERCHANT_ID"

        },

        "transactionInfo": {

            "totalPrice": "10.00",

            "totalPriceStatus": "FINAL",

            "currencyCode": "EUR",

            "countryCode": "ES"

        }

    }

""")
```

### Launch Google Pay[​](#launch-google-pay "Direct link to Launch Google Pay")

Launch the Google Pay payment sheet when the user taps the button:

```
private val googlePayLauncher = registerForActivityResult(

    ActivityResultContracts.StartActivityForResult()

) { result ->

    when (result.resultCode) {

        Activity.RESULT_OK -> {

            result.data?.let { intent ->

                val paymentData = PaymentData.getFromIntent(intent)

                handlePaymentSuccess(paymentData)

            }

        }

        Activity.RESULT_CANCELED -> {

            // User cancelled

        }

        AutoResolveHelper.RESULT_ERROR -> {

            // Handle error

        }

    }

}



fun requestPayment() {

    val task = paymentsClient.loadPaymentData(paymentDataRequest)

    AutoResolveHelper.resolveTask(task, this, GOOGLE_PAY_REQUEST_CODE)

}
```

### Process the Payment[​](#process-the-payment "Direct link to Process the Payment")

Extract the token from the payment data and send it to your backend:

```
private fun handlePaymentSuccess(paymentData: PaymentData?) {

    val paymentInfo = paymentData?.toJson() ?: return

    val json = JSONObject(paymentInfo)



    // Extract the Google Pay token

    val token = json

        .getJSONObject("paymentMethodData")

        .getJSONObject("tokenizationData")

        .getString("token")



    // Base64 encode the token

    val encodedToken = Base64.encodeToString(token.toByteArray(), Base64.NO_WRAP)



    // Send to your backend to create MONEI payment token and confirm payment

    sendToBackend(encodedToken)

}
```

### Backend Integration[​](#backend-integration "Direct link to Backend Integration")

On your backend, exchange the Google Pay token for a MONEI payment token:

```
# 1. Create a payment

curl -X POST https://api.monei.com/v1/payments \

  -H "Authorization: YOUR_API_KEY" \

  -H "Content-Type: application/json" \

  -d '{

    "amount": 1000,

    "currency": "EUR",

    "orderId": "order_123"

  }'



# 2. Create a token from Google Pay token

curl -X POST https://api.monei.com/v1/payment-tokens \

  -H "Authorization: YOUR_API_KEY" \

  -H "Content-Type: application/json" \

  -d '{

    "paymentId": "PAYMENT_ID_FROM_STEP_1",

    "paymentMethod": {

      "googlePay": {

        "token": "BASE64_ENCODED_GOOGLE_PAY_TOKEN"

      }

    }

  }'



# 3. Confirm the payment

curl -X POST https://api.monei.com/v1/payments/PAYMENT_ID/confirm \

  -H "Authorization: YOUR_API_KEY" \

  -H "Content-Type: application/json" \

  -d '{

    "paymentToken": "MONEI_TOKEN_FROM_STEP_2"

  }'
```

## Before you go live[​](#before-you-go-live "Direct link to Before you go live")

* Make sure that you are using [live (production) mode](https://docs.monei.com/testing/.md) Account ID and API Key.
* Make sure that you have at least one [enabled card processor](https://dashboard.monei.com/settings/payment-methods/card).
* Make sure that you have Google Pay enabled in [MONEI Dashboard → Settings → Payment Methods](https://dashboard.monei.com/settings/payment-methods).
* For direct API integration, use the `merchantId` from the [Get PaymentMethods](https://docs.monei.com/apis/rest/payment-methods-get/.md) API.
