webview_flutter to embed frames in Flutter applications. The WebView communicates with your Dart code through JavaScript channels.
Read the manual integration
overview for core concepts
before you continue.
Setup
Dependencies
The examples below use cryptography for X25519 key exchange, but you can use any library that supports X25519 and AES-GCM. Add the required packages to yourpubspec.yaml:
flutter pub get to install the packages.
Platform configuration
- iOS
- Android
Add the following to your
ios/Runner/Info.plist:Key generation
Create a utility class for X25519 key generation and decryption:Base WebView widget
Create a reusable widget for frame communication:Handle JavaScript dialogs
In test mode, the Buy Button frame renders a mock Apple Pay or Google Pay button for a wallet quote and useswindow.prompt to simulate the Apple Pay or Google Pay payment sheet. OK authorizes the test payment and Cancel declines it.
webview_flutter does not surface window.prompt (or alert / confirm) by default — the underlying WKWebView on iOS and WebView on Android both require explicit dialog handlers. Without one, the call returns null with no UI shown, the frame interprets that as the customer cancelling, and every test transaction comes back with status: "failed".
This WebView dialog requirement is captured on the Buy Button frame reference, which is the source of truth.
Wire dialog handling via the platform-specific implementations. On iOS use WebKitWebViewController’s UI delegate hooks to handle the text input panel; on Android use AndroidWebViewController to attach a WebChromeClient whose onJsPrompt returns the entered text, or cancels when the customer declines. Surface the prompt with a Flutter AlertDialog so it matches the rest of your UI, and call back into the controller with the result.
Wire dialog handling even if you only plan to ship live mode. It 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 widget
Usage
Connect frame
The connect frame establishes a customer connection to your application. See connect frame reference for event details.Connect widget
Usage
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 returnsconnectionRequired, typically when you drive onboarding through the Customer API. See auth frame reference for event details.
Auth widget
Usage
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:- A
clientTokenfrom a successful connect flow
Add Card widget
Usage
Buy Button frame
The Buy Button frame takes a quote to payment. It renders the Apple Pay, 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.For a Cash App quote, your app opens the Cash App link. The frame sends a
challenge event with payload.kind set to link_payment_method. See Cash
App Pay link
challenge.Apple Pay only works on iOS devices with Apple Pay configured. Google Pay
needs extra setup in an Android WebView and isn’t available in an iOS WebView.
See Mobile WebViews. For the
Apple Pay button in test mode, also handle JavaScript
dialogs.
What you’ll need
Before you initialize the Buy Button frame, you need:- A
clientTokenfrom a successful connect flow - A valid quote signature for the transaction
Buy Button widget
The widget renders the button at 44px tall. See Size. Passheadless: true to run the purchase without a button, as described in Headless mode.
Challenge widget
When the Buy Button frame emits achallenge event, for example for 3-D Secure, render the challenge frame at the URL. Opened without resume, as below, it finishes the purchase for every payment method and sends complete. To have the customer finish an Apple Pay or Google Pay purchase on your buy button instead, see Finish on your buy button.
Usage
Headless mode
For card and bank transfer quotes, you can draw your own purchase button and passheadless: true to MoonPayBuyButtonFrame. The frame loads with headless=true, renders nothing, and starts the purchase as soon as it loads. The base widget keeps it at 1x1 and transparent. Mount it when the customer confirms the purchase in your UI, not before. See Headless mode.
In headless mode, the frame:
- Sends
ready, then exactly one ofcomplete,challenge, orerror. - Never sends
buttonPressedorcancelled. - Accepts card and bank transfer (SEPA) quotes only. An Apple Pay or Google Pay quote returns
errorwithunsupportedPaymentMethod. - Needs a connected customer. A guest checkout quote returns
errorwithinvalidQuote. - Reads
setQuoteonly after anerrorwithquoteExpired.
challenge, render the challenge widget without resume. It finishes the purchase and sends its own complete.
For a bank transfer (SEPA) quote, onComplete 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.
Reuse PaymentScreen from the usage example above with two changes: show your own purchase button and loading state, and mount the frame with headless: true only after the customer taps your button.
After complete, or an error other than quoteExpired, unmount the frame so a retry mounts a new one. After a challenge, call _endHeadlessPurchase() from the challenge widget’s callbacks too.
Widget frame
To hand the whole purchase to MoonPay, including amount entry and payment method selection, use the widget frame. It renders the full MoonPay buy experience inside a WebView, including payment collection and transaction confirmation. See pay with widget for a full walkthrough.Widget widget
Usage
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 yourclientToken so your app is authorized to embed the frame. Do not block sign-out if reset fails.