예전에 사용하던 카카오페이 코드가 제대로 작동을 안하길래 찾아보니
https://devtalk.kakao.com/t/api/134037
24년 2월 이후로 API가 새로 바뀌었다
이에따라서 service 코드를 약간 수정해주었다
기존 API service코드 (24년 2월 이전 구 코드)
host주소와 header부분(secret키와 json방식인코딩) 그리고 LinkedMultiValueMap대신 HashMap을 사용하게 바뀐듯
public class KakaoPayService {
private static final String HOST = "https://kapi.kakao.com";
// 카카오 Admin 키
private static final String kakaoKey = "카카오AK키";
private KakaoPayReadyVO kakaoPayReadyVO;
private KakaoPayApprovalVO kakaoPayApprovalVO;
public String kakaoPayReady(Map<String, String> param) {
RestTemplate restTemplate = new RestTemplate();
// 서버로 요청할 Header
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "KakaoAK " + kakaoKey);
headers.add("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
// 서버로 요청할 Body
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
params.add("cid", "TC0ONETIME");
params.add("partner_order_id", "1001");
params.add("partner_user_id", "test12");
params.add("item_name", param.get("item_name"));
params.add("quantity", param.get("quantity"));
params.add("total_amount", param.get("total_amount"));
params.add("tax_free_amount", "500");
params.add("approval_url", "http://localhost/mvc/kakaoPaySuccess");
params.add("cancel_url", "http://localhost/mvc/kakaoPayCancel");
params.add("fail_url", "http://localhost/mvc/kakaoPaySuccessFail");
params.add("msg", "결제가 완료되었습니다.");
HttpEntity<MultiValueMap<String, String>> body = new HttpEntity<MultiValueMap<String, String>>(params, headers);
System.out.println(body);
try {
kakaoPayReadyVO = restTemplate.postForObject(new URI(HOST + "/v1/payment/ready"), body,
KakaoPayReadyVO.class);
log.info("" + kakaoPayReadyVO);
return kakaoPayReadyVO.getNext_redirect_pc_url();
} catch (RestClientException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return "/";
}
public KakaoPayApprovalVO kakaoPayInfo(String pg_token, Map<String, String> param) {
log.info("KakaoPayInfoVO............................................");
log.info("-----------------------------");
RestTemplate restTemplate = new RestTemplate();
// 서버로 요청할 Header
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "KakaoAK " + kakaoKey);
headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8");
// 서버로 요청할 Body
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
params.add("cid", "TC0ONETIME");
params.add("tid", kakaoPayReadyVO.getTid());
params.add("partner_order_id", "1001");
params.add("partner_user_id", "test12");
params.add("pg_token", pg_token);
params.add("total_amount", param.get("total_amount"));
HttpEntity<MultiValueMap<String, String>> body = new HttpEntity<MultiValueMap<String, String>>(params, headers);
try {
kakaoPayApprovalVO = restTemplate.postForObject(new URI(HOST + "/v1/payment/approve"), body,
KakaoPayApprovalVO.class);
log.info("" + kakaoPayApprovalVO);
return kakaoPayApprovalVO;
} catch (RestClientException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
수정 API service코드 (24년 2월 이후)
@Log4j2
@Service
public class KakaoPayService {
private static final String HOST = "https://open-api.kakaopay.com/online";
@Value("${kakao.kakaopay.secretKeyDev}")
private String secretKey;
private KakaoPayReadyVO kakaoPayReadyVO;
private KakaoPayApprovalVO kakaoPayApprovalVO;
public String kakaoPayReady(Map<String, String> param) {
RestTemplate restTemplate = new RestTemplate();
// 서버로 요청할 Header
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "SECRET_KEY " + secretKey);
headers.add("Content-type", "application/json");
// headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
// 서버로 요청할 Body
Map<String, String> params = new HashMap<String, String>();
params.put("cid", "TC0ONETIME");
params.put("partner_order_id", "1002");
params.put("partner_user_id", "Atriel");
params.put("item_name", param.get("item_name"));
params.put("quantity", param.get("quantity"));
params.put("total_amount", param.get("total_amount"));
params.put("tax_free_amount", "500");
params.put("approval_url", "http://localhost/kakao/kakaoPaySuccess");
params.put("cancel_url", "http://localhost/kakao/kakaoPayCancel");
params.put("fail_url", "http://localhost/kakao/kakaoPaySuccessFail");
params.put("msg", "결제가 완료되었습니다.");
HttpEntity<Map<String, String>> body = new HttpEntity<>(params, headers); //new HttpEntity<Map<String, String>>(params, headers);
System.out.println("dbg1 body: " + body);
try {
kakaoPayReadyVO = restTemplate.postForObject(new URI(HOST + "/v1/payment/ready"), body,
KakaoPayReadyVO.class);
log.info("dbg2: " + kakaoPayReadyVO);
return kakaoPayReadyVO.getNext_redirect_pc_url();
} catch (RestClientException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return "/";
}
public KakaoPayApprovalVO kakaoPayInfo(String pg_token, Map<String, String> param) {
log.info("KakaoPayInfoVO............................................");
log.info("-----------------------------");
RestTemplate restTemplate = new RestTemplate();
// 서버로 요청할 Header
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "SECRET_KEY " + secretKey);
headers.add("Content-type", "application/json");
// headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
// headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8");
// 서버로 요청할 Body
Map<String, String> params = new HashMap<String, String>();
params.put("cid", "TC0ONETIME");
params.put("tid", kakaoPayReadyVO.getTid());
params.put("partner_order_id", "1002");
params.put("partner_user_id", "Atriel");
params.put("pg_token", pg_token);
params.put("total_amount", param.get("total_amount"));
HttpEntity<Map<String, String>> body = new HttpEntity<>(params, headers);
try {
kakaoPayApprovalVO = restTemplate.postForObject(new URI(HOST + "/v1/payment/approve"), body,
KakaoPayApprovalVO.class);
log.info("" + kakaoPayApprovalVO);
return kakaoPayApprovalVO;
} catch (RestClientException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
}