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;
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<NiceCiDTO.InputDataXit> inputDataXits = xit.getInputDataXits();
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);
//----------------------------------------------------------
// 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<Map<String, Object>> getPayUrl(final NiceCiDTO.InputDataXit dataXit, NiceCiDTO.BillHistDTO billHistDTO, final OrgMng orgMng, final KkoPayUrlRespData kkoPayUrlRespData){
// 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.put("biller_user_key", dataXit.getDataId());
map.put("expire_at", kkoPayUrlRespData.getExpireAt());
map.put("custom_url", gson.toJsonTree(kkoPayUrlRespData.getCustomUrl()));
Map<String, Object> 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<String, Object> requestParam) {
billHistDTO = NiceCiDTO.BillHistDTO.builder()
.linkedUuid(dataXit.getDataId())
.orgCd(orgMng.getOrgCd())
.requestData(gson.toJson(param))
.requestData(gson.toJson(requestParam))
.build();
billHistoryService.insertBillHistory(billHistDTO);
}
ResponseEntity<String> resEntity = billKkoPayApi.url(orgMng.getKkoBpBillerCode(), orgMng.getKkoBpAuthorization(),
gson.toJson(param));
// 마스터 상태 실패처리
String rsltStr = resEntity.getBody();
if(resEntity.getStatusCode() != HttpStatus.OK){
private void handleApiError(NiceCiDTO.BillHistDTO billHistDTO, ResponseEntity<String> resEntity, String responseBody) {
String errorMessage = "[" + BillLogSeCd.VD + "] " + EnsErrCd.API_COMM_ERROR.getCodeNm() + " " + resEntity.getStatusCode().toString();
billHistDTO.setErrorCode(EnsErrCd.API_COMM_ERROR.getCode());
billHistDTO.setErrorMessage("["+ BillLogSeCd.VD +"] " + EnsErrCd.API_COMM_ERROR.getCodeNm() + " " + resEntity.getStatusCode().toString());
billHistDTO.setErrorMessage(errorMessage);
billHistoryService.updateBillHistory(billHistDTO);
throw new EnsException(EnsErrCd.API_COMM_ERROR, "["+ BillLogSeCd.VD +"] " + EnsErrCd.API_COMM_ERROR.getCodeNm() + " " + resEntity.getStatusCode().toString(), rsltStr);
throw new EnsException(EnsErrCd.API_COMM_ERROR, errorMessage, responseBody);
}
private PayApiRespDTO<Map<String, Object>> parseApiResponse(String responseBody, NiceCiDTO.BillHistDTO billHistDTO) {
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) {
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);
}
}
}

@ -81,42 +81,38 @@ public class NiceCiSendBulkService {
});
for(NiceCiDTO.InputXit xit : list) {
try {
processSendBulk(xit);
// FIXME: API 호출 에러
} catch (EnsException e) {
// FIXME : API 통신에러 인경우 skip - 재시도 되어야
handleEnsException(xit, e);
} catch (Exception e){
xit.setPrcsCd(IupPrcsCd.FAIL.getCode());
xit.setErrMsg(ObjectUtils.isNotEmpty(e.getCause())? e.getCause().getMessage() : e.getMessage());
niceCiMapper.updatePrcsCdAndErrorOfInputXit(xit);
}
}
return EnsResponseVO.okBuilder()
//.resultInfo(niceCiMapper.selectAcceptTgts(null))
.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()));
NiceCiDTO.BillHistDTO billHistDTO = null;
try {
List<NiceCiDTO.InputDataXit> inputDataXits = xit.getInputDataXits();
for (NiceCiDTO.InputDataXit data : inputDataXits) {
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 = 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());
NiceCiApiSendDTO.Request ciRequest = createCiRequest(tmpltMngDTO, data, niceSmsReqId);
niceCiMapper.insertNiceSmsSndngRequest(ciRequest);
//----------------------------------------------------------
// API Call 전처리 END - request 저장
@ -147,33 +143,35 @@ public class NiceCiSendBulkService {
}
xit.setPrcsCd(IupPrcsCd.IPCP.getCode());
niceCiMapper.updatePrcsCdAndErrorOfInputXit(xit);
}
// 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);
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();
} catch (Exception e){
xit.setPrcsCd(IupPrcsCd.FAIL.getCode());
xit.setErrMsg(ObjectUtils.isNotEmpty(e.getCause())? e.getCause().getMessage() : e.getMessage());
niceCiMapper.updatePrcsCdAndErrorOfInputXit(xit);
}
}
return EnsResponseVO.okBuilder()
//.resultInfo(niceCiMapper.selectAcceptTgts(null))
.build();
// 공통부 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;
}
/**
@ -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);
}
}

Loading…
Cancel
Save