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,
  6. required String accessToken,
})

Implementation

Future<Either<VerificationFailure, Map<String, dynamic>>> verifyDocument({
  required String? frontImage,
  required String? backImage,
  required String? sideImage,
  required String documentType,
  required String apiKey,
  required String accessToken,
}) async {
  try {
    final String timeStamp = getCurrentDateTimeInISO8601();
    var requestBody = await DocumentVerificationDto().toJson(
      frontImage,
      backImage,
      sideImage,
      documentType,
      timeStamp,
    );

    var tempBody = {
      'frontImage_base64': frontImage,
      'backImage_base64': backImage,
      'sideImage_base64': sideImage,
      'documentType': documentType,
      'timestamp': timeStamp
    };

    final signature = createJWT(tempBody, jwtSecret);

    headers.addAll({"x-api-key": apiKey});
    headers.addAll({"x-signature": signature});
    headers.addAll({"x-timestamp": timeStamp});
    headers.addAll({"Authorization": "Bearer $accessToken"});

    final http.Response response = await http.post(
      Uri.parse('${apiBaseUrl}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,
              accessToken: accessToken,
            )
          : {
              "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()));
  }
}