From aba45f8bb480d0b2b7cfe9cde55831aef88e3473 Mon Sep 17 00:00:00 2001 From: Combi153 Date: Sat, 2 Mar 2024 09:46:29 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EA=B2=B0=EC=A0=9C=20=EC=8B=A4?= =?UTF-8?q?=ED=8C=A8=20=EB=A1=9C=EA=B9=85=20=EB=B0=A9=EB=B2=95=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=EB=B0=8F=20=EB=A9=94=EC=84=9C=EB=93=9C,=20?= =?UTF-8?q?=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=9D=B4=EB=A6=84=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../petqua/application/payment/PaymentDtos.kt | 6 ++-- .../payment/PaymentFacadeService.kt | 16 +++++++---- .../application/payment/PaymentService.kt | 2 +- .../presentation/payment/PaymentController.kt | 6 ++-- .../presentation/payment/PaymentDtos.kt | 8 +++--- .../payment/PaymentFacadeServiceTest.kt | 28 +++++++++---------- .../payment/PaymentControllerSteps.kt | 12 ++++---- .../payment/PaymentControllerTest.kt | 24 ++++++++-------- .../petqua/test/fixture/PaymentFixtures.kt | 16 +++++------ 9 files changed, 62 insertions(+), 56 deletions(-) diff --git a/src/main/kotlin/com/petqua/application/payment/PaymentDtos.kt b/src/main/kotlin/com/petqua/application/payment/PaymentDtos.kt index 413e4765..e992ca99 100644 --- a/src/main/kotlin/com/petqua/application/payment/PaymentDtos.kt +++ b/src/main/kotlin/com/petqua/application/payment/PaymentDtos.kt @@ -7,7 +7,7 @@ import com.petqua.exception.payment.FailPaymentException import com.petqua.exception.payment.FailPaymentExceptionType.INVALID_ORDER_ID import java.math.BigDecimal -data class PayOrderCommand( +data class SucceedPaymentCommand( val memberId: Long, val paymentType: TossPaymentType, val orderNumber: OrderNumber, @@ -29,8 +29,8 @@ data class PayOrderCommand( orderId: String, paymentKey: String, amount: BigDecimal, - ): PayOrderCommand { - return PayOrderCommand( + ): SucceedPaymentCommand { + return SucceedPaymentCommand( memberId = memberId, paymentType = TossPaymentType.from(paymentType), orderNumber = OrderNumber.from(orderId), diff --git a/src/main/kotlin/com/petqua/application/payment/PaymentFacadeService.kt b/src/main/kotlin/com/petqua/application/payment/PaymentFacadeService.kt index 7491884d..a12c2c58 100644 --- a/src/main/kotlin/com/petqua/application/payment/PaymentFacadeService.kt +++ b/src/main/kotlin/com/petqua/application/payment/PaymentFacadeService.kt @@ -2,6 +2,7 @@ package com.petqua.application.payment import com.petqua.exception.payment.FailPaymentCode.PAY_PROCESS_ABORTED import com.petqua.exception.payment.FailPaymentCode.PAY_PROCESS_CANCELED +import com.petqua.exception.payment.FailPaymentCode.REJECT_CARD_COMPANY import org.slf4j.LoggerFactory import org.springframework.stereotype.Service @@ -13,20 +14,25 @@ class PaymentFacadeService( private val log = LoggerFactory.getLogger(PaymentFacadeService::class.java) - fun payOrder(command: PayOrderCommand) { + fun succeedPayment(command: SucceedPaymentCommand) { paymentService.validateAmount(command) val paymentResponse = paymentGatewayService.confirmPayment(command.toPaymentConfirmRequest()) paymentService.save(paymentResponse.toPayment()) } fun failPayment(command: FailPaymentCommand): FailPaymentResponse { - if (command.code == PAY_PROCESS_ABORTED) { - log.error("PG사에서 결제가 중단되었습니다. message: ${command.message}, OrderNumber: ${command.orderNumber}, MemberId: ${command.memberId}") - } - + log(command) if (command.code != PAY_PROCESS_CANCELED) { paymentService.cancelOrder(command.memberId, command.toOrderNumber()) } return FailPaymentResponse(command.code, command.message) } + + private fun log(command: FailPaymentCommand) { + when (command.code) { + PAY_PROCESS_ABORTED -> log.error("PG사에서 결제를 중단했습니다. message: ${command.message}, OrderNumber: ${command.orderNumber}, MemberId: ${command.memberId}") + PAY_PROCESS_CANCELED -> log.warn("사용자가 결제를 중단했습니다. message: ${command.message}, OrderNumber: ${command.orderNumber}, MemberId: ${command.memberId}") + REJECT_CARD_COMPANY -> log.warn("카드사에서 결제를 거절했습니다.. message: ${command.message}, OrderNumber: ${command.orderNumber}, MemberId: ${command.memberId}") + } + } } diff --git a/src/main/kotlin/com/petqua/application/payment/PaymentService.kt b/src/main/kotlin/com/petqua/application/payment/PaymentService.kt index a3c7ebcb..06405100 100644 --- a/src/main/kotlin/com/petqua/application/payment/PaymentService.kt +++ b/src/main/kotlin/com/petqua/application/payment/PaymentService.kt @@ -18,7 +18,7 @@ class PaymentService( ) { @Transactional(readOnly = true) - fun validateAmount(command: PayOrderCommand) { + fun validateAmount(command: SucceedPaymentCommand) { val order = orderRepository.findByOrderNumberOrThrow(command.orderNumber) { OrderException(ORDER_NOT_FOUND) } diff --git a/src/main/kotlin/com/petqua/presentation/payment/PaymentController.kt b/src/main/kotlin/com/petqua/presentation/payment/PaymentController.kt index a35c8b45..3fc96d64 100644 --- a/src/main/kotlin/com/petqua/presentation/payment/PaymentController.kt +++ b/src/main/kotlin/com/petqua/presentation/payment/PaymentController.kt @@ -21,11 +21,11 @@ class PaymentController( ) { @PostMapping("/success") - fun payOrder( + fun succeedPayment( @Auth loginMember: LoginMember, - request: PayOrderRequest, + request: SucceedPaymentRequest, ): ResponseEntity { - paymentFacadeService.payOrder(request.toCommand(loginMember.memberId)) + paymentFacadeService.succeedPayment(request.toCommand(loginMember.memberId)) return ResponseEntity.noContent().build() } diff --git a/src/main/kotlin/com/petqua/presentation/payment/PaymentDtos.kt b/src/main/kotlin/com/petqua/presentation/payment/PaymentDtos.kt index 97f59f95..a8319578 100644 --- a/src/main/kotlin/com/petqua/presentation/payment/PaymentDtos.kt +++ b/src/main/kotlin/com/petqua/presentation/payment/PaymentDtos.kt @@ -1,18 +1,18 @@ package com.petqua.presentation.payment import com.petqua.application.payment.FailPaymentCommand -import com.petqua.application.payment.PayOrderCommand +import com.petqua.application.payment.SucceedPaymentCommand import java.math.BigDecimal -data class PayOrderRequest( +data class SucceedPaymentRequest( val paymentType: String, val orderId: String, val paymentKey: String, val amount: BigDecimal, ) { - fun toCommand(memberId: Long): PayOrderCommand { - return PayOrderCommand.of( + fun toCommand(memberId: Long): SucceedPaymentCommand { + return SucceedPaymentCommand.of( memberId = memberId, paymentType = paymentType, orderId = orderId, diff --git a/src/test/kotlin/com/petqua/application/payment/PaymentFacadeServiceTest.kt b/src/test/kotlin/com/petqua/application/payment/PaymentFacadeServiceTest.kt index c4f15062..96d3a562 100644 --- a/src/test/kotlin/com/petqua/application/payment/PaymentFacadeServiceTest.kt +++ b/src/test/kotlin/com/petqua/application/payment/PaymentFacadeServiceTest.kt @@ -24,7 +24,7 @@ import com.petqua.test.DataCleaner import com.petqua.test.fixture.failPaymentCommand import com.petqua.test.fixture.member import com.petqua.test.fixture.order -import com.petqua.test.fixture.payOrderCommand +import com.petqua.test.fixture.succeedPaymentCommand import io.kotest.assertions.assertSoftly import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.BehaviorSpec @@ -47,7 +47,7 @@ class PaymentFacadeServiceTest( @SpykBean private val tossPaymentsApiClient: TossPaymentsApiClient, ) : BehaviorSpec({ - Given("주문을 결제할 때") { + Given("결제 성공을 처리할 때") { val member = memberRepository.save(member()) val order = orderRepository.save( order( @@ -58,8 +58,8 @@ class PaymentFacadeServiceTest( ) When("유효한 요쳥이면") { - paymentFacadeService.payOrder( - command = payOrderCommand( + paymentFacadeService.succeedPayment( + command = succeedPaymentCommand( memberId = order.memberId, orderNumber = order.orderNumber, amount = order.totalAmount, @@ -74,8 +74,8 @@ class PaymentFacadeServiceTest( } When("결제 승인에 성공하면") { - paymentFacadeService.payOrder( - command = payOrderCommand( + paymentFacadeService.succeedPayment( + command = succeedPaymentCommand( memberId = order.memberId, orderNumber = order.orderNumber, amount = order.totalAmount, @@ -100,8 +100,8 @@ class PaymentFacadeServiceTest( Then("예외를 던진다") { shouldThrow { - paymentFacadeService.payOrder( - command = payOrderCommand( + paymentFacadeService.succeedPayment( + command = succeedPaymentCommand( memberId = order.memberId, orderNumber = orderNumber, amount = order.totalAmount, @@ -116,8 +116,8 @@ class PaymentFacadeServiceTest( Then("예외를 던진다") { shouldThrow { - paymentFacadeService.payOrder( - command = payOrderCommand( + paymentFacadeService.succeedPayment( + command = succeedPaymentCommand( memberId = memberId, orderNumber = order.orderNumber, amount = order.totalAmount, @@ -132,8 +132,8 @@ class PaymentFacadeServiceTest( Then("예외를 던진다") { shouldThrow { - paymentFacadeService.payOrder( - command = payOrderCommand( + paymentFacadeService.succeedPayment( + command = succeedPaymentCommand( memberId = order.memberId, orderNumber = order.orderNumber, amount = amount, @@ -156,8 +156,8 @@ class PaymentFacadeServiceTest( Then("예외를 던진다") { shouldThrow { - paymentFacadeService.payOrder( - command = payOrderCommand( + paymentFacadeService.succeedPayment( + command = succeedPaymentCommand( memberId = order.memberId, orderNumber = order.orderNumber, amount = order.totalAmount, diff --git a/src/test/kotlin/com/petqua/presentation/payment/PaymentControllerSteps.kt b/src/test/kotlin/com/petqua/presentation/payment/PaymentControllerSteps.kt index d0a42a48..1da2f9fb 100644 --- a/src/test/kotlin/com/petqua/presentation/payment/PaymentControllerSteps.kt +++ b/src/test/kotlin/com/petqua/presentation/payment/PaymentControllerSteps.kt @@ -6,18 +6,18 @@ import io.restassured.module.kotlin.extensions.Then import io.restassured.module.kotlin.extensions.When import io.restassured.response.Response -fun requestPayOrder( +fun requestSucceedPayment( accessToken: String, - payOrderRequest: PayOrderRequest, + succeedPaymentRequest: SucceedPaymentRequest, ): Response { return Given { log().all() auth().preemptive().oauth2(accessToken) params( - "paymentType", payOrderRequest.paymentType, - "orderId", payOrderRequest.orderId, - "paymentKey", payOrderRequest.paymentKey, - "amount", payOrderRequest.amount + "paymentType", succeedPaymentRequest.paymentType, + "orderId", succeedPaymentRequest.orderId, + "paymentKey", succeedPaymentRequest.paymentKey, + "amount", succeedPaymentRequest.amount ) } When { post("/orders/payment/success") diff --git a/src/test/kotlin/com/petqua/presentation/payment/PaymentControllerTest.kt b/src/test/kotlin/com/petqua/presentation/payment/PaymentControllerTest.kt index 6870676e..711efb24 100644 --- a/src/test/kotlin/com/petqua/presentation/payment/PaymentControllerTest.kt +++ b/src/test/kotlin/com/petqua/presentation/payment/PaymentControllerTest.kt @@ -18,7 +18,7 @@ import com.petqua.exception.payment.FailPaymentExceptionType.INVALID_ORDER_ID import com.petqua.exception.payment.PaymentExceptionType.UNAUTHORIZED_KEY import com.petqua.test.ApiTestConfig import com.petqua.test.fixture.order -import com.petqua.test.fixture.payOrderRequest +import com.petqua.test.fixture.succeedPaymentRequest import io.kotest.assertions.assertSoftly import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNotBe @@ -40,7 +40,7 @@ class PaymentControllerTest( init { - Given("주문을 결제할 때") { + Given("결제 성공을 처리할 때") { val accessToken = signInAsMember().accessToken val memberId = getMemberIdByAccessToken(accessToken) @@ -53,9 +53,9 @@ class PaymentControllerTest( ) When("유효한 요청이면") { - val response = requestPayOrder( + val response = requestSucceedPayment( accessToken = accessToken, - payOrderRequest = payOrderRequest( + succeedPaymentRequest = succeedPaymentRequest( orderId = order.orderNumber.value, amount = order.totalAmount ) @@ -81,9 +81,9 @@ class PaymentControllerTest( When("존재하지 않는 주문이면") { val orderNumber = "wrongOrderNumber" - val response = requestPayOrder( + val response = requestSucceedPayment( accessToken = accessToken, - payOrderRequest = payOrderRequest( + succeedPaymentRequest = succeedPaymentRequest( orderId = orderNumber, amount = order.totalAmount ) @@ -102,9 +102,9 @@ class PaymentControllerTest( When("권한이 없는 회원의 요청이면") { val otherAccessToken = signInAsMember().accessToken - val response = requestPayOrder( + val response = requestSucceedPayment( accessToken = otherAccessToken, - payOrderRequest = payOrderRequest( + succeedPaymentRequest = succeedPaymentRequest( orderId = order.orderNumber.value, amount = order.totalAmount ) @@ -123,9 +123,9 @@ class PaymentControllerTest( When("주문의 총가격과 결제 금액이 다르면") { val wrongAmount = order.totalAmount + ONE - val response = requestPayOrder( + val response = requestSucceedPayment( accessToken = accessToken, - payOrderRequest = payOrderRequest( + succeedPaymentRequest = succeedPaymentRequest( orderId = order.orderNumber.value, amount = wrongAmount ) @@ -152,9 +152,9 @@ class PaymentControllerTest( null ) - val response = requestPayOrder( + val response = requestSucceedPayment( accessToken = accessToken, - payOrderRequest = payOrderRequest( + succeedPaymentRequest = succeedPaymentRequest( orderId = order.orderNumber.value, amount = order.totalAmount ) diff --git a/src/test/kotlin/com/petqua/test/fixture/PaymentFixtures.kt b/src/test/kotlin/com/petqua/test/fixture/PaymentFixtures.kt index 1f75135d..c9bd1e6b 100644 --- a/src/test/kotlin/com/petqua/test/fixture/PaymentFixtures.kt +++ b/src/test/kotlin/com/petqua/test/fixture/PaymentFixtures.kt @@ -1,23 +1,23 @@ package com.petqua.test.fixture import com.petqua.application.payment.FailPaymentCommand -import com.petqua.application.payment.PayOrderCommand +import com.petqua.application.payment.SucceedPaymentCommand import com.petqua.domain.order.OrderNumber import com.petqua.domain.payment.tosspayment.TossPaymentType import com.petqua.domain.payment.tosspayment.TossPaymentType.NORMAL -import com.petqua.presentation.payment.PayOrderRequest +import com.petqua.presentation.payment.SucceedPaymentRequest import java.math.BigDecimal import java.math.BigDecimal.ONE import java.math.BigDecimal.ZERO -fun payOrderCommand( +fun succeedPaymentCommand( memberId: Long = 0L, paymentType: TossPaymentType = NORMAL, orderNumber: OrderNumber = OrderNumber.from("OrderNumber"), paymentKey: String = "paymentKey", amount: BigDecimal = ZERO, -): PayOrderCommand { - return PayOrderCommand( +): SucceedPaymentCommand { + return SucceedPaymentCommand( memberId = memberId, paymentType = paymentType, orderNumber = orderNumber, @@ -26,13 +26,13 @@ fun payOrderCommand( ) } -fun payOrderRequest( +fun succeedPaymentRequest( paymentType: String = NORMAL.name, orderId: String = "orderId", paymentKey: String = "paymentKey", amount: BigDecimal = ONE, -): PayOrderRequest { - return PayOrderRequest( +): SucceedPaymentRequest { + return SucceedPaymentRequest( paymentType = paymentType, orderId = orderId, paymentKey = paymentKey,