charge method

Starts a transaction by calling the Standard service

Implementation

Future<ChargeResponse> charge() async {
  final request = StandardRequest(
      txRef: txRef,
      amount: amount,
      customer: customer,
      paymentOptions: paymentOptions,
      customization: customization,
      isTestMode: isTestMode,
      redirectUrl: redirectUrl,
      publicKey: publicKey,
      currency: currency,
      paymentPlanId: paymentPlanId,
      subAccounts: subAccounts,
      meta: meta);

  StandardResponse? standardResponse;

  try {
    standardResponse = await request.execute(Client());
    if ("error" == standardResponse.status) {
      FlutterwaveViewUtils.showToast(context, standardResponse.message!);
      return ChargeResponse(
          txRef: request.txRef, status: "error", success: false);
    }

    if (standardResponse.data?.link == null ||
        standardResponse.data?.link?.isEmpty == true) {
      FlutterwaveViewUtils.showToast(
          context,
          "Unable to process this transaction. " +
              "Please check that you generated a new tx_ref");
      return ChargeResponse(
          txRef: request.txRef, status: "error", success: false);
    }
  } catch (error) {
    FlutterwaveViewUtils.showToast(context, error.toString());
    return ChargeResponse(
        txRef: request.txRef, status: "error", success: false);
  }

  final response = await Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => StandardPaymentWidget(
          webUrl: standardResponse!.data!.link!,
        ),
      ),
    );

  if (response != null) return response!;
  return ChargeResponse(txRef: request.txRef, status: "cancelled", success: false);
  }