보라코딩
스프링부트 iamport 이니시스 환불하기!! 본문

실제 환불된다!!!
근데 왜 아임포트에서는 안보일까...?
html
<button onClick="cancelPay()" class="iamport">iamport 환불</button>
function cancelPay() {
$.ajax({
"url": "/pay/inicisCancel",
"type": "POST",
"contentType": "application/json",
"data": JSON.stringify({
"merchant_uid": "merchant_1686295852827",
"cancel_request_amount": 123, // 환불금액
"reason": "테스트 결제 환불" // 환불사유
}),
"dataType": "json"
});
}
$.ajax({
"url": "/pay/inicisCancel",
"type": "POST",
"contentType": "application/json",
"data": JSON.stringify({
"merchant_uid": "merchant_1686295852827",
"cancel_request_amount": 123, // 환불금액
"reason": "테스트 결제 환불" // 환불사유
}),
"dataType": "json"
});
}
Controller
@PostMapping("/inicisCancel")
@ResponseBody
public void inicisCancel(@RequestBody InicisRefundVO inicisRefundVO) throws IOException {
log.info("inicisCancel!!!!!!!!!!!!!!! : " + inicisRefundVO);
//토큰 DB에서 가져오기
String token = "토큰값 취소할거 넣기!!!!!";
paymentService.inicisRefund(inicisRefundVO, token);
log.info("controller 환불 완료!!!");
}
@ResponseBody
public void inicisCancel(@RequestBody InicisRefundVO inicisRefundVO) throws IOException {
log.info("inicisCancel!!!!!!!!!!!!!!! : " + inicisRefundVO);
//토큰 DB에서 가져오기
String token = "토큰값 취소할거 넣기!!!!!";
paymentService.inicisRefund(inicisRefundVO, token);
log.info("controller 환불 완료!!!");
}
Service
public void inicisRefund(InicisRefundVO inicisRefundVO, String token) throws IOException {
String cancelUrl = "https://api.iamport.kr/payments/cancel";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", token);
RestTemplate restTemplate = new RestTemplate();
JSONObject requestData = new JSONObject();
requestData.put("reason", inicisRefundVO.getReason());
requestData.put("merchant_uid", inicisRefundVO.getMerchant_uid());
requestData.put("imp_uid", "imp_688853734311"); // DB값 사용하기
requestData.put("amount", inicisRefundVO.getCancel_request_amount());
HttpEntity<String> requestEntity = new HttpEntity<>(requestData.toString(), headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(cancelUrl, HttpMethod.POST, requestEntity, String.class);
if (responseEntity.getStatusCode().is2xxSuccessful()) {
log.info("으어어어?");
String responseData = responseEntity.getBody();
log.info("responseData?"+responseData);
//JSONObject responseJson = new JSONObject();
// JSONObject response = responseJson.getJSONObject("response");
// 처리 결과에 대한 작업 수행
} else {
// 요청 실패에 대한 작업 수행
}
}
String cancelUrl = "https://api.iamport.kr/payments/cancel";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", token);
RestTemplate restTemplate = new RestTemplate();
JSONObject requestData = new JSONObject();
requestData.put("reason", inicisRefundVO.getReason());
requestData.put("merchant_uid", inicisRefundVO.getMerchant_uid());
requestData.put("imp_uid", "imp_688853734311"); // DB값 사용하기
requestData.put("amount", inicisRefundVO.getCancel_request_amount());
HttpEntity<String> requestEntity = new HttpEntity<>(requestData.toString(), headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(cancelUrl, HttpMethod.POST, requestEntity, String.class);
if (responseEntity.getStatusCode().is2xxSuccessful()) {
log.info("으어어어?");
String responseData = responseEntity.getBody();
log.info("responseData?"+responseData);
//JSONObject responseJson = new JSONObject();
// JSONObject response = responseJson.getJSONObject("response");
// 처리 결과에 대한 작업 수행
} else {
// 요청 실패에 대한 작업 수행
}
}
InicisRefundVO
package com.tastemate.domain;
import lombok.Data;
import org.apache.ibatis.type.Alias;
@Data
@Alias("InicisRefundVO")
public class InicisRefundVO {
private String merchant_uid;
private String cancel_request_amount;
private String reason;
}
import lombok.Data;
import org.apache.ibatis.type.Alias;
@Data
@Alias("InicisRefundVO")
public class InicisRefundVO {
private String merchant_uid;
private String cancel_request_amount;
private String reason;
}
'코딩 > Spring' 카테고리의 다른 글
스프링부트 페이징처리 + 별점순/인기순/거리순 + 검색기능 + join까지.. (1) | 2023.06.10 |
---|---|
깔끔한 댓글창 (부트스트랩) (1) | 2023.06.09 |
Day113_230608 스프링부트 iamport 이니시스 통합인증(토큰받기) (0) | 2023.06.08 |
스프링부트 카카오 API 로그인 (0) | 2023.06.06 |
알아서 잘 딱 깔끔하고 센스있게 정리하는 GitHub 핵심 개념 (2) | 2023.06.06 |