400 BAD_REQUEST 오류 확인 요청의 건

image

“msg”:“cid can’t be null.”,“code”:-2
결제 승인을 진행할 때 다음과 같이 cid null 오류가 발생합니다. 사진의 에러 코드 함께 전달드립니다.
HTTP POST https://kapi.kakao.com/v1/payment/approve
2024-03-20T11:30:05.983+09:00 DEBUG 20016 — [nio-8080-exec-3] o.s.web.client.RestTemplate : Accept=[application/json, application/cbor, application/+json]
2024-03-20T11:30:05.984+09:00 DEBUG 20016 — [nio-8080-exec-3] o.s.web.client.RestTemplate : Writing [{partner_order_id=140, quantity=1, total_amount=1000, tax_free_amount=0, pg_token=43c80328f08af7d931d5, partner_user_id=2, item_name=test item, tid=T5fa4a110f9c719a1692, cid=TC0ONETIME}] as “application/json”
2024-03-20T11:30:06.038+09:00 DEBUG 20016 — [nio-8080-exec-3] o.s.web.client.RestTemplate : Response 400 BAD_REQUEST
2024-03-20T11:30:06.041+09:00 DEBUG 20016 — [nio-8080-exec-3] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using ‘text/html’, given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, /;q=0.8, application/signed-exchange;v=b3;q=0.7] and supported [text/plain, /, application/json, application/
+json, application/cbor]
2024-03-20T11:30:06.042+09:00 DEBUG 20016 — [nio-8080-exec-3] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [“Payment approval failed: 400 Bad Request: “{“msg”:“cid can’t be null.”,“code”:-2}””]
2024-03-20T11:30:06.043+09:00 DEBUG 20016 — [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Completed 400 BAD_REQUEST

관련 서비스 코드는 다음과 같습니다.

public PaymentResponse approvePayment(String pgToken, String partnerOrderId, String partnerUserId) throws Exception {

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "KakaoAK " + kakaoPayAuthorization);
    headers.setContentType(MediaType.APPLICATION_JSON);

    System.out.println("~~~~~~~~~~~~~~~~~~~approvePayment 1");

    CustomerOrder order = customerOrderProductMapper.findByPartnerOrderId(partnerOrderId);
    if (order == null) {
        throw new Exception("Order not found with partner_order_id: " + partnerOrderId);
    }
    String tid = order.getTid();

    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~approvePayment 2");

    // JSON 형식으로 요청 바디 구성
    Map<String, Object> requestBody = new HashMap<>();
    requestBody.put("cid", "TC0ONETIME");
    requestBody.put("tid", tid);
    requestBody.put("pg_token", pgToken);
    requestBody.put("partner_order_id", 140);
    requestBody.put("partner_user_id", 2);
    requestBody.put("item_name", "test item");
    requestBody.put("quantity", 1);
    requestBody.put("total_amount", 1000);
    requestBody.put("tax_free_amount", 0);

    HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestBody, headers);
    ResponseEntity<PaymentResponse> responseEntity = restTemplate.exchange(
            kakaoPayApproveUrl, HttpMethod.POST, entity, PaymentResponse.class);

    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~approvePayment 3");

    return responseEntity.getBody();
}

관련 컨트롤러 코드는 다음과 같습니다.

@GetMapping("/approve")
public ResponseEntity<?> approvePayment(@RequestParam String pg_token,
                                        @RequestParam String partner_order_id,
                                        @RequestParam String partner_user_id) {
    try {
        PaymentResponse paymentResponse = paymentService.approvePayment(pg_token, partner_order_id, partner_user_id);
        return ResponseEntity.ok(paymentResponse);
    } catch (Exception e) {
        return ResponseEntity.badRequest().body("Payment approval failed: " + e.getMessage());
    }
}

안녕하세요 카카오페이 온라인결제 입니다.

가맹점의 개발 환경 및 로직은 가맹점별로 상이하여 가맹점의 소스코드 상의 오류까지는 상세한 기술 지원이 어려운 점 양해 부탁 드립니다.

개발 테스트 진행 중 발생하는 오류에 대하여는 당 사 로그를 기준으로 답변 드리고 있으며, 문의 주신 ‘cid can’t be null’ 오류의 경우 필수 파라미터인 cid의 값이 올라오지 않은 경우 발생하는 오류이나 간혹 데이터 포맷오류로 값을 인식할 수 없는 경우에도 발생할 수 있습니다.

현재 카카오페이 가맹점은 기존 api와 신규 api를 사용하는 곳이 혼재되어 있는데요,
api 별로 content type이 상이하여 사용하시는 api에 맞는 content type이 올바르게 사용되었는 지 확인 부탁 드립니다.

더불어 전달주신 내용 상의 도메인은 기존 api 의 도메인으로 신규 가맹점인 경우 신규 api 로만 연동이 가능한 점 함께 참고 부탁 드립니다.

가맹점 설정 및 CID 등록 정보 등은 오픈된 게시판에서 답변드리기 어려워 관련하여 추가적인 문의가 있으신 경우 가맹점 정보 (사업자번호 및 CID) 와 함께 카카오페이 온라인 지원 그룹메일 kakaopay_pg@kakaopaycorp.com 으로 문의 주시기 바랍니다.

감사합니다.