initializePayment method

  1. @override
Future<PaymentResponse> initializePayment(
  1. PaymentRequest request
)
override

Initialize a payment transaction

Implementation

@override
Future<PaymentResponse> initializePayment(PaymentRequest request) async {
  try {
    request.validate();
    final result = await methodChannel.invokeMethod<Map<dynamic, dynamic>>(
      'initializePayment',
      request.toJson(),
    );
    if (result == null) {
      throw PaystackError(message: 'No response from payment initialization');
    }
    final castedResult = result.cast<String, dynamic>();
    if (castedResult['status'] != 'success') {
      throw PaystackError.fromApiResponse(castedResult);
    }
    return PaymentResponse.fromApiResponse(castedResult);
  } on PlatformException catch (e) {
    throw PaystackError(
      message: e.message ?? 'Failed to initialize payment',
      code: e.code,
    );
  }
}