-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from marinesnow34/tossPay
Feat: 결제 로직 구현
- Loading branch information
Showing
24 changed files
with
338 additions
and
0 deletions.
There are no files selected for viewing
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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/readyvery/readyverydemo/src/payment/PaymentController.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,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<TossPaymentRes> requestTossPayment(@RequestBody PaymentReq paymentReq) { | ||
TossPaymentRes tossPaymentRes = paymentService.requestTossPayment(paymentReq); | ||
return new ResponseEntity<>(tossPaymentRes, HttpStatus.OK); | ||
} | ||
|
||
@GetMapping("/toss/success") // 토스 결제 성공 | ||
public ResponseEntity<TossPaymentSuccessRes> successTossPayment( | ||
@RequestParam String paymentKey, | ||
@RequestParam String orderId, | ||
@RequestParam Long amount) { | ||
|
||
return new ResponseEntity<>( | ||
paymentService.tossPaymentSuccess(paymentKey, orderId, amount).toTossPaymentSuccessRes(), | ||
HttpStatus.OK); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/readyvery/readyverydemo/src/payment/PaymentService.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,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); | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/com/readyvery/readyverydemo/src/payment/PaymentServiceImpl.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,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; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/readyvery/readyverydemo/src/payment/config/TossPaymentConfig.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,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; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/readyvery/readyverydemo/src/payment/dto/PaymentReq.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,7 @@ | ||
package com.readyvery.readyverydemo.src.payment.dto; | ||
|
||
public class PaymentReq { | ||
private Long storeId; | ||
private Long coupon; | ||
//TODO: 장바구니, 장바구니 상세 추가 | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/readyvery/readyverydemo/src/payment/dto/TossPaymentRes.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,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; | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/readyvery/readyverydemo/src/payment/dto/TossPaymentSuccessDto.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,4 @@ | ||
package com.readyvery.readyverydemo.src.payment.dto; | ||
|
||
public class TossPaymentSuccessDto { | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/readyvery/readyverydemo/src/payment/dto/TossPaymentSuccessRes.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,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; | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Cancels.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,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; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Card.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,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; | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/CashReceipt.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,4 @@ | ||
package com.readyvery.readyverydemo.src.payment.dto.toss; | ||
|
||
public class CashReceipt { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/CashReceipts.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,4 @@ | ||
package com.readyvery.readyverydemo.src.payment.dto.toss; | ||
|
||
public class CashReceipts { | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Checkout.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,5 @@ | ||
package com.readyvery.readyverydemo.src.payment.dto.toss; | ||
|
||
public class Checkout { | ||
private String url; | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Discount.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,5 @@ | ||
package com.readyvery.readyverydemo.src.payment.dto.toss; | ||
|
||
public class Discount { | ||
private Long amount; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/EasyPay.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,7 @@ | ||
package com.readyvery.readyverydemo.src.payment.dto.toss; | ||
|
||
public class EasyPay { | ||
private String provider; | ||
private Long amount; | ||
private Long discountAmount; | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Failure.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,6 @@ | ||
package com.readyvery.readyverydemo.src.payment.dto.toss; | ||
|
||
public class Failure { | ||
private String code; | ||
private String message; | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/GiftCertificate.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,4 @@ | ||
package com.readyvery.readyverydemo.src.payment.dto.toss; | ||
|
||
public class GiftCertificate { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/MobliePhone.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,4 @@ | ||
package com.readyvery.readyverydemo.src.payment.dto.toss; | ||
|
||
public class MobliePhone { | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Payments.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,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(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Receipt.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,5 @@ | ||
package com.readyvery.readyverydemo.src.payment.dto.toss; | ||
|
||
public class Receipt { | ||
private String url; | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/Transfer.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,4 @@ | ||
package com.readyvery.readyverydemo.src.payment.dto.toss; | ||
|
||
public class Transfer { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/readyvery/readyverydemo/src/payment/dto/toss/VirtualAccount.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,4 @@ | ||
package com.readyvery.readyverydemo.src.payment.dto.toss; | ||
|
||
public class VirtualAccount { | ||
} |
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