pay method

  1. @override
Future<PaymentResult> pay({
  1. required BuildContext context,
  2. required int amountSmallestUnit,
  3. required String email,
  4. String currency = 'NGN',
  5. String? reference,
})
override

Opens a Paystack WebView checkout and resolves to a PaymentResult.

Parameters:

  • context: Build context used to push the WebView route.
  • amountSmallestUnit: Amount in the smallest unit (e.g., kobo for NGN).
  • email: Customer email address.
  • currency: ISO code of the currency (defaults to 'NGN').
  • reference: Optional transaction reference. If omitted, one is generated via _genRef.

Returns:

  • PaymentSuccess with data including reference, currency, amount, and email, if the payment succeeds.
  • PaymentFailure if the checkout is closed, cancelled, or errors.

Implementation

@override
Future<PaymentResult> pay({
  required BuildContext context,
  required int amountSmallestUnit,
  required String email,
  String currency = 'NGN',
  String? reference,
}) async {
  final ref = reference ?? _genRef();
  try {
    final result = await Navigator.push<Map<String, dynamic>?>(
      context,
      MaterialPageRoute(
        builder: (_) => PaystackWebView(
          publicKey: publicKey,
          amountSmallestUnit: amountSmallestUnit,
          currency: currency,
          email: email,
          reference: ref,
        ),
      ),
    );

    if (result == null) {
      return PaymentFailure(name, 'Payment cancelled.');
    }
    final status = result['status'];
    if (status == 'success') {
      return PaymentSuccess(name, data: {
        'reference': result['reference'] ?? ref,
        'currency': currency,
        'amount_smallest_unit': amountSmallestUnit,
        'email': email,
      });
    }
    if (status == 'cancelled') {
      return PaymentFailure(name, 'Closed by user.');
    }
    return PaymentFailure(name, 'Payment error.', cause: result);
  } catch (e) {
    return PaymentFailure(name, 'Exception', cause: e);
  }
}