# Google Pay for Android Accept Google Pay payments in your Android app with [Flow for mobile](https://www.checkout.com/docs/payments/accept-payments/accept-a-payment-on-your-mobile-app.md), our pre-built, customizable payment user interface that you can embed directly into your mobile application. Flow enables you to accept payments using Checkout.com's global network of payment methods with a single integration. --- ## How it works 1. The customer opens the checkout screen. 2. You perform a server-side request to create a payment session. 3. You use the payment session data on the client side to create and render Flow for mobile. 4. Flow for mobile displays the Google Pay payment method to the customer, if it's available on their device. 5. When the customer taps _Pay_, Flow for mobile launches the Google Pay screen and performs the payment request. 6. You receive a webhook notifying you of the payment status. --- ## Before you start Make sure you have a [test account](https://www.checkout.com/get-test-account) with Checkout.com. Create a [public key and a secret key](https://www.checkout.com/docs/resources/api-authentication/api-keys.md) from the [Dashboard](https://dashboard.checkout.com), with the following key scopes: - Public key – `payment-sessions:pay` and `vault-tokenization` - Secret key – `payment-sessions` To receive webhooks for the [payment events](https://www.checkout.com/docs/developer-resources/event-notifications/event-types.md#Gateway), [configure your webhook server](https://www.checkout.com/docs/developer-resources/event-notifications/receive-webhooks/configure-your-webhook-server.md). Integrate with [Flow for mobile](https://www.checkout.com/docs/payments/accept-payments/accept-a-payment-on-your-mobile-app/get-started-with-flow-for-mobile.md) before continuing. > **Note** > > Google Pay has [additional requirements](https://www.checkout.com/docs/payments/accept-payments/accept-a-payment-on-your-mobile-app.md#Apple_Pay_and_Google_Pay_requirements) you must adhere to. --- ## Add the SDK dependency In your project-level `build.gradle` file, add: ```gradle repositories { mavenCentral() // Ensure the following Maven repositories are included, as they are specifically required for resolving Risk SDK dependencies: maven { url = uri("https://jitpack.io") } maven { url = uri("https://maven.fpregistry.io/releases") } } ``` In your app-level `build.gradle` file, add: ```gradle dependencies { implementation 'com.checkout:checkout-android-components:$latest_version' } ``` Google Pay support is included in the `checkout-android-components` package, so you don't need to add a separate dependency. Add the Google Pay API to your Android manifest file. In your `AndroidManifest.xml`, add the following meta-data tag within the `` element: ```xml ``` --- ## Create a payment session `Server side` When the customer is ready to pay, send a server-side request to securely create a `PaymentSession` object. The `PaymentSession` contains the information required to handle all steps of the payment flow. > **Note** > > Do not embed your secret key in your client-side code. Call the [Create a Payment Session](https://api-reference.checkout.com/tag/Flow/#operation/CreatePaymentSession) endpoint. Google Pay is processed as a card payment. The `currency` and `billing.address.currency` must be supported by [Google Pay](https://support.google.com/googlepay/answer/12429287?hl=en-GB). > **Information** > > Your base URL's `{prefix}` value is unique to your account and environment. To learn how to retrieve your base URLs for the sandbox and production environments, see [API endpoints](https://www.checkout.com/docs/developer-resources/api/api-endpoints.md). **Production** `POST` `https://{prefix}.api.checkout.com/payment-sessions` **Sandbox** `POST` `https://{prefix}.api.sandbox.checkout.com/payment-sessions` ### Request example ```json { "amount": 1000, "currency": "GBP", "reference": "ORD-123A", "billing": { "address": { "country": "GB" } }, "customer": { "name": "Jia Tsang", "email": "jia.tsang@example.com" }, "success_url": "https://example.com/payments/success", "failure_url": "https://example.com/payments/failure" } ``` ### Response example ```json { "id": "ps_2Un6I6lRpIAiIEwQIyxWVnV9CqQ", "payment_session_secret": "pss_e21237b4-214a-4e35-a35d-f33wkwo158f6" } ``` The payment session response also determines which payment methods can be displayed to the customer. The response is determined by the customer's device, the customer's region, and the payment methods activated for your account. --- ## Enable Google Pay in Flow for mobile `Client side` To accept Google Pay, you must include a `GooglePayFlowCoordinator` in the `flowCoordinators` you pass to `CheckoutComponentConfiguration`, and forward the Google Pay activity result to `CheckoutComponents`. 1. In the `ViewModel` class, create a `lateinit` variable of `CheckoutComponents`, and expose a function to handle the Google Pay activity result: ```kotlin lateinit var checkoutComponents: CheckoutComponents fun handleActivityResult(resultCode: Int, data: String) { checkoutComponents.handleActivityResult(resultCode, data) } ``` 2. In the `Activity#onCreate` method, create a `GooglePayFlowCoordinator`, and pass `viewModel#handleActivityResult` to it: ```kotlin val coordinator = GooglePayFlowCoordinator( context = activity, // your activity context handleActivityResult = { resultCode, data -> viewModel.handleActivityResult(resultCode, data) } ) val flowCoordinators = mapOf(PaymentMethodName.GooglePay to coordinator) ``` > **Note** > > The `activity` object that you pass to `GooglePayFlowCoordinator` must implement `ViewModelStoreOwner`, `ActivityResultRegistryOwner`, and `LifecycleOwner`. A standard `ComponentActivity` or `AppCompatActivity` implements all three. If your `activity` object does not implement these interfaces, the creation of `GooglePayFlowCoordinator` fails, and the app throws an exception. 3. Pass the `flowCoordinators` back to the `ViewModel`, and use them to create your `CheckoutComponents` instance. `CheckoutComponentsFactory#create` is a suspend function, so call it from a coroutine: ```kotlin val config = CheckoutComponentConfiguration( context = context, paymentSession = paymentSession, publicKey = publicKey, environment = environment, flowCoordinators = flowCoordinators, ) CoroutineScope(Dispatchers.IO).launch { try { checkoutComponents = CheckoutComponentsFactory(config = config).create() } catch (checkoutError: CheckoutError) { handleError(checkoutError) } } ``` 4. Create a `googlePayComponent`, and check that Google Pay `isAvailable` before you render it. `isAvailable()` is also a suspend function: ```kotlin val googlePayComponent = checkoutComponents.create(PaymentMethodName.GooglePay) lifecycleScope.launch { if (googlePayComponent.isAvailable()) { // Render it in a Composable function googlePayComponent.Render() // Or render it in a View based function googlePayComponent.provideView(containerView) } } ``` > **Note** > > `Render()` and `provideView()` are alternative ways to display the component, one for Compose and one for View-based layouts. Use whichever matches your app's UI, not both. --- ## Handle the payment response `Client side` You can listen to the existing `onSuccess` [callback](https://www.checkout.com/docs/payments/accept-payments/accept-a-payment-on-your-mobile-app/flow-for-mobile-library-reference/android/componentcallback.md#onSuccess(PaymentMethodComponent,_PaymentId)) or `onError` [callback](https://www.checkout.com/docs/payments/accept-payments/accept-a-payment-on-your-mobile-app/flow-for-mobile-library-reference/android/componentcallback.md#onError(PaymentMethodComponent,_CheckoutError)) to handle the payment response for Google Pay, alongside card and other payment method outcomes: ```kotlin val configuration = CheckoutComponentConfiguration( context = context, paymentSession = paymentSession, publicKey = publicKey, environment = environment, flowCoordinators = flowCoordinators, componentCallback = object : ComponentCallback { override fun onSuccess(component: PaymentMethodComponent, paymentId: String) { // Payment completed — navigate to confirmation screen } override fun onError(component: PaymentMethodComponent, error: CheckoutError) { // Payment declined, failed, or cancelled — show error UI } } ) ``` If the customer dismisses the Google Pay screen, Flow for mobile calls `onError`. We send you a webhook notifying you of changes to the payment's status. Wait for the webhook callback before you start your order fulfillment process. For payments submitted with Flow, the `PaymentSession` ID appears in the webhook payload's `data.metadata.cko_payment_session_id` field. Flow for mobile also provides other callbacks so you can customize the payment flow further. For example, you can [dynamically adjust the payment amount](https://www.checkout.com/docs/payments/accept-payments/accept-a-payment-on-your-mobile-app/extend-your-flow-for-mobile-integration/dynamically-adjust-the-payment-amount.md) or [retrieve card metadata](https://www.checkout.com/docs/payments/accept-payments/accept-a-payment-on-your-mobile-app/extend-your-flow-for-mobile-integration/retrieve-card-metadata.md) and [card tokenization results](https://www.checkout.com/docs/payments/accept-payments/accept-a-payment-on-your-mobile-app/extend-your-flow-for-mobile-integration/retrieve-card-tokenization-results.md). For more information, see [Extend your Flow for mobile integration](https://www.checkout.com/docs/payments/accept-payments/accept-a-payment-on-your-mobile-app/extend-your-flow-for-mobile-integration.md). --- ## Test the integration Use [test cards](https://www.checkout.com/docs/developer-resources/testing/test-cards.md) to simulate different payment flows and ensure your integration is set up correctly and behaving as expected. You can check the status of a payment from the _Payments_ > _Processing_ > _All payments_ page in the [Dashboard](https://dashboard.checkout.com). ### Google Data safety form When you submit your Android app to Google Play, provide the [required responses](https://www.checkout.com/docs/developer-resources/sdks/data-safety-form.md) in your Data safety form. --- ## Next steps **[Customize Flow for mobile](https://www.checkout.com/docs/payments/accept-payments/accept-a-payment-on-your-mobile-app/customize-flow-for-mobile.md)** Learn how to customize Flow for mobile. **[Flow for mobile library reference](https://www.checkout.com/docs/payments/accept-payments/accept-a-payment-on-your-mobile-app/flow-for-mobile-library-reference/android.md)** See the configuration options and error handling steps for Flow for mobile on Android.