refactor: PaymentConfig 코틀린 마이그레이션 및 RestClient를 반환하도록 수정

This commit is contained in:
이상진 2025-07-15 18:32:32 +09:00
parent 6aba38a9ab
commit 199cbe573f
2 changed files with 39 additions and 34 deletions

View File

@ -1,36 +1,41 @@
package roomescape.payment.infrastructure.client; package roomescape.payment.infrastructure.client
import java.nio.charset.StandardCharsets; import org.springframework.boot.context.properties.EnableConfigurationProperties
import java.time.Duration; import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder
import java.util.Base64; import org.springframework.boot.http.client.ClientHttpRequestFactorySettings
import org.springframework.context.annotation.Bean
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration
import org.springframework.boot.web.client.ClientHttpRequestFactories; import org.springframework.web.client.RestClient
import org.springframework.boot.web.client.ClientHttpRequestFactorySettings; import java.nio.charset.StandardCharsets
import org.springframework.context.annotation.Bean; import java.time.Duration
import org.springframework.context.annotation.Configuration; import java.util.*
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.web.client.RestClient;
@Configuration @Configuration
@EnableConfigurationProperties(PaymentProperties.class) @EnableConfigurationProperties(PaymentProperties::class)
public class PaymentConfig { class PaymentConfig {
@Bean @Bean
public RestClient.Builder restClientBuilder(PaymentProperties paymentProperties) { fun paymentClient(
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.DEFAULTS paymentProperties: PaymentProperties,
.withReadTimeout(Duration.ofSeconds(paymentProperties.getReadTimeout())) restClientBuilder: RestClient.Builder
.withConnectTimeout(Duration.ofSeconds(paymentProperties.getConnectTimeout())); ): RestClient {
ClientHttpRequestFactory requestFactory = ClientHttpRequestFactories.get(settings); val settings: ClientHttpRequestFactorySettings = ClientHttpRequestFactorySettings.defaults().also {
it.withReadTimeout(Duration.ofSeconds(paymentProperties.readTimeout.toLong()))
it.withConnectTimeout(Duration.ofSeconds(paymentProperties.connectTimeout.toLong()))
}
val requestFactory = ClientHttpRequestFactoryBuilder.jdk().build(settings)
return RestClient.builder().baseUrl("https://api.tosspayments.com") return restClientBuilder
.defaultHeader("Authorization", getAuthorizations(paymentProperties.getConfirmSecretKey())) .baseUrl(paymentProperties.apiBaseUrl)
.requestFactory(requestFactory); .defaultHeader("Authorization", getAuthorizations(paymentProperties.confirmSecretKey))
} .requestFactory(requestFactory)
.build()
}
private String getAuthorizations(String secretKey) { private fun getAuthorizations(secretKey: String): String {
Base64.Encoder encoder = Base64.getEncoder(); val encodedSecretKey = Base64.getEncoder()
byte[] encodedBytes = encoder.encode((secretKey + ":").getBytes(StandardCharsets.UTF_8)); .encodeToString("$secretKey:".toByteArray(StandardCharsets.UTF_8))
return "Basic " + new String(encodedBytes);
} return "Basic $encodedSecretKey"
}
} }

View File

@ -27,15 +27,15 @@ public class TossPaymentClient {
private static final Logger log = LoggerFactory.getLogger(TossPaymentClient.class); private static final Logger log = LoggerFactory.getLogger(TossPaymentClient.class);
private final RestClient restClient; private final RestClient paymentClient;
public TossPaymentClient(RestClient.Builder restClientBuilder) { public TossPaymentClient(RestClient paymentClient) {
this.restClient = restClientBuilder.build(); this.paymentClient = paymentClient;
} }
public PaymentResponse confirmPayment(PaymentRequest paymentRequest) { public PaymentResponse confirmPayment(PaymentRequest paymentRequest) {
logPaymentInfo(paymentRequest); logPaymentInfo(paymentRequest);
return restClient.post() return paymentClient.post()
.uri("/v1/payments/confirm") .uri("/v1/payments/confirm")
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.body(paymentRequest) .body(paymentRequest)
@ -49,7 +49,7 @@ public class TossPaymentClient {
logPaymentCancelInfo(cancelRequest); logPaymentCancelInfo(cancelRequest);
Map<String, String> param = Map.of("cancelReason", cancelRequest.cancelReason()); Map<String, String> param = Map.of("cancelReason", cancelRequest.cancelReason());
return restClient.post() return paymentClient.post()
.uri("/v1/payments/{paymentKey}/cancel", cancelRequest.paymentKey()) .uri("/v1/payments/{paymentKey}/cancel", cancelRequest.paymentKey())
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.body(param) .body(param)