generatePayload function

EmvEncode generatePayload(
  1. EmvqrModel emv
)

Implementation

EmvEncode generatePayload(EmvqrModel emv) {
  String s = "";
  try {
    s += tlvToString(emv.payloadFormatIndicator);
    s += tlvToString(emv.pointOfInitiationMethod);
    List<String> keys = [];

    if (emv.merchantAccountInformation != null) {
      final merchantAccountInformation = emv.merchantAccountInformation ?? {};
      merchantAccountInformation.forEach((key, value) {
        keys.add(key);
      });
    }

    keys.sort();
    for (var key in keys) {
      final merchantAcInfo = emv.merchantAccountInformation?.entries
          .firstWhere((element) => element.key == key);

      String _globallyUnique =
          tlvToString(merchantAcInfo?.value.value?.globallyUniqueIdentifier);
      String _paymentNS = "";
      merchantAcInfo?.value.value?.paymentNetworkSpecific?.forEach((tlv) {
        _paymentNS += tlvToString(tlv);
      });
      s +=
          "${merchantAcInfo?.value.tag}${merchantAcInfo?.value.length}$_globallyUnique$_paymentNS";
    }
    s += tlvToString(emv.merchantCategoryCode);
    s += tlvToString(emv.transactionCurrency);
    s += tlvToString(emv.transactionAmount);
    s += tlvToString(emv.tipOrConvenienceIndicator);
    s += tlvToString(emv.valueOfConvenienceFeeFixed);
    s += tlvToString(emv.valueOfConvenienceFeePercentage);
    s += tlvToString(emv.countryCode);
    s += tlvToString(emv.merchantName);
    s += tlvToString(emv.merchantCity);
    s += tlvToString(emv.postalCode);

    // addition data
    s += _additionalTemplateToString(emv.additionalDataFieldTemplate);

    // merchant Info Language Template
    s += _merchantInfoLanguageTemplateToStrng(
        emv.merchantInformationLanguageTemplate);

    // rfu
    emv.rfuForEmvCo?.forEach((tlv) {
      s += tlvToString(tlv);
    });

    // unreserverved templates
    emv.unreservedTemplates?.forEach((key, value) {
      if (value.tag != null && value.length != null && value.value != null) {
        s += value.tag ?? "";
        s += value.length ?? "";
        s += tlvToString(value.value?.globallyUniqueIdentifier);
        value.value?.contextSpecificData?.forEach((element) {
          s += tlvToString(element);
        });
      }
    });

    final crcFormat = _formatCrc(s);

    if (crcFormat["err"] != null) {
      return EmvEncode(
          value: s,
          error: EmvError(
              message: "generate emvqr error",
              type: EmvErrorType.generateQrErr));
    }

    s += crcFormat["value"];
    return EmvEncode(value: s, error: null);
  } catch (e) {
    return EmvEncode(
        value: null,
        error: EmvError(
            message: "generate qr error ", type: EmvErrorType.generateQrErr));
  }
}