단편 결제 관련 코드는 다음과 같습니다.
@Service
public class KakaoPaymentServiceImpl implements PaymentService<KakaoPayRequest, KakaoPayResponseApproval>{
private KakaoPayResponseReady kakaoPayResponseReady;
private KakaoPayResponseApproval kakaoPayResponseApproval;
@Override
public String payReady(KakaoPayRequest kakaoPayRequest) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new RestTemplateErrorHandler()); // 오류 확인
HttpHeaders httpHeaders = getHttpHeaders();
/*
// 서버로 요청할 Body 정보
KakaoPayRequest kakaoPayRequest1 = KakaoPayRequest.builder()
.cid("TC0ONETIME")
.partner_order_id("1")
.partner_user_id("1")
.item_name("스터디룸")
.quantity(1)
.total_amount(2200)
.tax_free_amount(100)
.approval_url("http://localhost:8080/kakaoPaySuccess")
.cancel_url("http://localhost:8080/kakaoPayCancel")
.fail_url("http://localhost:8080/kakaoPayFail")
.build();
*/
MultiValueMap<String, String> body = MultiValueMapConverter.convert(kakaoPayRequest);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(body, httpHeaders);
try {
ResponseEntity<KakaoPayResponseReady> response = restTemplate.postForEntity(new URI(Host.KAKAO_HOST + "/v1/payment/ready"), request, KakaoPayResponseReady.class);
System.out.println("response = " + response);
kakaoPayResponseReady = response.getBody();
return kakaoPayResponseReady.getNext_redirect_pc_url();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return "/pay";
}
package com.scascanner.studycafe.web.pay.dto.kakao.response;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.time.LocalDateTime;
@Data
@ToString
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class KakaoPayResponseReady {
private String tid;
private String next_redirect_app_url;
private String next_redirect_pc_url;
private String next_redirect_mobile_url;
private String android_app_schema;
private String ios_app_scheme;
private LocalDateTime create_date;
}
서버로 부터 받은 응답을 저장할 객체
package com.scascanner.studycafe.web.pay.service;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.util.Map;
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class MultiValueMapConverter {
private static final ObjectMapper objectMapper = new ObjectMapper();
public static MultiValueMap<String, String> convert(Object request) {
try {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
Map<String, String> map = objectMapper.convertValue(request, new TypeReference<>() {
});
params.setAll(map);
return params;
} catch (Exception e) {
log.error("Url Parameter 변환중 오류가 발생했습니다. requestDto={}", request, e);
throw new IllegalStateException("Url Parameter 변환중 오류가 발생했습니다.");
}
}
}
포스트맨으로 다음과 같이 응답을 보냈습니다.
콘솔을 확인해보면 다음과 같이 restTemplate을 사용하여 응답 결과를 받은 객체에 아무것도 담기지 않습니다.
자바 스프링부트를 사용하여 개발중이며 어디가 문제인지 모르겠습니다ㅜㅜ