From 95654565423dcbe2cebeb8ee4b24cb599c239229 Mon Sep 17 00:00:00 2001 From: "Jonguk. Lim" Date: Fri, 11 Oct 2024 15:57:12 +0900 Subject: [PATCH] refactor: Nice CI Accept, Send code refactoring --- .../nice/service/NiceCiAcceptService.java | 142 ++++++++------- .../nice/service/NiceCiSendBulkService.java | 166 ++++++++++-------- 2 files changed, 167 insertions(+), 141 deletions(-) diff --git a/src/main/java/cokr/xit/ens/modules/nice/service/NiceCiAcceptService.java b/src/main/java/cokr/xit/ens/modules/nice/service/NiceCiAcceptService.java index b7fea2b..b0fd3f3 100644 --- a/src/main/java/cokr/xit/ens/modules/nice/service/NiceCiAcceptService.java +++ b/src/main/java/cokr/xit/ens/modules/nice/service/NiceCiAcceptService.java @@ -1,5 +1,6 @@ package cokr.xit.ens.modules.nice.service; +import java.time.*; import java.util.*; import org.apache.commons.lang3.*; @@ -104,36 +105,24 @@ public class NiceCiAcceptService { d.setInputDataXits(niceCiMapper.selectInputDataXits(niceCiParam)); }); - final String prefixBillUid = PostSeCd.intgrnNoti.getCode() + "-" + IdGenerator.getCurrentTimeSec(); - final KkoPayUrlRespData kkoPayUrlRespData = KkoPayUrlRespData.builder() - .customUrl(CustomUrl.builder() - .noticeUrl(BILL_HOST + BILL_NOTICE_URL) - .prepayUrl(BILL_HOST + BILL_PREPAY_URL) - .payResultUrl(BILL_HOST + BILL_PAYREUSLT_URL) - .build()) - .expireAt(DateUtil.getStringFromLocalDate(list.get(0).getExpiresDt(), "yyyyMMddHHmmss")) - .build(); - - for(NiceCiDTO.InputXit xit : list){ + final KkoPayUrlRespData kkoPayUrlRespData = buildKkoPayUrlRespData(xit.getPayExpiresDt()); final OrgMng orgMng = niceCiMapper.selectKkoBpApiUrlFromEnsOrgMng(xit.getOrgCd()) .orElseThrow(() -> new EnsException(EnsErrCd.NO_DATA_FOUND, EnsErrCd.NO_DATA_FOUND.getCodeNm())); + NiceCiDTO.BillHistDTO billHistDTO = null; try { List inputDataXits = xit.getInputDataXits(); for (NiceCiDTO.InputDataXit data : inputDataXits) { //---------------------------------------------------------- - // pay Url API call & Get Result START + // pay Url API call & Get Result //---------------------------------------------------------- PayApiRespDTO> respDTO = getPayUrl(data, billHistDTO, orgMng, kkoPayUrlRespData); - //---------------------------------------------------------- - // pay Url API call & Get Result START - //---------------------------------------------------------- //---------------------------------------------------------- - // API Call 후처리 START - response 반영 & 결제 이력 생성 + // API Call 후처리 - response 반영 & 결제 이력 생성 //---------------------------------------------------------- // FIXME: API 호출 결과 SET - 연계 설정후 확인 필요 data.setPayUrl(String.valueOf(respDTO.getData())); @@ -146,31 +135,10 @@ public class NiceCiAcceptService { billHistoryService.updateBillHistory(billHistDTO); // FIXME: bill_se_cd, org_cd 설정 및 확인 필요??? - niceCiMapper.insertBill( - NiceCiDTO.BillDTO.builder() - .billId(keySequenceService.getKeySequence("Bill_id")) - .billUid(billUid) - .billerUserKey(data.getDataId()) - .billSeCd(BillSeCd.privt.getCode()) - .orgCd(xit.getOrgCd()) - .build() - ); - // FIXME: biller_notice_key, custom_url, expire_at 설정 및 확인 필요??? - niceCiMapper.mergeBillKko( - NiceCiDTO.BillKkoDTO.builder() - .billId(keySequenceService.getKeySequence("BillKko_id")) - .billerUserKey(data.getDataId()) - .billerNoticeKey(billUid) - // FIXME: 저장형태 확인 필요 - .customUrl(gson.toJson(kkoPayUrlRespData.getCustomUrl())) - .expireAt(kkoPayUrlRespData.getExpireAt()) //d.getExpiresDt() - .billUid(billUid) - .build() - ); - //---------------------------------------------------------- - // API Call 후처리 END - response 반영 & 결제 이력 생성 - //---------------------------------------------------------- + insertBillRecord(data, xit, billUid); + // FIXME: biller_notice_key, custom_url, expire_at 설정 및 확인 필요??? + insertBillKkoRecord(data, kkoPayUrlRespData, billUid); } xit.setPrcsCd(IupPrcsCd.GRUC.getCode()); niceCiMapper.updatePrcsCdAndErrorOfInputXit(xit); @@ -190,19 +158,13 @@ public class NiceCiAcceptService { niceCiMapper.updatePrcsCdAndErrorOfInputXit(xit); } } - - return EnsResponseVO.okBuilder() //.resultInfo(niceCiMapper.selectAcceptTgts(null)) .build(); } /** - * { - * "res_code": "OK" - * "mseeage": "정상응답" - * "data": {"url":"https://billgates-web.kakao.com/r/platform/pages/paynow/search/1832/11/1a481760-b0da-4a77-9a7a-74951db5fa01"} - * } + * NICE CI payUrn Call * @param dataXit NiceCiDTO.InputDataXit * @param billHistDTO NiceCiDTO.BillHistDTO * @param orgMng OrgMng @@ -211,40 +173,94 @@ public class NiceCiAcceptService { */ private PayApiRespDTO> getPayUrl(final NiceCiDTO.InputDataXit dataXit, NiceCiDTO.BillHistDTO billHistDTO, final OrgMng orgMng, final KkoPayUrlRespData kkoPayUrlRespData){ // FIXME: 파라메터 확인 필요 + Map requestParam = createRequestParam(dataXit, kkoPayUrlRespData); + + // FIXME: API 호출 로그 저장 + saveBillHistory(dataXit, billHistDTO, orgMng, requestParam); + + ResponseEntity resEntity = billKkoPayApi.url(orgMng.getKkoBpBillerCode(), orgMng.getKkoBpAuthorization(), + gson.toJson(requestParam)); + + // 마스터 상태 실패처리 + String responseBody = resEntity.getBody(); + if(resEntity.getStatusCode() != HttpStatus.OK){ + handleApiError(billHistDTO, resEntity, responseBody); + } + return parseApiResponse(responseBody, billHistDTO); + } + + private KkoPayUrlRespData buildKkoPayUrlRespData(LocalDateTime expiresDt) { + return KkoPayUrlRespData.builder() + .customUrl(CustomUrl.builder() + .noticeUrl(BILL_HOST + BILL_NOTICE_URL) + .prepayUrl(BILL_HOST + BILL_PREPAY_URL) + .payResultUrl(BILL_HOST + BILL_PAYREUSLT_URL) + .build()) + .expireAt(DateUtil.getStringFromLocalDate(expiresDt, "yyyyMMddHHmmss")) + .build(); + } + + private void insertBillRecord(NiceCiDTO.InputDataXit data, NiceCiDTO.InputXit xit, String billUid) { + niceCiMapper.insertBill( + NiceCiDTO.BillDTO.builder() + .billId(keySequenceService.getKeySequence("Bill_id")) + .billUid(billUid) + .billerUserKey(data.getDataId()) + .billSeCd(BillSeCd.privt.getCode()) + .orgCd(xit.getOrgCd()) + .build() + ); + } + + private void insertBillKkoRecord(NiceCiDTO.InputDataXit data, KkoPayUrlRespData kkoPayUrlRespData, String billUid) { + niceCiMapper.mergeBillKko( + NiceCiDTO.BillKkoDTO.builder() + .billId(keySequenceService.getKeySequence("BillKko_id")) + .billerUserKey(data.getDataId()) + .billerNoticeKey(billUid) + .customUrl(gson.toJson(kkoPayUrlRespData.getCustomUrl())) + .expireAt(kkoPayUrlRespData.getExpireAt()) + .billUid(billUid) + .build() + ); + } + + private Map createRequestParam(NiceCiDTO.InputDataXit dataXit, KkoPayUrlRespData kkoPayUrlRespData) { Map map = new HashMap<>(); map.put("biller_user_key", dataXit.getDataId()); map.put("expire_at", kkoPayUrlRespData.getExpireAt()); map.put("custom_url", gson.toJsonTree(kkoPayUrlRespData.getCustomUrl())); Map param = new HashMap<>(); param.put("data", map); + return param; + } - // FIXME: API 호출 로그 저장 + private void saveBillHistory(final NiceCiDTO.InputDataXit dataXit, NiceCiDTO.BillHistDTO billHistDTO, final OrgMng orgMng, Map requestParam) { billHistDTO = NiceCiDTO.BillHistDTO.builder() .linkedUuid(dataXit.getDataId()) .orgCd(orgMng.getOrgCd()) - .requestData(gson.toJson(param)) + .requestData(gson.toJson(requestParam)) .build(); billHistoryService.insertBillHistory(billHistDTO); + } - ResponseEntity resEntity = billKkoPayApi.url(orgMng.getKkoBpBillerCode(), orgMng.getKkoBpAuthorization(), - gson.toJson(param)); - - // 마스터 상태 실패처리 - String rsltStr = resEntity.getBody(); - if(resEntity.getStatusCode() != HttpStatus.OK){ - billHistDTO.setErrorCode(EnsErrCd.API_COMM_ERROR.getCode()); - billHistDTO.setErrorMessage("["+ BillLogSeCd.VD +"] " + EnsErrCd.API_COMM_ERROR.getCodeNm() + " " + resEntity.getStatusCode().toString()); - billHistoryService.updateBillHistory(billHistDTO); - throw new EnsException(EnsErrCd.API_COMM_ERROR, "["+ BillLogSeCd.VD +"] " + EnsErrCd.API_COMM_ERROR.getCodeNm() + " " + resEntity.getStatusCode().toString(), rsltStr); - } + private void handleApiError(NiceCiDTO.BillHistDTO billHistDTO, ResponseEntity resEntity, String responseBody) { + String errorMessage = "[" + BillLogSeCd.VD + "] " + EnsErrCd.API_COMM_ERROR.getCodeNm() + " " + resEntity.getStatusCode().toString(); + billHistDTO.setErrorCode(EnsErrCd.API_COMM_ERROR.getCode()); + billHistDTO.setErrorMessage(errorMessage); + billHistoryService.updateBillHistory(billHistDTO); + throw new EnsException(EnsErrCd.API_COMM_ERROR, errorMessage, responseBody); + } + private PayApiRespDTO> parseApiResponse(String responseBody, NiceCiDTO.BillHistDTO billHistDTO) { try { - return gson.fromJson(rsltStr, new TypeToken>>() {}.getType()); + return gson.fromJson(responseBody, new TypeToken>>() {}.getType()); } catch (Exception ex) { + String errorMessage = "[" + BillLogSeCd.VD + "] " + EnsErrCd.INVALID_RESPONSE.getCodeNm(); billHistDTO.setErrorCode(EnsErrCd.INVALID_RESPONSE.getCode()); - billHistDTO.setErrorMessage("["+ BillLogSeCd.VD +"] " + EnsErrCd.INVALID_RESPONSE.getCodeNm()); + billHistDTO.setErrorMessage(errorMessage); billHistoryService.updateBillHistory(billHistDTO); - throw new EnsException(EnsErrCd.INVALID_RESPONSE, "["+ BillLogSeCd.VD +"] " + EnsErrCd.INVALID_RESPONSE.getCodeNm(), ex); + throw new EnsException(EnsErrCd.INVALID_RESPONSE, errorMessage, ex); } } } diff --git a/src/main/java/cokr/xit/ens/modules/nice/service/NiceCiSendBulkService.java b/src/main/java/cokr/xit/ens/modules/nice/service/NiceCiSendBulkService.java index a90372a..6b25d7c 100644 --- a/src/main/java/cokr/xit/ens/modules/nice/service/NiceCiSendBulkService.java +++ b/src/main/java/cokr/xit/ens/modules/nice/service/NiceCiSendBulkService.java @@ -81,89 +81,14 @@ public class NiceCiSendBulkService { }); for(NiceCiDTO.InputXit xit : list) { - final NiceCiDTO.TmpltMngDTO tmpltMngDTO = niceCiMapper.selectTmpltMsg(xit.getJobCd()) - .orElseThrow(() -> new EnsException(EnsErrCd.NO_DATA_FOUND, EnsErrCd.NO_DATA_FOUND.getCodeNm())); - NiceCiDTO.BillHistDTO billHistDTO = null; try { - List inputDataXits = xit.getInputDataXits(); - for (NiceCiDTO.InputDataXit data : inputDataXits) { - - // 기관전문관리번호, TB_NICE_SMS_SNDNG_REQUEST - NICE_SMS_SNDNG_REQUST_ID - final String niceSmsReqId = niceCiMapper.selectNiceCiRequestId().orElseThrow( - () -> new EnsException(EnsErrCd.MAKE521, EnsErrCd.MAKE521.getCodeNm()) - ); - - NiceCiApiSendDTO.Request ciRequest = new NiceCiApiSendDTO.Request(); - NiceCiApiCommon nCommon = new NiceCiApiCommon(); - NiceCiApiSendDTO.QueryRequest queryRequest = new NiceCiApiSendDTO.QueryRequest(); - NiceCiApiSendDTO.ButtonRequest btnRequest = new NiceCiApiSendDTO.ButtonRequest(); - - // 공통부 set - setNiceCiApiRequestCommon(nCommon, niceSmsReqId); - // 개별부 set - setNiceCiApiPrivateReq(ciRequest, tmpltMngDTO, data); - // 조회요청반복부 set - setQueryRequest(queryRequest, data); - // 버튼요청반복부 set - setBtnRequest(btnRequest, data); - - ciRequest.setNiceCommon(nCommon); - ciRequest.getQueryRequests().add(queryRequest); - ciRequest.getButtonRequests().add(btnRequest); - - //---------------------------------------------------------- - // API Call 전처리 START - request 저장 - //---------------------------------------------------------- - ciRequest.setNiceSmsSndngRequstId(niceSmsReqId); - ciRequest.setDataId(data.getDataId()); - niceCiMapper.insertNiceSmsSndngRequest(ciRequest); - //---------------------------------------------------------- - // API Call 전처리 END - request 저장 - //---------------------------------------------------------- - - //---------------------------------------------------------- - // Status API call & Get Result START - //---------------------------------------------------------- - // FIXME : 연계 이후 확인 필요 - EnsResponseVO responseVO = niceCiApiService.requestSendBulk(ciRequest); - if (!EnsErrCd.OK.equals(responseVO.getErrCode())) - throw new EnsException(responseVO.getErrCode(), responseVO.getErrMsg()); - NiceCiApiSendDTO.Response resDTO = (NiceCiApiSendDTO.Response)responseVO.getResultInfo(); - //---------------------------------------------------------- - // Status API call & Get Result END - //---------------------------------------------------------- - - //---------------------------------------------------------- - // API Call 후처리 START - response 저장 - //---------------------------------------------------------- - resDTO.setNiceSmsSndngRequstId(niceSmsReqId); - niceCiMapper.insertNiceSmsSndngResponse(resDTO); - //---------------------------------------------------------- - // API Call 후처리 START - response 저장 - //---------------------------------------------------------- - - log.info(responseVO.toString()); - } - xit.setPrcsCd(IupPrcsCd.IPCP.getCode()); - niceCiMapper.updatePrcsCdAndErrorOfInputXit(xit); + processSendBulk(xit); - // FIXME: API 호출 에러 + // FIXME: API 호출 에러 } catch (EnsException e) { // FIXME : API 통신에러 인경우 skip - 재시도 되어야 - if(EnsErrCd.API_COMM_ERROR.equals(e.getErrCd())){ - xit.setPrcsCd(IupPrcsCd.FAIL.getCode()); - xit.setErrMsg(e.getMessage()); - niceCiMapper.updatePrcsCdAndErrorOfInputXit(xit); - - return EnsResponseVO.errBuilder() - .errCode(e.getErrCd()) - .errMsg(e.getMessage()) - .build(); - } - xit.setPrcsCd(IupPrcsCd.FAIL.getCode()); - xit.setErrMsg(e.getMessage()); - niceCiMapper.updatePrcsCdAndErrorOfInputXit(xit); + handleEnsException(xit, e); } catch (Exception e){ xit.setPrcsCd(IupPrcsCd.FAIL.getCode()); @@ -176,6 +101,79 @@ public class NiceCiSendBulkService { .build(); } + private void processSendBulk(NiceCiDTO.InputXit xit) throws EnsException { + final NiceCiDTO.TmpltMngDTO tmpltMngDTO = niceCiMapper.selectTmpltMsg(xit.getJobCd()) + .orElseThrow(() -> new EnsException(EnsErrCd.NO_DATA_FOUND, EnsErrCd.NO_DATA_FOUND.getCodeNm())); + + for (NiceCiDTO.InputDataXit data : xit.getInputDataXits()) { + + // 기관전문관리번호, TB_NICE_SMS_SNDNG_REQUEST - NICE_SMS_SNDNG_REQUST_ID + final String niceSmsReqId = niceCiMapper.selectNiceCiRequestId().orElseThrow( + () -> new EnsException(EnsErrCd.MAKE521, EnsErrCd.MAKE521.getCodeNm()) + ); + + NiceCiApiSendDTO.Request ciRequest = createCiRequest(tmpltMngDTO, data, niceSmsReqId); + niceCiMapper.insertNiceSmsSndngRequest(ciRequest); + //---------------------------------------------------------- + // API Call 전처리 END - request 저장 + //---------------------------------------------------------- + + //---------------------------------------------------------- + // Status API call & Get Result START + //---------------------------------------------------------- + // FIXME : 연계 이후 확인 필요 + EnsResponseVO responseVO = niceCiApiService.requestSendBulk(ciRequest); + if (!EnsErrCd.OK.equals(responseVO.getErrCode())) + throw new EnsException(responseVO.getErrCode(), responseVO.getErrMsg()); + NiceCiApiSendDTO.Response resDTO = (NiceCiApiSendDTO.Response)responseVO.getResultInfo(); + //---------------------------------------------------------- + // Status API call & Get Result END + //---------------------------------------------------------- + + //---------------------------------------------------------- + // API Call 후처리 START - response 저장 + //---------------------------------------------------------- + resDTO.setNiceSmsSndngRequstId(niceSmsReqId); + niceCiMapper.insertNiceSmsSndngResponse(resDTO); + //---------------------------------------------------------- + // API Call 후처리 START - response 저장 + //---------------------------------------------------------- + + log.info(responseVO.toString()); + } + xit.setPrcsCd(IupPrcsCd.IPCP.getCode()); + niceCiMapper.updatePrcsCdAndErrorOfInputXit(xit); + } + + + private NiceCiApiSendDTO.Request createCiRequest(NiceCiDTO.TmpltMngDTO tmpltMngDTO, NiceCiDTO.InputDataXit data, String niceSmsReqId) { + NiceCiApiSendDTO.Request ciRequest = new NiceCiApiSendDTO.Request(); + NiceCiApiCommon nCommon = new NiceCiApiCommon(); + NiceCiApiSendDTO.QueryRequest queryRequest = new NiceCiApiSendDTO.QueryRequest(); + NiceCiApiSendDTO.ButtonRequest btnRequest = new NiceCiApiSendDTO.ButtonRequest(); + + // 공통부 set + setNiceCiApiRequestCommon(nCommon, niceSmsReqId); + // 개별부 set + setNiceCiApiPrivateReq(ciRequest, tmpltMngDTO, data); + // 조회요청반복부 set + setQueryRequest(queryRequest, data); + // 버튼요청반복부 set + setBtnRequest(btnRequest, data); + + ciRequest.setNiceCommon(nCommon); + ciRequest.getQueryRequests().add(queryRequest); + ciRequest.getButtonRequests().add(btnRequest); + + //---------------------------------------------------------- + // API Call 전처리 START - request 저장 + //---------------------------------------------------------- + ciRequest.setNiceSmsSndngRequstId(niceSmsReqId); + ciRequest.setDataId(data.getDataId()); + + return ciRequest; + } + /** * 공통부 set * @param nCommon NiceCiApiCommon @@ -284,4 +282,16 @@ public class NiceCiSendBulkService { //--------------------------------------------------------- btnRequest.setBttonUrlWebLink_1(data.getPayUrl()); // 공란 - 36 자리 } + + private void handleEnsException(NiceCiDTO.InputXit xit, EnsException e) { + if (EnsErrCd.API_COMM_ERROR.equals(e.getErrCd())) { + xit.setPrcsCd(IupPrcsCd.FAIL.getCode()); + xit.setErrMsg(e.getMessage()); + niceCiMapper.updatePrcsCdAndErrorOfInputXit(xit); + return; + } + xit.setErrMsg(e.getMessage()); + xit.setPrcsCd(IupPrcsCd.FAIL.getCode()); + niceCiMapper.updatePrcsCdAndErrorOfInputXit(xit); + } }