-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feat 예약 및 결제 구현
- Loading branch information
Showing
11 changed files
with
248 additions
and
134 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/main/java/univ/goormthon/kongju/domain/payment/SessionUtils.java
This file contains 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 univ.goormthon.kongju.domain.payment; | ||
|
||
import org.springframework.web.context.request.RequestAttributes; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
|
||
import java.util.Objects; | ||
|
||
public class SessionUtils { | ||
|
||
public static void addAttribute(String name, Object value) { | ||
Objects.requireNonNull(RequestContextHolder.getRequestAttributes()).setAttribute(name, value, RequestAttributes.SCOPE_SESSION); | ||
} | ||
|
||
public static String getStringAttributeValue(String name) { | ||
return String.valueOf(getAttribute(name)); | ||
} | ||
|
||
public static Object getAttribute(String name) { | ||
return Objects.requireNonNull(RequestContextHolder.getRequestAttributes()).getAttribute(name, RequestAttributes.SCOPE_SESSION); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/univ/goormthon/kongju/domain/payment/controller/KakaoPayController.java
This file contains 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,38 @@ | ||
package univ.goormthon.kongju.domain.payment.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import univ.goormthon.kongju.domain.payment.SessionUtils; | ||
import univ.goormthon.kongju.domain.payment.dto.request.PayApproveRequest; | ||
import univ.goormthon.kongju.domain.payment.dto.response.PayApproveResponse; | ||
import univ.goormthon.kongju.domain.payment.dto.response.PayReadyResponse; | ||
import univ.goormthon.kongju.domain.payment.service.KakaoPayService; | ||
import univ.goormthon.kongju.domain.reservation.entity.Reservation; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/kongju/payment") | ||
public class KakaoPayController { | ||
|
||
private final KakaoPayService kakaoPayService; | ||
|
||
@PostMapping("/ready") | ||
public ResponseEntity<PayReadyResponse> ready(@RequestBody Reservation reservation, @RequestParam int totalFee) { | ||
PayReadyResponse response = kakaoPayService.ready(reservation, totalFee); | ||
SessionUtils.addAttribute("tid", response.tid()); | ||
return ResponseEntity.ok(response); | ||
} | ||
|
||
@GetMapping("/approval") | ||
public ResponseEntity<PayApproveResponse> approval(@RequestParam("pg_token") String pgToken) { | ||
String tid = SessionUtils.getStringAttributeValue("tid"); | ||
PayApproveResponse response = kakaoPayService.approve(pgToken, tid); | ||
return ResponseEntity.ok(response); | ||
} | ||
|
||
@PostMapping("/cancel") | ||
public ResponseEntity<?> cancel(@RequestParam String tid, @RequestParam int cancelAmount) { | ||
return ResponseEntity.ok(null); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/univ/goormthon/kongju/domain/payment/dto/request/PayApproveRequest.java
This file contains 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 univ.goormthon.kongju.domain.payment.dto.request; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record PayApproveRequest( | ||
@JsonProperty("cid") String cid, | ||
@JsonProperty("tid") String tid, | ||
@JsonProperty("partner_order_id") String partnerOrderId, | ||
@JsonProperty("partner_user_id") String partnerUserId, | ||
@JsonProperty("pg_token") String pgToken | ||
) { | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/univ/goormthon/kongju/domain/payment/dto/request/PayRequest.java
This file contains 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,19 @@ | ||
package univ.goormthon.kongju.domain.payment.dto.request; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record PayRequest( | ||
@JsonProperty("cid") String cid, | ||
@JsonProperty("partner_order_id") String partnerOrderId, | ||
@JsonProperty("partner_user_id") String partnerUserId, | ||
@JsonProperty("item_name") String itemName, | ||
@JsonProperty("quantity") int quantity, | ||
@JsonProperty("total_amount") int totalAmount, | ||
@JsonProperty("tax_free_amount") int taxFreeAmount, | ||
@JsonProperty("approval_url") String approvalUrl, | ||
@JsonProperty("cancel_url") String cancelUrl, | ||
@JsonProperty("fail_url") String failUrl | ||
) { | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/univ/goormthon/kongju/domain/payment/dto/response/PayApproveResponse.java
This file contains 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,26 @@ | ||
package univ.goormthon.kongju.domain.payment.dto.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public record PayApproveResponse( | ||
@JsonProperty("aid") String aid, | ||
@JsonProperty("tid") String tid, | ||
@JsonProperty("cid") String cid, | ||
@JsonProperty("partner_order_id") String partnerOrderId, | ||
@JsonProperty("partner_user_id") String partnerUserId, | ||
@JsonProperty("payment_method_type") String paymentMethodType, | ||
|
||
@JsonProperty("amount") Amount amount, | ||
@JsonProperty("item_name") String itemName, | ||
@JsonProperty("approved_at") LocalDateTime approvedAt | ||
|
||
) { | ||
public record Amount( | ||
@JsonProperty("total") int total, | ||
@JsonProperty("tax_free") int taxFree, | ||
@JsonProperty("vat") int vat, | ||
@JsonProperty("point") int point, | ||
@JsonProperty("discount") int discount) {} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/univ/goormthon/kongju/domain/payment/dto/response/PayReadyResponse.java
This file contains 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,9 @@ | ||
package univ.goormthon.kongju.domain.payment.dto.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record PayReadyResponse( | ||
@JsonProperty("tid") String tid, | ||
@JsonProperty("next_redirect_pc_url") String nextRedirectPcUrl | ||
) { | ||
} |
70 changes: 70 additions & 0 deletions
70
src/main/java/univ/goormthon/kongju/domain/payment/service/KakaoPayService.java
This file contains 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,70 @@ | ||
package univ.goormthon.kongju.domain.payment.service; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
import univ.goormthon.kongju.domain.payment.dto.request.PayApproveRequest; | ||
import univ.goormthon.kongju.domain.payment.dto.request.PayRequest; | ||
import univ.goormthon.kongju.domain.payment.dto.response.PayApproveResponse; | ||
import univ.goormthon.kongju.domain.payment.dto.response.PayReadyResponse; | ||
import univ.goormthon.kongju.domain.reservation.entity.Reservation; | ||
|
||
@Service | ||
public class KakaoPayService { | ||
|
||
@Value("${kakaopay.secret-key}") | ||
private String secretKey; | ||
|
||
@Value("${kakaopay.cid}") | ||
private String cid; | ||
|
||
public PayReadyResponse ready(Reservation reservation, int totalFee) { | ||
PayRequest request = PayRequest.builder() | ||
.partnerOrderId(String.valueOf(reservation.getId())) | ||
.partnerUserId(String.valueOf(reservation.getMemberId())) | ||
.itemName("공유 주차장 결제 진행") | ||
.quantity(1) | ||
.totalAmount(totalFee) | ||
.taxFreeAmount(0) | ||
.approvalUrl("http://localhost:8080/api/kongju/payment/approval") | ||
.cancelUrl("http://localhost:8080/api/kongju/payment/cancel") | ||
.failUrl("http://localhost:8080/api/kongju/payment/fail") | ||
.build(); | ||
|
||
HttpEntity<PayRequest> entity = new HttpEntity<>(request, this.getHeaders()); | ||
|
||
RestTemplate restTemplate = new RestTemplate(); | ||
String url = "https://kapi.kakao.com/v1/payment/ready"; | ||
ResponseEntity<PayReadyResponse> response = restTemplate.postForEntity(url, request, PayReadyResponse.class); | ||
|
||
return response.getBody(); | ||
} | ||
|
||
public PayApproveResponse approve(String pgToken, String tid) { | ||
PayApproveRequest request = PayApproveRequest.builder() | ||
.cid(cid) | ||
.tid(tid) | ||
.partnerOrderId("1") | ||
.partnerUserId("1") | ||
.pgToken(pgToken) | ||
.build(); | ||
|
||
HttpEntity<PayApproveRequest> entity = new HttpEntity<>(request, this.getHeaders()); | ||
|
||
RestTemplate restTemplate = new RestTemplate(); | ||
String url = "https://kapi.kakao.com/v1/payment/approve"; | ||
|
||
return restTemplate.postForObject(url, request, PayApproveResponse.class); | ||
} | ||
|
||
private HttpHeaders getHeaders() { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("Authorization", "SECRET_KEY " + secretKey); | ||
headers.set("Content-type", "application/json"); | ||
|
||
return headers; | ||
} | ||
} |
This file contains 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
Oops, something went wrong.