Skip to content

Commit

Permalink
refactor: 결제 실패 로깅 방법 수정 및 메서드, 클래스 이름 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Combi153 committed Mar 2, 2024
1 parent e643f1e commit aba45f8
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 56 deletions.
6 changes: 3 additions & 3 deletions src/main/kotlin/com/petqua/application/payment/PaymentDtos.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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}")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class PaymentController(
) {

@PostMapping("/success")
fun payOrder(
fun succeedPayment(
@Auth loginMember: LoginMember,
request: PayOrderRequest,
request: SucceedPaymentRequest,
): ResponseEntity<Unit> {
paymentFacadeService.payOrder(request.toCommand(loginMember.memberId))
paymentFacadeService.succeedPayment(request.toCommand(loginMember.memberId))
return ResponseEntity.noContent().build()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -47,7 +47,7 @@ class PaymentFacadeServiceTest(
@SpykBean private val tossPaymentsApiClient: TossPaymentsApiClient,
) : BehaviorSpec({

Given("주문을 결제할 때") {
Given("결제 성공을 처리할 때") {
val member = memberRepository.save(member())
val order = orderRepository.save(
order(
Expand All @@ -58,8 +58,8 @@ class PaymentFacadeServiceTest(
)

When("유효한 요쳥이면") {
paymentFacadeService.payOrder(
command = payOrderCommand(
paymentFacadeService.succeedPayment(
command = succeedPaymentCommand(
memberId = order.memberId,
orderNumber = order.orderNumber,
amount = order.totalAmount,
Expand All @@ -74,8 +74,8 @@ class PaymentFacadeServiceTest(
}

When("결제 승인에 성공하면") {
paymentFacadeService.payOrder(
command = payOrderCommand(
paymentFacadeService.succeedPayment(
command = succeedPaymentCommand(
memberId = order.memberId,
orderNumber = order.orderNumber,
amount = order.totalAmount,
Expand All @@ -100,8 +100,8 @@ class PaymentFacadeServiceTest(

Then("예외를 던진다") {
shouldThrow<OrderException> {
paymentFacadeService.payOrder(
command = payOrderCommand(
paymentFacadeService.succeedPayment(
command = succeedPaymentCommand(
memberId = order.memberId,
orderNumber = orderNumber,
amount = order.totalAmount,
Expand All @@ -116,8 +116,8 @@ class PaymentFacadeServiceTest(

Then("예외를 던진다") {
shouldThrow<OrderException> {
paymentFacadeService.payOrder(
command = payOrderCommand(
paymentFacadeService.succeedPayment(
command = succeedPaymentCommand(
memberId = memberId,
orderNumber = order.orderNumber,
amount = order.totalAmount,
Expand All @@ -132,8 +132,8 @@ class PaymentFacadeServiceTest(

Then("예외를 던진다") {
shouldThrow<OrderException> {
paymentFacadeService.payOrder(
command = payOrderCommand(
paymentFacadeService.succeedPayment(
command = succeedPaymentCommand(
memberId = order.memberId,
orderNumber = order.orderNumber,
amount = amount,
Expand All @@ -156,8 +156,8 @@ class PaymentFacadeServiceTest(

Then("예외를 던진다") {
shouldThrow<PaymentException> {
paymentFacadeService.payOrder(
command = payOrderCommand(
paymentFacadeService.succeedPayment(
command = succeedPaymentCommand(
memberId = order.memberId,
orderNumber = order.orderNumber,
amount = order.totalAmount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,7 +40,7 @@ class PaymentControllerTest(

init {

Given("주문을 결제할") {
Given("결제 성공을 처리할") {
val accessToken = signInAsMember().accessToken
val memberId = getMemberIdByAccessToken(accessToken)

Expand All @@ -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
)
Expand All @@ -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
)
Expand All @@ -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
)
Expand All @@ -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
)
Expand All @@ -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
)
Expand Down
16 changes: 8 additions & 8 deletions src/test/kotlin/com/petqua/test/fixture/PaymentFixtures.kt
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit aba45f8

Please sign in to comment.