registerTerminal method

  1. @override
Future<bool> registerTerminal(
  1. String provider, {
  2. required Terminal terminal,
})
override

Implementation

@override
Future<bool> registerTerminal(
  String provider, {
  required Terminal terminal,
}) async {
  // Adding payment gateway specific data to terminal info
  var existingData = terminal.app.data;
  var data = {};

  String providerKey = 'provider_sdk_versions';

  if (core.isRegistered<SoftPOSPaymentGateway>()) {
    SoftPOSPaymentGateway softPOSGateway = core.get<SoftPOSPaymentGateway>();
    try {
      data['softPosVersion'] = softPOSGateway.providerSDKVersion;
    } catch (e) {
      logger.error(
        this,
        'Error occurred whilst getting SDK version from payment gateway: $e',
        error: e,
        stackTrace: StackTrace.current,
      );
    }
  }

  final modifiableData = Map<String, String>.from(existingData);
  if (data.isNotEmpty) {
    modifiableData[providerKey] = jsonEncode(data);
  }
  terminal.app.data = modifiableData;
  // End of adding payment gateway specific data to terminal info
  final manager = this as TerminalManager;
  if (!isReady) {
    logger.warning(
      this,
      'Terminal Manager Service not initialised, call initialise first',
    );

    return false;
  }

  if (!authService.isAuthenticated) {
    logger.warning(
      this,
      'user not authenticated, device registration cancelled for device: ${terminal.name}',
    );

    return false;
  }

  debugPrint('### SignalR user ${authService.user?.uid}');

  final lfHttpClient = LittleFishHttpClient();
  final payload = terminalToJson(terminal);
  // ignore: prefer_typing_uninitialized_variables
  var result;
  try {
    result = await lfHttpClient
        .post(
      url: manager._endpoint,
      requestData: payload,
      user: authService.user,
      // ignore: body_might_complete_normally_catch_error
    )
        .catchError((error) {
      logger.error(
        this,
        'Error occurred whilst making HTTP POST request: $error',
        error: error,
        stackTrace: StackTrace.current,
      );
      return null;
    });
  } catch (e) {
    logger.error(
      this,
      'Error occurred whilst registering terminal: $e',
      error: e,
      stackTrace: StackTrace.current,
    );
  }
  if (result!.statusCode == HttpStatus.created ||
      result.statusCode == HttpStatus.ok) {
    //NB! this is the server side copy, very important to have....
    try {
      if (result.data != null &&
          ((result.data is Map) && (result.data as Map).isNotEmpty)) {
        final terminalResult = terminalFromJson(result.data);
        setThisTerminal(terminalResult);
      }
    } on Exception catch (e) {
      logger.error(
        this,
        'Error occurred whilst parsing terminal from server. ',
        error: e,
        stackTrace: StackTrace.current,
      );
      return true;
    }

    logger.info(
      this,
      'Terminal registered successfully: ${terminal.name}',
    );
  } else {
    logger.warning(
      this,
      'Terminal registration failed: ${terminal.name}, ${result.data}',
    );

    return false;
  }
  return true;
}