terminalDoInit method

  1. @override
Future<ApiResponse<DataInitRet>> terminalDoInit({
  1. required String terminalId,
  2. required String trc,
})
override

To make a transaction, you must create a terminal initial connection using the previously provided terminalId and Trc code.

Implementation

@override
Future<ApiResponse<DataInitRet>> terminalDoInit(
    {required String terminalId, required String trc}) async {
  Map<String, dynamic> reqBody = {
    "terminal_id": terminalId,
    "trc": trc,
  };
  Msg? msg;
  ApiResponse<DataInitRet> initRet = await DtbLinkNet.instance.request(
    method: Method.post,
    path: "/v1/init_trc",
    prefix: "do init",
    headers: {
      HttpHeaders.contentTypeHeader: "application/json; charset=utf-8"
    },
    body: json.encode(reqBody),
    fromJson: (dynamic ret) => DataInitRet.fromJson(ret),
  );
  debugPrint("apiResp: ${initRet.toRawJson()}");
  DataInitRet? initData = initRet.ret;
  if (initRet.statusCode == "ok" && initData != null) {
    final authPassEnc = DtbLinkCrypto.encrypt("${initData.authPass}");
    final authUserEnc = DtbLinkCrypto.encrypt("${initData.authUser}");
    final netEncKeyEnc = DtbLinkCrypto.encrypt("${initData.netEncKey}");
    initData.authPass = authPassEnc;
    initData.authUser = authUserEnc;
    initData.netEncKey = netEncKeyEnc;
    await _storage.saveInitData(initData);
    AuthResp? authResp = await DtbLinkNet.instance.newToken(
      username: "${initData.authUser}",
      password: "${initData.authUser}",
    );
    _log("authResp: [${authResp?.toJson()}]");
    String macAddress = await DtbLinkCrypto.generateMacAddress();
    Map<String, dynamic> bodyReqComplete = {
      "reader_id": "SPOS",
      "mac_addr": macAddress,
    };
    ApiResponse<EncApiRet> completeRet = await DtbLinkNet.instance.request(
      method: Method.post,
      path: "/v1/init_complete",
      prefix: "init complete",
      timeOut: const Duration(seconds: 20),
      body: json.encode(bodyReqComplete),
    );
    _log("completeRet: [${completeRet.toJson()}]");
    ApiResponse<DownloadKeyRet> syncResp = await syncKey();
    if (syncResp.statusCode == "ok") {
      DataInitRet? data = await _storage.initData;
      return ApiResponse(
        statusCode: "ok",
        ret: data,
      );
    } else {
      msg = syncResp.msg;
    }
  } else {
    msg = initRet.msg;
  }
  return ApiResponse(statusCode: "ng", msg: msg);
}