Native Pay Methods

Apple Pay

In the Flutter SDK, Apple Pay can be used to process payments with a smooth and native experience. Before displaying the Apple Pay sheet, it is essential to ensure that the device supports Apple Pay.

Check if the Device is Compatible

To verify if the device supports Apple Pay, you can call the isDeviceCompatibleWithApplePay method. This ensures that you only attempt to display the Apple Pay sheet on supported devices.

final isCompatible = await moneyHash.isDeviceCompatibleWithApplePay();

if (isCompatible) {
  // Proceed with Apple Pay
} else {
  // Handle case where the device is not compatible
  print("This device does not support Apple Pay");
}

🚧

This feature is only supported on iOS devices. Calling this method on other platforms will throw an UnsupportedError.


Proceed with Apple Pay

Once you've confirmed that the device is compatible, you can proceed by displaying the Apple Pay sheet. Use the proceedWithApplePay method to trigger the Apple Pay dialog, allowing the user to complete the payment.

💡

Important: Make sure you have the required merchantIdentifier, currencyCode, and countryCode to proceed with Apple Pay.

Retrieving Apple Pay Data from NativePay State

When handling an intent state, you may receive a NativePay state that contains the necessary information to proceed with Apple Pay. The NativePay state contains an instance of ApplePayData, which provides the following properties:

  • merchantId: The merchant identifier needed to process Apple Pay.
  • countryCode: The country code for the payment.
  • currencyCode: The currency code for the payment.
  • amount: The total amount for the payment.

You can use this data when calling the proceedWithApplePay method.

Example of Proceeding with Apple Pay:

if (intentState is NativePay) {
  final applePayData = intentState.nativePayData as ApplePayData?;

  if (applePayData != null) {
    try {
      final intentId = "<intent_id>"; // The unique identifier for the payment intent
      final depositAmount = applePayData.amount ?? 0.0; // Amount from state
      final merchantIdentifier = applePayData.merchantId ?? "merchant.com.example"; // Merchant ID from state
      final currencyCode = applePayData.currencyCode ?? "USD"; // Currency code from state
      final countryCode = applePayData.countryCode ?? "US"; // Country code from state

      final IntentDetails? intentDetails = await moneyHash.proceedWithApplePay(
        intentId,
        depositAmount,
        merchantIdentifier,
        currencyCode,
        countryCode,
      );

      if (intentDetails != null) {
        // Handle successful payment
        print("Payment completed successfully: $intentDetails");
      } else {
        // Handle case where no result is returned
        print("Payment was cancelled or no result was returned.");
      }
    } on MHException catch (e) {
      // Handle error during Apple Pay process
      print("Error during Apple Pay: ${e.message}");
    }
  } else {
    print("No Apple Pay data available in NativePay state.");
  }
}

This method shows the Apple Pay sheet to the user, processes the payment, and returns the IntentDetails once completed successfully or throws an error if something goes wrong.

📘

If billing data is required but not provided, the SDK will throw an error, prompting you to provide the necessary data before proceeding.


Google Pay

In the Flutter SDK, Google Pay can be integrated to process payments with a seamless and native experience on Android devices. Before initiating a Google Pay transaction, it's important to ensure that the device supports Google Pay.

Check if the Device is Ready for Google Pay

To verify if the device is ready to use Google Pay, you can call the isReadyForGooglePay method. This ensures that Google Pay can be used on the current device.

final isReadyForGooglePay = await moneyHash.isReadyForGooglePay();

if (isReadyForGooglePay) {
  // Proceed with Google Pay
} else {
  // Handle case where the device is not ready for Google Pay
  print("This device does not support Google Pay");
}

🚧

This feature is only supported on Android devices. Calling this method on other platforms will throw an UnsupportedError.


Here’s how the section on Google Pay Configuration should look like, formatted with the detailed information based on your provided specification:


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 handled through the NativeGooglePayConfig class. This setup is required to manage the native Google Pay payment flow within the embedded form.

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, Discover, and more.
  • Supported Methods: A list of all supported payment methods
    • Includes pan_only (traditional card numbers) and cryptogram_3ds (3D Secure cryptograms).

Customizing Google Pay Configuration

To modify the Google Pay configuration, you need to pass a NativeGooglePayConfig object that contains the necessary details for Google Pay integration. Use this to 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:

import 'package:moneyhash/moneyhash.dart';

void main() {
  // Create a custom Google Pay configuration
  var googlePayConfig = NativeGooglePayConfig(
    environment: GooglePayEnvironment.test, // Switch to production when ready
    allowedCards: [
      AllowedCards.visa,
      AllowedCards.mastercard,
      AllowedCards.amex, // Add more card types if needed
    ],
    supportedMethods: [
      SupportedMethods.pan_only, // Traditional card numbers
      SupportedMethods.cryptogram_3ds, // 3D Secure cryptograms
    ],
  );

  // Initialize the SDK with the custom Google Pay configuration
  var moneyHashSDK = MoneyHashSDKBuilder()
    .setNativeGooglePayConfig(googlePayConfig)
    .build();

  print("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 configuration allows you to customize 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 (pan_only, cryptogram_3ds)
)

By customizing these settings, you can adjust the environment, card types, and payment methods based on your specific needs. This allows you to tailor the Google Pay flow to your merchant settings, customer preferences, or testing environment.


Proceed with Google Pay

Once you've confirmed that the device is ready for Google Pay, you can proceed by displaying the Google Pay dialog. Use the proceedWithGooglePay method to trigger the Google Pay flow, allowing the user to complete the payment.

💡

Important: Ensure you have the required merchantId, currencyCode, countryCode, gateway, and gatewayMerchantId to proceed with Google Pay.

Retrieving Google Pay Data from NativePay State

When handling an intent state, you may receive a NativePay state that contains the necessary information to proceed with Google Pay. The NativePay state contains an instance of GooglePayData, which provides the following properties:

  • merchantId: The Google Pay merchant identifier needed to process the payment.
  • countryCode: The country code for the payment.
  • currencyCode: The currency code for the payment.
  • amount: The total amount for the payment.
  • gateway: The payment gateway to process the Google Pay transaction.
  • gatewayMerchantId: The merchant identifier used by the payment gateway.

You can use this data when calling the proceedWithGooglePay method.

Example of Proceeding with Google Pay:

if (intentState is NativePay) {
  final googlePayData = intentState.nativePayData as GooglePayData?;

  if (googlePayData != null) {
    try {
      final intentId = "<intent_id>"; // The unique identifier for the payment intent
      final amount = googlePayData.amount ?? 0.0; // Amount from state
      final currencyCode = googlePayData.currencyCode ?? "USD"; // Currency code from state
      final countryCode = googlePayData.countryCode ?? "US"; // Country code from state
      final gateway = googlePayData.gateway ?? "exampleGateway"; // Payment gateway from state
      final gatewayMerchantId = googlePayData.gatewayMerchantId ?? "gatewayMerchantId"; // Gateway merchant ID from state
      final merchantId = googlePayData.merchantId ?? "merchant.com.example"; // Google Pay merchant ID from state
      final merchantName = googlePayData.merchantName ?? "Your Merchant Name"; // Merchant name

      final IntentDetails? intentDetails = await moneyHash.proceedWithGooglePay(
        intentId,
        currencyCode,
        amount,
        countryCode,
        gateway,
        gatewayMerchantId,
        merchantId,
        merchantName,
      );

      if (intentDetails != null) {
        // Handle successful payment
        print("Payment completed successfully: $intentDetails");
      } else {
        // Handle case where no result is returned
        print("Payment was cancelled or no result was returned.");
      }
    } on MHException catch (e) {
      // Handle error during Google Pay process
      print("Error during Google Pay: ${e.message}");
    }
  } else {
    print("No Google Pay data available in NativePay state.");
  }
}

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 MHException, prompting you to provide the necessary details before proceeding.