run method

Future<String> run({
  1. required BuildContext context,
  2. required bool isDev,
  3. required InitiateIvorypayTransaction data,
  4. required dynamic onError(
    1. bool,
    2. Object
    )?,
  5. required dynamic onSuccess(
    1. VerifyTransactionRes
    )?,
  6. required dynamic onLoading(
    1. bool
    )?,
})

Implementation

Future<String> run({
  required BuildContext context,
  required bool isDev,
  required InitiateIvorypayTransaction data,
  required Function(bool, Object)? onError,
  required Function(VerifyTransactionRes)? onSuccess,
  required Function(bool)? onLoading,
}) async {
  final baseUrl = isDev ? Constants.devURL : Constants.prodURL;
  final paymentLinkBaseUrl =
      isDev ? Constants.paymentLinkDevURL : Constants.paymentLinkProdURL;

  onLoading!(true);
  try {
    final response = await http.post(
      Uri.parse(baseUrl),
      body: json.encode({
        "baseFiat": data.baseFiat,
        "amount": data.amount,
        "crypto": data.crypto,
        "email": data.email,
        "metadata": null
      }),
      headers: {
        'Content-Type': ' application/json',
        'Authorization': data.authorization ?? ''
      },
    );

    if (response.statusCode == 201) {
      final data = TransactionResponseDto.fromJson(
        jsonDecode(response.body),
      );

      /// The below code is creating a string variable called [link[ that
      /// concatenates a base payment link URL with a reference ID obtained from
      /// [data.data[. The [?.[ operator is used to safely access the [reference[
      /// property of [data.data[ without causing a null reference exception if
      /// [data.data[ is null.
      final link = "$paymentLinkBaseUrl/${data.data?.reference}";

      await launchUrl(Uri.parse(link), mode: LaunchMode.inAppWebView);
      ref = data.data?.reference ?? '';
      return link;
    } else {
      onError!(true, response.body);

      return response.body.toString();
    }
  } catch (e) {
    onError!(true, e);
    return e.toString();
  } finally {
    onLoading(false);
  }
}