Skip to content

Commit d42663f

Browse files
committed
create requestCancel in PaymentService
1 parent 1756b92 commit d42663f

File tree

7 files changed

+145
-9
lines changed

7 files changed

+145
-9
lines changed

src/main/kotlin/goodspace/teaming/payment/controller/PaymentController.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class PaymentController(
7070
}
7171

7272
@RequestMapping("/cancelAuth")
73-
fun cancelAuth(@RequestParam userRoomId: Long): ResponseEntity<Void> {
74-
//TODO :userRoom찾고, 거기서 !벌칙안당한!사람들이 환급 -> 근데 userRoom에 벌칙안당한 사람들을 체크하는 상태가 없다.. ㅜ
73+
fun requestCancel(@RequestParam roomId: Long): ResponseEntity<Void> {
74+
return paymentService.requestCancel(roomId)
7575
}
7676
}

src/main/kotlin/goodspace/teaming/payment/domain/PaymentApproveRespond.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PaymentApproveRespond(
3232

3333
val ediDate: LocalDateTime? = null,
3434
val signature: String,
35-
val status: String,
35+
var status: String,
3636
val paidAt: LocalDateTime? = null,
3737
val failedAt: LocalDateTime? = null,
3838
val cancelledAt: LocalDateTime? = null,

src/main/kotlin/goodspace/teaming/payment/dto/PaymentApproveRespondDto.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ data class PaymentApproveRespondDto(
7979
receiptUrl = receiptUrl,
8080
mallUserId = mallUserId,
8181
issuedCashReceipt = issuedCashReceipt,
82-
user = null
82+
user = null,
83+
room = null
8384
).apply {
8485
this.card = cardInfo
8586
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
package goodspace.teaming.payment.dto
22

3-
data class PaymentCancelRequestDto()
3+
import java.util.*
4+
5+
data class PaymentCancelRequestDto(
6+
val amount: String,
7+
val reason: String,
8+
val orderId: String = UUID.randomUUID().toString()
9+
)
Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,83 @@
11
package goodspace.teaming.payment.dto
22

3-
data class PaymentCancelResponseDto()
3+
import com.fasterxml.jackson.annotation.JsonProperty
4+
5+
data class PaymentCancelResponseDto(
6+
@JsonProperty("resultCode")
7+
val resultCode: String,
8+
9+
@JsonProperty("resultMsg")
10+
val resultMsg: String,
11+
12+
@JsonProperty("tid")
13+
val tid: String,
14+
15+
@JsonProperty("cancelledTid")
16+
val cancelledTid: String?,
17+
18+
@JsonProperty("orderId")
19+
val orderId: String,
20+
21+
@JsonProperty("ediDate")
22+
val ediDate: String?,
23+
24+
@JsonProperty("signature")
25+
val signature: String?,
26+
27+
@JsonProperty("status")
28+
val status: String,
29+
30+
@JsonProperty("paidAt")
31+
val paidAt: String?,
32+
33+
@JsonProperty("failedAt")
34+
val failedAt: String?,
35+
36+
@JsonProperty("cancelledAt")
37+
val cancelledAt: String?,
38+
39+
@JsonProperty("payMethod")
40+
val payMethod: String,
41+
42+
@JsonProperty("amount")
43+
val amount: Int,
44+
45+
@JsonProperty("balanceAmt")
46+
val balanceAmount: Int,
47+
48+
@JsonProperty("goodsName")
49+
val goodsName: String,
50+
51+
@JsonProperty("mallReserved")
52+
val mallReserved: String?,
53+
54+
@JsonProperty("useEscrow")
55+
val useEscrow: Boolean,
56+
57+
@JsonProperty("currency")
58+
val currency: String,
59+
60+
@JsonProperty("channel")
61+
val channel: String?,
62+
63+
@JsonProperty("approveNo")
64+
val approveNo: String,
65+
66+
@JsonProperty("buyerName")
67+
val buyerName: String?,
68+
69+
@JsonProperty("buyerTel")
70+
val buyerTel: String?,
71+
72+
@JsonProperty("buyerEmail")
73+
val buyerEmail: String?,
74+
75+
@JsonProperty("issuedCashReceipt")
76+
val issuedCashReceipt: Boolean,
77+
78+
@JsonProperty("receiptUrl")
79+
val receiptUrl: String,
80+
81+
@JsonProperty("mallUserId")
82+
val mallUserId: String?
83+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package goodspace.teaming.payment.repository
22

3+
import goodspace.teaming.global.entity.room.Room
4+
import goodspace.teaming.global.entity.user.User
35
import goodspace.teaming.payment.domain.PaymentApproveRespond
46
import org.springframework.data.jpa.repository.JpaRepository
57
import org.springframework.stereotype.Repository
68

79
@Repository
810
interface PaymentRepository: JpaRepository<PaymentApproveRespond, Long> {
911
fun findByTid(tid: String): PaymentApproveRespond?
12+
fun findByUserAndRoom(user: User, room: Room): PaymentApproveRespond?
1013
}

src/main/kotlin/goodspace/teaming/payment/service/PaymentService.kt

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import goodspace.teaming.global.repository.UserRoomRepository
77
import org.springframework.web.reactive.function.client.WebClient
88
import goodspace.teaming.payment.config.NicepayProperties
99
import goodspace.teaming.payment.domain.PaymentApproveRespond
10-
import goodspace.teaming.payment.dto.PaymentApproveRequestDto
11-
import goodspace.teaming.payment.dto.PaymentApproveRespondDto
12-
import goodspace.teaming.payment.dto.PaymentVerifyRespondDto
10+
import goodspace.teaming.payment.dto.*
1311

1412
import goodspace.teaming.payment.repository.PaymentRepository
13+
import jakarta.persistence.EntityNotFoundException
1514
import org.springframework.http.HttpHeaders
1615
import org.springframework.http.HttpStatus
1716
import org.springframework.http.MediaType
@@ -64,6 +63,53 @@ class PaymentService(
6463
.build()
6564
}
6665

66+
@Transactional
67+
fun requestCancel(roomId: Long): ResponseEntity<Void> {
68+
val room = roomRepository.findById(roomId).orElseThrow { EntityNotFoundException("Room not found") }
69+
70+
val userRoomsToCancel = room.userRooms.filter{( !it.isPunished) }
71+
72+
userRoomsToCancel.forEach { userRoom ->
73+
val user = userRoom.user
74+
75+
val payment = paymentRepository.findByUserAndRoom(user, room)
76+
?: throw EntityNotFoundException("유저(id:${user.id})의 결제 정보를 찾을 수 없습니다.")
77+
78+
if (payment.status != "CANCELLED") {
79+
approveCancel(payment.tid, payment.amount.toString())
80+
}
81+
}
82+
// 위에서 오류가 나는 경우는 이미다 예외처리를 해놨으니 return에서 어떤 상태를 반환할 필요가 없다.?
83+
// TODO: 준이 체크
84+
return ResponseEntity.ok().build()
85+
}
86+
87+
@Transactional
88+
fun approveCancel(tid: String, amount: String): PaymentCancelResponseDto {
89+
val url = "${nicepayProperties.approveUrl}/$tid/cancel"
90+
val requestBody = PaymentCancelRequestDto(
91+
amount = amount,
92+
reason = "미션 성공으로 인한 미벌칙자 환급"
93+
)
94+
95+
val cancelResponseDto = webClient.build()
96+
.post()
97+
.uri(url)
98+
.header(HttpHeaders.AUTHORIZATION, getAuthHeader())
99+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
100+
.bodyValue(requestBody)
101+
.retrieve()
102+
.bodyToMono(PaymentCancelResponseDto::class.java)
103+
.block() ?: throw IllegalStateException("결제 취소 응답이 올바르지 않습니다.")
104+
105+
if(cancelResponseDto.resultCode == "0000") {
106+
var paymentApproveRespond = paymentRepository.findByTid(tid) ?: throw EntityNotFoundException("결제 취소 로직 중에 해당 TID에 해당하는 결제 정보를 찾을 수 없습니다.")
107+
paymentApproveRespond.status = "CANCELLED"
108+
}
109+
110+
return cancelResponseDto
111+
}
112+
67113
@Transactional
68114
fun approvePayment(tid: String, amount: String, userId: String, roomId: String, platform: String): PaymentApproveRespondDto {
69115
val url = "${nicepayProperties.approveUrl}/$tid"

0 commit comments

Comments
 (0)