From bc70be8695fe4d3b20c16885d7f7ed28c7c20a6f Mon Sep 17 00:00:00 2001 From: "Jonguk. Lim" Date: Thu, 12 Sep 2024 16:01:35 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=B9=B4=EC=B9=B4=EC=98=A4=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EA=B2=B0=EC=9E=AC=20=EC=B2=98=EB=A6=AC=EC=8B=9C=20?= =?UTF-8?q?=EB=A1=9C=EA=B9=85=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kko/service/process/KkoPayUrlService.java | 221 +++++++++++------- 1 file changed, 136 insertions(+), 85 deletions(-) diff --git a/src/main/java/cokr/xit/ens/modules/common/ctgy/intgrnbill/kko/service/process/KkoPayUrlService.java b/src/main/java/cokr/xit/ens/modules/common/ctgy/intgrnbill/kko/service/process/KkoPayUrlService.java index c8e4f9d..05f0d40 100644 --- a/src/main/java/cokr/xit/ens/modules/common/ctgy/intgrnbill/kko/service/process/KkoPayUrlService.java +++ b/src/main/java/cokr/xit/ens/modules/common/ctgy/intgrnbill/kko/service/process/KkoPayUrlService.java @@ -1,42 +1,33 @@ package cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.service.process; -import cokr.xit.ens.core.exception.EnsException; -import cokr.xit.ens.core.exception.code.EnsErrCd; -import cokr.xit.ens.core.utils.CmmnUtil; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.api.BillKkoPayApiSpec; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.entity.BillKkoPay; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.entity.QBillKkoPay; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.entity.repository.BillKkoPayRepository; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.service.process.model.KkoPayUrlRespData; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.api.PayUseSysApi; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.code.BillLogSeCd; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.code.BillReqSeCd; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.code.BillSeCd; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.code.PayUseSysResCd; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.entity.Bill; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.entity.BillHist; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.entity.QBill; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.entity.repository.BillHistRepository; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.model.PayApiRespDTO; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.service.process.kit.PayUrlService; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.service.process.model.PayUrlData; -import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.service.process.model.ReqAndRespInf; -import cokr.xit.ens.modules.common.ctgy.sys.mng.domain.OrgMng; -import cokr.xit.ens.modules.common.ctgy.sys.mng.domain.QOrgMng; -import cokr.xit.ens.modules.common.domain.support.FieldError; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.reflect.TypeToken; -import com.querydsl.jpa.impl.JPAQueryFactory; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Service; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; +import java.util.*; +import java.util.function.*; + +import org.springframework.http.*; +import org.springframework.stereotype.*; + +import com.google.gson.*; +import com.google.gson.reflect.*; +import com.querydsl.jpa.impl.*; + +import cokr.xit.ens.core.exception.*; +import cokr.xit.ens.core.exception.code.*; +import cokr.xit.ens.core.utils.*; +import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.api.*; +import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.entity.*; +import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.entity.repository.*; +import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.service.process.model.*; +import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.api.*; +import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.code.*; +import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.entity.*; +import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.entity.repository.*; +import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.model.*; +import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.service.process.kit.*; +import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.service.process.model.*; +import cokr.xit.ens.modules.common.ctgy.sys.mng.domain.*; +import cokr.xit.ens.modules.common.domain.support.*; +import lombok.*; +import lombok.extern.slf4j.*; @Slf4j @RequiredArgsConstructor @@ -77,7 +68,7 @@ public class KkoPayUrlService implements PayUrlService, S @Override public void validate(String billUid, PayUrlData data) { - + if (CmmnUtil.isEmpty(data.getBill())) throw new EnsException(EnsErrCd.NO_DATA_FOUND, "일치하는 청구서 자료가 없습니다."); if (CmmnUtil.isEmpty(data.getBillDetail())) @@ -93,14 +84,14 @@ public class KkoPayUrlService implements PayUrlService, S Bill bill = data.getBill(); OrgMng orgMng = bill.getOrgMng(); - + final String useSysUrl = orgMng.getKkoBpUrlApi(); final String billerCode = orgMng.getKkoBpBillerCode(); final String authorization = orgMng.getKkoBpAuthorization(); /** - * FIXME: 카카오페이 결제 Url API + * FIXME: Comment 카카오페이 결제 Url API * [모바일 페이지 카카오페이 결제 버튼 이벤트 시 실행] * 1. payUseSysApi.payUrl * 운영 : https://phts-manager.koti.re.kr/iup/bill/kkopay/gnr/url @@ -117,23 +108,74 @@ public class KkoPayUrlService implements PayUrlService, S * 더즌 청구서 API 실패 시 더즌 청구서 RE API 호출 */ Optional.ofNullable(data.getBillDetail()) - .map(billKkoPay -> createMessage(billKkoPay.getBillerUserKey())) - .map(param -> loadReqData(data, useSysUrl, param)) - .map(param -> payUseSysApi.payUrl(useSysUrl, param, null)) - .map(response -> loadRespData(data, response)) - .map(response -> checkResponse(response)) - .map(document -> savePayUrlInfo(data, document)) - .map(document -> createGnrUrlMessage(data, document)) - .map(param -> loadGnrUrlReqData(data, param)) - .map(param -> billKkoPayApi.url(billerCode, authorization, param)) - .map(response -> loadGnrUrlRespData(data, response)) - .map(response -> checkGnrUrlResponse(false, response)) - .map(respData -> saveUrl(data, respData)) - .filter(respData -> respData == null) - .map(respData -> loadGnrReUrlReqData(data)) - .map(param -> billKkoPayApi.reUrl(billerCode, authorization, param)) - .map(response -> checkGnrUrlResponse(true, response)) - .map(respData -> saveUrl(data, respData)) + .map(billKkoPay -> { + log.info("KkoPayUrlService::callApi - createMessage: {}", billKkoPay.getBillerUserKey()); + return safely(() -> createMessage(billKkoPay.getBillerUserKey())); + }) + .map(param -> { + log.info("KkoPayUrlService::callApi - loadReqData: {}", param); + return safely(() -> loadReqData(data, useSysUrl, param)); + }) + .map(param -> { + log.info("KkoPayUrlService::callApi - payUseSysApi.payUrl", param); + return safely(() -> payUseSysApi.payUrl(useSysUrl, param, null)); + }) + .map(response -> { + log.info("KkoPayUrlService::callApi - loadRespData: {}", response); + return safely(() -> loadRespData(data, response)); + }) + .map(response -> { + log.info("KkoPayUrlService::callApi - checkResponse: {}", response); + return safely(() -> checkResponse(response)); + }) + .map(document -> { + log.info("KkoPayUrlService::callApi - savePayUrlInfo: {}", document); + return safely(() -> savePayUrlInfo(data, document)); + }) + .map(document -> { + log.info("KkoPayUrlService::callApi - createGnrUrlMessage: {}", document); + return safely(() -> createGnrUrlMessage(data, document)); + }) + .map(param -> { + log.info("KkoPayUrlService::callApi - loadGnrUrlReqData: {}", param); + return safely(() -> loadGnrUrlReqData(data, param)); + }) + .map(param -> { + log.info("KkoPayUrlService::callApi - billKkoPayApi.url: {}", param); + return safely(() -> billKkoPayApi.url(billerCode, authorization, param)); + }) + .map(response -> { + log.info("KkoPayUrlService::callApi - loadGnrUrlRespData: {}", response); + return safely(() -> loadGnrUrlRespData(data, response)); + }) + .map(response -> { + log.info("KkoPayUrlService::callApi - checkGnrUrlResponse: {}", response); + return safely(() -> checkGnrUrlResponse(false, response)); + }) + .map(respData -> { + log.info("KkoPayUrlService::callApi - saveUrl: {}", respData); + return safely(() -> saveUrl(data, respData)); + }) + .filter(respData -> { + log.info("KkoPayUrlService::callApi - respData == null: {}", respData); + return safely(() -> respData == null); + }) + .map(respData -> { + log.info("KkoPayUrlService::callApi - loadGnrReUrlReqData: {}", respData); + return safely(() -> loadGnrReUrlReqData(data)); + }) + .map(param -> { + log.info("KkoPayUrlService::callApi - billKkoPayApi.reUrl: {}", param); + return safely(() -> billKkoPayApi.reUrl(billerCode, authorization, param)); + }) + .map(response -> { + log.info("KkoPayUrlService::callApi - checkGnrReUrlResponse: {}", response); + return safely(() -> checkGnrUrlResponse(true, response)); + }) + .map(respData -> { + log.info("KkoPayUrlService::callApi - saveUrl: {}", respData); + return safely(() -> saveUrl(data, respData)); + }) ; } @@ -146,24 +188,24 @@ public class KkoPayUrlService implements PayUrlService, S } private Map loadReqData(PayUrlData data, String url, Map param) { - + data.getReqAndRespInf().setUrl(url); data.getReqAndRespInf().setReqData(CmmnUtil.toJsonString(param)); return param; } private ResponseEntity loadRespData(PayUrlData data, ResponseEntity response) { - + data.getReqAndRespInf().setRespRawData(response.getBody()); return response; } private PayApiRespDTO checkResponse(ResponseEntity response) { - + if (!HttpStatus.OK.equals(response.getStatusCode())) throw new EnsException(EnsErrCd.API_COMM_ERROR, "["+ BillLogSeCd.US +"] " + EnsErrCd.API_COMM_ERROR.getCodeNm() + " " + response.getStatusCode().toString(), response.getBody()); - + PayApiRespDTO result = null; try { result = gson.fromJson(response.getBody(), new TypeToken>() { @@ -171,7 +213,7 @@ public class KkoPayUrlService implements PayUrlService, S } catch (Exception ex) { throw new EnsException(EnsErrCd.INVALID_RESPONSE, "["+ BillLogSeCd.US +"] " + EnsErrCd.INVALID_RESPONSE.getCodeNm(), ex); } - + try { PayUseSysResCd.valueOf(result.getResCode()); } catch (Exception ex) { @@ -205,7 +247,7 @@ public class KkoPayUrlService implements PayUrlService, S final BillKkoPay billKkoPay = data.getBillDetail(); final KkoPayUrlRespData urlData = document.getData(); - Map parameters = null; + Map parameters = null; parameters = CmmnUtil.isEmpty(urlData.getParameters()) ? new HashMap<>() : urlData.getParameters(); parameters.put("orgCd", orgMng == null ? null : orgMng.getOrgCd()); @@ -228,24 +270,24 @@ public class KkoPayUrlService implements PayUrlService, S } private String loadGnrUrlReqData(PayUrlData data, String message) { - + data.getReqAndRespInf().getEtc().put(DataKeys.gnrUrlReqMessage.name(), message); return message; } private ResponseEntity loadGnrUrlRespData(PayUrlData data, ResponseEntity response) { - + data.getReqAndRespInf().getEtc().put(DataKeys.gnrUrlRespMessage.name(), response.getBody()); return response; } private PayApiRespDTO> checkGnrUrlResponse(boolean isReurl, ResponseEntity response) { - + if (!HttpStatus.OK.equals(response.getStatusCode())) throw new EnsException(EnsErrCd.API_COMM_ERROR, "["+ BillLogSeCd.VD +"] " + EnsErrCd.API_COMM_ERROR.getCodeNm() + " " + response.getStatusCode().toString(), response.getBody()); - - if (!isReurl && response.getBody().contains("Read timed out")) + + if (!isReurl && response.getBody().contains("Read timed out")) return null; PayApiRespDTO> result = null; try { @@ -255,11 +297,11 @@ public class KkoPayUrlService implements PayUrlService, S if (isReurl) throw new EnsException(EnsErrCd.INVALID_RESPONSE, "["+ BillLogSeCd.VD +"] " + EnsErrCd.INVALID_RESPONSE.getCodeNm(), ex); else - return null; + return null; } - + if (!"OK".equals(result.getResCode())) - if (!isReurl && "E818".equals(result.getResCode())) + if (!isReurl && "E818".equals(result.getResCode())) return null; else { result.setMessage("["+ BillLogSeCd.VD +"] " + result.getMessage()); @@ -281,7 +323,7 @@ public class KkoPayUrlService implements PayUrlService, S } private String loadGnrReUrlReqData(PayUrlData data) { - + final String message = (String) data.getReqAndRespInf().getEtc().get(DataKeys.gnrReUrlReqMessage.name()); data.getReqAndRespInf().getEtc().put(DataKeys.gnrReUrlReqMessage.name(), message); @@ -290,7 +332,7 @@ public class KkoPayUrlService implements PayUrlService, S @Override public PayApiRespDTO> generateResult(String billUid, PayUrlData data, EnsException e) { - if (e == null) { + if (e == null) { final String urlResponse = (String) data.getReqAndRespInf().getEtc().get(DataKeys.gnrUrlRespMessage.name()); final String reurlResponse = (String) data.getReqAndRespInf().getEtc().get(DataKeys.gnrReUrlRespMessage.name()); if (CmmnUtil.isEmpty(reurlResponse)) @@ -299,7 +341,7 @@ public class KkoPayUrlService implements PayUrlService, S else return gson.fromJson(reurlResponse, new TypeToken>>() { }.getType()); - } else { + } else { if (EnsErrCd.URL_ALREADY_CREATED.equals(e.getErrCd())) { Map m = new HashMap<>(); m.put("url", data.getBillDetail().getUrl()); @@ -326,11 +368,11 @@ public class KkoPayUrlService implements PayUrlService, S @Override // @Transactional(propagation = Propagation.REQUIRED) public void saveHist(String billUid, PayUrlData data, EnsException e) { - saveHistByGetUrlInf(data, e); - saveHistByGnrUrl(data, e); - saveHistByGnrReUrl(data, e); + saveHistByGetUrlInf(data, e); + saveHistByGnrUrl(data, e); + saveHistByGnrReUrl(data, e); - if (e != null) { + if (e != null) { if (data.getBillDetail() != null) { data.getBillDetail().setError(FieldError.initBuilder() .errorCode(e.getErrCd().getCode()) @@ -350,8 +392,8 @@ public class KkoPayUrlService implements PayUrlService, S if (!CmmnUtil.isEmpty(requestData)) { BillHist billHist = BillHist.builder() .billUid(CmmnUtil.isEmpty(data.getBill()) ? null : data.getBill().getBillUid()) - .billSe(BillSeCd.bpKko) - .reqSe(BillReqSeCd.US_URLINF) + .billSe(BillSeCd.bpKko) + .reqSe(BillReqSeCd.US_URLINF) .orgMng(CmmnUtil.isEmpty(data.getBill()) ? null : data.getBill().getOrgMng()) .linkedUuid(CmmnUtil.isEmpty(data.getBillDetail()) ? null : data.getBillDetail().getBillerUserKey()) .requestData(requestData) @@ -378,8 +420,8 @@ public class KkoPayUrlService implements PayUrlService, S if (!CmmnUtil.isEmpty(requestData)) { BillHist billHist = BillHist.builder() .billUid(CmmnUtil.isEmpty(data.getBill()) ? null : data.getBill().getBillUid()) - .billSe(BillSeCd.bpKko) - .reqSe(BillReqSeCd.VD_URL) + .billSe(BillSeCd.bpKko) + .reqSe(BillReqSeCd.VD_URL) .orgMng(CmmnUtil.isEmpty(data.getBill()) ? null : data.getBill().getOrgMng()) .linkedUuid(CmmnUtil.isEmpty(data.getBillDetail()) ? null : data.getBillDetail().getBillerUserKey()) .requestData(requestData) @@ -406,8 +448,8 @@ public class KkoPayUrlService implements PayUrlService, S if (!CmmnUtil.isEmpty(requestData)) { BillHist billHist = BillHist.builder() .billUid(CmmnUtil.isEmpty(data.getBill()) ? null : data.getBill().getBillUid()) - .billSe(BillSeCd.bpKko) - .reqSe(BillReqSeCd.VD_REURL) + .billSe(BillSeCd.bpKko) + .reqSe(BillReqSeCd.VD_REURL) .orgMng(CmmnUtil.isEmpty(data.getBill()) ? null : data.getBill().getOrgMng()) .linkedUuid(CmmnUtil.isEmpty(data.getBillDetail()) ? null : data.getBillDetail().getBillerUserKey()) .requestData(requestData) @@ -422,7 +464,7 @@ public class KkoPayUrlService implements PayUrlService, S billHistRepository.save(billHist); - if (e != null) { + if (e != null) { if (data.getBillDetail() != null) { data.getBillDetail().setError(FieldError.initBuilder() .errorCode(e.getErrCd().getCode()) @@ -434,4 +476,13 @@ public class KkoPayUrlService implements PayUrlService, S } } + + private T safely(Supplier supplier) { + try { + return supplier.get(); + } catch (Exception e) { + log.info("Error occurred", e); + throw new EnsException(EnsErrCd.UNKNOWN, e.getMessage()); + } + } }