initializeTransaction method

Future<PaystackResponse> initializeTransaction({
  1. required String email,
  2. required int amount,
  3. required String callbackUrl,
  4. String? reference,
  5. List<String>? channels,
  6. String currency = 'NGN',
})

Initializes a Paystack transaction.

Implementation

Future<PaystackResponse> initializeTransaction({
  required String email,
  required int amount,
  required String callbackUrl,
  String? reference,
  List<String>? channels,
  String currency = 'NGN',
}) async {
  Logger.logInfo('Initializing transaction for $email with amount $amount');

  _validateAmount(amount);
  _validateEmail(email);
  _validateCallbackUrl(callbackUrl);

  final url = Uri.parse('$_baseUrl/transaction/initialize');
  final body = jsonEncode({
    'email': email,
    'amount': amount * 100,
    'callback_url': callbackUrl,
    'reference': reference ?? _generateReference(),
    'currency': currency,
    if (channels != null && channels.isNotEmpty) 'channels': channels,
  });

  return _retryRequest(() => _postRequest(url, body));
}