안녕하세요 카카오페이 사용하려고 컨트롤러 작성하는데 오류나가 나서 문의드립니다.
아래 코드를 실행시켰을때 400이 나오고 뷰단에서 값이 undefined가 나오더라구요
위에서 다른 url로 똑같은 코드를 실행했을때는 작동을 하는데 얘는 안되는 이유를 도저히 모르겠어요…
어떤게 문제일까요…ㅠㅠ
–코드–
@PostMapping(“/buyCart”)
@ResponseBody
public String buyCart(@RequestParam(“cartNos[]”) List cartNos ,int totalSum) {
System.out.println("buyCart 카트번호들 = " + cartNos);
System.out.println("buyCart 총합계 = " + totalSum);
try {
URL url = new URL("https://kapi.kakao.com/v1/payment/ready");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
// 인증 헤더 설정
conn.setRequestProperty("Authorization", "KakaoAK 내adminkey");
conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
conn.setDoOutput(true);
String parameter = "cid=TC0ONETIME"
+ "&partner_order_id=partner_order_id"
+ "&partner_user_id=partner_user_id"
+ "&item_name=name"
+ "&quantity=n개"
+ "&total_amount=" + totalSum
+ "&tax_free_amount=0"
+ "&approval_url=http://localhost:8080/purchaseSuccess"
+ "&cancel_url=http://localhost:8080"
+ "&fail_url=http://localhost:8080";
OutputStream ops = conn.getOutputStream();
DataOutputStream dops = new DataOutputStream(ops);
dops.writeBytes(parameter);
dops.close();
int result = conn.getResponseCode();
System.out.println("result = " + result);
for(Map.Entry<String, List<String>> header : conn.getHeaderFields().entrySet()) {
for(String value : header.getValue()) {
System.out.println(header.getKey() + " : " + value);
}
}
InputStream ips;
if(result == 200) {
// 정상적인건 200 나머지는 다 에러
ips = conn.getInputStream();
}else {
ips = conn.getErrorStream();
System.out.println("200 아님");
}
InputStreamReader ipsr = new InputStreamReader(ips);
BufferedReader bdr = new BufferedReader(ipsr);
return bdr.readLine();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "{\"result\":\"NO\"}";
}
–응답코드–
null : HTTP/1.1 400 Bad Request
Server : Apache
X-Request-ID : d0e7f376-e3b2-4fe1-bf1c-30d20cbaacd9
Access-Control-Allow-Origin : *
Access-Control-Allow-Methods : GET, POST, PUT, DELETE, OPTIONS
Connection : close
Caller-AppId : 941254
Content-Length : 127
Access-Control-Allow-Headers : Content-Type,X-Requested-With,Accept,Authorization,Origin,KA,Cache-Control,Pragma
Date : Fri, 21 Jul 2023 02:15:31 GMT
Content-Type : application/json;charset=UTF-8