doSale method

  1. @override
Future<Map<String, dynamic>> doSale({
  1. required String track2,
  2. required String emv,
  3. required String traceNo,
  4. required double amount,
})
override

transaction do sale

Implementation

@override
Future<Map<String, dynamic>> doSale({
  required String track2,
  required String emv,
  required String traceNo,
  required double amount,
}) async {
  DataInitRet? initData = await _storage.initData;
  DownloadKeyRet? keyRet = await _storage.downloadKey;
  String tdk = "${keyRet?.tdk}";
  String tpk = "${keyRet?.tpk}";
  String merchantName = initData?.merchantName ?? "";
  String terminalId = initData?.terminalId ?? "";
  String merchantId = initData?.merchantId ?? "";
  String netEncKey = DtbLinkCrypto.decrypt("${initData?.netEncKey}");
  String amountStr = amount.toStringAsFixed(2);
  debugPrint(
      "track2: $track2, emv: $emv, traceNo: $traceNo, amount: $amountStr\n"
      "tdk: $tdk, tpk: $tpk, merchantName: $merchantName, merchantId: $merchantId\n");
  final saleBuildResp = await _doSaleBuild(
    amount: amountStr,
    traceNo: traceNo,
    track2: track2,
    emv: emv,
    tdk: tdk,
    tpk: tpk,
    terminalId: terminalId,
    merchantId: merchantId,
    merchantName: merchantName,
    netEncKey: netEncKey,
  );
  if (saleBuildResp["status_code"] == "ng") {
    return saleBuildResp;
  }
  String dbRefNo = saleBuildResp["db_ref_no"];
  Map<String, dynamic> bodySale = {
    "db_ref_no": dbRefNo,
    "service_data": saleBuildResp["service_data"],
    "terminal_id": saleBuildResp["terminal_id"],
  };
  String entryMode = '${saleBuildResp["entry_mode"]}';
  String entryModeText = '${saleBuildResp["entry_mode_text"]}';
  String reqID = "";
  ApiResponse<EncApiRet> apiResp = await DtbLinkNet.instance.request(
    method: Method.post,
    path: "/v1/sale",
    prefix: "sale tran",
    body: json.encode(bodySale),
    onRespHeader: (Map<String, String> headers) {
      debugPrint('resp headers: $headers');
      reqID = headers["request-id"] ?? reqID;
    },
    fromJson: (dynamic ret) => EncApiRet.fromJson(ret),
    timeOut: const Duration(seconds: 20),
  );
  if (apiResp.statusCode == "ng") {
    return apiResp.toJson();
  }
  EncApiRet? encApiRet = apiResp.ret;
  debugPrint("saleResp: [${apiResp.ret?.toJson()}]");
  final parseSaleResp = await _saleParse(
    isoData: "${encApiRet?.isoData}",
    track2: track2,
    tdk: tdk,
    netEncKey: netEncKey,
  );
  debugPrint("parseSaleResp: $parseSaleResp");
  parseSaleResp.putIfAbsent("request_id", () => reqID);
  parseSaleResp.putIfAbsent("db_ref_no", () => saleBuildResp["db_ref_no"]);
  if (parseSaleResp["resp_code"] == "000") {
    String dayTime = tranDateFormatter.format(DateTime.now());
    final regParse = await _registerTran(
      operation: "SALE",
      reqId: reqID,
      amountStr: amountStr,
      traceNo: traceNo,
      dbRefNo: dbRefNo,
      entryMode: entryMode,
      entryModeText: entryModeText,
      terminalId: terminalId,
      merchantId: merchantId,
      merchantName: merchantName,
      netEncKey: netEncKey,
      dayTime: dayTime,
      parseMap: parseSaleResp,
    );
    _log('regParse: $regParse');
    await _apiComplete(
      traceNo: traceNo,
      dbRefNo: dbRefNo,
      terminalId: terminalId,
      netEncKey: netEncKey,
    );
    return {"status_code": "ok", "ret": parseSaleResp};
  }
  return {"status_code": "ng", "ret": parseSaleResp};
}