Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

토스페이먼츠 프록시 설정 #198

Merged
merged 6 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ public CorsConfigurationSource configurationSource() {
configuration.addAllowedMethod(HttpMethod.DELETE);

configuration.addAllowedOriginPattern("http://localhost:3000");
configuration.addAllowedOriginPattern("https://k6f3d3b1a0696a.user-app.krampoline.com");
configuration.addAllowedOriginPattern("https://k5c1813d97f50a.user-app.krampoline.com"); // 프론트 테스트용
configuration.addAllowedOriginPattern("https://k0b2de7d86228a.user-app.krampoline.com");
configuration.addAllowedOriginPattern("https://k5c1813d97f50a.user-app.krampoline.com"); // 프론트

configuration.setAllowCredentials(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@
import com.kakao.sunsuwedding.user.base_user.UserJPARepository;
import com.kakao.sunsuwedding.user.constant.Grade;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.client.RestTemplate;

import java.util.*;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Base64;
import java.util.Collections;
import java.util.Objects;
import java.util.Optional;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand Down Expand Up @@ -73,29 +82,26 @@ public void approve(Long userId, PaymentRequest.ApproveDTO requestDTO) {

private void tossPayApprove(PaymentRequest.ApproveDTO requestDTO){
// 토스페이먼츠 승인 api 요청
String basicToken = "Basic " + Base64.getEncoder().encodeToString((secretKey + ":").getBytes());

WebClient webClient =
WebClient
.builder()
.baseUrl("https://api.tosspayments.com")
.build();

TossPaymentResponse.TosspayDTO result =
webClient
.post()
.uri("/v1/payments/confirm")
.headers(headers -> {
headers.add(HttpHeaders.AUTHORIZATION, basicToken);
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
})
.bodyValue(requestDTO)
.retrieve()
.bodyToMono(TossPaymentResponse.TosspayDTO.class)
.onErrorResume(e -> {
throw new ServerException(BaseException.PAYMENT_FAIL);
})
.block();
String basicToken = Base64.getEncoder().encodeToString((secretKey + ":").getBytes());

HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(basicToken);
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,
new InetSocketAddress("krmp-proxy.9rum.cc",3128));
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setProxy(proxy);
RestTemplate restTemplate = new RestTemplate(factory);

try {
restTemplate.postForEntity("https://api.tosspayments.com/v1/payments/confirm",
new HttpEntity<>(requestDTO, headers),
String.class);
} catch (Exception e) {
throw new ServerException(BaseException.PAYMENT_FAIL);
}
}

// 받아온 payment와 관련된 데이터(orderId, amount)가 정확한지 확인)
Expand Down
24 changes: 24 additions & 0 deletions sunsu-wedding/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
server:
servlet:
encoding:
charset: utf-8
force: true
port: 8080
spring:
config:
import:
- optional:classpath:/env/env.yaml

datasource:
url: ${DATABASE_URL}?allowPublicKeyRetrieval=true&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: ${MYSQL_USERNAME}
password: ${MYSQL_PASSWORD}
hibernate:
ddl-auto: none
show-sql: true
properties:
hibernate:
format_sql: true
default_batch_fetch_size: 100
open-in-view: false
Loading