MoneyHash SDK - CardForm Documentation

The CardForm class in the MoneyHash SDK for Flutter provides a secure and user-friendly way to collect and handle card data for payment processing. This document details the methods available in the CardForm class, organized based on their dependencies.

Introduction

The CardForm class is designed to securely collect card information from users, tokenize the card data, and process payments. It interacts with the MoneyHashSDK and depends on certain configurations and data to function correctly.


Dependencies Overview

  • No Dependencies: Some methods do not require any additional setup or dependencies.
  • Public Key: Some methods require the public API key to be set in the MoneyHashSDK before they can be used.
  • Payment Intent ID: Certain methods require a valid payment intent ID to process transactions.
  • Card Intent ID: The method createCardToken requires a valid card intent ID to create a token for the card data.

Class: CardForm

class CardForm {
  Future<bool> isValid();
  Future<VaultData?> collect();
  Future<IntentDetails?> pay(
    String intentId,
    VaultData cardData,
    bool saveCard,
    Map<String, String>? billingData,
    Map<String, String>? shippingData,
  );
  Future<IntentStateDetails?> createCardToken(
    String cardIntentId,
    VaultData cardData,
  );
}

Methods That Do Not Require Any Dependencies

These methods can be used without setting any additional configurations or dependencies.


1. isValid

Future<bool> isValid()
  • Purpose: Checks if the card data collected is valid.
  • Parameters: None.
  • Returns: A bool indicating whether the card data is valid (true if valid, false otherwise).
  • Example:
bool isCardValid = await cardForm.isValid();
print("Is card data valid? $isCardValid");

Methods That Require Public Key

These methods depend on the public API key being set in the MoneyHashSDK using the setPublicKey method.

To obtain your public API key, please refer to the MoneyHash Authentication Documentation.

Prerequisite

Before using these methods, ensure that the public key is set:

moneyHashSDK.setPublicKey("your_public_api_key");

2. collect

Future<VaultData?> collect()
  • Purpose: Collects card data from the user using the public API key.
  • Parameters: None.
  • Returns: VaultData containing the collected card information, or null if the operation was unsuccessful.
  • Throws: An MHException if failed to collect card data.
  • Example:
try {
  moneyHashSDK.setPublicKey("your_public_api_key"); // Ensure the public key is set

  // Initialize CardForm using CardFormBuilder
  CardFormBuilder cardFormBuilder = CardFormBuilder();
  // Configure cardFormBuilder as needed
  CardForm cardForm = cardFormBuilder.build();

  VaultData? cardData = await cardForm.collect();

  if (cardData != null) {
    print("Card data collected: $cardData");
  } else {
    print("Card data collection was unsuccessful");
  }
} catch (e) {
  print("Error collecting card data: $e");
}

Methods That Require Payment Intent ID

These methods require a valid payment intent ID to process transactions. Ensure you have a payment intent ID before using these methods.


3. pay

Future<IntentDetails?> pay(
  String intentId,
  VaultData cardData,
  bool saveCard,
  Map<String, String>? billingData,
  Map<String, String>? shippingData,
)
  • Purpose: Processes a payment using the provided card data and payment intent.
  • Parameters:
    • intentId: The unique identifier of the payment intent.
    • cardData: The card data collected (from the collect method).
    • saveCard: A boolean indicating whether to save the card for future transactions.
    • billingData: (Optional) A map containing billing information.
    • shippingData: (Optional) A map containing shipping information.
  • Returns: IntentDetails if the payment is processed successfully, null otherwise.
  • Throws: An MHException if the payment fails.
  • Example:
try {
  // Assuming cardData is obtained from the collect method
  var intentDetails = await cardForm.pay(
    "payment_intent_id",
    cardData!,
    true,  // Save the card
    {"address": "123 Main St", "city": "New York"},  // Billing data
    {"address": "456 Elm St", "city": "Boston"},     // Shipping data
  );
  print("Payment processed successfully: $intentDetails");
} catch (e) {
  print("Error processing payment: $e");
}

Methods That Require Card Intent ID

The following method requires a valid card intent ID to create a token for the card data.


4. createCardToken

Future<IntentStateDetails?> createCardToken(
  String cardIntentId,
  VaultData cardData,
)
  • Purpose: Creates a token for the card data provided, which can be used for subsequent transactions.
  • Parameters:
    • cardIntentId: The unique identifier of the card intent.
    • cardData: The card data collected.
  • Returns: IntentStateDetails containing the state of the intent after token creation.
  • Throws: An MHException if token creation fails.
  • Example:
try {
  var intentState = await cardForm.createCardToken("card_intent_id", cardData!);
  print("Card token created: $intentState");
} catch (e) {
  print("Error creating card token: $e");
}