public PepsiColaResponseInfo process(
      CupsTrmnl cupsTrmnl, CupsKeyManage cupsKeyManage, PepsiColaRequestInfo request)
      throws TransferFailedException {
    logger.info("[PepsiCola渠道IC卡脚本上送开始...]");
    // 1.组装报文
    IcScriptUploadTradeRequestEntity requestEntity = packUp(cupsTrmnl, cupsKeyManage, request);
    // iso mac计算
    byte[] iso_8583_main = requestEntity.pack();
    byte[] input = ArrayUtils.subarray(iso_8583_main, 6, iso_8583_main.length - 8);
    byte[] macKey = CodecUtils.hex2byte(cupsKeyManage.getTrmnlMacKey());
    byte[] mac;
    try {
      mac = hsmService.calculateMAC_CUPS(macKey, input).getBytes();
      input = ArrayUtils.subarray(iso_8583_main, 0, iso_8583_main.length - 8);
      iso_8583_main = ArrayUtils.addAll(input, mac);
    } catch (HSMException e) {
      String hsmRespCode = XPOSPClientUtils.getCode(e);
      if (!AppExCode.UNKNOWN.equals(hsmRespCode)) {
        hsmRespCode = Const.TP_BEGIN_HSM + hsmRespCode;
      }
      logger.error("[IC卡脚本上送失败,calculate mac form hsm error code:" + hsmRespCode + "]");
      throw new TransferFailedException(
          hsmRespCode, "[IC卡脚本上送失败, 加密机计算MAC失败, errorCode:" + hsmRespCode + "]");
    }

    // 组装报文报文头
    byte[] iso_8583_body =
        ArrayUtils.addAll(
            XPOSPClientUtils.getTpduHeader(XpospSysProperty.getPepsiColaTPDU()).pack(),
            iso_8583_main);
    String iso_8583_body_length =
        StringUtils.leftPad(Integer.toHexString(iso_8583_body.length), 4, "0");
    byte[] iso_8583_length = ISOUtils.hex2byte(iso_8583_body_length);
    byte[] iso_8583 = ArrayUtils.addAll(iso_8583_length, iso_8583_body);

    // 新增银联CUPS流水记录 {2,3,4,11,22,25,26,35,36,41,42,49,52,53,60,64}
    String payNo = transRefNoGenerator.generate();
    PayCups payCups = new PayCups();
    payCups.setPayNo(payNo);
    payCups.setCardNo(requestEntity.getCardNo());
    payCups.setProcessCode(requestEntity.getProcessCode());
    payCups.setTrmnlFlowNo(requestEntity.getTrmnlFlowNo());
    payCups.setTrmnlBatchNo(cupsTrmnl.getTrmnlBatchNo());
    payCups.setPosEntryMode(requestEntity.getPosEntryMode());
    payCups.setCardSeqNum(request.getCardSeqNum());
    payCups.setTrmnlNo(requestEntity.getTrmnlNo());
    payCups.setTrmnlMrchNo(requestEntity.getTrmnlMrchNo());
    payCups.setCurrency(requestEntity.getCurrency());
    payCups.setIcData(request.getEmvTransInfo());
    payCups.setSelfDefined060(requestEntity.getSelfDefined60());
    payCups.setOriginalInfo(requestEntity.getOriginalMessage());
    payCups.setTransType(TransType.ICSCRIPTUPLOAD.getOrdinal());
    payCups.setSubChannel(SubChannelType.PEPSICOLATRANSFERCHANNEL.getChannelName());
    payCupsService.addPayCups(payCups);
    // 打包,新增流水记录后更新终端流水信息
    cupsTrmnlService.updateCupsTrmnlFlowNo(cupsTrmnl);

    // 2.发送报文
    byte[] respData =
        TCPUtils.getInstance().sendReciveMsg(iso_8583, XpospSysProperty.getPepsiColaIP_PORT());

    // 3.解析报文
    PepsiColaResponseInfo pepsiColaResponseInfo = new PepsiColaResponseInfo();
    try {
      IcScriptUploadTradeResponseEntity responseEntity = unpack(respData);
      String isoRespCode = responseEntity.getRespCode();
      if (!Const.ISO_RESPCODE_OK.equals(isoRespCode)) {
        payCups.setRespCode(isoRespCode);
        isoRespCode = isoRespCode.replace("A", "10");
        payCupsService.updatePayCups(payCups);
        isoRespCode =
            Const.TP_BEGIN_ISO + StringUtils.leftPad("" + Integer.parseInt(isoRespCode), 3, "0");

        logger.error("[pay via cups failed, iso error code:" + isoRespCode + "]");
        throw new TransferFailedException(
            isoRespCode, "[pay via cups failed, so error code:" + isoRespCode + "]");
      }

      // 更新交易流水
      payCups.setTransDate(responseEntity.getTransDate());
      payCups.setTransTime(responseEntity.getTransTime());
      payCups.setSettlementDate(responseEntity.getSettlementDate());
      payCups.setRespCode(responseEntity.getRespCode());
      payCupsService.updatePayCups(payCups);

      pepsiColaResponseInfo.setTransCode(
          Const.TP_BEGIN_ISO + StringUtils.leftPad("" + Integer.parseInt(isoRespCode), 3, "0"));
      // 响应
    } catch (RuntimeException e) {
      throw e;
    }
    logger.info("[PepsiCola渠道IC卡脚本上送结束...]");
    return pepsiColaResponseInfo;
  }
  private IcScriptUploadTradeRequestEntity packUp(
      CupsTrmnl cupsTrmnl, CupsKeyManage cupsKeyManage, PepsiColaRequestInfo request)
      throws TransferFailedException {
    if (request.getIcScript() == null) {
      throw new TransferFailedException(
          AppExCode.ILLEGAL_PARAMS, "isScript upload failed! IcScript objet is null");
    }
    PayCups originalPayCups = payCupsService.findPayCups(request.getOriginalPayNo());
    // 脚本上送打包
    IcScriptUploadTradeRequestEntity entity = new IcScriptUploadTradeRequestEntity();
    entity.setCardNo(originalPayCups.getCardNo()); // 2 卡号
    entity.setProcessCode(originalPayCups.getProcessCode()); // 3 未选卡种
    String amount = null;
    if (originalPayCups.getTransAmt() != null
        || BigDecimal.ZERO.compareTo(originalPayCups.getTransAmt()) != 0) {
      amount = com.newland.base.util.StringUtils.amtToBCD(originalPayCups.getTransAmt());
    }
    entity.setTransAmt(amount); // 4 交易金额
    entity.setTrmnlFlowNo(cupsTrmnl.getTrmnlFlowNo()); // 11 终端流水号
    entity.setPosEntryMode(originalPayCups.getPosEntryMode()); // 22 卡输入方式
    if (StringUtils.isBlank(originalPayCups.getIcData())) { // 磁条卡
      entity.setSelfDefined60(
          Const.SD60_1_MSGTYPE_CONTROL
              + cupsTrmnl.getTrmnlBatchNo()); // 60 自定义域 22(交易类型码)+000001(批次号)+000(网络管理信息码)
    } else { // IC卡
      entity.setCardSeqNum(originalPayCups.getCardSeqNum()); // IC卡必须上送该域
      byte[] icData = getIcData(originalPayCups.getIcData(), request.getIcScript());
      entity.setIcData(icData); // 55 IC卡数据
      String selfDefined60 =
          Const.SD60_1_MSGTYPE_CONTROL
              + cupsTrmnl.getTrmnlBatchNo()
              + Const.SD60_3_IC_CRIPT_UPLOAD
              + Const.SD60_4_TERMINAL_READING_ABILITY_CONTACT_ICCARD
              + Const.SD60_5_IC_SERVICE_CODE_NORMAL;
      entity.setSelfDefined60(selfDefined60);
    }
    entity.setTrmnlRefferNo(originalPayCups.getTrmnlReferNo()); // 37
    entity.setTrmnlNo(originalPayCups.getTrmnlNo()); // 41 终端号
    entity.setTrmnlMrchNo(originalPayCups.getTrmnlMrchNo()); // 42 终端商户号
    entity.setCurrency(originalPayCups.getCurrency()); // 49 货币代码
    entity.setOriginalMessage(
        originalPayCups.getTrmnlBatchNo()
            + originalPayCups.getTrmnlFlowNo()
            + originalPayCups.getTransDate()); // 61 原批次号+原流水号
    entity.setMacCode(new byte[8]); // 64 mac

    return entity;
  }