Native Pay Methods
Google Pay
In the MoneyHash SDK for Android, Google Pay can be integrated to provide a seamless and native payment experience on Android devices. Before initiating a Google Pay transaction, it's important to ensure that the device supports Google Pay.
Setup Google Pay Launcher
You can initialize the Google Pay launcher using either Jetpack Compose or the traditional approach (Activity or Fragment).
Compose Setup
When using Jetpack Compose, you can set up the Google Pay launcher using the rememberGooglePayLauncher
function. This setup includes callback handling for successful and failed Google Pay transactions.
val googlePayLauncher = rememberGooglePayLauncher(object : GooglePayResultCallback {
override fun onResult(result: IntentDetails) {
viewModel.onGooglePaySuccess(result)
}
override fun onError(throwable: Throwable) {
viewModel.onGooglePayFailed()
}
})
// Bind the Google Pay launcher to the composable lifecycle
BindEffect(googlePayLauncher)
The BindEffect
function ensures the lifecycle is properly bound to the composable context, allowing you to manage Google Pay flow within the Compose UI.
Activity Setup
For a traditional Android approach, you can create the Google Pay launcher within an Activity by passing the FragmentActivity
context and the result callback.
val googlePayLauncher = GooglePayLauncher(activity, object : GooglePayResultCallback {
override fun onResult(result: IntentDetails) {
// Handle successful payment
Log.d("GooglePay", "Payment successful: $result")
}
override fun onError(throwable: Throwable) {
// Handle payment error
Log.e("GooglePay", "Payment error: ${throwable.message}")
}
})
Fragment Setup
Similarly, if you are working within a Fragment
, you can initialize the Google Pay launcher by passing the Fragment
context.
val googlePayLauncher = GooglePayLauncher(fragment, object : GooglePayResultCallback {
override fun onResult(result: IntentDetails) {
// Handle successful payment
Log.d("GooglePay", "Payment successful: $result")
}
override fun onError(throwable: Throwable) {
// Handle payment error
Log.e("GooglePay", "Payment error: ${throwable.message}")
}
})
Check if the Device is Ready for Google Pay
To verify if the device is ready to use Google Pay, you can call the isReady
method of the GooglePayLauncher
. This ensures that Google Pay can be used on the current device.
val isReadyForGooglePay = googlePayLauncher.isReady()
if (isReadyForGooglePay) {
// Proceed with Google Pay
} else {
// Handle case where the device is not ready for Google Pay
}
Google Pay Configuration
Google Pay integration allows you to process payments within the MoneyHash SDK via a native Google Pay popup. The configuration for Google Pay is managed through the NativeGooglePayConfig
class. This setup is required to handle the native Google Pay payment flow.
Default Google Pay Configuration
If no custom configuration is provided, the SDK will use the following default values:
- Environment:
GooglePayEnvironment.PRODUCTION
- Google Pay will operate in the production environment by default.
- Allowed Cards: A list of all supported card types.
- This includes all major card networks such as Visa, MasterCard, Amex, and Discover.
- Supported Methods: A list of all supported payment methods.
- Includes
pan_only
(traditional card numbers) andcryptogram_3ds
(3D Secure cryptograms).
- Includes
Customizing Google Pay Configuration
To modify the Google Pay configuration, pass a NativeGooglePayConfig
object containing the necessary details for Google Pay integration. You can customize the environment, allowed cards, supported methods, and other merchant-related configurations.
Example of Customizing Google Pay Configuration
Here’s how to set up Google Pay with custom configurations:
val googlePayConfig = NativeGooglePayConfig(
environment = GooglePayEnvironment.TEST, // Use PRODUCTION when ready
allowedCards = listOf(AllowedCards.VISA, AllowedCards.MASTERCARD, AllowedCards.AMEX),
supportedMethods = listOf(SupportedMethods.PAN_ONLY, SupportedMethods.CRYPTOGRAM_3DS)
)
val moneyHashSDK = MoneyHashSDKBuilder()
.setNativeGooglePayConfig(googlePayConfig)
.build()
Log.d("MoneyHash", "SDK initialized with custom Google Pay configuration.")
Purpose
The setNativeGooglePayConfig
method configures the Google Pay integration for displaying the native Google Pay popup within the embedded form. This allows customization of how Google Pay is handled in your payment flow, ensuring a smooth user experience.
Default Configuration Overview
If you don’t provide a custom configuration, the SDK will use the following defaults:
NativeGooglePayConfig(
environment = GooglePayEnvironment.PRODUCTION,
allowedCards = AllowedCards.entries, // All supported card types
supportedMethods = SupportedMethods.entries // All supported methods
)
Customizing these settings allows you to adjust the environment, card types, and payment methods based on your specific needs, such as a test environment, customer preferences, or merchant settings.
Proceed with Google Pay
Once you've confirmed that the device is ready for Google Pay, you can display the Google Pay dialog to the user. Use the presentForPaymentIntent
method in the GooglePayLauncher
class to trigger the Google Pay flow and let the user complete the payment.
Important: Ensure you have the required
merchantId
,currencyCode
,countryCode
,gateway
, andgatewayMerchantId
to proceed with Google Pay.
Example of Proceeding with Google Pay:
googlePayLauncher.presentForPaymentIntent(
intentId = "<intent_id>",
currency = "USD",
countryCode = "US",
amount = 100.0,
gateway = "exampleGateway",
gatewayMerchantId = "gatewayMerchantId",
merchantId = "merchant.com.example",
merchantName = "Your Merchant Name"
)
This method triggers the Google Pay dialog, processes the payment, and returns the IntentDetails
once the payment is completed successfully or throws an error if something goes wrong.
If billing or other required data is missing, the SDK will throw an exception prompting you to provide the necessary details before proceeding.
Google Pay Launcher
The Google Pay flow is initiated using the GooglePayLauncher
class. It provides two scenarios for integrating Google Pay: one for use with traditional activities or fragments and another for Jetpack Compose.
GooglePayResultCallback
To handle the result of a Google Pay transaction, you must implement the GooglePayResultCallback
interface, which provides onResult
and onError
methods.
interface GooglePayResultCallback {
fun onResult(result: IntentDetails)
fun onError(throwable: Throwable)
}
2. Starting Google Pay Transaction
You can trigger the Google Pay flow by calling the startGooglePay
function within the ViewModel. This method checks if the device is ready for Google Pay and, if so, presents the payment intent.
fun startGooglePay(googlePayLauncher: GooglePayLauncher) {
val isReady = googlePayLauncher.isReady()
println("Google Pay is ready: $isReady")
if (isReady) {
// Present Google Pay with the payment intent
googlePayLauncher.presentForPaymentIntent(
intentId = "98veArg", // Payment intent ID
currency = "USD", // Currency code
countryCode = "US", // Country code
amount = 50.0, // Amount to charge
gateway = "checkoutltd", // Payment gateway
gatewayMerchantId = "pk_example", // Gateway Merchant ID
merchantId = "123", // Google Pay Merchant ID
merchantName = "MoneyHash" // Merchant name for display
)
}
}
3. Handling Google Pay Result
Once the Google Pay flow is completed, the onGooglePaySuccess
or onGooglePayFailed
methods in the ViewModel are triggered, depending on the result.
// ViewModel functions for handling Google Pay result
fun onGooglePaySuccess(result: IntentDetails?) {
println("Google Pay Success: $result")
// Handle the successful payment result
}
fun onGooglePayFailed() {
// Handle Google Pay failure
println("Google Pay failed")
}
For more information on configuring Google Pay, refer to:
Updated 25 days ago