package Service;
import java.net.URI;
import java.net.URISyntaxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import DTO.KakaoPayApprovalVO;
import DTO.KakaoPayReadyVO;
import lombok.extern.java.Log;
@Service
@Log
public class KakaoPay {
private static final String HOST = "https://kapi.kakao.com";
private KakaoPayReadyVO kakaoPayReadyVO;
private KakaoPayApprovalVO kakaoPayApprovalVO;
public String kakaoPayReady() {
RestTemplate restTemplate = new RestTemplate();
// 서버로 요청할 Header
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "KakaoAK " + "어드민 키");
headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_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("partner_order_id", "1001");
params.add("partner_user_id", "gorany");
params.add("item_name", "S9");
params.add("quantity", "1");
params.add("total_amount", "2100");
params.add("tax_free_amount", "100");
params.add("approval_url", "http://localhost:8080/kakaoPaySuccess");
params.add("cancel_url", "http://localhost:8080/kakaoPayCancel");
params.add("fail_url", "http://localhost:8080/kakaoPaySuccessFail");
HttpEntity<MultiValueMap<String, String>> body = new HttpEntity<MultiValueMap<String, String>>(params, headers);
try {
kakaoPayReadyVO = restTemplate.postForObject(new URI(HOST + "/v1/payment/ready"), body, KakaoPayReadyVO.class);
return kakaoPayReadyVO.getNext_redirect_pc_url();
} catch (RestClientException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "/pay";
}
public KakaoPayApprovalVO kakaoPayInfo(String pg_token) {
Logger log = LoggerFactory.getLogger(this.getClass());
log.info("KakaoPayInfoVO............................................");
log.info("-----------------------------");
RestTemplate restTemplate = new RestTemplate();
// 서버로 요청할 Header
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "KakaoAK " + "어드민 키");
headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_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", "gorany");
params.add("pg_token", pg_token);
params.add("total_amount", "2100");
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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
작성은 이렇게 되어있습니다. 401에러 해결하고 난 400에러가 발생하여 질문 남깁니다.
현재 콘솔에서 나는 에러는 안보이고 디버그 결과 값 올리겠습니다.
2019-09-05 16:51:24 [DEBUG](DispatcherServlet :865) DispatcherServlet with name ‘dispatcher’ processing POST request for [/pays/kakaoPay]
2019-09-05 16:51:24 [DEBUG](RequestMappingHandlerMapping :304) Looking up handler method for path /kakaoPay
2019-09-05 16:51:24 [DEBUG](RequestMappingHandlerMapping :311) Returning handler method [public java.lang.String Controller.SampleController.kakaoPay()]
2019-09-05 16:51:24 [DEBUG](DefaultListableBeanFactory :251) Returning cached instance of singleton bean 'Controller.SampleController#0’
2019-09-05 16:51:24 [INFO ](SampleController :35 ) kakaoPay post…
aaaaa
2019-09-05 16:51:24 [DEBUG](RestTemplate :79 ) Created POST request for "https://kapi.kakao.com/v1/payment/ready"
2019-09-05 16:51:24 [DEBUG](RestTemplate :746) Setting request Accept header to [application/json, application/*+json]
2019-09-05 16:51:24 [DEBUG](RestTemplate :841) Writing [{cid=[TC0ONETIME], partner_order_id=[1001], partner_user_id=[gorany], item_name=[S9], quantity=[1], total_amount=[2100], tax_free_amount=[100], approval_url=[http://localhost:8080/kakaoPaySuccess], cancel_url=[http://localhost:8080/kakaoPayCancel], fail_url=[http://localhost:8080/kakaoPaySuccessFail]}] as “application/x-www-form-urlencoded;charset=UTF-8” using [org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@1f12548e]
2019-09-05 16:51:24 [DEBUG](RestTemplate :658) POST request for “https://kapi.kakao.com/v1/payment/ready” resulted in 200 (OK)
2019-09-05 16:51:24 [DEBUG](HttpMessageConverterExtractor :92 ) Reading [class DTO.KakaoPayReadyVO] as “application/json;charset=UTF-8” using [org.springframework.http.converter.json.GsonHttpMessageConverter@3dcf4bfb]
2019-09-05 16:51:24 [DEBUG](ExceptionHandlerExceptionResolver :133) Resolving exception from handler [public java.lang.String Controller.SampleController.kakaoPay()]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: 2019-09-05T16:51:24; nested exception is com.google.gson.JsonSyntaxException: 2019-09-05T16:51:24
2019-09-05 16:51:24 [DEBUG](ResponseStatusExceptionResolver :133) Resolving exception from handler [public java.lang.String Controller.SampleController.kakaoPay()]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: 2019-09-05T16:51:24; nested exception is com.google.gson.JsonSyntaxException: 2019-09-05T16:51:24
2019-09-05 16:51:24 [DEBUG](DefaultHandlerExceptionResolver :133) Resolving exception from handler [public java.lang.String Controller.SampleController.kakaoPay()]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: 2019-09-05T16:51:24; nested exception is com.google.gson.JsonSyntaxException: 2019-09-05T16:51:24
2019-09-05 16:51:24 [WARN ](DefaultHandlerExceptionResolver :379) Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: 2019-09-05T16:51:24; nested exception is com.google.gson.JsonSyntaxException: 2019-09-05T16:51:24
2019-09-05 16:51:24 [DEBUG](DispatcherServlet :1044) Null ModelAndView returned to DispatcherServlet with name ‘dispatcher’: assuming HandlerAdapter completed request handling
2019-09-05 16:51:24 [DEBUG](DispatcherServlet :1000) Successfully completed request
어드민 키는 잘 입력했습니다…