openFlutterWave static method

Future<void> openFlutterWave({
  1. required BuildContext context,
  2. required String publicKey,
  3. required String currency,
  4. required String amount,
  5. required String customerName,
  6. required String customerEmail,
  7. String? customerPhone,
  8. String redirectUrl = "https://www.google.cn/",
  9. String paymentOptions = "ussd, card, barter, pay attitude",
  10. String title = "Payment SDK",
  11. String paymentGatewayName = "Flutter wave",
  12. bool isTestMode = true,
  13. dynamic onShowToast()?,
  14. dynamic onPaymentSuccess()?,
  15. dynamic onPaymentFailure()?,
})

Implementation

static Future<void> openFlutterWave({
  required BuildContext context,
  required String publicKey,
  required String currency,
  required String amount,
  required String customerName,
  required String customerEmail,
  String? customerPhone,
  String redirectUrl = "https://www.google.cn/",
  String paymentOptions = "ussd, card, barter, pay attitude",
  String title = "Payment SDK",
  String paymentGatewayName = "Flutter wave",
  bool isTestMode = true,
  Function()? onShowToast,
  Function()? onPaymentSuccess,
  Function()? onPaymentFailure,
}) async {
  try {
    final Customer customer = Customer(
      name: customerName,
      email: customerEmail,
      phoneNumber: customerPhone ?? '',
    );

    final Flutterwave flutterWave = Flutterwave(
      publicKey: publicKey,
      currency: currency,
      redirectUrl: redirectUrl,
      txRef: "TX-${DateTime.now().millisecondsSinceEpoch}",
      amount: amount,
      customer: customer,
      paymentOptions: paymentOptions,
      customization: Customization(title: title),
      isTestMode: isTestMode,
    );

    log("Flutter wave Payment Starting...");

    final ChargeResponse response = await flutterWave.charge(context);

    if (response.status?.toLowerCase() == "successful") {
      final successResponse = IncodesPaymentResponse(
        "Payment Successful",
        true,
        response.transactionId ?? "",
      );

      if (onShowToast != null) {
        onShowToast();
      } else {
        IncodesListener().onSuccess(
          response: successResponse,
          paymentGatewayName: paymentGatewayName,
          isShowToast: true,
        );
      }

      onPaymentSuccess?.call();
      log("Flutter wave Payment Successful: ${response.toString()}");
    } else {
      final failureResponse = IncodesPaymentResponse(
        "Payment Failed",
        false,
        response.transactionId ?? "",
      );

      if (onShowToast != null) {
        onShowToast();
      } else {
        IncodesListener().onFailure(
          response: failureResponse,
          paymentGatewayName: paymentGatewayName,
          isShowToast: true,
        );
      }

      onPaymentFailure?.call();
      log("Flutter wave Payment Failed: ${response.toString()}");
    }
  } catch (e) {
    log("Flutter wave Exception => $e");

    final failureResponse = IncodesPaymentResponse(
      "Payment Error: $e",
      false,
      "",
    );

    if (onShowToast != null) {
      onShowToast();
    } else {
      IncodesListener().onFailure(
        response: failureResponse,
        paymentGatewayName: paymentGatewayName,
        isShowToast: true,
      );
    }

    onPaymentFailure?.call();
  }
}