receipt static method

EcrReceipt receipt(
  1. Object? payload, {
  2. required String operationId,
})

Reads the answer to a receipt request.

Implementation

static EcrReceipt receipt(Object? payload, {required String operationId}) {
  final Map<Object?, Object?>? map = _asMap(payload);
  if (map == null) {
    return EcrReceiptFailed(
      merchantReference: '',
      failure: EcrMalformed(_notAMap(payload, 'a receipt')),
    );
  }

  final String reference = _string(map, EcrResultKeys.merchantReference);

  switch (_string(map, EcrResultKeys.outcome)) {
    case EcrOutcomes.ready:
      final String url = _string(map, EcrResultKeys.url);
      if (url.isEmpty) {
        // A receipt with no URL is not a receipt, whatever the host called
        // it. The native SDKs apply the same rule.
        return EcrReceiptUnavailable(
          merchantReference: reference,
          reason: _string(map, EcrResultKeys.responseMessage),
          raw: _string(map, EcrResultKeys.raw),
        );
      }
      return EcrReceiptReady(
        merchantReference: reference,
        url: url,
        raw: _string(map, EcrResultKeys.raw),
      );
    case EcrOutcomes.unavailable:
      return EcrReceiptUnavailable(
        merchantReference: reference,
        reason: _string(map, EcrResultKeys.responseMessage),
        raw: _string(map, EcrResultKeys.raw),
      );
    case EcrOutcomes.failed:
      return EcrReceiptFailed(
        merchantReference: reference,
        failure: failure(map[EcrResultKeys.failure]),
      );
    case final String unknown:
      return EcrReceiptFailed(
        merchantReference: reference,
        failure: EcrMalformed(_unknownOutcome(unknown, 'a receipt')),
      );
  }
}