refactor: Nice CI Accept, Send code refactoring

dev
Jonguk. Lim 1 month ago
parent 1abe698e9c
commit 9565456542

@ -1,5 +1,6 @@
package cokr.xit.ens.modules.nice.service; package cokr.xit.ens.modules.nice.service;
import java.time.*;
import java.util.*; import java.util.*;
import org.apache.commons.lang3.*; import org.apache.commons.lang3.*;
@ -104,36 +105,24 @@ public class NiceCiAcceptService {
d.setInputDataXits(niceCiMapper.selectInputDataXits(niceCiParam)); d.setInputDataXits(niceCiMapper.selectInputDataXits(niceCiParam));
}); });
final String prefixBillUid = PostSeCd.intgrnNoti.getCode() + "-" + IdGenerator.getCurrentTimeSec(); 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){ for(NiceCiDTO.InputXit xit : list){
final KkoPayUrlRespData kkoPayUrlRespData = buildKkoPayUrlRespData(xit.getPayExpiresDt());
final OrgMng orgMng = niceCiMapper.selectKkoBpApiUrlFromEnsOrgMng(xit.getOrgCd()) final OrgMng orgMng = niceCiMapper.selectKkoBpApiUrlFromEnsOrgMng(xit.getOrgCd())
.orElseThrow(() -> new EnsException(EnsErrCd.NO_DATA_FOUND, EnsErrCd.NO_DATA_FOUND.getCodeNm())); .orElseThrow(() -> new EnsException(EnsErrCd.NO_DATA_FOUND, EnsErrCd.NO_DATA_FOUND.getCodeNm()));
NiceCiDTO.BillHistDTO billHistDTO = null; NiceCiDTO.BillHistDTO billHistDTO = null;
try { try {
List<NiceCiDTO.InputDataXit> inputDataXits = xit.getInputDataXits(); List<NiceCiDTO.InputDataXit> inputDataXits = xit.getInputDataXits();
for (NiceCiDTO.InputDataXit data : inputDataXits) { for (NiceCiDTO.InputDataXit data : inputDataXits) {
//---------------------------------------------------------- //----------------------------------------------------------
// pay Url API call & Get Result START // pay Url API call & Get Result
//---------------------------------------------------------- //----------------------------------------------------------
PayApiRespDTO<Map<String, Object>> respDTO = getPayUrl(data, billHistDTO, orgMng, kkoPayUrlRespData); PayApiRespDTO<Map<String, Object>> respDTO = getPayUrl(data, billHistDTO, orgMng, kkoPayUrlRespData);
//----------------------------------------------------------
// pay Url API call & Get Result START
//----------------------------------------------------------
//---------------------------------------------------------- //----------------------------------------------------------
// API Call 후처리 START - response 반영 & 결제 이력 생성 // API Call 후처리 - response 반영 & 결제 이력 생성
//---------------------------------------------------------- //----------------------------------------------------------
// FIXME: API 호출 결과 SET - 연계 설정후 확인 필요 // FIXME: API 호출 결과 SET - 연계 설정후 확인 필요
data.setPayUrl(String.valueOf(respDTO.getData())); data.setPayUrl(String.valueOf(respDTO.getData()));
@ -146,31 +135,10 @@ public class NiceCiAcceptService {
billHistoryService.updateBillHistory(billHistDTO); billHistoryService.updateBillHistory(billHistDTO);
// FIXME: bill_se_cd, org_cd 설정 및 확인 필요??? // FIXME: bill_se_cd, org_cd 설정 및 확인 필요???
niceCiMapper.insertBill( insertBillRecord(data, xit, billUid);
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 반영 & 결제 이력 생성
//----------------------------------------------------------
// FIXME: biller_notice_key, custom_url, expire_at 설정 및 확인 필요???
insertBillKkoRecord(data, kkoPayUrlRespData, billUid);
} }
xit.setPrcsCd(IupPrcsCd.GRUC.getCode()); xit.setPrcsCd(IupPrcsCd.GRUC.getCode());
niceCiMapper.updatePrcsCdAndErrorOfInputXit(xit); niceCiMapper.updatePrcsCdAndErrorOfInputXit(xit);
@ -190,19 +158,13 @@ public class NiceCiAcceptService {
niceCiMapper.updatePrcsCdAndErrorOfInputXit(xit); niceCiMapper.updatePrcsCdAndErrorOfInputXit(xit);
} }
} }
return EnsResponseVO.okBuilder() return EnsResponseVO.okBuilder()
//.resultInfo(niceCiMapper.selectAcceptTgts(null)) //.resultInfo(niceCiMapper.selectAcceptTgts(null))
.build(); .build();
} }
/** /**
* { * NICE CI payUrn Call
* "res_code": "OK"
* "mseeage": "정상응답"
* "data": {"url":"https://billgates-web.kakao.com/r/platform/pages/paynow/search/1832/11/1a481760-b0da-4a77-9a7a-74951db5fa01"}
* }
* @param dataXit NiceCiDTO.InputDataXit * @param dataXit NiceCiDTO.InputDataXit
* @param billHistDTO NiceCiDTO.BillHistDTO * @param billHistDTO NiceCiDTO.BillHistDTO
* @param orgMng OrgMng * @param orgMng OrgMng
@ -211,40 +173,94 @@ public class NiceCiAcceptService {
*/ */
private PayApiRespDTO<Map<String, Object>> getPayUrl(final NiceCiDTO.InputDataXit dataXit, NiceCiDTO.BillHistDTO billHistDTO, final OrgMng orgMng, final KkoPayUrlRespData kkoPayUrlRespData){ private PayApiRespDTO<Map<String, Object>> getPayUrl(final NiceCiDTO.InputDataXit dataXit, NiceCiDTO.BillHistDTO billHistDTO, final OrgMng orgMng, final KkoPayUrlRespData kkoPayUrlRespData){
// FIXME: 파라메터 확인 필요 // FIXME: 파라메터 확인 필요
Map<String, Object> requestParam = createRequestParam(dataXit, kkoPayUrlRespData);
// FIXME: API 호출 로그 저장
saveBillHistory(dataXit, billHistDTO, orgMng, requestParam);
ResponseEntity<String> 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<String, Object> createRequestParam(NiceCiDTO.InputDataXit dataXit, KkoPayUrlRespData kkoPayUrlRespData) {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("biller_user_key", dataXit.getDataId()); map.put("biller_user_key", dataXit.getDataId());
map.put("expire_at", kkoPayUrlRespData.getExpireAt()); map.put("expire_at", kkoPayUrlRespData.getExpireAt());
map.put("custom_url", gson.toJsonTree(kkoPayUrlRespData.getCustomUrl())); map.put("custom_url", gson.toJsonTree(kkoPayUrlRespData.getCustomUrl()));
Map<String, Object> param = new HashMap<>(); Map<String, Object> param = new HashMap<>();
param.put("data", map); param.put("data", map);
return param;
}
// FIXME: API 호출 로그 저장 private void saveBillHistory(final NiceCiDTO.InputDataXit dataXit, NiceCiDTO.BillHistDTO billHistDTO, final OrgMng orgMng, Map<String, Object> requestParam) {
billHistDTO = NiceCiDTO.BillHistDTO.builder() billHistDTO = NiceCiDTO.BillHistDTO.builder()
.linkedUuid(dataXit.getDataId()) .linkedUuid(dataXit.getDataId())
.orgCd(orgMng.getOrgCd()) .orgCd(orgMng.getOrgCd())
.requestData(gson.toJson(param)) .requestData(gson.toJson(requestParam))
.build(); .build();
billHistoryService.insertBillHistory(billHistDTO); billHistoryService.insertBillHistory(billHistDTO);
}
ResponseEntity<String> resEntity = billKkoPayApi.url(orgMng.getKkoBpBillerCode(), orgMng.getKkoBpAuthorization(), private void handleApiError(NiceCiDTO.BillHistDTO billHistDTO, ResponseEntity<String> resEntity, String responseBody) {
gson.toJson(param)); String errorMessage = "[" + BillLogSeCd.VD + "] " + EnsErrCd.API_COMM_ERROR.getCodeNm() + " " + resEntity.getStatusCode().toString();
billHistDTO.setErrorCode(EnsErrCd.API_COMM_ERROR.getCode());
// 마스터 상태 실패처리 billHistDTO.setErrorMessage(errorMessage);
String rsltStr = resEntity.getBody(); billHistoryService.updateBillHistory(billHistDTO);
if(resEntity.getStatusCode() != HttpStatus.OK){ throw new EnsException(EnsErrCd.API_COMM_ERROR, errorMessage, responseBody);
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 PayApiRespDTO<Map<String, Object>> parseApiResponse(String responseBody, NiceCiDTO.BillHistDTO billHistDTO) {
try { try {
return gson.fromJson(rsltStr, new TypeToken<PayApiRespDTO<Map<String, Object>>>() {}.getType()); return gson.fromJson(responseBody, new TypeToken<PayApiRespDTO<Map<String, Object>>>() {}.getType());
} catch (Exception ex) { } catch (Exception ex) {
String errorMessage = "[" + BillLogSeCd.VD + "] " + EnsErrCd.INVALID_RESPONSE.getCodeNm();
billHistDTO.setErrorCode(EnsErrCd.INVALID_RESPONSE.getCode()); billHistDTO.setErrorCode(EnsErrCd.INVALID_RESPONSE.getCode());
billHistDTO.setErrorMessage("["+ BillLogSeCd.VD +"] " + EnsErrCd.INVALID_RESPONSE.getCodeNm()); billHistDTO.setErrorMessage(errorMessage);
billHistoryService.updateBillHistory(billHistDTO); billHistoryService.updateBillHistory(billHistDTO);
throw new EnsException(EnsErrCd.INVALID_RESPONSE, "["+ BillLogSeCd.VD +"] " + EnsErrCd.INVALID_RESPONSE.getCodeNm(), ex); throw new EnsException(EnsErrCd.INVALID_RESPONSE, errorMessage, ex);
} }
} }
} }

@ -81,89 +81,14 @@ public class NiceCiSendBulkService {
}); });
for(NiceCiDTO.InputXit xit : list) { 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 { try {
List<NiceCiDTO.InputDataXit> inputDataXits = xit.getInputDataXits(); processSendBulk(xit);
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);
// FIXME: API 호출 에러 // FIXME: API 호출 에러
} catch (EnsException e) { } catch (EnsException e) {
// FIXME : API 통신에러 인경우 skip - 재시도 되어야 // FIXME : API 통신에러 인경우 skip - 재시도 되어야
if(EnsErrCd.API_COMM_ERROR.equals(e.getErrCd())){ handleEnsException(xit, e);
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);
} catch (Exception e){ } catch (Exception e){
xit.setPrcsCd(IupPrcsCd.FAIL.getCode()); xit.setPrcsCd(IupPrcsCd.FAIL.getCode());
@ -176,6 +101,79 @@ public class NiceCiSendBulkService {
.build(); .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 * set
* @param nCommon NiceCiApiCommon * @param nCommon NiceCiApiCommon
@ -284,4 +282,16 @@ public class NiceCiSendBulkService {
//--------------------------------------------------------- //---------------------------------------------------------
btnRequest.setBttonUrlWebLink_1(data.getPayUrl()); // 공란 - 36 자리 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);
}
} }

Loading…
Cancel
Save