@Override
 public void syncPayCancelLog(PayCancel payCancel) {
   PayCups payCups = payCupsService.findPayCups(payCancel.getPaycancelNo());
   if (payCups == null) {
     // throw new TransferFailedException(AppExCode.TRANS_NOT_EXIST, "sysncPayLog failed,not find
     // payCups");
     payCancel.setTransStatus(Const.TransStatus.FAILED); // 银联直连表无交易流水记录,未发交易,撤销失败
     return;
   }
   // 如果iso中心已经返回,但app与xposp已经断开
   String isoRespCode = payCups.getRespCode();
   if (StringUtils.isNotBlank(isoRespCode)) {
     // modify zengyj 20140716 服务端到银联已经响应成功,但客户端因超时交易未知
     if (Const.ISO_RESPCODE_OK.equals(isoRespCode)) {
       payCancel.setTransStatus(Const.TransStatus.SUCCESS);
     } else {
       payCancel.setTransStatus(Const.TransStatus.FAILED);
       isoRespCode =
           Const.TP_BEGIN_ISO + StringUtils.leftPad("" + Integer.parseInt(isoRespCode), 3, "0");
       payCancel.setErrCode(isoRespCode);
     }
     return;
   }
   CupsTrmnl cupsTrmnl =
       cupsTrmnlService.findByTrmnlNoAndMrchNo(payCups.getTrmnlNo(), payCups.getTrmnlMrchNo());
   if (cupsTrmnl == null) {
     throw new TransferFailedException(
         AppExCode.TERMINAL_NOT_EXIST, "goodee sysncPayLog failed,not find CupsTrmnl");
   }
   reversalProcess.process(cupsTrmnl, payCups, getAddress(payCancel.getAppAccessId()));
   payCancel.setTransStatus(TransStatus.FAILED);
 }
  @Override
  public void cancel(Device device, PayCancel cancel, CancelRequest request)
      throws TransferFailedException {
    logger.info("entryMode:" + request.getEntryMode());
    logger.info("Pinblock is not null:" + StringUtils.isNotBlank(request.getPinblock()));
    logger.info("EmvTransInfo is not null:" + StringUtils.isNotBlank(request.getEmvTransInfo()));

    // 校验数据
    if (StringUtils.isBlank(request.getTrack2Data())
        && StringUtils.isBlank(request.getTrackData())) {
      throw new TransferFailedException(
          AppExCode.ILLEGAL_PARAMS, "goodee cancel failed,track2data or trackData null");
    }
    // 卡输入方式
    if (StringUtils.isNotBlank(request.getEntryMode())) {
      boolean isOk =
          isEntryModeOk(request.getEntryMode(), request.getPinblock(), request.getEmvTransInfo());
      if (!isOk) {
        throw new TransferFailedException(
            AppExCode.ILLEGAL_PARAMS,
            "goodee cancel failed,entryMode is "
                + request.getEntryMode()
                + " but pinBlock is not null:"
                + StringUtils.isNotBlank(request.getPinblock())
                + " ,emvTransInfo is not null:"
                + StringUtils.isNotBlank(request.getEmvTransInfo()));
      }
    }
    // modify by zengyj 20140331 cancel by orginal trmnl not by device trmnl;
    // 获取当前设备的终端信息
    // CupsTrmnl cupsTrmnl = cupsTrmnlService.findById(device.getBindTransId());
    PayCups originalPayCups = payCupsService.findPayCups(request.getLastTransRefNo());
    if (originalPayCups == null) {
      throw new TransferFailedException(
          AppExCode.TRANS_NOT_EXIST, "goodee cancel failed,not find originalPayCups");
    }
    CupsTrmnl cupsTrmnl =
        cupsTrmnlService.findByTrmnlNoAndMrchNo(
            originalPayCups.getTrmnlNo(), originalPayCups.getTrmnlMrchNo());
    if (cupsTrmnl == null) {
      throw new TransferFailedException(
          AppExCode.TERMINAL_NOT_EXIST, "goodee cancel failed,not find CupsTrmnl");
    }
    // 获取密钥信息
    DeviceKeymanage deviceKeymanage =
        deviceKeymanageService.findDeviceKeymanageByDevId(device.getDevId());
    if (deviceKeymanage == null) {
      throw new TransferFailedException(
          AppExCode.DEVICE_KEY_NOT_EXIST, "goodee cancel failed,not find deviceKeymanage info");
    }
    PaySignUp paySignUp =
        paySignUpService.findPaySignUpByMrchNoAndTrmnlNo(
            cupsTrmnl.getMrchNo(), cupsTrmnl.getTrmnlNo());
    if (paySignUp == null) {
      throw new TransferFailedException(
          AppExCode.TERMINAL_KEY_NOT_EXIST, "goodee pay failed,not find cups signUp info");
    }
    // 密钥体系转换
    CupsKeyManage cupsKeyManage = new CupsKeyManage();
    cupsKeyManage.setDevPinKey(deviceKeymanage.getPinkey());
    cupsKeyManage.setTrmnlPinKey(paySignUp.getTrmnlPinKey());
    cupsKeyManage.setTrmnlMacKey(paySignUp.getTrmnlMacKey());
    // CUPS请求信息
    CupsRequestInfo requestInfo = new CupsRequestInfo();
    requestInfo.setPayNo(cancel.getPaycancelNo());
    requestInfo.setAmount(cancel.getPayAmt());
    requestInfo.setOriginalPayNo(request.getLastTransRefNo());
    requestInfo.setPinblock(request.getPinblock());
    requestInfo.setEmvTransInfo(request.getEmvTransInfo());
    requestInfo.setCardSeqNum(request.getCardSeqNum());
    requestInfo.setExtFld01(request.getStoreNo());
    requestInfo.setOrderNo(request.getOrderNo());
    try {
      TrackContext trackContext =
          getTrackData(deviceKeymanage.getDatakey(), request.getTrackData());
      requestInfo.setCardNo(trackContext.getTrack2Data().split("=")[0]);
      requestInfo.setTrack2Data(trackContext.getTrack2Data());
      requestInfo.setTrack3Data(trackContext.getTrack3Data());
    } catch (HSMException e) {
      String hsmRespCode = XPOSPClientUtils.getCode(e);
      if (!AppExCode.UNKNOWN.equals(hsmRespCode)) {
        hsmRespCode = Const.TP_BEGIN_HSM + hsmRespCode;
      }
      throw new TransferFailedException(
          hsmRespCode,
          "goodee cancel failed, when descrypt track data form hsm error code["
              + hsmRespCode
              + "]");
    }
    try {
      cancelProcess.process(
          cupsTrmnl, cupsKeyManage, requestInfo, getAddress(cancel.getAppAccessId()));
    } catch (TransferFailedException e) {
      String errorCode = XPOSPClientUtils.getCode(e);
      processErrorCode(errorCode, cupsTrmnl);
      throw e;
    }
    // 如果没有抛异常,则交易成功
    cancel.setTransStatus(Const.TransStatus.SUCCESS);
  }