Initialize the SDK in your application with the flow, environment and any parameters related to Buy, Sell, NFT or Swap. Additionally you can choose to render the widget either as an in-app browser or a webview.
💡How to choose?
An in-app browser will allow you to use native features like Google Pay, social sign-in and popups.
A webview will allow you to use a communication layer between the widget and your app and therefore get your app to perform certain actions once some events occur in the widget.
val handlers = MoonPayHandlers(
onSwapsCustomerSetupComplete = {
Log.i("HANDLER CALLED","onSwapsCustomerSetupComplete called!")
},
onAuthToken = {
Log.i("HANDLER CALLED", "onAuthToken called with payload $it")
},
onLogin = {
Log.i("HANDLER CALLED", "onLogin called with payload $it")
},
onInitiateDeposit = {
Log.i("HANDLER CALLED","onInitiateDeposit called with payload $it")
OnInitiateDepositResponsePayload(depositId = "someDepositId")
},
onKmsWalletCreated = {
Log.i("HANDLER CALLED", "onKmsWalletCreated called!")
},
onUnsupportedRegion = {
Log.i("HANDLER CALLED", "onUnsupportedRegion called!")
}
)
val params = MoonPayBuyQueryParams("pk_test_123")
params.setCurrencyCode("ETH")
val config = MoonPaySdkBuyConfig(
environment = MoonPayWidgetEnvironment.Sandbox,
debug = true,
params = params,
handlers = handlers
)
// Initialize the SDK using 'this' reference for MainActivity
val moonPaySdk = MoonPayAndroidSdk(config = config, activity = this)
moonPaySdk.show(MoonPayRenderingOptionAndroid.WebViewOverlay())
See the parameters pages for Buy, Sell and Swap for details on which parameters can be passed for each flow.
Arguments:
config: MoonPaySdkConfig,
Your config variant must match your query params variant.
Variants:
MoonPaySdkBuyConfig
MoonPaySdkSellConfig
MoonPaySdkSwapsCustomerSetupConfig
MoonPaySdkLegacyConfig
Arguments:
debug: Boolean
environment: MoonPayWidgetEnvironment
params: MoonPayQueryParams
handlers: MoonPayHandlers?
📘Configuration
Possible values for environment: MoonPayWidgetEnvironment.sandbox, MoonPayWidgetEnvironment.production
Your query params variant must match your config variant.
Variants:
MoonPayBuyQueryParams
MoonPaySellQueryParams
MoonPaySwapsCustomerSetupParams
MoonPayLegacyParams
All of the following are optional. However, you must explicitly pass nil for each handler you don't need to implement.
If you're using the walletAddress or walletAddresses query param, you need to sign your widget URL before you can display the widget. Learn more about URL signing.
moonPaySdk.show takes one argument, mode, which takes a value of type MoonPayRenderingOptionAndroid. The mode variants are MoonPayRenderingOptionAndroid.WebViewOverlay and MoonPayRenderingOptionAndroid.InAppBrowser.
You need to sign your widget URL before you can display the widget. Learn more about URL signing.
val handlers = MoonPayHandlers(
onSwapsCustomerSetupComplete = {
Log.i("HANDLER CALLED","onSwapsCustomerSetupComplete called!")
},
onAuthToken = {
Log.i("HANDLER CALLED", "onAuthToken called with payload $it")
},
onLogin = {
Log.i("HANDLER CALLED", "onLogin called with payload $it")
},
onInitiateDeposit = {
Log.i("HANDLER CALLED","onInitiateDeposit called with payload $it")
OnInitiateDepositResponsePayload(depositId = "someDepositId")
},
onKmsWalletCreated = {
Log.i("HANDLER CALLED", "onKmsWalletCreated called!")
},
onUnsupportedRegion = {
Log.i("HANDLER CALLED", "onUnsupportedRegion called!")
}
)
val params = MoonPayBuyQueryParams("pk_test_123")
params.setCurrencyCode("ETH")
val config = MoonPaySdkBuyConfig(
environment = MoonPayWidgetEnvironment.Sandbox,
debug = true,
params = params,
handlers = handlers
)
// Initialize the SDK using 'this' reference for MainActivity
val moonPaySdk = MoonPayAndroidSdk(activity = this)
moonPaySdk.updateConfig(config)
moonPaySdk.show(MoonPayRenderingOptionAndroid.WebViewOverlay())
// Initialise the SDK as above
val urlSignature = moonpay.generateUrlForSignature();
val jsonObj = JSONObj.getJSONObject(urlSignature);
// The URL for signature should be sent to your backend, which should then
// sign it with your API secret and return the signature.
val response = { make a post request to '/sign-url' with the jsonObj }
// Once you have the signature, you can update the SDK with it and show the
// widget.
moonpay.updateSignature(response)