getCaptcha method

dynamic getCaptcha({
  1. required double height,
  2. required double width,
  3. required String visiterId,
  4. required CyberCieraModel cieraModel,
})

Fetches a CAPTCHA challenge for user verification.

This function sends a POST request to the captchaForAndroid endpoint with device and user session details. It retrieves a CAPTCHA URL in HTML format if the request is successful.

Parameters:

  • height: The device screen height (used for UI adjustments).
  • width: The device screen width (used for UI adjustments).
  • visiterId: A unique identifier for the user session.
  • cieraModel: An instance of CyberCieraModel containing API-related configurations.

Function Workflow:

  1. Sets isOtherLoading to true to indicate a loading state.
  2. Clears previous error messages and CAPTCHA URLs.
  3. Fetches the device's IP and unique ID (udid).
  4. Detects the device type (Android or iOS) and extracts the device name.
  5. Constructs a request payload containing:
    • Master URL ID
    • Request URL
    • Browser Identity (udid)
    • Device IP and details
    • Screen dimensions
    • User session (VisiterId)
  6. Sends the request using ApiManager.post().
  7. Parses the response:
    • If successful (Message == "success"), updates captchaUrl with the received HTML.
    • Otherwise, displays an API error message.
  8. Catches and logs any errors encountered.
  9. Finally, sets isOtherLoading to false to stop the loading state.

Returns:

  • Updates the captchaUrl observable with the CAPTCHA HTML.
  • Displays an error toast in case of failure.

Implementation

getCaptcha({required double height, required double width, required String visiterId, required CyberCieraModel cieraModel}) async {
  isOtherLoading(true);
  error("");
  captchaUrl("");
  try {
    deviceName = "";
    deviceIp = await _ipAddress.getIp();
    udid = await FlutterUdid.udid;

    if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      deviceName = androidInfo.brand;
    } else {
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      deviceName = iosInfo.model;
    }
    Map<String, dynamic> map = {
      "MasterUrlId": cieraModel.masterUrlId, // "VYz433DfqQ5LhBcgaamnbw4Wy4K9CyQT",
      "RequestUrl": cieraModel.requestUrl, // "com.app.cyber_ceiara",
      "BrowserIdentity": udid,
      "DeviceIp": deviceIp,
      "DeviceType": Platform.isAndroid ? "Android" : "ios",
      "DeviceBrowser": 'Chrome',
      "DeviceName": deviceName,
      "DeviceHeight": height.round(),
      "DeviceWidth": width.round(),
      "VisiterId": visiterId,
    };
    ResponseAPI responseAPI = await ApiManager.post(methodName: ApiConstant.captchaForAndroid, params: map);
    Map<String, dynamic> valueMap = responseAPI.response;
    if (valueMap["Message"] == "success") {
      captchaUrl(valueMap["HtmlFormate"]);
    } else {
      toast("Api Error");
    }
  } catch (err) {
    error(err.toString());
    toast(error.value);
  } finally {
    isOtherLoading(false);
  }
}