Skip to content

Commit

Permalink
[MOA-483] 쿠폰 취소 API (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
shin-mallang authored Mar 1, 2024
1 parent 339727a commit 29ada0f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
20 changes: 20 additions & 0 deletions api/src/main/java/moa/order/OrderApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,31 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

@Tag(name = "주문 API", description = "주문 관련 API")
@SecurityRequirement(name = "JWT")
public interface OrderApi {

@ApiResponses(
value = {
@ApiResponse(responseCode = "200"),
@ApiResponse(responseCode = "400"),
@ApiResponse(responseCode = "401"),
@ApiResponse(responseCode = "403"),
@ApiResponse(responseCode = "404"),
@ApiResponse(responseCode = "500"),
}
)
@Operation(summary = "쿠폰 취소")
@PostMapping("/{orderId}/cancel-coupon")
ResponseEntity<Void> cancelCoupon(
@Auth(permit = {SIGNED_UP}) Long memberId,

@Parameter(in = PATH, required = true, description = "주문 ID")
@PathVariable("orderId") Long orderId
);

@ApiResponses(
value = {
@ApiResponse(responseCode = "200"),
Expand Down
10 changes: 10 additions & 0 deletions api/src/main/java/moa/order/OrderController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -42,6 +43,15 @@ public ResponseEntity<Void> reissueCoupon(
return ResponseEntity.ok().build();
}

@PostMapping("/{orderId}/cancel-coupon")
public ResponseEntity<Void> cancelCoupon(
@Auth(permit = {SIGNED_UP}) Long memberId,
@PathVariable("orderId") Long orderId
) {
orderService.cancelCoupon(orderId, memberId);
return ResponseEntity.ok().build();
}

@GetMapping
public ResponseEntity<PageResponse<OrderResponse>> findOrders(
@Auth(permit = {SIGNED_UP}) Long memberId,
Expand Down
17 changes: 11 additions & 6 deletions core/src/main/java/moa/client/wincube/WincubeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static moa.client.exception.ExternalApiExceptionType.EXTERNAL_API_EXCEPTION;
import static moa.product.exception.ProductExceptionType.COUPONS_CANNOT_BE_REISSUED;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Nullable;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -24,7 +23,6 @@ public class WincubeClient {
private static final String TR_ID_PREFIX = "giftmoa_";
private static final String JSON = "JSON";

private final ObjectMapper objectMapper;
private final WincubeProperty wincubeProperty;
private final WincubeAuthClient authClient;
private final WincubeApiClient client;
Expand Down Expand Up @@ -65,19 +63,26 @@ private void validateIssueCoupon(WincubeIssueCouponResponse response) {
if (response.isSuccess()) {
log.info("윈큐브 쿠폰 발행 완료 {}", response);
} else {
throw new ExternalApiException(EXTERNAL_API_EXCEPTION.withDetail("윈큐브 쿠폰 발행 실패: " + response));
throw new ExternalApiException(EXTERNAL_API_EXCEPTION.withDetail(
"윈큐브 쿠폰 발행 실패: " + response
));
}
}

public void cancelCoupon(Long orderId) {
public void cancelCoupon(String transactionId) {
String authToken = authClient.getAuthToken();
WincubeCancelCouponResponse response = client.cancelCoupon(
wincubeProperty.mdCode(),
TR_ID_PREFIX + orderId,
TR_ID_PREFIX + transactionId,
JSON,
authToken
);
log.info("윈큐브 쿠폰 취소 API 호출 완료.\n -> 응답: {}", response);
log.info("윈큐브 쿠폰 취소 API 호출 완료.");
if (!response.isSuccess()) {
throw new ExternalApiException(EXTERNAL_API_EXCEPTION.withDetail(
"윈큐브 쿠폰 취소 실해: " + response
));
}
}

// TODO 2차때 회의 후, trId 어케할지 결정하고 처리
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/java/moa/order/application/OrderService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package moa.order.application;

import lombok.RequiredArgsConstructor;
import moa.client.wincube.WincubeClient;
import moa.member.domain.Member;
import moa.member.domain.MemberRepository;
import moa.order.application.command.CouponReissueCommand;
import moa.order.domain.Order;
import moa.order.domain.OrderRepository;
import moa.order.domain.OrderTransaction;
import moa.order.domain.OrderTransactionRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -16,6 +19,8 @@ public class OrderService {

private final OrderRepository orderRepository;
private final MemberRepository memberRepository;
private final OrderTransactionRepository orderTransactionRepository;
private final WincubeClient wincubeClient;

// TODO 이거 윈큐브 상품(or 쿠폰형 상품에 특화된 로직이라 나중에 상품 종류 추가되면 구조 변경)
public void reissueCoupon(CouponReissueCommand command) {
Expand All @@ -26,4 +31,12 @@ public void reissueCoupon(CouponReissueCommand command) {
// TODO 윈큐브 쿠폰 발행 API 호출해서 쿠폰 발행하기(바뀐 번호로)
order.reIssueCoupon(command.phoneNumber());
}

public void cancelCoupon(Long orderId, Long memberId) {
Order order = orderRepository.getById(orderId);
Member member = memberRepository.getById(memberId);
order.validateOwner(member);
OrderTransaction orderTx = orderTransactionRepository.getLastedByOrder(order);
wincubeClient.cancelCoupon(orderTx.getTransactionId());
}
}

0 comments on commit 29ada0f

Please sign in to comment.