Skip to content

Commit

Permalink
Merge pull request #62 from marinesnow34/cancel
Browse files Browse the repository at this point in the history
Feat: 결제 취소 구현
  • Loading branch information
marinesnow34 authored Nov 23, 2023
2 parents 4969de2 + 990559d commit 025746c
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/readyvery/readyverydemo/domain/Receipt.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Getter
@Setter
@Entity
@Builder
@Table(name = "RECEIPT")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public class Constant {
public static final Long TAKE_OUT = 2L;
public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm";
public static final String IMG_URL = "/images/";
public static final String USER_CANCEL_RESON = "단순 변심";
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public enum ExceptionCode {
TOSS_PAYMENT_SUCCESS_FAIL(400, "Toss payment success fail."),
ORDER_NOT_FOUND(400, "Order does not exists."),
TOSS_PAYMENT_AMOUNT_NOT_MATCH(400, "Toss payment amount not match."),
ORDER_NOT_CURRENT(400, "Order is not current.");

ORDER_NOT_CURRENT(400, "Order is not current."),
ORDER_NOT_CANCELABLE(400, "Order is not cancelable.");

private int status;
private String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.readyvery.readyverydemo.src.order.dto.FoodyDetailRes;
import com.readyvery.readyverydemo.src.order.dto.HistoryRes;
import com.readyvery.readyverydemo.src.order.dto.PaymentReq;
import com.readyvery.readyverydemo.src.order.dto.TossCancelReq;
import com.readyvery.readyverydemo.src.order.dto.TosspaymentMakeRes;

import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -97,6 +98,13 @@ public ResponseEntity<TosspaymentMakeRes> requestTossPayment(@AuthenticationPrin
return new ResponseEntity<>(tosspaymentMakeRes, HttpStatus.OK);
}

@PostMapping("/toss/cancel")
public ResponseEntity<Object> cancelTossPayment(@AuthenticationPrincipal CustomUserDetails userDetails,
@RequestBody TossCancelReq tossCancelReq) {
Object result = orderService.cancelTossPayment(userDetails, tossCancelReq);
return new ResponseEntity<>(result, HttpStatus.OK);
}

@PutMapping("/cart")
public ResponseEntity<CartEidtRes> updateCart(@AuthenticationPrincipal CustomUserDetails userDetails,
@RequestBody CartEditReq cartEditReq) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.readyvery.readyverydemo.src.order.dto.FoodyDetailRes;
import com.readyvery.readyverydemo.src.order.dto.HistoryRes;
import com.readyvery.readyverydemo.src.order.dto.PaymentReq;
import com.readyvery.readyverydemo.src.order.dto.TossCancelReq;
import com.readyvery.readyverydemo.src.order.dto.TosspaymentMakeRes;

public interface OrderService {
Expand All @@ -39,4 +40,6 @@ public interface OrderService {
HistoryRes getHistories(CustomUserDetails userDetails);

CurrentRes getCurrent(String orderId);

Object cancelTossPayment(CustomUserDetails userDetails, TossCancelReq tossCancelReq);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.readyvery.readyverydemo.src.order;

import static com.readyvery.readyverydemo.global.Constant.*;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Collections;
Expand Down Expand Up @@ -55,6 +57,7 @@
import com.readyvery.readyverydemo.src.order.dto.HistoryRes;
import com.readyvery.readyverydemo.src.order.dto.OrderMapper;
import com.readyvery.readyverydemo.src.order.dto.PaymentReq;
import com.readyvery.readyverydemo.src.order.dto.TossCancelReq;
import com.readyvery.readyverydemo.src.order.dto.TosspaymentDto;
import com.readyvery.readyverydemo.src.order.dto.TosspaymentMakeRes;

Expand Down Expand Up @@ -209,6 +212,63 @@ public CurrentRes getCurrent(String orderId) {
return orderMapper.orderToCurrentRes(order);
}

@Override
public Object cancelTossPayment(CustomUserDetails userDetails, TossCancelReq tossCancelReq) {
UserInfo user = getUserInfo(userDetails);
Order order = getOrder(tossCancelReq.getOrderId());
verifyCancel(order, user);

TosspaymentDto tosspaymentDto = requestTossPaymentCancel(order.getPaymentKey());

applyCancelTosspaymentDto(order, tosspaymentDto);

orderRepository.save(order);
return orderMapper.tosspaymentDtoToCancelRes();
}

private void applyCancelTosspaymentDto(Order order, TosspaymentDto tosspaymentDto) {
order.setProgress(Progress.CANCEL);
order.setPayStatus(false);
order.getReceipt().setCancels(tosspaymentDto.getCancels().toString());
order.getReceipt().setStatus(tosspaymentDto.getStatus());
}

private TosspaymentDto requestTossPaymentCancel(String paymentKey) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = makeTossHeader();
JSONObject params = new JSONObject();

params.put("cancelReason", USER_CANCEL_RESON);

try {
return restTemplate.postForObject(TossPaymentConfig.PAYMENT_URL + paymentKey + "/cancel",
new HttpEntity<>(params, headers),
TosspaymentDto.class);
} catch (Exception e) {
System.out.println("e.getMessage() = " + e.getMessage());
throw new BusinessLogicException(ExceptionCode.TOSS_PAYMENT_SUCCESS_FAIL);
}
}

private void verifyCancel(Order order, UserInfo user) {
verifyCancelStatus(order);
verifyOrederUser(order, user);
}

private void verifyOrederUser(Order order, UserInfo user) {
if (order.getUserInfo().equals(user)) {
return;
}
throw new BusinessLogicException(ExceptionCode.ORDER_NOT_CANCELABLE);
}

private void verifyCancelStatus(Order order) {
if (order.getProgress().equals(Progress.ORDER)) {
return;
}
throw new BusinessLogicException(ExceptionCode.ORDER_NOT_CANCELABLE);
}

private void verifyOrderCurrent(Order order) {
if (order.getProgress().equals(Progress.ORDER)
|| order.getProgress().equals(Progress.MAKE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@Configuration
@Getter
public class TossPaymentConfig {
public static final String PAYMENT_URL = "https://api.tosspayments.com/v1/payments/";
public static final String CONFIRM_URL = "https://api.tosspayments.com/v1/payments/confirm";

@Value("${payment.toss.client_key}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.readyvery.readyverydemo.src.order.dto;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class DefaultRes {
private String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,10 @@ public CurrentRes orderToCurrentRes(Order order) {
? order.getEstimatedTime().format(DateTimeFormatter.ofPattern(TIME_FORMAT)) : null)
.build();
}

public DefaultRes tosspaymentDtoToCancelRes() {
return DefaultRes.builder()
.message("취소 성공")
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.readyvery.readyverydemo.src.order.dto;

import lombok.Getter;

@Getter
public class TossCancelReq {
private String orderId;
}

0 comments on commit 025746c

Please sign in to comment.