initializeMyPayment function

Future<String> initializeMyPayment(
  1. BuildContext context,
  2. String email,
  3. String phone,
  4. String amount,
  5. String currency,
  6. String firstName,
  7. String lastName,
  8. String transactionReference,
  9. String customTitle,
  10. String customDescription,
  11. String fallBackNamedRoute,
  12. String publicKey,
  13. dynamic onPaymentFinished(
    1. String,
    2. String,
    3. String
    )?,
)

Initializes a payment using the Chapa API.

This function sends a payment initialization request to the Chapa API, processes the response, and redirects the user to the Chapa payment page if successful.

Returns a String with the redirect URL, or an empty string if the payment initialization fails.

Implementation

/// Returns a `String` with the redirect URL, or an empty string if the payment initialization fails.
Future<String> initializeMyPayment(
  /// [context] - The current `BuildContext` for navigation.
  BuildContext context,

  /// [customers] - The user's email address.
  String email,

  /// [phone] - The customers's phone number.
  String phone,

  /// [amount] - The amount to be paid.
  String amount,

  /// [currency] - The currency code (e.g., "ETB").

  String currency,

  /// [firstName] - The user's first name.
  String firstName,

  /// [lastName] - The customers's last name.
  String lastName,

  /// [transactionReference] - A unique reference for the transaction.

  String transactionReference,

  /// [customTitle] - Custom title for the payment.
  String customTitle,

  /// [customDescription] - Custom description for the payment.
  String customDescription,

  /// [fallBackNamedRoute] - Named route to navigate to if the payment is canceled or fails.

  String fallBackNamedRoute,

  /// [publicKey] - The public key for authentication for Merchant with the Chapa API.

  String publicKey,

  /// [onPaymentFinished] - Optional callback to execute when the payment is completed.
  Function(String, String, String)? onPaymentFinished,
) async {
  try {
    final http.Response response = await http.post(
      Uri.parse(ChapaUrl.chapaPay),
      body: {
        'public_key': publicKey,
        'phone_number': phone,
        'amount': amount,
        'currency': currency.toUpperCase(),
        'first_name': firstName,
        'last_name': lastName,
        "email": email,
        'tx_ref': transactionReference,
        'customization[title]': customTitle,
        'customization[description]': customDescription
      },
    );
    if (response.statusCode == 400) {
      ApiErrorResponse apiErrorResponse = ApiErrorResponse.fromJson(
          json.decode(response.body), response.statusCode);
      showToast({
        'message': apiErrorResponse.message ??
            "Something went wrong. Please Contact Us.",
      });

      return '';
    } else if (response.statusCode == 302) {
      String? redirectUrl = response.headers['location'];
      if (redirectUrl != null) {
        Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => ChapaWebView(
                    url: redirectUrl,
                    fallBackNamedUrl: fallBackNamedRoute,
                    transactionReference: transactionReference,
                    amountPaid: amount,
                    onPaymentFinished: onPaymentFinished,
                  )),
        );
      }
      return redirectUrl.toString();
    } else {
      try {
        ApiErrorResponse apiErrorResponse = ApiErrorResponse.fromJson(
            json.decode(response.body), response.statusCode);
        showToast({
          'message': apiErrorResponse.message ??
              "Something went wrong. Please Contact Us.",
        });
        log(response.body);
        return '';
      } catch (e) {
        return '';
      }
    }
  } on SocketException catch (_) {
    showToast({
      'message':
          "There is no Internet Connection \n Please check your Internet Connection and Try it again."
    });
    return '';
  } catch (e) {
    log(e.toString());
    log("Exception here");
    return '';
  }
}