From e6f8d4a5f1b9a2437308b4ffc9540dea7f364390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EA=B2=BD=EC=88=99?= Date: Mon, 6 Apr 2026 19:39:10 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B2=B0=EC=A0=9C=20-=20=ED=82=A4=EC=9B=80?= =?UTF-8?q?=EA=B2=B0=EC=A0=9C=20=EC=A3=BC=EB=AC=B8=EC=9E=90=EB=AA=85=20PC?= =?UTF-8?q?=EC=9D=B8=EC=BD=94=EB=94=A9=20=ED=99=95=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gcone/ordr/pay/service/BOPayService.java | 205 +++++++++++++++--- .../service/BORtngudrefndService.java | 98 +++++++++ .../ordr/controller/OrdrController.java | 30 +-- .../WEB-INF/views/sopmal/ordr/MbrPurchsC.jsp | 2 + 4 files changed, 295 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/wb/domainserver/domain/gcone/ordr/pay/service/BOPayService.java b/src/main/java/com/wb/domainserver/domain/gcone/ordr/pay/service/BOPayService.java index 5ef3cbb7..c30419a1 100644 --- a/src/main/java/com/wb/domainserver/domain/gcone/ordr/pay/service/BOPayService.java +++ b/src/main/java/com/wb/domainserver/domain/gcone/ordr/pay/service/BOPayService.java @@ -300,7 +300,7 @@ public class BOPayService{ /** * KCP 결제 - * @param BOPayVO + * @param payVO */ public Map insertKcpPay(BOPayVO payVO) throws Exception { @@ -359,7 +359,7 @@ public class BOPayService{ /** * KCP 수기결제 - * @param BOPayVO + * @param payVO */ public Map insertKcpPayKeyin(BOPayVO payVO) throws Exception { @@ -432,7 +432,7 @@ public class BOPayService{ /** * KCP 결제 취소 - * @param BOPayVO + * @param payVO */ public Map cancelKcpPay(BOPayVO payVO) throws Exception { Map resParam = new HashMap<>(); @@ -639,7 +639,7 @@ public class BOPayService{ /** * KCP 에스크로 상태변경 : 배송정보전송 - * @param BOPayVO + * @param payVO */ public Map updateEscwKcpPay(BOPayVO payVO) throws Exception { @@ -758,7 +758,7 @@ public class BOPayService{ /** * KCP 현금영수증 발급 - * @param BOPayVO + * @param payVO */ public Map insertKcpCashReceipt(BOPayVO payVO) throws Exception { @@ -819,7 +819,7 @@ public class BOPayService{ /** * KCP 현금영수증 취소 - * @param BOPayVO + * @param payVO */ public Map cancelKcpCashReceipt(BOPayVO payVO) throws Exception { @@ -869,7 +869,7 @@ public class BOPayService{ /** * KCP 현금영수증 조회 - * @param BOPayVO + * @param payVO */ public Map selectKcpCashReceipt(BOPayVO payVO) throws Exception { @@ -912,7 +912,7 @@ public class BOPayService{ /** * 토스 현금영수증 발급 - * @param BOPayVO + * @param payVO */ public Map insertTossCashReceipt(BOPayVO payVO) throws Exception { @@ -952,7 +952,7 @@ public class BOPayService{ /** * 토스 현금영수증 취소 - * @param BOPayVO + * @param payVO */ public Map cancelTossCashReceipt(BOPayVO payVO) throws Exception { @@ -983,7 +983,7 @@ public class BOPayService{ /** * 토스 결제 * 일반결제(toss), 토스페이(tossPay), 브랜드페이(brandPay) 같이 들어옴 - * @param BOPayVO + * @param payVO */ public Map insertTossPay(BOPayVO payVO) throws Exception { @@ -1020,7 +1020,7 @@ public class BOPayService{ /** * 토스 수기결제 - * @param BOPayVO + * @param payVO */ public Map insertTossPayKeyin(BOPayVO payVO) throws Exception { @@ -1059,7 +1059,7 @@ public class BOPayService{ /** * 토스 결제취소 - * @param BOPayVO + * @param payVO */ public Map cancelTossPay(BOPayVO payVO) throws Exception { @@ -1076,17 +1076,6 @@ public class BOPayService{ obj.put("refundReceiveAccount", refundReceiveAccount); } - /* 취소 호출 시 setModGubun을 하기로 함 - String memorSetleAt = StringUtil.isNullToString(payVO.getMemorSetleAt()); - if("Y".equals(memorSetleAt)){ - payVO.setModGubun("keyIn"); - }else if("G".equals(memorSetleAt)){ //이부분에 대한 처리 필요 - payVO.setModGubun("brandPay"); - }else{ - payVO.setModGubun("toss"); - } - // easyPay 추가되어야 함 - */ payVO.setType("cancel"); URL url = new URL("https://api.tosspayments.com/v1/payments/" + payVO.getPaymentKey() + "/cancel"); @@ -1277,7 +1266,7 @@ public class BOPayService{ /** * 토스 에스크로 상태변경 : 배송정보전송 - * @param BOPayVO + * @param payVO */ public Map updateEscwTossPay(BOPayVO payVO) throws Exception { //관련 키값 DB 조회 @@ -1333,7 +1322,6 @@ public class BOPayService{ rcvname = payVO.getRcvName(); // 실수령인명 boolean resp = false; //응답결과 -// mertkey = resultMap.get("tossPublicKey").toString(); //토스페이먼츠에서 발급한 상점키로 변경해 주시기 바랍니다. //TODO : 확인필요 mertkey = payVO.getMertkey(); // 머트키(퍼블릭키) // mertkey = "9fd0de8489b4dc4af0dc40459958cd70"; //토스페이먼츠에서 발급한 상점키로 변경해 주시기 바랍니다. @@ -1475,7 +1463,7 @@ public class BOPayService{ /** * Kiwoom 결제 - * @param BOPayVO + * @param payVO */ public Map insertKiwoomPay(BOPayVO payVO) throws Exception { @@ -1559,6 +1547,171 @@ public class BOPayService{ } + /** + * 키움 결제취소 + * @param payVO + */ + public Map cancelKiwoomPay(BOPayVO payVO) throws Exception { + + //관련 키값 DB 조회 + DataMap resultMap = selectPayLink(payVO.getSopmalCd()); + + //요청코드 + String authKey = resultMap.get("kcpSecretKey").toString(); + String cpId = resultMap.get("kcpMid").toString(); + String payMethod = ""; //취소결제수단 + String cancelReq = "Y"; //취소URL 요청여부 + String amount = payVO.getCancelAmount(); //취소금액 //todo :: db연결해서 가져와야 함 nullCheck(request.getParameter("AUTHKEY")); + String trxId = payVO.getTno(); //거래번호 + String cancelReason = payVO.getCancelReason(); //취소사유 + //String TAXFREEAMT = nullCheck(request.getParameter("TAXFREEAMT")); //비과세 취소금액 + + if(payVO.getSetleTyCd().equals("01")) { + payMethod = "CARD"; + } + + //1차 요청코드 + JSONObject obj = new JSONObject(); + obj.put("cancelReason", payVO.getCancelReason()); //취소사유 + obj.put("cancelAmount", payVO.getCancelAmount()); //취소금액 (금액이 없으면 전체취소) + + JSONObject jsonObj = new JSONObject(); + jsonObj.put("CPID", cpId); + jsonObj.put("PAYMETHOD", payMethod); + jsonObj.put("AMOUNT",amount); + jsonObj.put("CANCELREQ",cancelReq); + jsonObj.put("TRXID",trxId); + jsonObj.put("CANCELREASON",cancelReason); + //jsonObj.put("TAXFREEAMT",TAXFREEAMT); + + // ====== step 1. 취소URL 요청 ====== + + //결제 API URL + String targetURL = ""; + if(cpId.equals("CTS18056")) { + targetURL = "https://apitest.kiwoompay.co.kr/pay/ready"; //개발서버 + }else{ + targetURL = "https://api.kiwoompay.co.kr/pay/ready"; //운영서버 + } + URL goUrl = new URL(targetURL); + + HttpURLConnection con = (HttpURLConnection) goUrl.openConnection(); + con.setRequestMethod("POST"); + con.setRequestProperty("Content-Type", "application/json"); + con.setRequestProperty("Authorization", authKey); + con.setUseCaches(false); + con.setDoOutput(true); + con.setDoInput(true); + + OutputStream os = con.getOutputStream(); + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os)); + + writer.write(""); + os.write((jsonObj.toString()).getBytes("UTF-8")); + writer.flush(); + writer.close(); + os.close(); + con.connect(); + + //호출 결과 값 + int responseCode = con.getResponseCode(); + String responseMsg = con.getResponseMessage(); + + String token = ""; + String returnUrl = ""; + + //정상 수신 했을경우 (응답코드:200) + if(responseCode == 200){ + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + String inputLine = null; + StringBuffer response1 = new StringBuffer(); + + while((inputLine = in.readLine()) != null){ + response1.append(inputLine); + } + + in.close(); + + //응답 데이터 처리 + String responseBody = response1.toString(); + JSONObject jObj = (JSONObject)JSONValue.parse(responseBody); + token = jObj.get("TOKEN").toString(); + returnUrl = jObj.get("RETURNURL").toString(); + } + + con.disconnect(); + + log.debug("cancelKiwoom STEP1 승인 URL ====={}",returnUrl); + + // ====== step 2. 취소 승인 요청 ====== + + targetURL = returnUrl; + goUrl = new URL(targetURL); + + con = (HttpURLConnection) goUrl.openConnection(); + con.setRequestMethod("POST"); + con.setRequestProperty("Content-Type", "application/json"); + con.setRequestProperty("Authorization", authKey); + con.setRequestProperty("TOKEN", token);//TOKEN + con.setUseCaches(false); + con.setDoOutput(true); + con.setDoInput(true); + os = con.getOutputStream(); + writer = new BufferedWriter(new OutputStreamWriter(os)); + + writer.write(""); + os.write((jsonObj.toString()).getBytes("EUC-KR")); + writer.flush(); + writer.close(); + os.close(); + con.connect(); + + //호출 결과 값 + responseCode = con.getResponseCode(); + responseMsg = con.getResponseMessage(); + + JSONObject jObj = new JSONObject(); + + //정상 수신 했을경우 (응답코드:200) + if(responseCode == 200){ + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); + String inputLine = null; + StringBuffer response1 = new StringBuffer(); + + while((inputLine = in.readLine()) != null){ + response1.append(inputLine); + } + + in.close(); + + //응답 데이터 처리 + String responseBody = response1.toString(); + + jObj = (JSONObject)JSONValue.parse(responseBody); + /*resultcode = checkObjNull(jObj.get("RESULTCODE")).toString(); + errormessage= checkObjNull(jObj.get("ERRORMESSAGE")).toString(); + + if(resultcode.equals("0000")){ + daoutrx = checkObjNull(jObj.get("DAOUTRX")).toString(); + amount = checkObjNull(jObj.get("AMOUNT")).toString(); + orderno = checkObjNull(jObj.get("ORDERNO")).toString(); + canceldate = checkObjNull(jObj.get("CANCELDATE")).toString(); + cpname = checkObjNull(jObj.get("CPNAME")).toString(); + cpurl = checkObjNull(jObj.get("CPURL")).toString(); + cptelno = checkObjNull(jObj.get("CPTELNO")).toString(); + }*/ + } + + con.disconnect(); + + JSONObject finalJObj = jObj; + return new HashMap<>() {{ + put("response", finalJObj); + }}; + + } + + } diff --git a/src/main/java/com/wb/domainserver/domain/gcone/ordr/rtngudrefnd/service/BORtngudrefndService.java b/src/main/java/com/wb/domainserver/domain/gcone/ordr/rtngudrefnd/service/BORtngudrefndService.java index d7c07e0f..03fc2afd 100644 --- a/src/main/java/com/wb/domainserver/domain/gcone/ordr/rtngudrefnd/service/BORtngudrefndService.java +++ b/src/main/java/com/wb/domainserver/domain/gcone/ordr/rtngudrefnd/service/BORtngudrefndService.java @@ -1062,6 +1062,104 @@ public class BORtngudrefndService { log.info("리스펀스 결과 값 : {}", responseMap.get("res_cd")); + if ("0000".equals(responseMap.get("res_cd"))) { + // 8729 : 기 취소된 거래 + // 0000 : 취소성공 + log.debug("0000"); + message.put("setleStatus", responseMap.get("res_cd")); + message.put("setleMessage", responseMap.get("res_msg")); + message = refundCancelCashReceipt(setle, ordrSetleInnb, sopmalCd, receiptCancelAmt, refundModeTypeSales, updusrDvCd, updusr); // 현금영수증 취소 + if (!"0000".equals(message.get("cashReceiptStatus"))) { + logDtlCn = "현금영수증 취소에 실패하였습니다."; + // throw new WnbtRestException("현금영수증 취소에 실패하였습니다."); // TODO : 현금영수증 취소 실패해도 트렌젝션 걸러야하는지 확인 필요 + } + } else { + + if ("8729".equals(responseMap.get("res_cd"))) { // 계좌이체 기 취소된 거래 + logDtlCn = String.valueOf(responseMap.get("res_msg")); +// throw new WnbtRestException(logDtlCn); + } else if ("8332".equals(responseMap.get("res_cd"))) { // 이미 사용중지된 가상계좌 사용중지요청 + logDtlCn = String.valueOf(responseMap.get("res_msg")); + throw new WnbtRestException(logDtlCn); + } else if ("8133".equals(responseMap.get("res_cd"))) { // 기취소된 신용카드 거래 취소요청 + logDtlCn = String.valueOf(responseMap.get("res_msg")); +// throw new WnbtRestException(logDtlCn); + } else if ("8317".equals(responseMap.get("res_cd"))) { // 가상계좌 환불 불가상태 + logDtlCn = String.valueOf(responseMap.get("res_msg")); + throw new WnbtRestException(logDtlCn); + } else if ("5002".equals(responseMap.get("res_cd"))) { // 취소 불가 상태 (에스크로일 경우 '구매확인' 이후 처리가 가능해서 5002 에러가 들어옴 + if ("05".equals(setle.getSetleTyCd()) || "06".equals(setle.getSetleTyCd())) { + logDtlCn = String.valueOf(responseMap.get("res_msg") + "\n에스크로 결제의 경우, 배송 완료 후 수령자 이메일에 있는 '구매확인' 버튼을 눌러야 취소가 가능합니다."); + } else { + logDtlCn = String.valueOf(responseMap.get("res_msg")); + } + throw new WnbtRestException(logDtlCn); + } else { + log.debug("1111"); + message.put("setleStatus", responseMap.get("res_cd")); + message.put("setleMessage", responseMap.get("res_msg")); + logDtlCn = "잘못된 요청입니다. 관리자에게 문의하세요."; + throw new WnbtRestException("잘못된 요청입니다. 관리자에게 문의하세요."); + } + + } + + // kiwoom + } else if ("kiwoom".equals(setle.getPgCmpnyNm())) { + log.debug("kiwoom 취소 진입"); + + BOPayVO payVO = new BOPayVO(); + payVO.setOrderId(ordrInnb); // 주문 고유번호 + payVO.setSopmalCd(sopmalCd); // 쇼핑몰 코드 + + payVO.setMId(setle.getSetleMid()); // 결제 MID(상점키) + payVO.setSetleTyCd(setle.getSetleTyCd()); // 결제 유형 코드 + payVO.setCancelReason(refundVO.getRefundResnCn()); // 취소사유 + payVO.setTno(setle.getDelngNb()); // 거래번호 + + //부분 취소 처리 경우 승인금액보다 취소요청금액이 적으면 부분 취소 처리 + payVO.setCancelAmount(String.valueOf(wonCancelAmt)); // 취소금액 + + if ("Y".equals(setle.getMemorSetleAt())) { + payVO.setMemorSetleAt("Y"); + } + + log.info("kiwoom api 전달 파라미터 확인 : {}", payVO); + setleCancelResultMessage = payService.cancelKiwoomPay(payVO); + log.info("결제 취소 API 결과 확인 : {}", setleCancelResultMessage); + log.info("결제 취소 API 결과 확인 : {}", setleCancelResultMessage.get("response")); + log.info("결제 취소 API 결과 확인 RESULTCODE : {}", setleCancelResultMessage.get("RESULTCODE")); + log.info("결제 취소 API 결과 확인 ERRORMESSAGE : {}", setleCancelResultMessage.get("ERRORMESSAGE")); + + + /*resultcode = checkObjNull(jObj.get("RESULTCODE")).toString(); + errormessage= checkObjNull(jObj.get("ERRORMESSAGE")).toString(); + + if(resultcode.equals("0000")){ + daoutrx = checkObjNull(jObj.get("DAOUTRX")).toString(); + amount = checkObjNull(jObj.get("AMOUNT")).toString(); + orderno = checkObjNull(jObj.get("ORDERNO")).toString(); + canceldate = checkObjNull(jObj.get("CANCELDATE")).toString(); + cpname = checkObjNull(jObj.get("CPNAME")).toString(); + cpurl = checkObjNull(jObj.get("CPURL")).toString(); + cptelno = checkObjNull(jObj.get("CPTELNO")).toString(); + }*/ + + responseMap = (Map) setleCancelResultMessage.get("response"); +// String jsonString = (String) setleCancelResultMessage.get("response"); +// ObjectMapper objectMapper = new ObjectMapper(); +// responseMap = objectMapper.readValue(jsonString, Map.class); + log.info("리스펀스 결과 : {}", responseMap); + log.info("리스펀스 결과 값 : {}", responseMap.get("res_cd")); + + + String jsonString = (String) setleCancelResultMessage.get("response"); + ObjectMapper objectMapper = new ObjectMapper(); + responseMap = objectMapper.readValue(jsonString, Map.class); + log.debug("리스펀스 결과 : {}", responseMap); + log.info("리스펀스 결과 값 : {}", responseMap.get("status")); + + if ("0000".equals(responseMap.get("res_cd"))) { // 8729 : 기 취소된 거래 // 0000 : 취소성공 diff --git a/src/main/java/com/wb/domainserver/domain/sopmal/ordr/controller/OrdrController.java b/src/main/java/com/wb/domainserver/domain/sopmal/ordr/controller/OrdrController.java index 0b4a0d3e..aceeb15a 100644 --- a/src/main/java/com/wb/domainserver/domain/sopmal/ordr/controller/OrdrController.java +++ b/src/main/java/com/wb/domainserver/domain/sopmal/ordr/controller/OrdrController.java @@ -1437,15 +1437,6 @@ public class OrdrController { @RequestMapping(value="/kiwoomPayPreSuccss") public String confirmKiwoom(HttpServletRequest request, ModelMap model) throws Exception { - /*System.out.println("=====sopmalCd===="+request.getParameter("sopmalCd")); - System.out.println("=====mbrInnb===="+request.getParameter("mbrInnb")); - System.out.println("=====mbrId===="+request.getParameter("mbrId")); - System.out.println("=====pgCmpnyNm===="+request.getParameter("pgCmpnyNm")); - System.out.println("=====setleTyCd===="+request.getParameter("setleTyCd")); - System.out.println("=====ordrrNm===="+request.getParameter("ordrrNm")); -*/ - //return Util.getDeviceType(request) + "/ordr/OrdrReqstKiwoomPreR"; - return "sopmal/ordr/OrdrReqstKiwoomPreR"; } @@ -1458,7 +1449,7 @@ public class OrdrController { */ @Policy(AuthPolicy.RESTRICTED) @RequestMapping(value="/insertKiwoomSetle") - public String insertKiwoomSetle(ModelMap model, HttpServletRequest request) throws UnsupportedEncodingException { + public String insertKiwoomSetle(ModelMap model, HttpServletRequest request) { log.info("insertKiwoomSetle controller start ::: "); @@ -1476,11 +1467,22 @@ public class OrdrController { /*String ordrrNm = new String(StringEscapeUtils.unescapeHtml4(request.getParameter("ordrrNm")).getBytes("ISO-8859-1"),"UTF-8"); payVO.setSetlerNm(ordrrNm);*/ - String ordrrNm = request.getParameter("ordrrNm"); - payVO.setSetlerNm(new String(Base64.getDecoder().decode(ordrrNm),"UTF-8")); - log.info("insertKiwoomSetle controller payVO ::: {}", payVO.getSetlerNm()); - log.info("insertKiwoomSetle controller payVO ::: {}", payVO); + log.info("insertKiwoomSetle controller payVO 1 ::: {}", payVO); + + + String ordrrNm = request.getParameter("ordrrNm"); + log.info("insertKiwoomSetle controller payVO 1 ordrrNm ::: {}", ordrrNm); + + + try { + payVO.setSetlerNm(new String(Base64.getDecoder().decode(ordrrNm), "UTF-8")); + }catch(Exception e){ + log.debug("throws UnsupportedEncodingException:::::"); + } + + log.info("insertKiwoomSetle controller payVO 2 ::: {}", payVO.getSetlerNm()); + log.info("insertKiwoomSetle controller payVO 3 ::: {}", payVO); String agentId = ""; String jSesionId = ""; diff --git a/src/main/webapp/WEB-INF/views/sopmal/ordr/MbrPurchsC.jsp b/src/main/webapp/WEB-INF/views/sopmal/ordr/MbrPurchsC.jsp index 8ceed66f..fc68be41 100644 --- a/src/main/webapp/WEB-INF/views/sopmal/ordr/MbrPurchsC.jsp +++ b/src/main/webapp/WEB-INF/views/sopmal/ordr/MbrPurchsC.jsp @@ -3544,6 +3544,8 @@