diff --git a/service/src/main/kotlin/com/sangdol/roomescape/payment/business/event/PaymentEvent.kt b/service/src/main/kotlin/com/sangdol/roomescape/payment/business/event/PaymentEvent.kt new file mode 100644 index 00000000..d8db4678 --- /dev/null +++ b/service/src/main/kotlin/com/sangdol/roomescape/payment/business/event/PaymentEvent.kt @@ -0,0 +1,22 @@ +package com.sangdol.roomescape.payment.business.event + +import com.sangdol.roomescape.payment.business.domain.PaymentDetail +import com.sangdol.roomescape.payment.business.domain.PaymentMethod +import com.sangdol.roomescape.payment.business.domain.PaymentStatus +import com.sangdol.roomescape.payment.business.domain.PaymentType +import java.time.Instant + +class PaymentEvent( + val reservationId: Long, + val paymentKey: String, + val orderId: String, + val type: PaymentType, + val status: PaymentStatus, + val totalAmount: Int, + val vat: Int, + val suppliedAmount: Int, + val method: PaymentMethod, + val requestedAt: Instant, + val approvedAt: Instant, + val detail: PaymentDetail +) diff --git a/service/src/main/kotlin/com/sangdol/roomescape/payment/mapper/PaymentEventMappingExtensions.kt b/service/src/main/kotlin/com/sangdol/roomescape/payment/mapper/PaymentEventMappingExtensions.kt new file mode 100644 index 00000000..c25c8cc2 --- /dev/null +++ b/service/src/main/kotlin/com/sangdol/roomescape/payment/mapper/PaymentEventMappingExtensions.kt @@ -0,0 +1,53 @@ +package com.sangdol.roomescape.payment.mapper + +import com.sangdol.roomescape.payment.business.domain.* +import com.sangdol.roomescape.payment.business.event.PaymentEvent +import com.sangdol.roomescape.payment.exception.PaymentErrorCode +import com.sangdol.roomescape.payment.exception.PaymentException +import com.sangdol.roomescape.payment.infrastructure.persistence.PaymentDetailEntity +import com.sangdol.roomescape.payment.infrastructure.persistence.PaymentEntity + +fun PaymentEvent.toEntity(id: Long) = PaymentEntity( + id = id, + reservationId = this.reservationId, + paymentKey = this.paymentKey, + orderId = this.orderId, + totalAmount = this.totalAmount, + requestedAt = this.requestedAt, + approvedAt = this.approvedAt, + type = this.type, + method = this.method, + status = this.status +) + +fun PaymentEvent.toDetailEntity(id: Long, paymentId: Long): PaymentDetailEntity { + val suppliedAmount = this.suppliedAmount + val vat = this.vat + + return when (this.method) { + PaymentMethod.TRANSFER -> { + (this.detail as? BankTransferPaymentDetail) + ?.toEntity(id, paymentId, suppliedAmount, vat) + ?: throw PaymentException(PaymentErrorCode.PAYMENT_UNEXPECTED_ERROR) + } + + PaymentMethod.EASY_PAY -> { + when (this.detail) { + is EasypayCardPaymentDetail -> { this.detail.toEntity(id, paymentId, suppliedAmount, vat) } + is EasypayPrepaidPaymentDetail -> { this.detail.toEntity(id, paymentId, suppliedAmount, vat) } + + else -> { + throw PaymentException(PaymentErrorCode.PAYMENT_UNEXPECTED_ERROR) + } + } + } + + PaymentMethod.CARD -> { + (this.detail as? CardPaymentDetail) + ?.toEntity(id, paymentId, suppliedAmount, vat) + ?: throw PaymentException(PaymentErrorCode.PAYMENT_UNEXPECTED_ERROR) + } + + else -> throw PaymentException(PaymentErrorCode.NOT_SUPPORTED_PAYMENT_TYPE) + } +}