CheckoutOrder.fromJson constructor

CheckoutOrder.fromJson(
  1. Map<Object?, Object?> value
)

Parses and validates an Order snapshot from the native bridge.

Implementation

factory CheckoutOrder.fromJson(Map<Object?, Object?> value) {
  _expectFields(
    value,
    const {
      'id',
      'status',
      'merchant',
      'amount',
      'expiresAt',
      'lineItems',
      'documents',
    },
  );
  final id = value['id'];
  final rawStatus = value['status'];
  final merchant = value['merchant'];
  final amount = value['amount'];
  final rawExpiresAt = value['expiresAt'];
  final lineItems = value['lineItems'];
  final documents = value['documents'];
  final status = rawStatus is String
      ? CheckoutOrderStatus.fromWireValue(rawStatus)
      : null;
  final expiresAt = rawExpiresAt == null
      ? null
      : rawExpiresAt is String
          ? DateTime.tryParse(rawExpiresAt)
          : null;
  if (id is! String ||
      id.isEmpty ||
      status == null ||
      merchant is! Map ||
      amount is! Map ||
      (rawExpiresAt != null && expiresAt == null) ||
      lineItems is! List ||
      documents is! Map ||
      lineItems.any((item) => item is! Map)) {
    throw const FormatException('Invalid Checkout Order');
  }
  return CheckoutOrder(
    id: id,
    status: status,
    merchant: CheckoutMerchant.fromJson(Map<Object?, Object?>.from(merchant)),
    amount: CheckoutMoney.fromJson(Map<Object?, Object?>.from(amount)),
    expiresAt: expiresAt,
    lineItems: lineItems
        .map(
          (item) => CheckoutLineItem.fromJson(
            Map<Object?, Object?>.from(item as Map),
          ),
        )
        .toList(growable: false),
    documents: CheckoutDocuments.fromJson(
      Map<Object?, Object?>.from(documents),
    ),
  );
}