Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public enum ErrorCode implements BaseCode {
// Common
BAD_REQUEST(HttpStatus.BAD_REQUEST, "COMMON_400", "잘못된 요청입니다."),
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "COMMON_500", "서버 에러, 서버 개발자에게 문의하세요."),
EXTERNAL_API_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "PAYMENT_5001", "외부 API 호출 중 오류가 발생했습니다."),


// User
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "USER_4041", "존재하지 않는 회원입니다."),
Expand All @@ -37,6 +39,8 @@ public enum ErrorCode implements BaseCode {
TID_NOT_EXIST(HttpStatus.BAD_REQUEST, "PAYMENT_4001", "tid가 존재하지 않습니다."),
TID_SID_UNSUPPORTED(HttpStatus.BAD_REQUEST, "PAYMENT_4002", "지원되지 않는 tid, sid 입니다."),
SID_NOT_EXIST(HttpStatus.BAD_REQUEST, "PAYMENT_4003", "sid가 존재하지 않습니다."),
TID_ALREADY_EXIST(HttpStatus.BAD_REQUEST, "PAYMENT_4004", "tid가 이미 존재합니다."),
PAYMENT_FAILED(HttpStatus.BAD_REQUEST, "PAYMENT_4005", "결제에 실패하였습니다."),

// Match
MATCH_NOT_EXIST(HttpStatus.BAD_REQUEST, "MATCH_4001", "매치 신청 정보가 존재하지 않습니다."),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.silverbridgeX_user.global.util;

import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@RequiredArgsConstructor
public class RestTemplateUtil {

private final RestTemplate restTemplate;

public <T> T post(String url, Map<String, Object> parameters, HttpHeaders headers, Class<T> responseType) {
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(parameters, headers);
ResponseEntity<T> response = restTemplate.postForEntity(url, requestEntity, responseType);
return response.getBody();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

import com.example.silverbridgeX_user.global.api_payload.ApiResponse;
import com.example.silverbridgeX_user.global.api_payload.SuccessCode;
import com.example.silverbridgeX_user.payment.converter.PaymentConverter;
import com.example.silverbridgeX_user.payment.domain.Payment;
import com.example.silverbridgeX_user.payment.dto.PaymentDto;
import com.example.silverbridgeX_user.payment.service.PaymentService;
import com.example.silverbridgeX_user.user.domain.User;
import com.example.silverbridgeX_user.user.jwt.CustomUserDetails;
import com.example.silverbridgeX_user.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
Expand All @@ -25,11 +27,12 @@ public class PaymentController {

@PostMapping("/ready")
@Operation(summary = "카카오페이 URL 생성 API", description = "카카오페이 URL을 생성하는 API 입니다.")
public ApiResponse<PaymentDto.KakaoReadyResponse> readyToKakaoPay(@AuthenticationPrincipal CustomUserDetails customUserDetails) {
PaymentDto.KakaoReadyResponse kakaoReadyResponse = paymentService.kakaoPayReady();
public ApiResponse<PaymentDto.KakaoReadyResponse> readyToKakaoPay(
@AuthenticationPrincipal CustomUserDetails customUserDetails
) {
Long userId = userService.findByUserName(customUserDetails.getUsername()).getId();

User user = userService.findByUserName(customUserDetails.getUsername());
Long userId = user.getId();
PaymentDto.KakaoReadyResponse kakaoReadyResponse = paymentService.kakaoPayReady(userId);

paymentService.saveTid(userId, kakaoReadyResponse.getTid());

Expand All @@ -38,20 +41,23 @@ public ApiResponse<PaymentDto.KakaoReadyResponse> readyToKakaoPay(@Authenticatio

@PostMapping("/ready/key")
@Operation(summary = "카카오페이 URL 생성 API", description = "key를 이용하여 카카오페이 URL을 생성하는 API 입니다.")
public ApiResponse<PaymentDto.KakaoReadyResponse> readyToKakaoPay(@RequestParam("id") String key) {
PaymentDto.KakaoReadyResponse kakaoReadyResponse = paymentService.kakaoPayReady();
public ApiResponse<PaymentDto.KakaoReadyResponse> readyToKakaoPay(
@RequestParam("id") String key
) {
Long userId = userService.findByUserName(key).getId();

User user = userService.findByUserName(key);
Long userId = user.getId();
PaymentDto.KakaoReadyResponse kakaoReadyResponse = paymentService.kakaoPayReady(userId);

paymentService.saveTid(userId, kakaoReadyResponse.getTid());

return ApiResponse.onSuccess(SuccessCode.PAYMENT_URL_CREATE_SUCCESS, kakaoReadyResponse);
}

@GetMapping("/success")
public ModelAndView afterPayRequest(@RequestParam("pg_token") String pgToken) {
PaymentDto.KakaoApproveResponse kakaoApproveResponse = paymentService.approveResponse(pgToken);
public ModelAndView afterPayRequest(
@RequestParam("pg_token") String pgToken, @RequestParam("userId") Long userId
) {
PaymentDto.KakaoApproveResponse kakaoApproveResponse = paymentService.approveResponse(pgToken, userId);

ModelAndView modelAndView = new ModelAndView("success"); // "success"는 템플릿 파일 이름
modelAndView.addObject("paymentInfo", kakaoApproveResponse);
Expand All @@ -67,10 +73,11 @@ public String fail() {
}

@GetMapping("/cancel")
public ApiResponse<PaymentDto.KakaoCancelResponse> refund(@AuthenticationPrincipal CustomUserDetails customUserDetails) {
public ApiResponse<PaymentDto.KakaoCancelResponse> refund(
@AuthenticationPrincipal CustomUserDetails customUserDetails
) {

User user = userService.findByUserName(customUserDetails.getUsername());
Long userId = user.getId();
Long userId = userService.findByUserName(customUserDetails.getUsername()).getId();

Payment kakaoPay = paymentService.getKakaoPayInfo(userId);

Expand All @@ -82,10 +89,11 @@ public ApiResponse<PaymentDto.KakaoCancelResponse> refund(@AuthenticationPrincip
}

@GetMapping("/cancel/key")
public ApiResponse<PaymentDto.KakaoCancelResponse> refund(@RequestParam("id") String key) {
public ApiResponse<PaymentDto.KakaoCancelResponse> refund(
@RequestParam("id") String key
) {

User user = userService.findByUserName(key);
Long userId = user.getId();
Long userId = userService.findByUserName(key).getId();

Payment kakaoPay = paymentService.getKakaoPayInfo(userId);

Expand All @@ -97,27 +105,31 @@ public ApiResponse<PaymentDto.KakaoCancelResponse> refund(@RequestParam("id") St
}

@PostMapping("/subscribe")
public ApiResponse<PaymentDto.KakaoApproveResponse> subscribePayRequest(@AuthenticationPrincipal CustomUserDetails customUserDetails) {
User user = userService.findByUserName(customUserDetails.getUsername());
Long userId = user.getId();
public ApiResponse<PaymentDto.KakaoApproveResponse> subscribePayRequest(
@AuthenticationPrincipal CustomUserDetails customUserDetails
) {
Long userId = userService.findByUserName(customUserDetails.getUsername()).getId();

Payment kakaoPay = paymentService.getKakaoPayInfo(userId);

PaymentDto.KakaoApproveResponse kakaoApproveResponse = paymentService.approveSubscribeResponse(kakaoPay.getSid());
PaymentDto.KakaoApproveResponse kakaoApproveResponse = paymentService.approveSubscribeResponse(
kakaoPay.getSid(), userId);

paymentService.savePayInfo(userId, kakaoApproveResponse);

return ApiResponse.onSuccess(SuccessCode.PAYMENT_SUBSCRIBE_SUCCESS, kakaoApproveResponse);
}

@PostMapping("/subscribe/key")
public ApiResponse<PaymentDto.KakaoApproveResponse> subscribePayRequest(@RequestParam("id") String key) {
User user = userService.findByUserName(key);
Long userId = user.getId();
public ApiResponse<PaymentDto.KakaoApproveResponse> subscribePayRequest(
@RequestParam("id") String key
) {
Long userId = userService.findByUserName(key).getId();

Payment kakaoPay = paymentService.getKakaoPayInfo(userId);

PaymentDto.KakaoApproveResponse kakaoApproveResponse = paymentService.approveSubscribeResponse(kakaoPay.getSid());
PaymentDto.KakaoApproveResponse kakaoApproveResponse = paymentService.approveSubscribeResponse(
kakaoPay.getSid(), userId);

paymentService.savePayInfo(userId, kakaoApproveResponse);

Expand All @@ -126,73 +138,51 @@ public ApiResponse<PaymentDto.KakaoApproveResponse> subscribePayRequest(@Request

@PostMapping("/subscribe/cancel")
@Operation(summary = "카카오페이 구독 취소 API", description = "카카오페이 구독을 취소하는 API 입니다.")
public ApiResponse<PaymentDto.KakaoSubscribeCancelResponse> subscribeCancelRequest(@AuthenticationPrincipal CustomUserDetails customUserDetails) {
User user = userService.findByUserName(customUserDetails.getUsername());
Long userId = user.getId();
public ApiResponse<PaymentDto.KakaoSubscribeCancelResponse> subscribeCancelRequest(
@AuthenticationPrincipal CustomUserDetails customUserDetails
) {
Long userId = userService.findByUserName(customUserDetails.getUsername()).getId();

Payment kakaoPay = paymentService.getKakaoPayInfo(userId);

PaymentDto.KakaoSubscribeCancelResponse kakaoSubscribeCancelResponse = paymentService.subscribeCancelResponse(kakaoPay.getSid());
PaymentDto.KakaoSubscribeCancelResponse kakaoSubscribeCancelResponse = paymentService.subscribeCancelResponse(
userId);

return ApiResponse.onSuccess(SuccessCode.PAYMENT_URL_CREATE_SUCCESS, kakaoSubscribeCancelResponse);
}

@PostMapping("/subscribe/cancel/key")
@Operation(summary = "카카오페이 구독 취소 API", description = "key를 이용하여 카카오페이 구독을 취소하는 API 입니다.")
public ApiResponse<PaymentDto.KakaoSubscribeCancelResponse> subscribeCancelRequest(@RequestParam("id") String key) {
User user = userService.findByUserName(key);
Long userId = user.getId();
public ApiResponse<PaymentDto.KakaoSubscribeCancelResponse> subscribeCancelRequest(
@RequestParam("id") String key
) {
Long userId = userService.findByUserName(key).getId();

Payment kakaoPay = paymentService.getKakaoPayInfo(userId);

PaymentDto.KakaoSubscribeCancelResponse kakaoSubscribeCancelResponse = paymentService.subscribeCancelResponse(kakaoPay.getSid());
PaymentDto.KakaoSubscribeCancelResponse kakaoSubscribeCancelResponse = paymentService.subscribeCancelResponse(
userId);

return ApiResponse.onSuccess(SuccessCode.PAYMENT_URL_CREATE_SUCCESS, kakaoSubscribeCancelResponse);
}

@GetMapping("/subscribe/status")
@Operation(summary = "카카오페이 구독 상태 확인 API", description = "카카오페이 구독 상태를 확인하는 API 입니다.")
public ApiResponse<PaymentDto.KakaoPayStatus> subscribeStatusRequest(@AuthenticationPrincipal CustomUserDetails customUserDetails) {
User user = userService.findByUserName(customUserDetails.getUsername());
Long userId = user.getId();

PaymentDto.KakaoSubscribeStatusResponse kakaoSubscribeStatusResponse = new PaymentDto.KakaoSubscribeStatusResponse();
public ApiResponse<PaymentDto.KakaoPayStatus> subscribeStatusRequest(
@AuthenticationPrincipal CustomUserDetails customUserDetails
) {
Long userId = userService.findByUserName(customUserDetails.getUsername()).getId();

boolean isLogExist = false;
if (paymentService.getKakaoPayLog(userId)) {
Payment kakaoPay = paymentService.getKakaoPayInfo(userId);
PaymentDto.KakaoPayStatus kakaoPayStatus = paymentService.getSubscribeStatus(userId);

if (kakaoPay.getSid() == null || kakaoPay.getSid().isEmpty()) {}
else {
isLogExist = true;

kakaoSubscribeStatusResponse = paymentService.subscribeStatusResponse(kakaoPay.getSid());
}
}

return ApiResponse.onSuccess(SuccessCode.PAYMENT_VIEW_SUBSCRIBE_STATUS_SUCCESS, PaymentConverter.toKakaoPayStatus(isLogExist, kakaoSubscribeStatusResponse));
return ApiResponse.onSuccess(SuccessCode.PAYMENT_VIEW_SUBSCRIBE_STATUS_SUCCESS, kakaoPayStatus);
}

@GetMapping("/subscribe/status/key")
@Operation(summary = "카카오페이 구독 상태 확인 API", description = "key를 이용하여 카카오페이 구독 상태를 확인하는 API 입니다.")
public ApiResponse<PaymentDto.KakaoPayStatus> subscribeStatusRequest(@RequestParam("id") String key) {
User user = userService.findByUserName(key);
Long userId = user.getId();

PaymentDto.KakaoSubscribeStatusResponse kakaoSubscribeStatusResponse = new PaymentDto.KakaoSubscribeStatusResponse();

boolean isLogExist = false;
if (paymentService.getKakaoPayLog(userId)) {
Payment kakaoPay = paymentService.getKakaoPayInfo(userId);

if (kakaoPay.getSid() == null || kakaoPay.getSid().isEmpty()) {}
else {
isLogExist = true;
public ApiResponse<PaymentDto.KakaoPayStatus> subscribeStatusRequest(
@RequestParam("id") String key
) {
Long userId = userService.findByUserName(key).getId();

kakaoSubscribeStatusResponse = paymentService.subscribeStatusResponse(kakaoPay.getSid());
}
}
PaymentDto.KakaoPayStatus kakaoPayStatus = paymentService.getSubscribeStatus(userId);

return ApiResponse.onSuccess(SuccessCode.PAYMENT_VIEW_SUBSCRIBE_STATUS_SUCCESS, PaymentConverter.toKakaoPayStatus(isLogExist, kakaoSubscribeStatusResponse));
return ApiResponse.onSuccess(SuccessCode.PAYMENT_VIEW_SUBSCRIBE_STATUS_SUCCESS, kakaoPayStatus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@

import com.example.silverbridgeX_user.global.entity.BaseEntity;
import com.example.silverbridgeX_user.user.domain.User;
import jakarta.persistence.*;
import lombok.*;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
Expand All @@ -23,12 +33,7 @@ public class Payment extends BaseEntity {
@JoinColumn(name = "user_id")
private User user;

public void updateTid(String tid) { this.tid = tid; }

public void updateSid(String sid) { this.sid = sid; }

public void updatePayInfo(String tid, String sid) {
this.tid = tid;
public void updateSid(String sid) {
this.sid = sid;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package com.example.silverbridgeX_user.payment.repository;

import com.example.silverbridgeX_user.payment.domain.Payment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface PaymentRepository extends JpaRepository<Payment, Long> {

boolean existsByUser_Id(Long userId);
Optional<Payment> findByUser_Id(Long userId);
boolean existsByUserId(Long userId);

Optional<Payment> findByTid(String tid);

boolean existsByTid(String tid);

@Query("SELECT k FROM Payment k JOIN FETCH k.user WHERE k.sid IS NOT NULL")
List<Payment> findAllWithMemberAndSidNotNull();

@Query("SELECT p FROM Payment p WHERE p.user.id = :userId ORDER BY p.createdAt DESC")
Optional<Payment> getLatestKakaoPayInfo(@Param("userId") Long userId);
}
Loading