createMerchantQr function

String createMerchantQr({
  1. required MerchantAccountInfo merchantAccount,
  2. required String merchantName,
  3. required String city,
  4. String? mcc,
  5. num? amount,
  6. bool? isDynamic,
  7. String? billNumber,
  8. String? mobileNumber,
  9. String? reference,
  10. DateTime? dueDate,
  11. num? amountAfterDueDate,
})

Creates an EMVCo merchant-presented QR payload following SBP's Pakistan QR standard (currency 586, country PK, CRC in tag 63), including the Pakistan-specific due-date fields.

SBP leaves the layout of the Raast merchant account template to the scheme and has shared it only with banks, so pass the template sub-fields your bank gives you in merchantAccount.

  • merchantName: up to 25 characters. city: up to 15 characters.
  • mcc: ISO 18245 merchant category code. Defaults to 0000.
  • amount: amount in PKR. When set, the QR is dynamic unless isDynamic is false.
  • isDynamic: overrides the point of initiation (static 11 / dynamic 12); dynamic in the npm package.
  • billNumber (tag 62-01, up to 25 characters), mobileNumber (62-02, up to 15) and reference (62-05, up to 25).
  • dueDate (Pakistan-specific tag 62-50) and amountAfterDueDate (62-51), which must be used together.

Throws an ArgumentError (a RangeError in the npm package) if a field is missing, too long, or out of range.

Implementation

String createMerchantQr({
  required MerchantAccountInfo merchantAccount,
  required String merchantName,
  required String city,
  String? mcc,
  num? amount,
  bool? isDynamic,
  String? billNumber,
  String? mobileNumber,
  String? reference,
  DateTime? dueDate,
  num? amountAfterDueDate,
}) {
  final tag = merchantAccount.tag;
  if (!_merchantTag.hasMatch(tag)) {
    throw ArgumentError.value(
      tag,
      'merchantAccount.tag',
      'Must be between 26 and 51',
    );
  }
  final accountFields = merchantAccount.fields.entries.toList();
  if (accountFields.isEmpty) {
    throw ArgumentError.value(
      merchantAccount.fields,
      'merchantAccount.fields',
      'Must not be empty',
    );
  }
  final categoryCode = mcc ?? '0000';
  if (!_fourDigits.hasMatch(categoryCode)) {
    throw ArgumentError.value(categoryCode, 'mcc', 'Must be 4 digits');
  }
  if ((dueDate == null) != (amountAfterDueDate == null)) {
    throw ArgumentError('dueDate and amountAfterDueDate must be used together');
  }

  final dynamicQr = isDynamic ?? amount != null;
  for (final entry in accountFields) {
    if (!_twoDigits.hasMatch(entry.key)) {
      throw ArgumentError.value(
        entry.key,
        'merchantAccount.fields',
        'Invalid merchant account sub-field ID',
      );
    }
  }
  final account = stableSorted(
    accountFields,
    (a, b) => int.parse(a.key).compareTo(int.parse(b.key)),
  ).map((e) => encodeField(e.key, e.value)).join();

  final buffer = StringBuffer()
    ..write(encodeField('00', '01'))
    ..write(encodeField('01', dynamicQr ? '12' : '11'))
    ..write(encodeField(tag, account))
    ..write(encodeField('52', categoryCode))
    ..write(encodeField('53', '586'));
  if (amount != null) {
    buffer.write(encodeField('54', _formatAmount(amount, 13, 'amount')));
  }
  buffer
    ..write(encodeField('58', 'PK'))
    ..write(encodeField('59', _checkLength(merchantName, 25, 'merchantName')))
    ..write(encodeField('60', _checkLength(city, 15, 'city')));

  final additional = StringBuffer();
  if (billNumber != null && billNumber.isNotEmpty) {
    additional.write(
      encodeField('01', _checkLength(billNumber, 25, 'billNumber')),
    );
  }
  if (mobileNumber != null && mobileNumber.isNotEmpty) {
    additional.write(
      encodeField('02', _checkLength(mobileNumber, 15, 'mobileNumber')),
    );
  }
  if (reference != null && reference.isNotEmpty) {
    additional.write(
      encodeField('05', _checkLength(reference, 25, 'reference')),
    );
  }
  if (dueDate != null && amountAfterDueDate != null) {
    final local = dueDate.toLocal();
    final ddmmyyyy = _two(local.day) + _two(local.month) + _four(local.year);
    additional
      ..write(encodeField('50', ddmmyyyy))
      ..write(
        encodeField(
          '51',
          _formatAmount(amountAfterDueDate, 13, 'amountAfterDueDate'),
        ),
      );
  }
  if (additional.isNotEmpty) {
    buffer.write(encodeField('62', additional.toString()));
  }

  final result = withCrc(buffer.toString());
  if (result.runes.length > 512) {
    throw ArgumentError('QR payload exceeds 512 characters');
  }
  return result;
}