onPaymentListener method

Future<PaymentResult?> onPaymentListener({
  1. required String paymentMethod,
  2. required PaymentIntentAttributes attributes,
  3. Future<bool> onRedirect(
    1. String url
    )?,
  4. String returnUrl = 'https://www.google.com/success',
})

Shortcut for using Card/PayMaya API. Must have PaymentMethodResponse to continue..

Implementation

Future<PaymentResult?> onPaymentListener({
  required String paymentMethod,
  required PaymentIntentAttributes attributes,
  Future<bool> Function(String url)? onRedirect,
  String returnUrl = 'https://www.google.com/success',
}) async {
  try {
    final intent = await create(attributes);
    final paymentIntentId =
        intent.attributes.clientKey.split('_client').first;

    if (intent.attributes.status == 'awaiting_payment_method' &&
        (intent.attributes.lastPaymentError?.isNotEmpty ?? false)) {
      // ignore: only_throw_errors
      throw intent.attributes.lastPaymentError ?? "Something went wrong";
    }
    final clientKey = intent.attributes.clientKey;
    // ignore: unused_local_variable
    final attachment = await attach(
      paymentIntentId,
      PaymentIntentAttach(
        paymentMethod: paymentMethod,
        clientKey: clientKey,
        returnUrl: returnUrl,
      ),
    );
    final result = await _paymentResult(
      paymentIntentId,
      clientKey,
      onRedirect: onRedirect,
    );
    return result;
  } catch (e) {
    rethrow;
  }
}