Android

📱 Installation guide for our Android SDK

💡

Supporting the latest versions

The SDK supports Android 9 and above

Install

Add the repository in your build.gradle or build.gradle.kts.

repositories {
    maven {
        url("https://s01.oss.sonatype.org/content/repositories/releases/")
    }
}

You can install the SDK by adding the following line to the build.gradle file under dependencies:

implementation("com.moonpay.sdk:MoonPaySdk-android:1.1.2")

Initialize

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())
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())

SDK Configuration

See the parameters pages for Buy, Sell and Swap for details on which parameters can be passed for each flow.

MoonPayAndroidSdk

Arguments:

  • config: MoonPaySdkConfig,

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

MoonPayQueryParams

Your query params variant must match your config variant.

Variants:

  • MoonPayBuyQueryParams
  • MoonPaySellQueryParams
  • MoonPaySwapsCustomerSetupParams
  • MoonPayLegacyParams

MoonPayHandlers

All of the following are optional. However, you must explicitly pass nil for each handler you don't need to implement.

Arguments:

  • onSwapsCustomerSetupComplete: () -> Void
  • onUnsupportedRegion: () -> Void
  • onKmsWalletCreated: () -> Void
  • onAuthToken: (OnAuthTokenRequestPayload) -> Void
  • onLogin: (OnLoginRequestPayload) -> Void
  • onInitiateDeposit: (OnInitiateDepositRequestPayload) -> OnInitiateDepositResponsePayload

Handler Payload Types

OnAuthTokenRequestPayload

Arguments:

  • token: String
  • csrfToken: String

OnLoginRequestPayload

Arguments:

  • isRefresh: Boolean

OnInitiateDepositRequestPayload

Arguments:

  • transactionId: String
  • cryptoCurrencyAmount: String
  • cryptoCurrencyAmountSmallestDenomination: String
  • fiatCurrencyAmount: String?
  • depositWalletAddress: String
  • cryptoCurrency: CryptoCurrency
  • fiatCurrency: FiatCurrency

OnInitiateDepositResponsePayload

Arguments:

  • depositId: String

CryptoCurrency

Arguments:

  • id: String
  • name: String
  • code: String
  • contractAddress: String?
  • chainId: String?
  • coinType: String?
  • networkCode: String?

FiatCurrency

Arguments:

  • id: String
  • name: String
  • code: String

Display

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.

// 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)