build static method

GatewayTransactionRequest build({
  1. required Map<String, dynamic> encryptedPayloadMap,
  2. required int amountMinorUnits,
  3. required String currency,
  4. required String idempotencyKey,
  5. Map<String, dynamic>? transactionMetadata,
})

Build a request from raw input. The encrypted-payload bridge map (encryptedPayload / ksn / entryMode / deviceSerial / batteryLevel / reader) is what the native transaction-runner emits today via TransactionPayloadMapper.buildEncryptedPayload.

Implementation

static GatewayTransactionRequest build({
  required Map<String, dynamic> encryptedPayloadMap,
  required int amountMinorUnits,
  required String currency,
  required String idempotencyKey,
  Map<String, dynamic>? transactionMetadata,
}) {
  final encryptedPayload = encryptedPayloadMap['encryptedPayload'];
  final ksn = encryptedPayloadMap['ksn'];
  final entryMode = encryptedPayloadMap['entryMode'];
  if (encryptedPayload is! String || encryptedPayload.isEmpty) {
    throw ArgumentError.value(
      encryptedPayload,
      'encryptedPayload',
      'must be a non-empty String',
    );
  }
  if (ksn is! String || ksn.isEmpty) {
    throw ArgumentError.value(ksn, 'ksn', 'must be a non-empty String');
  }
  if (entryMode is! String || entryMode.isEmpty) {
    throw ArgumentError.value(
      entryMode,
      'entryMode',
      'must be a non-empty String',
    );
  }
  Map<String, dynamic>? readerSubset;
  final rawReader = encryptedPayloadMap['reader'];
  if (rawReader is Map) {
    final out = <String, dynamic>{};
    for (final key in readerSafeKeys) {
      final v = rawReader[key];
      if (v != null) out[key] = v;
    }
    // Pull device serial out of the encrypted payload's
    // top-level slot if the reader sub-map did not carry
    // it.
    if (out['serialNumber'] == null) {
      final serial = encryptedPayloadMap['deviceSerial'];
      if (serial is String && serial.isNotEmpty) {
        out['serialNumber'] = serial;
      }
    }
    if (out['batteryPercent'] == null) {
      final battery = encryptedPayloadMap['batteryLevel'];
      if (battery is int) out['batteryPercent'] = battery;
    }
    readerSubset = out.isEmpty ? null : out;
  }
  return GatewayTransactionRequest(
    encryptedPayload: encryptedPayload,
    ksn: ksn,
    entryMode: entryMode,
    amountMinorUnits: amountMinorUnits,
    currency: currency,
    idempotencyKey: idempotencyKey,
    reader: readerSubset,
    transactionMetadata: transactionMetadata,
  );
}