verifyDocument function

Future<Either<VerificationFailure, Map<String, dynamic>>> verifyDocument({
  1. required String frontImage,
  2. required String backImage,
  3. required String sideImage,
  4. required String documentType,
  5. required String apiKey,
})

Implementation

Future<Either<VerificationFailure, Map<String, dynamic>>> verifyDocument({
  required String frontImage,
  required String backImage,
  required String sideImage,
  required String documentType,
  required String apiKey,
}) async {
  try {
    var requestBody = DocumentVerificationDto().toJson(
      frontImage,
      backImage,
      sideImage,
      documentType,
    );
    headers.addAll({"x-api-key": apiKey});

    final http.Response response = await http.post(
      Uri.parse('${apiBaseUrl}api/document-authentication'),
      headers: headers,
      body: jsonEncode(requestBody),
    );

    var result = jsonDecode(response.body);

    if (kDebugMode) {
      print(result);
    }

    if (result['success'] == true &&
        result.containsKey('documentIdentificationId')) {
      Map<String, dynamic> idData = result['result'];
      idData['documentIdentificationId'] = result['documentIdentificationId'];

      if (idData.containsKey("sex")) {
        if (idData["sex"] == "F") {
          idData["sex"] = Gender.female.value;
        } else {
          idData["sex"] = Gender.male.value;
        }
      } else {
        idData["sex"] = Gender.male.value;
      }

      var formattedAddress = idData.containsKey("address1")
          ? formatAddress(idData["address1"])
          : {
              "address": "",
              "building": "",
            };

      var getAddress = idData.containsKey("address1")
          ? await getPostalcode(
              address: formattedAddress["address"], apiKey: apiKey)
          : {
              "address1Ja": "",
              "address2Ja": "",
              "postalcode": "",
            };

      idData["address1Ja"] = getAddress["address1Ja"];
      idData["address2Ja"] = getAddress["address2Ja"];
      idData["address3Ja"] = formattedAddress["building"];
      idData["postalcode"] = getAddress["postalcode"];

      if (kDebugMode) {
        print(idData);
      }

      return right(idData);
    }

    if (result.containsKey("missMatch")) {
      return left(VerificationFailure.serverError(
          "Please provide a valid $documentType."));
    } else {
      return left(const VerificationFailure.documentNotRecongnized());
    }
  } catch (e) {
    if (kDebugMode) {
      print(e);
    }

    return left(VerificationFailure.serverError(e.toString()));
  }
}