From 7c4c9b4a672815517719099db96099dfc3177ebd Mon Sep 17 00:00:00 2001 From: Joowon Lim Date: Mon, 13 Nov 2023 22:50:48 +0900 Subject: [PATCH 1/6] =?UTF-8?q?Config:=20properties=EC=97=90=20toss=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application.properties | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index bd6a215..7e540fc 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -21,3 +21,9 @@ spring.security.oauth2.client.provider.kakao.authorization-uri=https://kauth.kak spring.security.oauth2.client.provider.kakao.token-uri=https://kauth.kakao.com/oauth/token spring.security.oauth2.client.provider.kakao.user-info-uri=https://kapi.kakao.com/v2/user/me spring.security.oauth2.client.provider.kakao.user-name-attribute=id +#payment +##tosspay +payment.toss.client_key=test_ck_pP2YxJ4K87By0b4RZeo0rRGZwXLO +payment.toss.secret_key=test_sk_pP2YxJ4K87BbBaQPDwEJrRGZwXLO +payment.toss.success_url=http://localhost:8080/api/v1/payment/toss/success +payment.toss.fail_url=http://localhost:8080/api/v1/payment/toss/fail From 7a532f96049a074beeffd99c3837b8ce7c507f31 Mon Sep 17 00:00:00 2001 From: marinesnow34 Date: Tue, 14 Nov 2023 05:30:51 +0900 Subject: [PATCH 2/6] =?UTF-8?q?Config:=20JPA=20=EC=9E=90=EB=8F=99=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/readyvery/readyverydemo/ReadyverydemoApplication.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/readyvery/readyverydemo/ReadyverydemoApplication.java b/src/main/java/com/readyvery/readyverydemo/ReadyverydemoApplication.java index 509e218..f9a9936 100644 --- a/src/main/java/com/readyvery/readyverydemo/ReadyverydemoApplication.java +++ b/src/main/java/com/readyvery/readyverydemo/ReadyverydemoApplication.java @@ -2,7 +2,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +@EnableJpaAuditing @SpringBootApplication public class ReadyverydemoApplication { From 18170a2f6adc477b5dbc48f28db6de795248b721 Mon Sep 17 00:00:00 2001 From: marinesnow34 Date: Tue, 14 Nov 2023 05:31:51 +0900 Subject: [PATCH 3/6] =?UTF-8?q?Feat:=20Payment=EA=B4=80=EB=A0=A8=20dto=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/payment/dto/toss/Cancels.java | 15 +++++ .../src/payment/dto/toss/Card.java | 16 ++++++ .../src/payment/dto/toss/CashReceipt.java | 4 ++ .../src/payment/dto/toss/CashReceipts.java | 4 ++ .../src/payment/dto/toss/Checkout.java | 5 ++ .../src/payment/dto/toss/Discount.java | 5 ++ .../src/payment/dto/toss/EasyPay.java | 7 +++ .../src/payment/dto/toss/Failure.java | 6 ++ .../src/payment/dto/toss/GiftCertificate.java | 4 ++ .../src/payment/dto/toss/MobliePhone.java | 4 ++ .../src/payment/dto/toss/Payments.java | 56 +++++++++++++++++++ .../src/payment/dto/toss/Receipt.java | 5 ++ .../src/payment/dto/toss/Transfer.java | 4 ++ .../src/payment/dto/toss/VirtualAccount.java | 4 ++ 14 files changed, 139 insertions(+) create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Cancels.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Card.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/CashReceipt.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/CashReceipts.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Checkout.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Discount.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/EasyPay.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Failure.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/GiftCertificate.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/MobliePhone.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Payments.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Receipt.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Transfer.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/VirtualAccount.java diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Cancels.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Cancels.java new file mode 100644 index 0000000..020994b --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Cancels.java @@ -0,0 +1,15 @@ +package com.readyvery.readyverydemo.src.payment.dto.toss; + +import java.time.LocalDateTime; + +public class Cancels { + private Long cancelAmount; + private String cancelReason; + private Long taxFreeAmount; + private Long taxExemptionAmount; + private Long refundableAmount; + private Long easyPayDiscountAmount; + private LocalDateTime canceledAt; + private String transactionKey; + private String receiptKey; +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Card.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Card.java new file mode 100644 index 0000000..2cbd02d --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Card.java @@ -0,0 +1,16 @@ +package com.readyvery.readyverydemo.src.payment.dto.toss; + +public class Card { + private Long amount; + private String issuerCode; + private String acquireCode; + private String number; + private Long installmentPlanMonths; + private String approveNo; + private Boolean useCardPoint; + private String cardType; + private String ownerType; + private String aquireStatus; + private Boolean isInterestFree; + private String interestPayer; +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/CashReceipt.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/CashReceipt.java new file mode 100644 index 0000000..6f34e1d --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/CashReceipt.java @@ -0,0 +1,4 @@ +package com.readyvery.readyverydemo.src.payment.dto.toss; + +public class CashReceipt { +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/CashReceipts.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/CashReceipts.java new file mode 100644 index 0000000..3a915d1 --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/CashReceipts.java @@ -0,0 +1,4 @@ +package com.readyvery.readyverydemo.src.payment.dto.toss; + +public class CashReceipts { +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Checkout.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Checkout.java new file mode 100644 index 0000000..f69d043 --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Checkout.java @@ -0,0 +1,5 @@ +package com.readyvery.readyverydemo.src.payment.dto.toss; + +public class Checkout { + private String url; +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Discount.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Discount.java new file mode 100644 index 0000000..06f5f22 --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Discount.java @@ -0,0 +1,5 @@ +package com.readyvery.readyverydemo.src.payment.dto.toss; + +public class Discount { + private Long amount; +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/EasyPay.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/EasyPay.java new file mode 100644 index 0000000..23cc202 --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/EasyPay.java @@ -0,0 +1,7 @@ +package com.readyvery.readyverydemo.src.payment.dto.toss; + +public class EasyPay { + private String provider; + private Long amount; + private Long discountAmount; +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Failure.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Failure.java new file mode 100644 index 0000000..12537a2 --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Failure.java @@ -0,0 +1,6 @@ +package com.readyvery.readyverydemo.src.payment.dto.toss; + +public class Failure { + private String code; + private String message; +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/GiftCertificate.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/GiftCertificate.java new file mode 100644 index 0000000..d4a4832 --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/GiftCertificate.java @@ -0,0 +1,4 @@ +package com.readyvery.readyverydemo.src.payment.dto.toss; + +public class GiftCertificate { +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/MobliePhone.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/MobliePhone.java new file mode 100644 index 0000000..ad36394 --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/MobliePhone.java @@ -0,0 +1,4 @@ +package com.readyvery.readyverydemo.src.payment.dto.toss; + +public class MobliePhone { +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Payments.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Payments.java new file mode 100644 index 0000000..f47003b --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Payments.java @@ -0,0 +1,56 @@ +package com.readyvery.readyverydemo.src.payment.dto.toss; + +import java.time.LocalDateTime; + +import com.readyvery.readyverydemo.src.payment.dto.TossPaymentSuccessRes; + +public class Payments { + private String version; + private String paymentKey; + private String type; + private String orderId; + private String orderName; + private String mid; + private String currency; + private String method; + private Long totalAmount; + private Long balanceAmount; + private String status; + private LocalDateTime requestedAt; + private LocalDateTime approvedAt; + private Boolean useEscrow; + private String lastTransactionKey; // nullable + private Long suppliedAmount; + private Long vat; + private Boolean cultureExpense; + private Long taxFreeAmount; + private Long taxExemptedAmount; + private Cancels cancels; + private Boolean isPartialCancel; + private Card card; + private VirtualAccount virtualAccount; + private String secret; + private MobliePhone mobilePhone; + private GiftCertificate giftCertificate; + private Transfer transfer; + private Receipt receipt; + private Checkout checkout; + private EasyPay easyPay; + private String country; + private Failure failure; + private CashReceipt cashReceipt; + private CashReceipts cashReceipts; + private Discount discount; + + public TossPaymentSuccessRes toTossPaymentSuccessRes() { + return TossPaymentSuccessRes.builder() + .orderId(orderId) + .orderName(orderName) + .method(method) + .totalAmount(totalAmount) + .status(status) + .requestedAt(requestedAt) + .approvedAt(approvedAt) + .build(); + } +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Receipt.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Receipt.java new file mode 100644 index 0000000..0542777 --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Receipt.java @@ -0,0 +1,5 @@ +package com.readyvery.readyverydemo.src.payment.dto.toss; + +public class Receipt { + private String url; +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Transfer.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Transfer.java new file mode 100644 index 0000000..8fa3c73 --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Transfer.java @@ -0,0 +1,4 @@ +package com.readyvery.readyverydemo.src.payment.dto.toss; + +public class Transfer { +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/VirtualAccount.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/VirtualAccount.java new file mode 100644 index 0000000..6777ff7 --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/VirtualAccount.java @@ -0,0 +1,4 @@ +package com.readyvery.readyverydemo.src.payment.dto.toss; + +public class VirtualAccount { +} From 7014cb4ea643e9b50dfd759ba5a631b1ff8d54e5 Mon Sep 17 00:00:00 2001 From: marinesnow34 Date: Tue, 14 Nov 2023 05:32:32 +0900 Subject: [PATCH 4/6] =?UTF-8?q?Feat:=20req,=20res=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/payment/dto/PaymentReq.java | 7 +++++++ .../src/payment/dto/TossPaymentRes.java | 12 ++++++++++++ .../src/payment/dto/TossPaymentSuccessDto.java | 4 ++++ .../src/payment/dto/TossPaymentSuccessRes.java | 17 +++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/PaymentReq.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/TossPaymentRes.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/TossPaymentSuccessDto.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/dto/TossPaymentSuccessRes.java diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/PaymentReq.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/PaymentReq.java new file mode 100644 index 0000000..d385a56 --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/PaymentReq.java @@ -0,0 +1,7 @@ +package com.readyvery.readyverydemo.src.payment.dto; + +public class PaymentReq { + private Long storeId; + private Long coupon; + //TODO: 장바구니, 장바구니 상세 추가 +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/TossPaymentRes.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/TossPaymentRes.java new file mode 100644 index 0000000..87951d1 --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/TossPaymentRes.java @@ -0,0 +1,12 @@ +package com.readyvery.readyverydemo.src.payment.dto; + +public class TossPaymentRes { + private String payType; + private Long amount; + private String orderId; + private String orderName; + private String customerName; + private String customerEmail; + private String successUrl; + private String failUrl; +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/TossPaymentSuccessDto.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/TossPaymentSuccessDto.java new file mode 100644 index 0000000..5b6122f --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/TossPaymentSuccessDto.java @@ -0,0 +1,4 @@ +package com.readyvery.readyverydemo.src.payment.dto; + +public class TossPaymentSuccessDto { +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/dto/TossPaymentSuccessRes.java b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/TossPaymentSuccessRes.java new file mode 100644 index 0000000..75534ad --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/dto/TossPaymentSuccessRes.java @@ -0,0 +1,17 @@ +package com.readyvery.readyverydemo.src.payment.dto; + +import java.time.LocalDateTime; + +import lombok.Builder; + +@Builder +public class TossPaymentSuccessRes { + private String orderId; + private String orderName; + private String method; + private Long totalAmount; + private String status; + private LocalDateTime requestedAt; + private LocalDateTime approvedAt; + +} From 21e6f33337d0b92fdc369de18c970f8c032c43bc Mon Sep 17 00:00:00 2001 From: marinesnow34 Date: Tue, 14 Nov 2023 05:32:54 +0900 Subject: [PATCH 5/6] =?UTF-8?q?Config:=20toss=20config=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/payment/config/TossPaymentConfig.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/config/TossPaymentConfig.java diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/config/TossPaymentConfig.java b/src/main/java/com/readyvery/readyverydemo/src/payment/config/TossPaymentConfig.java new file mode 100644 index 0000000..b8f8d2b --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/config/TossPaymentConfig.java @@ -0,0 +1,24 @@ +package com.readyvery.readyverydemo.src.payment.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; + +import lombok.Getter; + +@Configuration +@Getter +public class TossPaymentConfig { + public static final String CONFIRM_URL = "https://api.tosspayments.com/v1/payments/confirm"; + + @Value("${payment.toss.client_key}") + private String tossClientKey; + + @Value("${payment.toss.secret_key}") + private String tossSecretKey; + + @Value("${payment.toss.success_url}") + private String tossSuccessUrl; + + @Value("${payment.toss.fail_url}") + private String tossFailUrl; +} From 033d7b15042cb88d5eaa329c5248c5da48164b03 Mon Sep 17 00:00:00 2001 From: marinesnow34 Date: Tue, 14 Nov 2023 05:33:21 +0900 Subject: [PATCH 6/6] =?UTF-8?q?Feat:=20=EA=B2=B0=EC=A0=9C=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 결제 요청 구현 - 결제 승인 구현 --- .../src/payment/PaymentController.java | 40 ++++++++++ .../src/payment/PaymentService.java | 11 +++ .../src/payment/PaymentServiceImpl.java | 76 +++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/PaymentController.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/PaymentService.java create mode 100644 src/main/java/com/readyvery/readyverydemo/src/payment/PaymentServiceImpl.java diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/PaymentController.java b/src/main/java/com/readyvery/readyverydemo/src/payment/PaymentController.java new file mode 100644 index 0000000..74e3cce --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/PaymentController.java @@ -0,0 +1,40 @@ +package com.readyvery.readyverydemo.src.payment; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.readyvery.readyverydemo.src.payment.dto.PaymentReq; +import com.readyvery.readyverydemo.src.payment.dto.TossPaymentRes; +import com.readyvery.readyverydemo.src.payment.dto.TossPaymentSuccessRes; + +import lombok.RequiredArgsConstructor; + +@RestController +@RequestMapping("/api/v1/payments") +@RequiredArgsConstructor +public class PaymentController { + private final PaymentService paymentService; + + @PostMapping("/toss") // 토스 결제 요청 + public ResponseEntity requestTossPayment(@RequestBody PaymentReq paymentReq) { + TossPaymentRes tossPaymentRes = paymentService.requestTossPayment(paymentReq); + return new ResponseEntity<>(tossPaymentRes, HttpStatus.OK); + } + + @GetMapping("/toss/success") // 토스 결제 성공 + public ResponseEntity successTossPayment( + @RequestParam String paymentKey, + @RequestParam String orderId, + @RequestParam Long amount) { + + return new ResponseEntity<>( + paymentService.tossPaymentSuccess(paymentKey, orderId, amount).toTossPaymentSuccessRes(), + HttpStatus.OK); + } +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/PaymentService.java b/src/main/java/com/readyvery/readyverydemo/src/payment/PaymentService.java new file mode 100644 index 0000000..2a0cd68 --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/PaymentService.java @@ -0,0 +1,11 @@ +package com.readyvery.readyverydemo.src.payment; + +import com.readyvery.readyverydemo.src.payment.dto.PaymentReq; +import com.readyvery.readyverydemo.src.payment.dto.TossPaymentRes; +import com.readyvery.readyverydemo.src.payment.dto.toss.Payments; + +public interface PaymentService { + TossPaymentRes requestTossPayment(PaymentReq paymentReq); + + Payments tossPaymentSuccess(String paymentKey, String orderId, Long amount); +} diff --git a/src/main/java/com/readyvery/readyverydemo/src/payment/PaymentServiceImpl.java b/src/main/java/com/readyvery/readyverydemo/src/payment/PaymentServiceImpl.java new file mode 100644 index 0000000..6c66d11 --- /dev/null +++ b/src/main/java/com/readyvery/readyverydemo/src/payment/PaymentServiceImpl.java @@ -0,0 +1,76 @@ +package com.readyvery.readyverydemo.src.payment; + +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.Collections; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import net.minidev.json.JSONObject; + +import com.readyvery.readyverydemo.src.payment.config.TossPaymentConfig; +import com.readyvery.readyverydemo.src.payment.dto.PaymentReq; +import com.readyvery.readyverydemo.src.payment.dto.TossPaymentRes; +import com.readyvery.readyverydemo.src.payment.dto.toss.Payments; + +@Service +public class PaymentServiceImpl implements PaymentService { + @Autowired + TossPaymentConfig tosspaymentConfig; + + @Override + public TossPaymentRes requestTossPayment(PaymentReq paymentReq) { + Long amount = calculateTotalAmount(); + + return null; + } + + private Long calculateTotalAmount() { + //TODO: 장바구니, 장바구니 상세에서 총 금액 계산 + return 0L; + } + + @Override + public Payments tossPaymentSuccess(String paymentKey, String orderId, Long amount) { + // TODO: 검증, 결제 완료 처리 + Payments result = requestTossPaymentAccept(paymentKey, orderId, amount); + + return null; + } + + private Payments requestTossPaymentAccept(String paymentKey, String orderId, Long amount) { + RestTemplate restTemplate = new RestTemplate(); + HttpHeaders headers = makeTossHeader(); + JSONObject params = new JSONObject(); + + params.put("amount", amount); + params.put("orderId", orderId); + params.put("paymentKey", paymentKey); + + Payments result = null; + try { + result = restTemplate.postForObject(TossPaymentConfig.CONFIRM_URL, + new HttpEntity<>(params, headers), + Payments.class); + } catch (Exception e) { + throw new RuntimeException(); // 추후에 오류 수정 (이미 승인 됨) + } + + return result; + } + + private HttpHeaders makeTossHeader() { + HttpHeaders headers = new HttpHeaders(); + String encodedAuthKey = new String( + Base64.getEncoder().encode((tosspaymentConfig.getTossSecretKey() + ":").getBytes(StandardCharsets.UTF_8))); + headers.setBasicAuth(encodedAuthKey); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); + return headers; + } +}