fetchBulkChargeBatch method

Future<PaystackBulkChargeBatch?> fetchBulkChargeBatch({
  1. required String batchCodeOrId,
  2. String? secretKey,
  3. Duration? timeout,
  4. bool? enableLogging,
})

Retrieves the current status and metrics of a bulk charge batch.

batchCodeOrId can be the batch code (e.g. BCH_1878848492023) or integer ID.

Implementation

Future<PaystackBulkChargeBatch?> fetchBulkChargeBatch({
  required String batchCodeOrId,
  String? secretKey,
  Duration? timeout,
  bool? enableLogging,
}) async {
  final resolvedKey = secretKey ?? _globalConfig?.secretKey;
  final resolvedTimeout =
      timeout ?? _globalConfig?.timeout ?? const Duration(seconds: 30);
  final resolvedLogging =
      enableLogging ?? _globalConfig?.enableLogging ?? false;

  assert(
    resolvedKey != null,
    'secretKey must be provided either directly or via PayWithPayStack.configure().',
  );

  void log(String msg) {
    if (resolvedLogging) debugPrint('[PayWithPaystack] $msg');
  }

  log('→ GET /bulkcharge/$batchCodeOrId');

  http.Response response;
  try {
    response = await http.get(
      Uri.parse('https://api.paystack.co/bulkcharge/$batchCodeOrId'),
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer $resolvedKey',
      },
    ).timeout(resolvedTimeout);
  } on Exception catch (e) {
    log('[ERROR] $e');
    return null;
  }

  log('← ${response.statusCode} ${response.body}');

  if (response.statusCode == 200) {
    final decoded = jsonDecode(response.body) as Map<String, dynamic>;
    if (decoded['status'] == true && decoded['data'] != null) {
      return PaystackBulkChargeBatch.fromJson(
        decoded['data'] as Map<String, dynamic>,
      );
    }
    return null;
  }

  throw PaystackException(
    message: 'Fetch bulk charge batch failed',
    statusCode: response.statusCode,
    responseBody: response.body,
  );
}