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 java.time.Duration;
import java.util.Base64;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.ClientHttpRequestFactories;
import org.springframework.boot.web.client.ClientHttpRequestFactorySettings;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.web.client.RestClient;
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.client.RestClient
import java.nio.charset.StandardCharsets
import java.time.Duration
import java.util.*
@Configuration
@EnableConfigurationProperties(PaymentProperties.class)
public class PaymentConfig {
@EnableConfigurationProperties(PaymentProperties::class)
class PaymentConfig {
@Bean
public RestClient.Builder restClientBuilder(PaymentProperties paymentProperties) {
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.DEFAULTS
.withReadTimeout(Duration.ofSeconds(paymentProperties.getReadTimeout()))
.withConnectTimeout(Duration.ofSeconds(paymentProperties.getConnectTimeout()));
ClientHttpRequestFactory requestFactory = ClientHttpRequestFactories.get(settings);
fun paymentClient(
paymentProperties: PaymentProperties,
restClientBuilder: RestClient.Builder
): RestClient {
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")
.defaultHeader("Authorization", getAuthorizations(paymentProperties.getConfirmSecretKey()))
.requestFactory(requestFactory);
return restClientBuilder
.baseUrl(paymentProperties.apiBaseUrl)
.defaultHeader("Authorization", getAuthorizations(paymentProperties.confirmSecretKey))
.requestFactory(requestFactory)
.build()
}
private String getAuthorizations(String secretKey) {
Base64.Encoder encoder = Base64.getEncoder();
byte[] encodedBytes = encoder.encode((secretKey + ":").getBytes(StandardCharsets.UTF_8));
return "Basic " + new String(encodedBytes);
private fun getAuthorizations(secretKey: String): String {
val encodedSecretKey = Base64.getEncoder()
.encodeToString("$secretKey:".toByteArray(StandardCharsets.UTF_8))
return "Basic $encodedSecretKey"
}
}

View File

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