sendPIN static method

Future<APIResponse> sendPIN({
  1. required PaymentInfo paymentInfo,
  2. required String reference,
  3. required String pin,
})

Implementation

static Future<APIResponse> sendPIN({
  required PaymentInfo paymentInfo,
  required String reference,
  required String pin,
}) async {
  APIResponse apiResponse;

  //Preparing request payload
  final Map<String, dynamic> formDataMap = {
    "pin": pin,
    "reference": reference,
  };

  try {
    var response = await dio.post(
      APIs.submitPINUrl,
      data: formDataMap,
      options: Options(
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer ${paymentInfo.secretKey}',
        },
      ),
    );

    if (response.statusCode == 200) {
      //format the response data for system to continue operating
      apiResponse = APIResponse.fromObject(response.data);
    } else {
      var errorMessage = "Request Failed. Please try again later";

      try {
        //alert the user with the message from the server
        if (response.data["data"] != null) {
          errorMessage = response.data["data"]["message"];
        } else {
          errorMessage = response.data["message"];
        }
      } catch (error) {
        if (kDebugMode) {
          print("Error Data Getting Failed:: $error");
        }
      }

      throw errorMessage;
    }
  } catch (error) {
    DioError dioError = error as DioError;
    var errorMessage = "Request Failed. Please try again later";

    try {
      //alert the user with the message from the server
      if (dioError.response?.data["data"] != null) {
        errorMessage = dioError.response?.data["data"]["message"];
      } else {
        errorMessage = dioError.response?.data["message"];
      }
    } catch (error) {
      if (kDebugMode) {
        print("Error Data Getting Failed:: $error");
      }
    }

    throw errorMessage;
  }

  return apiResponse;
}