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

Feat: 결제 실패 구현 #41

Merged
merged 1 commit into from
Nov 21, 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
3 changes: 3 additions & 0 deletions src/main/java/com/readyvery/readyverydemo/domain/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public class Order {
@Enumerated(EnumType.STRING)
private Progress progress;

@Column
private Boolean payStatus; // 결제 성공 실패 여부

// 가게 아이템 연관 관계
// @OneToMany(mappedBy = "order")
// @Builder.Default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public enum Progress {
ORDER("ORDER", "주문 접수"),
MAKE("MAKE", "음식 제조 중"),
COMPLETE("COMPLETE", "제조 완료"),
PICKUP("PICKUP", "픽업 완료");
PICKUP("PICKUP", "픽업 완료"),
FAIL("FAIL", "주문 실패");

private final String key;
private final String value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteReq;
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteRes;
import com.readyvery.readyverydemo.src.order.dto.CartResetRes;
import com.readyvery.readyverydemo.src.order.dto.FailDto;
import com.readyvery.readyverydemo.src.order.dto.FoodyDetailRes;
import com.readyvery.readyverydemo.src.order.dto.PaymentReq;
import com.readyvery.readyverydemo.src.order.dto.TosspaymentMakeRes;
Expand Down Expand Up @@ -59,6 +60,15 @@ public ResponseEntity<String> tossPaymentSuccess(
return new ResponseEntity<>(result, HttpStatus.OK);
}

@GetMapping("/toss/fail")
public ResponseEntity<FailDto> tossPaymentFail(
@RequestParam("code") String code,
@RequestParam("orderId") String orderId,
@RequestParam("message") String message) {
FailDto result = orderService.tossPaymentFail(code, orderId, message);
return new ResponseEntity<>(result, HttpStatus.OK);
}

@PostMapping("/cart")
public ResponseEntity<CartAddRes> addCart(@AuthenticationPrincipal CustomUserDetails userDetails,
@RequestBody CartAddReq cartAddReq) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.readyvery.readyverydemo.src.order;

import com.readyvery.readyverydemo.global.exception.BusinessLogicException;
import com.readyvery.readyverydemo.security.jwt.dto.CustomUserDetails;
import com.readyvery.readyverydemo.src.order.dto.CartAddReq;
import com.readyvery.readyverydemo.src.order.dto.CartAddRes;
Expand All @@ -9,6 +10,7 @@
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteReq;
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteRes;
import com.readyvery.readyverydemo.src.order.dto.CartResetRes;
import com.readyvery.readyverydemo.src.order.dto.FailDto;
import com.readyvery.readyverydemo.src.order.dto.FoodyDetailRes;
import com.readyvery.readyverydemo.src.order.dto.PaymentReq;
import com.readyvery.readyverydemo.src.order.dto.TosspaymentMakeRes;
Expand All @@ -29,4 +31,6 @@ public interface OrderService {
TosspaymentMakeRes requestTossPayment(CustomUserDetails userDetails, PaymentReq paymentReq);

String tossPaymentSuccess(String paymentKey, String orderId, Long amount);

FailDto tossPaymentFail(String code, String orderId, String message) throws BusinessLogicException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteReq;
import com.readyvery.readyverydemo.src.order.dto.CartItemDeleteRes;
import com.readyvery.readyverydemo.src.order.dto.CartResetRes;
import com.readyvery.readyverydemo.src.order.dto.FailDto;
import com.readyvery.readyverydemo.src.order.dto.FoodyDetailRes;
import com.readyvery.readyverydemo.src.order.dto.OrderMapper;
import com.readyvery.readyverydemo.src.order.dto.PaymentReq;
Expand Down Expand Up @@ -181,14 +182,28 @@ public String tossPaymentSuccess(String paymentKey, String orderId, Long amount)
//TODO: 영수증 처리
Receipt receipt = orderMapper.tosspaymentDtoToReceipt(tosspaymentDto, order);
receiptRepository.save(receipt);
return "hi";
return "결제 성공";
}

@Override
public FailDto tossPaymentFail(String code, String orderId, String message) {
Order order = getOrder(orderId);
applyOrderFail(order);
orderRepository.save(order);
return orderMapper.makeFailDto(code, message);
}

private void applyOrderFail(Order order) {
order.setPayStatus(false);
order.setProgress(Progress.FAIL);
}

private void applyTosspaymentDto(Order order, TosspaymentDto tosspaymentDto) {
//TODO: orderNumber 적용
order.setPaymentKey(tosspaymentDto.getPaymentKey());
order.setMethod(tosspaymentDto.getMethod());
order.setProgress(Progress.ORDER);
order.setPayStatus(true);
}

private void verifyOrder(Order order, Long amount) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.readyvery.readyverydemo.src.order.dto;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class FailDto {
private String code;
private String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,11 @@ public Receipt tosspaymentDtoToReceipt(TosspaymentDto tosspaymentDto, Order orde
.cashReceipts(tosspaymentDto.getCashReceipts() != null ? tosspaymentDto.getCashReceipts().toString() : null)
.build();
}

public FailDto makeFailDto(String code, String message) {
return FailDto.builder()
.code(code)
.message(message)
.build();
}
}