encryptCard function

String encryptCard({
  1. required String cardNumber,
  2. required String cardHolderName,
  3. required String cardSecurityCode,
  4. required String cardExpMonth,
  5. required String cardExpYear,
  6. required String publicKey,
})

Implementation

String encryptCard({
  required String cardNumber,
  required String cardHolderName,
  required String cardSecurityCode,
  required String cardExpMonth,
  required String cardExpYear,
  required String publicKey,
}) {
  final encrypter = getEncrypter(
      '''-----BEGIN PUBLIC KEY-----\n
      $publicKey
      \n-----END PUBLIC KEY-----''');

  if (encrypter == null) {
    throw Exception('Invalid public key');
  }

  final pan = formatToString(cardNumber);
  final holder = trunkString(cardHolderName, 30);
  final securityCode = formatToString(cardSecurityCode);
  final expMonth = formatExpMonth(cardExpMonth);
  final expYear = formatToString(cardExpYear);
  final timestamp = DateTime.now().millisecondsSinceEpoch;

  final data = '$pan;$securityCode;$expMonth;$expYear;$holder;$timestamp';
  final encryptedData = encrypter.encrypt(data);

  return encryptedData.base64;
}