Save Card For Future Use

Don't forget to include "card_token_type": "UNIVERSAL" when creating the card intent.


To enable the functionality of saving a card for future use in your Flutter application, follow these steps using the MoneyHashSDK class and the CardFormBuilder to correctly build the card form.

Initialize MoneyHash SDK

First, initialize the MoneyHashSDK and set the public API key:

final moneyHash = MoneyHashSDKBuilder.build();

// Set the public API key
moneyHash.setPublicKey("<account_public_api_key>");

Build and Collect Card Data

You need to build the CardForm using the CardFormBuilder. After building the form, you can collect the card data required to create a card token for the customer.

final cardForm = CardFormBuilder()
  .setCardNumberField((CardFieldState? state) {
    // Handle card number field changes
  })
  .setCVVField((CardFieldState? state) {
    // Handle CVV field changes
  })
  .setExpiryMonthField((CardFieldState? state) {
    // Handle expiry month field changes
  })
  .setExpiryYearField((CardFieldState? state) {
    // Handle expiry year field changes
  })
  .setCardHolderNameField((CardFieldState? state) {
    // Handle cardholder name field changes
  })
  .build();

// Collect card data
final VaultData? cardData = await cardForm.collect();

Make sure to render the form fields in your UI and collect customer card information.

Create Card Token

Once the card data is collected, you can create a card token for future use by calling the createCardToken() method.

try {
  final String cardIntentId = "<card_intent_id>"; // Your card intent ID

  // Create card token
  final IntentStateDetails? intentStateDetails = await cardForm.createCardToken(
    cardIntentId,
    cardData!, // Collected card data
  );

  if (intentStateDetails is CardIntentSuccessful) {
    print("Card token created successfully: $intentStateDetails");
  } else if (intentStateDetails is UrlToRender) {
    print("Card token creation still pending for extra action, Open the url that returned to complete the flow.");
  } else {
    print("Card token creation Failed.");
  }
} on MHException catch (e) {
  print("Error creating card token: ${e.message}");
}

📘

You can run any business logic needed between collecting card data and tokenizing it.


This guide demonstrates how to save a card for future use in a Flutter app using the MoneyHash SDK by initializing the SDK, building the CardForm, collecting card data, and creating a card token.