-
Notifications
You must be signed in to change notification settings - Fork 1
[FEAT] 카카오페이 연동 구현(#46) #47
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
46e2e5e
[feat/#46] 카카오페이 연동 구현
hyowon0204 20fcadb
[feat/#46] 배포용으로 수정
hyowon0204 682243d
[feat/#46] userId 파라미터에서 현재 로그인된 사용자를 가져오는 방식으로 수정
hyowon0204 501d597
[feat/#46] null로 반환시 기본값 KAKAOPAY로 설정
hyowon0204 b7e9bc0
Merge branch 'develop' into feat/#46-kakaopay-integration-api
hyowon0204 03091ec
[mod/#46] 예약 서비스 코드 수정
hyowon0204 0a15af7
Merge remote-tracking branch 'origin/feat/#46-kakaopay-integration-ap…
hyowon0204 e88b74d
[fix/#46] 예약 서비스 코드 에러 수정
hyowon0204 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/sumte/payment/dto/KakaoPayApproveRequestDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.sumte.payment.dto; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| public class KakaoPayApproveRequestDTO { | ||
| private String cid; | ||
| private String tid; | ||
| private String partner_order_id; | ||
| private String partner_user_id; | ||
| private String pg_token; | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/sumte/payment/dto/KakaoPayApproveResponseDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.sumte.payment.dto; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| public class KakaoPayApproveResponseDTO { | ||
| private String aid; | ||
| private String tid; | ||
| private String cid; | ||
| private String partner_order_id; | ||
| private String partner_user_id; | ||
| private String payment_method_type; | ||
| private Amount amount; | ||
|
|
||
| @Getter | ||
| public static class Amount { | ||
| private int total; | ||
| private int tax_free; | ||
| private int vat; | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/sumte/payment/dto/KakaoPayReadyRequestDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.sumte.payment.dto; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import org.springframework.util.LinkedMultiValueMap; | ||
| import org.springframework.util.MultiValueMap; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| public class KakaoPayReadyRequestDTO { | ||
| private String cid; | ||
| private String partner_order_id; | ||
| private String partner_user_id; | ||
| private String item_name; | ||
| private String quantity; | ||
| private String total_amount; | ||
| private String tax_free_amount; | ||
| private String approval_url; | ||
| private String cancel_url; | ||
| private String fail_url; | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/sumte/payment/dto/KakaoPayReadyResponseDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.sumte.payment.dto; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| public class KakaoPayReadyResponseDTO { | ||
| private String tid; | ||
| private String next_redirect_app_url; | ||
| private String next_redirect_pc_url; | ||
| private String created_at; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/sumte/payment/kakaopay/KakaoPayClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.sumte.payment.kakaopay; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.sumte.payment.dto.KakaoPayApproveRequestDTO; | ||
| import com.sumte.payment.dto.KakaoPayApproveResponseDTO; | ||
| import com.sumte.payment.dto.KakaoPayReadyRequestDTO; | ||
| import com.sumte.payment.dto.KakaoPayReadyResponseDTO; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.reactive.function.client.WebClient; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class KakaoPayClient { | ||
|
|
||
| private final WebClient webClient; | ||
| private final ObjectMapper objectMapper; | ||
|
|
||
| @Value("${kakao.pay.secret-key}") | ||
| private String adminKey; | ||
|
|
||
| public KakaoPayReadyResponseDTO requestPayment(KakaoPayReadyRequestDTO request) { | ||
| return webClient.post() | ||
| .uri("https://open-api.kakaopay.com/online/v1/payment/ready") | ||
| .header("Authorization", "SECRET_KEY " + adminKey) | ||
| .header("Content-Type", "application/json") | ||
| .bodyValue(request) | ||
| .retrieve() | ||
| .bodyToMono(KakaoPayReadyResponseDTO.class) | ||
| .block(); | ||
| } | ||
|
|
||
| public KakaoPayApproveResponseDTO approvePayment(KakaoPayApproveRequestDTO request) { | ||
| return webClient.post() | ||
| .uri("https://open-api.kakaopay.com/online/v1/payment/approve") | ||
| .header("Authorization", "SECRET_KEY " + adminKey) | ||
| .header("Content-Type", "application/json") | ||
| .bodyValue(request) | ||
| .retrieve() | ||
| .bodyToMono(KakaoPayApproveResponseDTO.class) | ||
| .block(); | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
src/main/java/com/sumte/payment/kakaopay/WebClientConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.sumte.payment.kakaopay; | ||
|
|
||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.web.reactive.function.client.WebClient; | ||
|
|
||
| @Configuration | ||
| public class WebClientConfig { | ||
| @Bean | ||
| public WebClient webClient() { | ||
| return WebClient.builder().build(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| package com.sumte.payment.service; | ||
|
|
||
| import com.sumte.payment.dto.KakaoPayApproveResponseDTO; | ||
| import com.sumte.payment.dto.PaymentRequestDTO; | ||
| import com.sumte.payment.dto.PaymentResponseDTO; | ||
|
|
||
| public interface PaymentService { | ||
| PaymentResponseDTO.CreatePaymentDTO requestPayment(PaymentRequestDTO.CreatePaymentDTO dto); | ||
| void approvePayment(Long paymentId, String pgToken); | ||
| KakaoPayApproveResponseDTO approvePayment(Long paymentId, String pgToken); | ||
| } |
142 changes: 88 additions & 54 deletions
142
src/main/java/com/sumte/payment/service/PaymentServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,61 +1,95 @@ | ||
| package com.sumte.payment.service; | ||
|
|
||
| import com.sumte.apiPayload.code.error.PaymentErrorCode; | ||
| import com.sumte.apiPayload.exception.SumteException; | ||
| import com.sumte.payment.converter.PaymentConverter; | ||
| import com.sumte.payment.dto.PaymentRequestDTO; | ||
| import com.sumte.payment.dto.PaymentResponseDTO; | ||
| import com.sumte.payment.entity.Payment; | ||
| import com.sumte.payment.entity.PaymentStatus; | ||
| import com.sumte.payment.repository.PaymentRepository; | ||
| import com.sumte.reservation.entity.Reservation; | ||
| import com.sumte.reservation.repository.ReservationRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class PaymentServiceImpl implements PaymentService { | ||
|
|
||
| private final ReservationRepository reservationRepository; | ||
| private final PaymentRepository paymentRepository; | ||
| private final PaymentTransactionHelper transactionHelper; | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public PaymentResponseDTO.CreatePaymentDTO requestPayment(PaymentRequestDTO.CreatePaymentDTO dto) { | ||
| Reservation reservation = reservationRepository.findById(dto.getReservationId()) | ||
| .orElseThrow(() -> new SumteException(PaymentErrorCode.RESERVATION_NOT_FOUND)); | ||
|
|
||
| Payment payment; | ||
| try { | ||
| payment = PaymentConverter.toEntity(dto, reservation); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new SumteException(PaymentErrorCode.INVALID_PAYMENT_METHOD); | ||
| } | ||
| paymentRepository.save(payment); | ||
| package com.sumte.payment.service; | ||
|
|
||
| String paymentUrl = "https://pay.mock.kakao.com/" + payment.getId(); | ||
| return PaymentConverter.toCreateResponse(payment, paymentUrl); | ||
| } | ||
| import com.sumte.apiPayload.code.error.PaymentErrorCode; | ||
| import com.sumte.apiPayload.exception.SumteException; | ||
| import com.sumte.payment.converter.PaymentConverter; | ||
| import com.sumte.payment.dto.*; | ||
| import com.sumte.payment.entity.Payment; | ||
| import com.sumte.payment.entity.PaymentStatus; | ||
| import com.sumte.payment.kakaopay.KakaoPayClient; | ||
| import com.sumte.payment.repository.PaymentRepository; | ||
| import com.sumte.reservation.entity.Reservation; | ||
| import com.sumte.reservation.repository.ReservationRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public void approvePayment(Long paymentId, String pgToken) { | ||
| Payment payment = paymentRepository.findById(paymentId) | ||
| .orElseThrow(() -> new SumteException(PaymentErrorCode.PAYMENT_NOT_FOUND)); | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class PaymentServiceImpl implements PaymentService { | ||
|
|
||
| if (payment.getPaymentStatus() == PaymentStatus.PAID) { | ||
| throw new SumteException(PaymentErrorCode.ALREADY_APPROVED_PAYMENT); | ||
| } | ||
| private final ReservationRepository reservationRepository; | ||
| private final PaymentRepository paymentRepository; | ||
| private final PaymentTransactionHelper transactionHelper; | ||
| private final KakaoPayClient kakaoPayClient; | ||
|
|
||
| @Value("${app.host}") | ||
| private String appHost; | ||
|
|
||
| @Override | ||
| @Transactional | ||
| public PaymentResponseDTO.CreatePaymentDTO requestPayment(PaymentRequestDTO.CreatePaymentDTO dto) { | ||
| Reservation reservation = reservationRepository.findById(dto.getReservationId()) | ||
| .orElseThrow(() -> new SumteException(PaymentErrorCode.RESERVATION_NOT_FOUND)); | ||
|
|
||
| Payment payment; | ||
| try { | ||
| payment = PaymentConverter.toEntity(dto, reservation); | ||
| } catch (IllegalArgumentException e) { | ||
| throw new SumteException(PaymentErrorCode.INVALID_PAYMENT_METHOD); | ||
| } | ||
| paymentRepository.save(payment); | ||
|
|
||
|
|
||
| String itemName = reservation.getRoom().getName(); | ||
| Long totalAmount = reservation.getRoom().getPrice(); | ||
|
|
||
| KakaoPayReadyRequestDTO request = KakaoPayReadyRequestDTO.builder() | ||
| .cid("TC0ONETIME") | ||
| .partner_order_id("reservation_" + reservation.getId()) | ||
| .partner_user_id("user_" + reservation.getUser().getId()) | ||
| .item_name(itemName) | ||
| .quantity("1") | ||
| .total_amount(String.valueOf(totalAmount)) | ||
| .tax_free_amount("0") | ||
| .approval_url(appHost + "/pay/success") | ||
| .cancel_url(appHost + "/pay/cancel") | ||
| .fail_url(appHost + "/pay/fail") | ||
| .build(); | ||
|
|
||
| if (pgToken == null || pgToken.isBlank()) { | ||
| transactionHelper.markPaymentFailed(payment); | ||
| throw new SumteException(PaymentErrorCode.PG_TOKEN_MISSING); | ||
| KakaoPayReadyResponseDTO kakaoResponse = kakaoPayClient.requestPayment(request); | ||
| payment.setTid(kakaoResponse.getTid()); | ||
|
|
||
| return PaymentConverter.toCreateResponse(payment, kakaoResponse.getNext_redirect_pc_url()); | ||
| } | ||
|
|
||
| payment.markAsPaid(); | ||
| } | ||
| @Override | ||
| @Transactional | ||
| public KakaoPayApproveResponseDTO approvePayment(Long paymentId, String pgToken) { | ||
| Payment payment = paymentRepository.findById(paymentId) | ||
| .orElseThrow(() -> new SumteException(PaymentErrorCode.PAYMENT_NOT_FOUND)); | ||
|
|
||
| if (payment.getPaymentStatus() == PaymentStatus.PAID) { | ||
| throw new SumteException(PaymentErrorCode.ALREADY_APPROVED_PAYMENT); | ||
| } | ||
|
|
||
| if (pgToken == null || pgToken.isBlank()) { | ||
| transactionHelper.markPaymentFailed(payment); | ||
| throw new SumteException(PaymentErrorCode.PG_TOKEN_MISSING); | ||
| } | ||
|
|
||
| } | ||
| KakaoPayApproveRequestDTO request = KakaoPayApproveRequestDTO.builder() | ||
| .cid("TC0ONETIME") | ||
| .tid(payment.getTid()) | ||
| .partner_order_id("reservation_" + payment.getReservation().getId()) | ||
| .partner_user_id("user_" + payment.getReservation().getUser().getId()) | ||
| .pg_token(pgToken) | ||
| .build(); | ||
|
|
||
| KakaoPayApproveResponseDTO response = kakaoPayClient.approvePayment(request); | ||
| payment.markAsPaid(); | ||
|
|
||
| return response; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
${kakao.pay.secret-key} prod.yml에 반영 부탁드립니다! 키도 저한테 따로 보내주시면 깃허브시크릿으로 배포하겠습니다.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
노션에 키 올려놓으신 거 확인해서 깃허브 시크릿에 등록은 해두었습니다.
다만 변수 이름을 KAKAOPAY_SECRET_KEY로 올려두어서
kakao:
pay:
secret-key: ${KAKAOPAY_SECRET_KEY}
이렇게 prod.yml에 반영 부탁드립니다!