payWithRandomVouchers method

  1. @visibleForTesting
Future<String?> payWithRandomVouchers(
  1. InfoPayResponse infoPay,
  2. String? otc,
  3. String? password
)

Implementation

@visibleForTesting
Future<String?> payWithRandomVouchers(
    InfoPayResponse infoPay, String? otc, String? password) async {
  // final infoPay = await _pocketRepository.requestInfoPay(otc, password);
  List<Voucher> satisfyingVouchers;
  if (infoPay.simpleFilter != null) {
    satisfyingVouchers = _vouchers.where((v) {
      if (infoPay.simpleFilter!.aimCode != null &&
          !v.aim!.startsWith(infoPay.simpleFilter!.aimCode!)) {
        // Voucher does not match aim filter
        return false;
      }

      if (infoPay.simpleFilter!.bounds != null &&
          !infoPay.simpleFilter!.bounds!.contains(v.lat!, v.long!)) {
        // Voucher not contained in geographical bounds
        return false;
      }

      if (infoPay.simpleFilter!.maxAge != null &&
          DateTime.now().toUtc().difference(v.dateTime!) >
              Duration(days: infoPay.simpleFilter!.maxAge!)) {
        // Voucher too old
        return false;
      }

      return true;
    }).toList();
  } else {
    satisfyingVouchers = _vouchers;
  }

  if (infoPay.amount! > satisfyingVouchers.length) {
    throw InsufficientVouchers();
  }

  satisfyingVouchers.shuffle();
  final v = satisfyingVouchers.getRange(0, infoPay.amount!).toList();
  assert(v.length == infoPay.amount);
  final paymentResponse =
      await _pocketRepository.pay(otc, password, infoPay, v);

  v.forEach((element) => _vouchers.remove(element));
  return paymentResponse;
}