Skip to content

Commit

Permalink
Merge pull request #123 from Orange-Co/feature/payment
Browse files Browse the repository at this point in the history
Feature/payment
  • Loading branch information
Kang1221 authored Oct 15, 2024
2 parents 3f0696e + d6e7253 commit 13e0c60
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
13 changes: 9 additions & 4 deletions src/main/java/co/orange/ddanzi/service/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,15 @@ public void checkOrderPlacedOrder(){
LocalDateTime oneDayLimit = LocalDateTime.now().minusDays(1);
List<Order> orderPlaceOrders = orderRepository.findOverLimitTimeOrders(OrderStatus.ORDER_PLACE, oneDayLimit);
for(Order order : orderPlaceOrders){
paymentService.refundPayment(order.getBuyer(), order, "고객이 판매확정을 하지 않아 거래가 취소되어 결제 금액을 환불합니다.");
order.updateStatus(OrderStatus.CANCELLED);
fcmService.sendMessageToUser(order.getItem().getSeller(), FcmCase.A2, order);
fcmService.sendMessageToUser(order.getBuyer(), FcmCase.B1, order);
if(paymentService.refundPayment(order.getBuyer(), order, "고객이 판매확정을 하지 않아 거래가 취소되어 결제 금액을 환불합니다.")){
fcmService.sendMessageToAdmins("⚠️ 관리자 알림: 환불 성공", "거래 취소로 인해 환불되었습니다. orderId:" + order.getId());
order.updateStatus(OrderStatus.CANCELLED);
fcmService.sendMessageToUser(order.getItem().getSeller(), FcmCase.A2, order);
fcmService.sendMessageToUser(order.getBuyer(), FcmCase.B1, order);
}
else{
fcmService.sendMessageToAdmins("‼️관리자 알림: 환불 실패", "거래가 취소로 인한 환불에 실패하였습니다. orderId:" + order.getId());
}
}
}

Expand Down
32 changes: 22 additions & 10 deletions src/main/java/co/orange/ddanzi/service/PaymentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

Expand Down Expand Up @@ -90,12 +92,14 @@ public ApiResponse<?> endPayment(UpdatePaymentRequestDto requestDto){
Item newItem = itemRepository.findNearestExpiryItem(product).orElse(null);
if(newItem == null){
log.info("환불을 진행합니다.");
try {
refundPayment(buyer, order, "현재 남은 재고가 없어 고객에게 결제 금액 환불합니다.");
if(refundPayment(buyer, order, "현재 남은 재고가 없어 고객에게 결제 금액 환불합니다.")){
fcmService.sendMessageToAdmins("⚠️관리자 알림: 환불 성공", "중복 결제로 인해 환불되었습니다. orderId:" + order.getId());
payment.updatePaymentStatusAndEndedAt(PayStatus.CANCELLED);
historyService.createPaymentHistoryWithError(buyer, payment, "재고 없음- 환불 처리 성공");
return ApiResponse.onFailure(Error.NO_ITEM_ON_SALE, Map.of("orderId", order.getId()));
}catch (Exception e){
}
else{
fcmService.sendMessageToAdmins("‼️관리자 알림: 환불 실패", "중복 결제로 인한 환불에 실패하였습니다. orderId:" + order.getId());
historyService.createPaymentHistoryWithError(buyer, payment, "재고 없음 - 환불 처리 실패");
return ApiResponse.onFailure(Error.REFUND_FAILED, Map.of("orderId", order.getId()));
}
Expand Down Expand Up @@ -174,13 +178,21 @@ public String getPortOneAccessToken(){
HttpEntity<PortOneTokenRequestDto> entity = new HttpEntity<>(requestBody, headers);

RestTemplate restTemplate = new RestTemplate();
log.info("포트원 Access key 요청 생성");
ResponseEntity<PortOneTokenResponseDto> response = restTemplate.exchange(url, HttpMethod.POST, entity, PortOneTokenResponseDto.class);
log.info("포트원 Access key Get 성공");
return response.getBody().getResponse().getAccess_token();
try {
ResponseEntity<PortOneTokenResponseDto> response = restTemplate.exchange(url, HttpMethod.POST, entity, PortOneTokenResponseDto.class);
log.info("포트원 Access key Get 성공");
return response.getBody().getResponse().getAccess_token();
} catch (HttpClientErrorException e) {
log.error("HTTP 오류 발생: 상태 코드 {}, 응답 본문 {}", e.getStatusCode(), e.getResponseBodyAsString());
} catch (RestClientException e) {
log.error("REST 클라이언트 오류 발생: {}", e.getMessage());
} catch (Exception e) {
log.error("기타 오류 발생: {}", e.getMessage());
}
return null;
}

public void refundPayment(User user, Order order, String reason){
public boolean refundPayment(User user, Order order, String reason){
if(!user.equals(order.getBuyer()))
throw new RuntimeException("결제자와 요청자가 다르므로 환불이 어렵습니다.");
try{
Expand All @@ -205,10 +217,10 @@ public void refundPayment(User user, Order order, String reason){
RestTemplate restTemplate = new RestTemplate();
restTemplate.postForObject(url, entity, String.class);
log.info("결제 취소 api 호출");
fcmService.sendMessageToAdmins("⚠️관리자 알림: 환불실행", "중복 결제로 인해 환불되었습니다. orderId:" + order.getId());
return true;
}catch (Exception e){
log.info("환불 실패");
fcmService.sendMessageToAdmins("⚠️관리자 알림: 환불 실패", "환불에 실패했습니다. orderId:" + order.getId());
return false;
}

}
Expand Down

0 comments on commit 13e0c60

Please sign in to comment.