Skip to main content
Use WebView to embed frames in native Android applications. The WebView communicates with frames via JavaScript interfaces and evaluateJavascript.
Read the manual integration overview for core concepts before you continue.

Setup

Dependencies

The examples below use Tink for cryptographic operations, but you can use any library that supports X25519 and AES-GCM. The base fragment uses AndroidX WebKit to enable the Payment Request API for Google Pay.

Key generation

Decryption utility

Base frame fragment

Create a reusable base fragment for frame communication:

Handle JavaScript dialogs

In test mode, the Buy Button frame renders a mock Google Pay button for a Google Pay quote and uses window.prompt to simulate the Google Pay payment sheet. Android’s WebView doesn’t show alert, confirm, or prompt unless you attach a WebChromeClient that handles them. Without one, every test transaction silently comes back with status: "failed". Override onJsPrompt and surface the simulated payment sheet with an AlertDialog. Return the entered text through JsPromptResult.confirm(), or call cancel() when the customer declines. The onJsConfirm handler covers any other dialog the frame might show:
In the prompt, OK authorizes the test payment with the billing address in the text field (the frame emits complete with a non-failed status); Cancel declines it (the frame emits complete with status: "failed").
Attach the WebChromeClient even if you only plan to ship live mode. The default WebView behavior applies to any window.confirm, alert, or prompt the frame might surface, and makes test-mode debugging impossible without it.

Check frame

The check frame verifies whether a customer already has an active connection. It’s headless — no UI is rendered. Use it to skip the connect flow for returning customers. See check frame reference for event details.

Check fragment

Usage


Connect frame

The connect frame establishes a customer connection to your application. See connect frame reference for event details.

Connect fragment

Usage with Activity


Auth frame

The auth frame authenticates a customer with email or SMS one-time passcode and returns fully scoped credentials — without the hosted KYC UI in the connect frame. Launch it after the check frame returns connectionRequired, typically when you drive onboarding through the Customer API. See auth frame reference for event details.

Auth fragment

Usage with Activity


Add Card frame

The add card frame lets a customer save a new card to their account. See add card frame reference for event details.

What you’ll need

Before you initialize the add card frame, you need:
  1. A clientToken from a successful connect flow

Add Card fragment

Usage with Activity


Buy Button frame

The Buy Button frame takes a quote to payment. It renders the Google Pay or card button that matches the payment method in your quote, and supports guest checkout when your quote allows it. For card and bank transfer quotes, you can also run it in headless mode behind your own purchase button. See the Buy Button frame reference for event details, and Pay with the buy button for the full walkthrough.

Android WebView setup

Google Pay in a WebView needs the setup on Android WebView requirements. The base fragment already enables the Payment Request API with the AndroidX WebKit dependency from Dependencies. You still need to declare the Google Pay intent queries, meet the version requirements, and get your app approved before you load the Buy Button frame with a Google Pay quote.

What you’ll need

Before you initialize the Buy Button frame, you need:
  1. A clientToken from a successful connect flow
  2. A valid quote signature for the transaction

Buy Button fragment

Pass headless = true to newInstance to run the purchase without a button, as described in Headless mode.

Challenge handling

When the Buy Button fragment receives a challenge URL, for example for 3-D Secure, add a MoonPayChallengeFragment to present the challenge. Opened without resume, as below, the challenge frame finishes the purchase for every payment method and sends complete. On completion, cancellation, or error, remove both the challenge and Buy Button fragments. To have the customer finish a Google Pay purchase on your buy button instead, see Finish on your buy button.

Usage with Activity

Give the button container a height of 44dp. See Size.

Headless mode

For card and bank transfer quotes, you can draw your own purchase button and pass headless = true to newInstance. The frame loads with headless=true, renders nothing, and starts the purchase as soon as it loads. Add the fragment to a 1px container when the customer confirms the purchase in your UI, not before. See Headless mode. In headless mode, the frame:
  • Sends ready, then exactly one of complete, challenge, or error.
  • Never sends buttonPressed or cancelled.
  • Accepts card and bank transfer (SEPA) quotes only. A Google Pay quote returns error with unsupportedPaymentMethod.
  • Needs a connected customer. A guest checkout quote returns error with invalidQuote.
  • Reads setQuote only after an error with quoteExpired.
On challenge, add the challenge fragment without resume, as in challenge handling. It finishes the purchase and sends its own complete. For a bank transfer (SEPA) quote, onBuyButtonComplete receives the transaction id. Fetch the transaction by ID, and show the customer the bank details from bankTransferDepositInfo and the amount to send from source.amount natively so they can send the transfer. See Pay with bank transfer.
For bank transfers, the customer must include the payment reference from bankTransferDepositInfo with their transfer, or it is rejected. Render the deposit details, including the reference, in your own UI. See the transaction object.
Reuse the listener methods from the usage example above. They remove the fragments after the frame’s final event: complete, or an error other than quoteExpired. Add the fragment from your purchase button’s click handler:
Pass an externalTransactionId to newInstance as well. If the system recreates the fragment mid-purchase, it removes itself rather than start a second purchase, and the ID lets you find the interrupted attempt through the API.

Widget frame

Apple Pay is not available on Android. Use the widget frame instead to render the full MoonPay buy experience — including payment collection and transaction confirmation — for credit/debit card, Google Pay, bank transfers, and more. See pay with widget for a full walkthrough.

What you’ll need

Before you initialize the widget frame, you need:
  1. A clientToken from a successful connect flow
  2. A valid quote signature for the transaction

Widget fragment

Usage with Activity


Reset frame

The reset frame is headless. Use it when the customer signs out to clear MoonPay session state on MoonPay’s domain. See reset frame reference. Pass your clientToken so your app is authorized to embed the frame. Do not block sign-out if reset fails.

Reset fragment

Usage