Skip to content
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
12 changes: 12 additions & 0 deletions apps/commerce-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:${project.properties["springDocOpenApiVersion"]}")

// feign client
implementation("org.springframework.cloud:spring-cloud-starter-openfeign")

// resilience4j
implementation("io.github.resilience4j:resilience4j-spring-boot3")
implementation("io.github.resilience4j:resilience4j-core") // IntervalFunction์„ ์œ„ํ•œ core ๋ชจ๋“ˆ
implementation("io.github.resilience4j:resilience4j-circuitbreaker")
implementation("io.github.resilience4j:resilience4j-retry")
implementation("io.github.resilience4j:resilience4j-timelimiter")
implementation("io.github.resilience4j:resilience4j-bulkhead") // Bulkheads ํŒจํ„ด ๊ตฌํ˜„
implementation("org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j")

// batch
implementation("org.springframework.boot:spring-boot-starter-batch")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;
import java.util.TimeZone;

@ConfigurationPropertiesScan
@SpringBootApplication
@EnableScheduling
@EnableFeignClients
public class CommerceApiApplication {

@PostConstruct
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.loopers.application.purchasing;

import com.loopers.domain.order.Order;
import com.loopers.domain.order.OrderRepository;
import com.loopers.domain.order.OrderStatus;
import com.loopers.infrastructure.paymentgateway.PaymentGatewayClient;
import com.loopers.infrastructure.paymentgateway.PaymentGatewayDto;
import com.loopers.infrastructure.user.UserJpaRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* ๊ฒฐ์ œ ์ƒํƒœ ๋ณต๊ตฌ ์Šค์ผ€์ค„๋Ÿฌ.
* <p>
* ์ฝœ๋ฐฑ์ด ์˜ค์ง€ ์•Š์€ PENDING ์ƒํƒœ์˜ ์ฃผ๋ฌธ๋“ค์„ ์ฃผ๊ธฐ์ ์œผ๋กœ ์กฐํšŒํ•˜์—ฌ
* PG ์‹œ์Šคํ…œ์˜ ๊ฒฐ์ œ ์ƒํƒœ ํ™•์ธ API๋ฅผ ํ†ตํ•ด ์ƒํƒœ๋ฅผ ๋ณต๊ตฌํ•ฉ๋‹ˆ๋‹ค.
* </p>
* <p>
* <b>๋™์ž‘ ์›๋ฆฌ:</b>
* <ol>
* <li>์ฃผ๊ธฐ์ ์œผ๋กœ ์‹คํ–‰ (๊ธฐ๋ณธ: 1๋ถ„๋งˆ๋‹ค)</li>
* <li>PENDING ์ƒํƒœ์ธ ์ฃผ๋ฌธ๋“ค์„ ์กฐํšŒ</li>
* <li>๊ฐ ์ฃผ๋ฌธ์— ๋Œ€ํ•ด PG ๊ฒฐ์ œ ์ƒํƒœ ํ™•์ธ API ํ˜ธ์ถœ</li>
* <li>๊ฒฐ์ œ ์ƒํƒœ์— ๋”ฐ๋ผ ์ฃผ๋ฌธ ์ƒํƒœ ์—…๋ฐ์ดํŠธ</li>
* </ol>
* </p>
* <p>
* <b>์„ค๊ณ„ ๊ทผ๊ฑฐ:</b>
* <ul>
* <li><b>์ฃผ๊ธฐ์  ๋ณต๊ตฌ:</b> ์ฝœ๋ฐฑ์ด ์˜ค์ง€ ์•Š์•„๋„ ์ž๋™์œผ๋กœ ์ƒํƒœ ๋ณต๊ตฌ</li>
* <li><b>Eventually Consistent:</b> ์•ฝ๊ฐ„์˜ ์ง€์—ฐ ํ—ˆ์šฉ ๊ฐ€๋Šฅ</li>
* <li><b>์•ˆ์ „ํ•œ ์ฒ˜๋ฆฌ:</b> ๊ฐ ์ฃผ๋ฌธ๋ณ„๋กœ ๋…๋ฆฝ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜์—ฌ ์‹คํŒจ ์‹œ์—๋„ ๋‹ค๋ฅธ ์ฃผ๋ฌธ์— ์˜ํ–ฅ ์—†์Œ</li>
* <li><b>์„ฑ๋Šฅ ๊ณ ๋ ค:</b> ๋ฐฐ์น˜๋กœ ์ฒ˜๋ฆฌํ•˜์—ฌ PG ์‹œ์Šคํ…œ ๋ถ€ํ•˜ ์ตœ์†Œํ™”</li>
* </ul>
* </p>
*
* @author Loopers
* @version 1.0
*/
@Slf4j
@RequiredArgsConstructor
@Component
public class PaymentRecoveryScheduler {

private final OrderRepository orderRepository;
private final UserJpaRepository userJpaRepository;
private final PaymentGatewayClient paymentGatewayClient;
private final PurchasingFacade purchasingFacade;

/**
* PENDING ์ƒํƒœ์ธ ์ฃผ๋ฌธ๋“ค์˜ ๊ฒฐ์ œ ์ƒํƒœ๋ฅผ ๋ณต๊ตฌํ•ฉ๋‹ˆ๋‹ค.
* <p>
* 1๋ถ„๋งˆ๋‹ค ์‹คํ–‰๋˜์–ด PENDING ์ƒํƒœ์ธ ์ฃผ๋ฌธ๋“ค์„ ์กฐํšŒํ•˜๊ณ ,
* ๊ฐ ์ฃผ๋ฌธ์— ๋Œ€ํ•ด PG ๊ฒฐ์ œ ์ƒํƒœ ํ™•์ธ API๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ƒํƒœ๋ฅผ ๋ณต๊ตฌํ•ฉ๋‹ˆ๋‹ค.
* </p>
* <p>
* <b>์ฒ˜๋ฆฌ ์ „๋žต:</b>
* <ul>
* <li><b>๋ฐฐ์น˜ ์ฒ˜๋ฆฌ:</b> ํ•œ ๋ฒˆ์— ์—ฌ๋Ÿฌ ์ฃผ๋ฌธ ์ฒ˜๋ฆฌ</li>
* <li><b>๋…๋ฆฝ์  ์ฒ˜๋ฆฌ:</b> ๊ฐ ์ฃผ๋ฌธ๋ณ„๋กœ ๋…๋ฆฝ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜์—ฌ ์‹คํŒจ ์‹œ์—๋„ ๋‹ค๋ฅธ ์ฃผ๋ฌธ์— ์˜ํ–ฅ ์—†์Œ</li>
* <li><b>์•ˆ์ „ํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ:</b> ๊ฐœ๋ณ„ ์ฃผ๋ฌธ ์ฒ˜๋ฆฌ ์‹คํŒจ ์‹œ์—๋„ ๊ณ„์† ์ง„ํ–‰</li>
* </ul>
* </p>
*/
@Scheduled(fixedDelay = 60000) // 1๋ถ„๋งˆ๋‹ค ์‹คํ–‰
public void recoverPendingOrders() {
try {
log.debug("๊ฒฐ์ œ ์ƒํƒœ ๋ณต๊ตฌ ์Šค์ผ€์ค„๋Ÿฌ ์‹œ์ž‘");

// PENDING ์ƒํƒœ์ธ ์ฃผ๋ฌธ๋“ค ์กฐํšŒ
List<Order> pendingOrders = orderRepository.findAllByStatus(OrderStatus.PENDING);

if (pendingOrders.isEmpty()) {
log.debug("๋ณต๊ตฌํ•  PENDING ์ƒํƒœ ์ฃผ๋ฌธ์ด ์—†์Šต๋‹ˆ๋‹ค.");
return;
}

log.info("PENDING ์ƒํƒœ ์ฃผ๋ฌธ {}๊ฑด์— ๋Œ€ํ•œ ๊ฒฐ์ œ ์ƒํƒœ ๋ณต๊ตฌ ์‹œ์ž‘", pendingOrders.size());

int successCount = 0;
int failureCount = 0;

// ๊ฐ ์ฃผ๋ฌธ์— ๋Œ€ํ•ด ๊ฒฐ์ œ ์ƒํƒœ ํ™•์ธ ๋ฐ ๋ณต๊ตฌ
for (Order order : pendingOrders) {
try {
// Order์˜ userId๋Š” User์˜ id (Long)์ด๋ฏ€๋กœ User๋ฅผ ์กฐํšŒํ•˜์—ฌ userId (String)๋ฅผ ๊ฐ€์ ธ์˜ด
var userOptional = userJpaRepository.findById(order.getUserId());
if (userOptional.isEmpty()) {
log.warn("์ฃผ๋ฌธ์˜ ์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ณต๊ตฌ๋ฅผ ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค. (orderId: {}, userId: {})",
order.getId(), order.getUserId());
failureCount++;
continue;
}

String userId = userOptional.get().getUserId();

// ๊ฒฐ์ œ ์ƒํƒœ ํ™•์ธ ๋ฐ ๋ณต๊ตฌ
purchasingFacade.recoverOrderStatusByPaymentCheck(userId, order.getId());
successCount++;
} catch (Exception e) {
// ๊ฐœ๋ณ„ ์ฃผ๋ฌธ ์ฒ˜๋ฆฌ ์‹คํŒจ ์‹œ์—๋„ ๊ณ„์† ์ง„ํ–‰
log.error("์ฃผ๋ฌธ ์ƒํƒœ ๋ณต๊ตฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ. (orderId: {})", order.getId(), e);
failureCount++;
}
}

log.info("๊ฒฐ์ œ ์ƒํƒœ ๋ณต๊ตฌ ์™„๋ฃŒ. ์„ฑ๊ณต: {}๊ฑด, ์‹คํŒจ: {}๊ฑด", successCount, failureCount);

} catch (Exception e) {
log.error("๊ฒฐ์ œ ์ƒํƒœ ๋ณต๊ตฌ ์Šค์ผ€์ค„๋Ÿฌ ์‹คํ–‰ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ", e);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.loopers.application.purchasing;

import com.loopers.domain.order.OrderStatusUpdater;
import com.loopers.infrastructure.paymentgateway.DelayProvider;
import com.loopers.infrastructure.paymentgateway.PaymentGatewayAdapter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.Duration;

/**
* ๊ฒฐ์ œ ๋ณต๊ตฌ ์„œ๋น„์Šค.
* <p>
* ํƒ€์ž„์•„์›ƒ ๋ฐœ์ƒ ํ›„ ๊ฒฐ์ œ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜์—ฌ ์ฃผ๋ฌธ ์ƒํƒœ๋ฅผ ๋ณต๊ตฌํ•ฉ๋‹ˆ๋‹ค.
* </p>
*
* @author Loopers
* @version 1.0
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class PaymentRecoveryService {

private final PaymentGatewayAdapter paymentGatewayAdapter;
private final OrderStatusUpdater orderStatusUpdater;
private final DelayProvider delayProvider;

/**
* ํƒ€์ž„์•„์›ƒ ๋ฐœ์ƒ ํ›„ ๊ฒฐ์ œ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜์—ฌ ์ฃผ๋ฌธ ์ƒํƒœ๋ฅผ ๋ณต๊ตฌํ•ฉ๋‹ˆ๋‹ค.
* <p>
* ํƒ€์ž„์•„์›ƒ์€ ์š”์ฒญ์ด ์ „์†ก๋˜์—ˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ์‹ค์ œ ๊ฒฐ์ œ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜์—ฌ
* ๊ฒฐ์ œ๊ฐ€ ์„ฑ๊ณตํ–ˆ๋‹ค๋ฉด ์ฃผ๋ฌธ์„ ์™„๋ฃŒํ•˜๊ณ , ์‹คํŒจํ–ˆ๋‹ค๋ฉด ์ฃผ๋ฌธ์„ ์ทจ์†Œํ•ฉ๋‹ˆ๋‹ค.
* </p>
*
* @param userId ์‚ฌ์šฉ์ž ID
* @param orderId ์ฃผ๋ฌธ ID
*/
public void recoverAfterTimeout(String userId, Long orderId) {
try {
// ์ž ์‹œ ๋Œ€๊ธฐ ํ›„ ์ƒํƒœ ํ™•์ธ (PG ์ฒ˜๋ฆฌ ์‹œ๊ฐ„ ๊ณ ๋ ค)
// ํƒ€์ž„์•„์›ƒ์ด ๋ฐœ์ƒํ–ˆ์ง€๋งŒ ์š”์ฒญ์€ ์ „์†ก๋˜์—ˆ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ,
// PG ์‹œ์Šคํ…œ์ด ์ฒ˜๋ฆฌํ•  ์‹œ๊ฐ„์„ ์ฃผ๊ธฐ ์œ„ํ•ด ์งง์€ ๋Œ€๊ธฐ
delayProvider.delay(Duration.ofSeconds(1));

// PG์—์„œ ์ฃผ๋ฌธ๋ณ„ ๊ฒฐ์ œ ์ •๋ณด ์กฐํšŒ
var status = paymentGatewayAdapter.getPaymentStatus(userId, String.valueOf(orderId));

// ๋ณ„๋„ ํŠธ๋žœ์žญ์…˜์œผ๋กœ ์ƒํƒœ ์—…๋ฐ์ดํŠธ
orderStatusUpdater.updateByPaymentStatus(orderId, status, null, null);

} catch (InterruptedException e) {
Thread.currentThread().interrupt();
log.warn("ํƒ€์ž„์•„์›ƒ ํ›„ ์ƒํƒœ ํ™•์ธ ์ค‘ ์ธํ„ฐ๋ŸฝํŠธ ๋ฐœ์ƒ. (orderId: {})", orderId);
} catch (Exception e) {
// ๊ธฐํƒ€ ์˜ค๋ฅ˜: ๋‚˜์ค‘์— ์Šค์ผ€์ค„๋Ÿฌ๋กœ ๋ณต๊ตฌ ๊ฐ€๋Šฅ
log.error("ํƒ€์ž„์•„์›ƒ ํ›„ ์ƒํƒœ ํ™•์ธ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ. ๋‚˜์ค‘์— ์Šค์ผ€์ค„๋Ÿฌ๋กœ ๋ณต๊ตฌ๋ฉ๋‹ˆ๋‹ค. (orderId: {})", orderId, e);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.loopers.application.purchasing;

import com.loopers.infrastructure.paymentgateway.PaymentGatewayDto;

/**
* ๊ฒฐ์ œ ์š”์ฒญ ๋„๋ฉ”์ธ ๋ชจ๋ธ.
*
* @author Loopers
* @version 1.0
*/
public record PaymentRequest(
String userId,
String orderId,
PaymentGatewayDto.CardType cardType,
String cardNo,
Long amount,
String callbackUrl
) {
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.loopers.application.purchasing;

import com.loopers.infrastructure.paymentgateway.PaymentGatewayDto;
import com.loopers.support.error.CoreException;
import com.loopers.support.error.ErrorType;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
* ๊ฒฐ์ œ ์š”์ฒญ ๋นŒ๋”.
* <p>
* ๊ฒฐ์ œ ์š”์ฒญ ๋„๋ฉ”์ธ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
* </p>
*
* @author Loopers
* @version 1.0
*/
@Component
@RequiredArgsConstructor
public class PaymentRequestBuilder {

@Value("${server.port:8080}")
private int serverPort;

/**
* ๊ฒฐ์ œ ์š”์ฒญ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
*
* @param userId ์‚ฌ์šฉ์ž ID
* @param orderId ์ฃผ๋ฌธ ID
* @param cardType ์นด๋“œ ํƒ€์ž… ๋ฌธ์ž์—ด
* @param cardNo ์นด๋“œ ๋ฒˆํ˜ธ
* @param amount ๊ฒฐ์ œ ๊ธˆ์•ก
* @return ๊ฒฐ์ œ ์š”์ฒญ ๋„๋ฉ”์ธ ๋ชจ๋ธ
* @throws CoreException ์ž˜๋ชป๋œ ์นด๋“œ ํƒ€์ž…์ธ ๊ฒฝ์šฐ
*/
public PaymentRequest build(String userId, Long orderId, String cardType, String cardNo, Integer amount) {
// ์ฃผ๋ฌธ ID๋ฅผ 6์ž๋ฆฌ ์ด์ƒ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ (pg-simulator ๊ฒ€์ฆ ์š”๊ตฌ์‚ฌํ•ญ)
String orderIdString = formatOrderId(orderId);
return new PaymentRequest(
userId,
orderIdString,
parseCardType(cardType),
cardNo,
amount.longValue(),
generateCallbackUrl(orderId)
);
}

/**
* ์นด๋“œ ํƒ€์ž… ๋ฌธ์ž์—ด์„ CardType enum์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
*
* @param cardType ์นด๋“œ ํƒ€์ž… ๋ฌธ์ž์—ด
* @return CardType enum
* @throws CoreException ์ž˜๋ชป๋œ ์นด๋“œ ํƒ€์ž…์ธ ๊ฒฝ์šฐ
*/
private PaymentGatewayDto.CardType parseCardType(String cardType) {
try {
return PaymentGatewayDto.CardType.valueOf(cardType.toUpperCase());
} catch (IllegalArgumentException e) {
throw new CoreException(ErrorType.BAD_REQUEST,
String.format("์ž˜๋ชป๋œ ์นด๋“œ ํƒ€์ž…์ž…๋‹ˆ๋‹ค. (cardType: %s)", cardType));
}
}

/**
* ์ฝœ๋ฐฑ URL์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
*
* @param orderId ์ฃผ๋ฌธ ID
* @return ์ฝœ๋ฐฑ URL
*/
private String generateCallbackUrl(Long orderId) {
return String.format("http://localhost:%d/api/v1/orders/%d/callback", serverPort, orderId);
}

/**
* ์ฃผ๋ฌธ ID๋ฅผ 6์ž๋ฆฌ ์ด์ƒ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
* <p>
* pg-simulator์˜ ๊ฒ€์ฆ ์š”๊ตฌ์‚ฌํ•ญ์— ๋งž์ถ”๊ธฐ ์œ„ํ•ด ์ตœ์†Œ 6์ž๋ฆฌ๋กœ ํŒจ๋”ฉํ•ฉ๋‹ˆ๋‹ค.
* </p>
*
* @param orderId ์ฃผ๋ฌธ ID (Long)
* @return 6์ž๋ฆฌ ์ด์ƒ์˜ ์ฃผ๋ฌธ ID ๋ฌธ์ž์—ด
*/
public String formatOrderId(Long orderId) {
return String.format("%06d", orderId);
}
}

Loading