1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
| @ActiveProfiles("test") @SpringBootTest class PaymentConfirmApiServiceTest {
@Autowired private ProductRepository productRepository;
@Autowired private PaymentEventRepository paymentEventRepository;
@Autowired private PaymentOrderRepository paymentOrderRepository;
@Autowired private PaymentOrderHistoryRepository paymentOrderHistoryRepository;
@Autowired private PaymentCheckOutApiService paymentCheckOutApiService;
@Autowired private PaymentConfirmApiService paymentConfirmApiService;
@Autowired private PaymentEventQueryService paymentEventQueryService;
@MockBean protected TossPaymentClient tossPaymentClient;
@AfterEach void tearDown() { paymentOrderHistoryRepository.deleteAllInBatch(); paymentOrderRepository.deleteAllInBatch(); productRepository.deleteAllInBatch(); paymentEventRepository.deleteAllInBatch(); }
@Test @DisplayName("상품 주문 하기 위한 Confirm 기능을 실행 합니다.") void paymentConfirm() {
Product product1 = this.createProduct("AAA", "상품A", BigDecimal.valueOf(1000)); Product product2 = this.createProduct("BBB", "상품B", BigDecimal.valueOf(2000)); Product product3 = this.createProduct("CCC", "상품C", BigDecimal.valueOf(3000));
List<Product> productList = productRepository.saveAll(List.of(product1, product2, product3)); List<ProductOutPut> productOutPutList = ProductOutPut.of(productList); List<Long> productNoList = productOutPutList.stream() .map(ProductOutPut::getNo) .toList();
PaymentCheckOutRequest paymentCheckOutRequest = PaymentCheckOutRequest.of(productNoList); PaymentCheckOutResponse paymentCheckOutResponse = paymentCheckOutApiService.paymentCheckOut(paymentCheckOutRequest);
int intTotalAmount = 6000; String totalAmount = String.valueOf(intTotalAmount);
String paymentKey = this.paymentIdb64uuid(); PaymentConfirmRequest paymentConfirmRequest = PaymentConfirmRequest.of( paymentKey, paymentCheckOutResponse.getOrderId(), totalAmount );
this.createStubbingTossPostPayment(paymentKey, paymentCheckOutResponse.getOrderId(), totalAmount);
paymentConfirmApiService.paymentConfirm(paymentConfirmRequest);
List<PaymentEventOutPut> paymentEventOutPutList = paymentEventQueryService.selectPayments();
assertThat(paymentEventOutPutList).hasSize(1) .extracting("paymentKey", "orderId", "isPaymentDone") .usingRecursiveFieldByFieldElementComparator( RecursiveComparisonConfiguration.builder() .withComparatorForType(BigDecimal::compareTo, BigDecimal.class).build() ) .containsExactlyInAnyOrder( tuple(paymentKey, paymentCheckOutResponse.getOrderId(), true) );
for (PaymentEventOutPut paymentEventOutPut : paymentEventOutPutList) {
BigDecimal responseTotalAmount = paymentEventOutPut.getTotalAmount(); assertThat(responseTotalAmount).isEqualByComparingTo(BigDecimal.valueOf(intTotalAmount));
List<PaymentOrderOutPut> paymentOrderList = paymentEventOutPut.getPaymentOrderList();
assertThat(paymentOrderList).hasSize(3) .extracting("productId", "name", "amount", "status") .usingRecursiveFieldByFieldElementComparator( RecursiveComparisonConfiguration.builder() .withComparatorForType(BigDecimal::compareTo, BigDecimal.class).build() ) .containsExactlyInAnyOrder( tuple("AAA", "상품A", BigDecimal.valueOf(1000), PaymentOrderStatus.SUCCESS), tuple("BBB", "상품B", BigDecimal.valueOf(2000), PaymentOrderStatus.SUCCESS), tuple("CCC", "상품C", BigDecimal.valueOf(3000), PaymentOrderStatus.SUCCESS) ); } }
@Test @DisplayName("상품 주문 하기 위한 Confirm 기능을 실행시 경우 예외가 발생 합니다. - 재시도가 가능하지 않는 결제 에러") void paymentConfirmThenThrow() {
Product product1 = this.createProduct("AAA", "상품A", BigDecimal.valueOf(1000)); Product product2 = this.createProduct("BBB", "상품B", BigDecimal.valueOf(2000)); Product product3 = this.createProduct("CCC", "상품C", BigDecimal.valueOf(3000));
List<Product> productList = productRepository.saveAll(List.of(product1, product2, product3)); List<ProductOutPut> productOutPutList = ProductOutPut.of(productList); List<Long> productNoList = productOutPutList.stream() .map(ProductOutPut::getNo) .toList();
PaymentCheckOutRequest paymentCheckOutRequest = PaymentCheckOutRequest.of(productNoList); PaymentCheckOutResponse paymentCheckOutResponse = paymentCheckOutApiService.paymentCheckOut(paymentCheckOutRequest);
int intTotalAmount = 6000; String totalAmount = String.valueOf(intTotalAmount);
String paymentKey = this.paymentIdb64uuid(); PaymentConfirmRequest paymentConfirmRequest = PaymentConfirmRequest.of( paymentKey, paymentCheckOutResponse.getOrderId(), totalAmount );
this.createStubbingTossPostPaymentThenThrow();
paymentConfirmApiService.paymentConfirm(paymentConfirmRequest);
List<PaymentEventOutPut> paymentEventOutPutList = paymentEventQueryService.selectPayments();
assertThat(paymentEventOutPutList).hasSize(1) .extracting("paymentKey", "orderId", "isPaymentDone") .usingRecursiveFieldByFieldElementComparator( RecursiveComparisonConfiguration.builder() .withComparatorForType(BigDecimal::compareTo, BigDecimal.class).build() ) .containsExactlyInAnyOrder( tuple(paymentKey, paymentCheckOutResponse.getOrderId(), false) );
for (PaymentEventOutPut paymentEventOutPut : paymentEventOutPutList) {
BigDecimal responseTotalAmount = paymentEventOutPut.getTotalAmount(); assertThat(responseTotalAmount).isEqualByComparingTo(BigDecimal.valueOf(intTotalAmount));
List<PaymentOrderOutPut> paymentOrderList = paymentEventOutPut.getPaymentOrderList();
assertThat(paymentOrderList).hasSize(3) .extracting("productId", "name", "amount", "status") .usingRecursiveFieldByFieldElementComparator( RecursiveComparisonConfiguration.builder() .withComparatorForType(BigDecimal::compareTo, BigDecimal.class).build() ) .containsExactlyInAnyOrder( tuple("AAA", "상품A", BigDecimal.valueOf(1000), PaymentOrderStatus.FAILURE), tuple("BBB", "상품B", BigDecimal.valueOf(2000), PaymentOrderStatus.FAILURE), tuple("CCC", "상품C", BigDecimal.valueOf(3000), PaymentOrderStatus.FAILURE) ); } }
@Test @DisplayName("상품 주문 하기 위한 Confirm 기능을 실행시 경우 예외가 발생 합니다. - 이미 완료된 결제건") void paymentConfirmAlreadyCompletedPaymentThenThrow() {
Product product1 = this.createProduct("AAA", "상품A", BigDecimal.valueOf(1000)); Product product2 = this.createProduct("BBB", "상품B", BigDecimal.valueOf(2000)); Product product3 = this.createProduct("CCC", "상품C", BigDecimal.valueOf(3000));
List<Product> productList = productRepository.saveAll(List.of(product1, product2, product3)); List<ProductOutPut> productOutPutList = ProductOutPut.of(productList); List<Long> productNoList = productOutPutList.stream() .map(ProductOutPut::getNo) .toList();
PaymentCheckOutRequest paymentCheckOutRequest = PaymentCheckOutRequest.of(productNoList); PaymentCheckOutResponse paymentCheckOutResponse = paymentCheckOutApiService.paymentCheckOut(paymentCheckOutRequest);
int intTotalAmount = 6000; String totalAmount = String.valueOf(intTotalAmount);
String paymentKey = this.paymentIdb64uuid(); PaymentConfirmRequest paymentConfirmRequest = PaymentConfirmRequest.of( paymentKey, paymentCheckOutResponse.getOrderId(), totalAmount );
this.createStubbingAlreadyCompletedTossPostPaymentThenThrow(paymentKey, paymentCheckOutResponse.getOrderId(), totalAmount);
paymentConfirmApiService.paymentConfirm(paymentConfirmRequest);
List<PaymentEventOutPut> paymentEventOutPutList = paymentEventQueryService.selectPayments();
assertThat(paymentEventOutPutList).hasSize(1) .extracting("paymentKey", "orderId", "isPaymentDone") .usingRecursiveFieldByFieldElementComparator( RecursiveComparisonConfiguration.builder() .withComparatorForType(BigDecimal::compareTo, BigDecimal.class).build() ) .containsExactlyInAnyOrder( tuple(paymentKey, paymentCheckOutResponse.getOrderId(), true) );
for (PaymentEventOutPut paymentEventOutPut : paymentEventOutPutList) {
BigDecimal responseTotalAmount = paymentEventOutPut.getTotalAmount(); assertThat(responseTotalAmount).isEqualByComparingTo(BigDecimal.valueOf(intTotalAmount));
List<PaymentOrderOutPut> paymentOrderList = paymentEventOutPut.getPaymentOrderList();
assertThat(paymentOrderList).hasSize(3) .extracting("productId", "name", "amount", "status") .usingRecursiveFieldByFieldElementComparator( RecursiveComparisonConfiguration.builder() .withComparatorForType(BigDecimal::compareTo, BigDecimal.class).build() ) .containsExactlyInAnyOrder( tuple("AAA", "상품A", BigDecimal.valueOf(1000), PaymentOrderStatus.SUCCESS), tuple("BBB", "상품B", BigDecimal.valueOf(2000), PaymentOrderStatus.SUCCESS), tuple("CCC", "상품C", BigDecimal.valueOf(3000), PaymentOrderStatus.SUCCESS) ); } }
@Test @DisplayName("결제 금액이 유효하지 않을 경우 예외가 발생 합니다.") void invalidPaymentAmountThenThrow() {
Product product1 = this.createProduct("AAA", "상품A", BigDecimal.valueOf(1000)); Product product2 = this.createProduct("BBB", "상품B", BigDecimal.valueOf(2000)); Product product3 = this.createProduct("CCC", "상품C", BigDecimal.valueOf(3000));
List<Product> productList = productRepository.saveAll(List.of(product1, product2, product3)); List<ProductOutPut> productOutPutList = ProductOutPut.of(productList); List<Long> productNoList = productOutPutList.stream() .map(ProductOutPut::getNo) .toList();
PaymentCheckOutRequest paymentCheckOutRequest = PaymentCheckOutRequest.of(productNoList); PaymentCheckOutResponse paymentCheckOutResponse = paymentCheckOutApiService.paymentCheckOut(paymentCheckOutRequest);
int intTotalAmount = 5000; String totalAmount = String.valueOf(intTotalAmount);
String paymentKey = this.paymentIdb64uuid(); PaymentConfirmRequest paymentConfirmRequest = PaymentConfirmRequest.of( paymentKey, paymentCheckOutResponse.getOrderId(), totalAmount );
assertThatThrownBy(() -> paymentConfirmApiService.paymentConfirm(paymentConfirmRequest)) .isInstanceOf(BusinessException.class) .hasMessage("결제 금액이 유효하지 않습니다."); }
private void createStubbingTossPostPayment(String paymentKey, String orderId, String amount) {
String responsePostPaymentsBody = "{\"mId\":\"tvivarepublica\",\"lastTransactionKey\":\"F69384D0887558A85A78199D534FBBD5\",\"paymentKey\":\"" + paymentKey + "\",\"orderId\":\"" + orderId + "\",\"orderName\":\"상품A 그외\",\"taxExemptionAmount\":0,\"status\":\"DONE\",\"requestedAt\":\"2024-11-30T16:53:34+09:00\",\"approvedAt\":\"2024-11-30T16:53:57+09:00\",\"useEscrow\":false,\"cultureExpense\":false,\"card\":{\"issuerCode\":\"11\",\"acquirerCode\":\"11\",\"number\":\"52361232****606*\",\"installmentPlanMonths\":0,\"isInterestFree\":false,\"interestPayer\":null,\"approveNo\":\"00000000\",\"useCardPoint\":false,\"cardType\":\"신용\",\"ownerType\":\"개인\",\"acquireStatus\":\"READY\",\"amount\":" + amount + "},\"virtualAccount\":null,\"transfer\":null,\"mobilePhone\":null,\"giftCertificate\":null,\"cashReceipt\":null,\"cashReceipts\":null,\"discount\":null,\"cancels\":null,\"secret\":\"ps_GePWvyJnrKvelyyyB2pLVgLzN97E\",\"type\":\"NORMAL\",\"easyPay\":null,\"country\":\"KR\",\"failure\":null,\"isPartialCancelable\":true,\"receipt\":{\"url\":\"https://dashboard.tosspayments.com/receipt/redirection?transactionId=tviva20241130165334qVdY4&ref=PX\"},\"checkout\":{\"url\":\"https://api.tosspayments.com/v1/payments/tviva20241130165334qVdY4/checkout\"},\"currency\":\"KRW\",\"totalAmount\":" + amount + ",\"balanceAmount\":6000,\"suppliedAmount\":5455,\"vat\":545,\"taxFreeAmount\":0,\"method\":\"카드\",\"version\":\"2022-11-16\",\"metadata\":null}"; when(tossPaymentClient.paymentConfirm( any(String.class), any(PaymentConfirmInPut.class) )).thenReturn(ResponseEntity.ok(responsePostPaymentsBody)); }
private void createStubbingTossPostPaymentThenThrow() {
PaymentException paymentException = new PaymentException( PSPErrorCode.CLIENT_ERROR, "INVALID_STOPPED_CARD", new String[]{"정지된 카드 입니다."}, "{ \"code\": \"INVALID_STOPPED_CARD\", \"message\": \"정지된 카드 입니다.\" }" );
when(tossPaymentClient.paymentConfirm( any(String.class), any(PaymentConfirmInPut.class) )).thenThrow(paymentException); }
private void createStubbingAlreadyCompletedTossPostPaymentThenThrow(String paymentKey, String orderId, String amount) {
PaymentException paymentException = new PaymentException( PSPErrorCode.CLIENT_ERROR, "ALREADY_COMPLETED_PAYMENT", new String[]{"이미 완료된 결제 입니다."}, "{ \"code\": \"ALREADY_COMPLETED_PAYMENT\", \"message\": \"이미 완료된 결제 입니다.\" }" );
when(tossPaymentClient.paymentConfirm( any(String.class), any(PaymentConfirmInPut.class) )).thenThrow(paymentException);
String responsePostPaymentsBody = "{\"mId\":\"tvivarepublica\",\"lastTransactionKey\":\"F69384D0887558A85A78199D534FBBD5\",\"paymentKey\":\"" + paymentKey + "\",\"orderId\":\"" + orderId + "\",\"orderName\":\"상품A 그외\",\"taxExemptionAmount\":0,\"status\":\"DONE\",\"requestedAt\":\"2024-11-30T16:53:34+09:00\",\"approvedAt\":\"2024-11-30T16:53:57+09:00\",\"useEscrow\":false,\"cultureExpense\":false,\"card\":{\"issuerCode\":\"11\",\"acquirerCode\":\"11\",\"number\":\"52361232****606*\",\"installmentPlanMonths\":0,\"isInterestFree\":false,\"interestPayer\":null,\"approveNo\":\"00000000\",\"useCardPoint\":false,\"cardType\":\"신용\",\"ownerType\":\"개인\",\"acquireStatus\":\"READY\",\"amount\":" + amount + "},\"virtualAccount\":null,\"transfer\":null,\"mobilePhone\":null,\"giftCertificate\":null,\"cashReceipt\":null,\"cashReceipts\":null,\"discount\":null,\"cancels\":null,\"secret\":\"ps_GePWvyJnrKvelyyyB2pLVgLzN97E\",\"type\":\"NORMAL\",\"easyPay\":null,\"country\":\"KR\",\"failure\":null,\"isPartialCancelable\":true,\"receipt\":{\"url\":\"https://dashboard.tosspayments.com/receipt/redirection?transactionId=tviva20241130165334qVdY4&ref=PX\"},\"checkout\":{\"url\":\"https://api.tosspayments.com/v1/payments/tviva20241130165334qVdY4/checkout\"},\"currency\":\"KRW\",\"totalAmount\":" + amount + ",\"balanceAmount\":6000,\"suppliedAmount\":5455,\"vat\":545,\"taxFreeAmount\":0,\"method\":\"카드\",\"version\":\"2022-11-16\",\"metadata\":null}"; when(tossPaymentClient.getPayments( any(String.class) )).thenReturn(ResponseEntity.ok(responsePostPaymentsBody)); }
private Product createProduct(String productId, String name, BigDecimal price) {
return Product.of(productId, name, price); }
private String paymentIdb64uuid() { SecureRandom secureRandom = new SecureRandom(); byte[] randomBytes = new byte[25]; secureRandom.nextBytes(randomBytes); String identifier = Base64.getUrlEncoder().withoutPadding().encodeToString(randomBytes); return identifier.substring(0, 30); } }
|