Merge pull request 'PG 심사를 위한 캔 충전 로직 추가' (#180) from test into main
Reviewed-on: #180
This commit is contained in:
		| @@ -0,0 +1,34 @@ | ||||
| package kr.co.vividnext.sodalive.can.charge.temp | ||||
|  | ||||
| import kr.co.vividnext.sodalive.can.charge.VerifyRequest | ||||
| import kr.co.vividnext.sodalive.common.ApiResponse | ||||
| import kr.co.vividnext.sodalive.common.SodaException | ||||
| import kr.co.vividnext.sodalive.member.Member | ||||
| import org.springframework.security.core.annotation.AuthenticationPrincipal | ||||
| import org.springframework.security.core.userdetails.User | ||||
| import org.springframework.web.bind.annotation.PostMapping | ||||
| import org.springframework.web.bind.annotation.RequestBody | ||||
| import org.springframework.web.bind.annotation.RequestMapping | ||||
| import org.springframework.web.bind.annotation.RestController | ||||
|  | ||||
| @RestController | ||||
| @RequestMapping("/charge/temp") | ||||
| class ChargeTempController(private val service: ChargeTempService) { | ||||
|     @PostMapping | ||||
|     fun charge( | ||||
|         @RequestBody request: ChargeTempRequest, | ||||
|         @AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member? | ||||
|     ) = run { | ||||
|         if (member == null) { | ||||
|             throw SodaException("로그인 정보를 확인해주세요.") | ||||
|         } | ||||
|  | ||||
|         ApiResponse.ok(service.charge(member, request)) | ||||
|     } | ||||
|  | ||||
|     @PostMapping("/verify") | ||||
|     fun verify( | ||||
|         @RequestBody request: VerifyRequest, | ||||
|         @AuthenticationPrincipal user: User | ||||
|     ) = ApiResponse.ok(service.verify(user, request)) | ||||
| } | ||||
| @@ -0,0 +1,9 @@ | ||||
| package kr.co.vividnext.sodalive.can.charge.temp | ||||
|  | ||||
| import kr.co.vividnext.sodalive.can.payment.PaymentGateway | ||||
|  | ||||
| data class ChargeTempRequest( | ||||
|     val can: Int, | ||||
|     val price: Int, | ||||
|     val paymentGateway: PaymentGateway | ||||
| ) | ||||
| @@ -0,0 +1,84 @@ | ||||
| package kr.co.vividnext.sodalive.can.charge.temp | ||||
|  | ||||
| import com.fasterxml.jackson.databind.ObjectMapper | ||||
| import kr.co.bootpay.Bootpay | ||||
| import kr.co.vividnext.sodalive.can.charge.Charge | ||||
| import kr.co.vividnext.sodalive.can.charge.ChargeRepository | ||||
| import kr.co.vividnext.sodalive.can.charge.ChargeResponse | ||||
| import kr.co.vividnext.sodalive.can.charge.VerifyRequest | ||||
| import kr.co.vividnext.sodalive.can.charge.VerifyResult | ||||
| import kr.co.vividnext.sodalive.can.payment.Payment | ||||
| import kr.co.vividnext.sodalive.can.payment.PaymentGateway | ||||
| import kr.co.vividnext.sodalive.can.payment.PaymentStatus | ||||
| import kr.co.vividnext.sodalive.common.SodaException | ||||
| import kr.co.vividnext.sodalive.extensions.moneyFormat | ||||
| import kr.co.vividnext.sodalive.member.Member | ||||
| import kr.co.vividnext.sodalive.member.MemberRepository | ||||
| import org.springframework.beans.factory.annotation.Value | ||||
| import org.springframework.data.repository.findByIdOrNull | ||||
| import org.springframework.security.core.userdetails.User | ||||
| import org.springframework.stereotype.Service | ||||
| import org.springframework.transaction.annotation.Transactional | ||||
|  | ||||
| @Service | ||||
| @Transactional(readOnly = true) | ||||
| class ChargeTempService( | ||||
|     private val chargeRepository: ChargeRepository, | ||||
|     private val memberRepository: MemberRepository, | ||||
|  | ||||
|     private val objectMapper: ObjectMapper, | ||||
|  | ||||
|     @Value("\${bootpay.application-id}") | ||||
|     private val bootpayApplicationId: String, | ||||
|     @Value("\${bootpay.private-key}") | ||||
|     private val bootpayPrivateKey: String | ||||
| ) { | ||||
|  | ||||
|     @Transactional | ||||
|     fun charge(member: Member, request: ChargeTempRequest): ChargeResponse { | ||||
|         val charge = Charge(request.can, 0) | ||||
|         charge.title = "${request.can.moneyFormat()} 캔" | ||||
|         charge.member = member | ||||
|  | ||||
|         val payment = Payment(paymentGateway = request.paymentGateway) | ||||
|         payment.price = request.price.toDouble() | ||||
|         charge.payment = payment | ||||
|  | ||||
|         chargeRepository.save(charge) | ||||
|  | ||||
|         return ChargeResponse(chargeId = charge.id!!) | ||||
|     } | ||||
|  | ||||
|     @Transactional | ||||
|     fun verify(user: User, verifyRequest: VerifyRequest) { | ||||
|         val charge = chargeRepository.findByIdOrNull(verifyRequest.orderId.toLong()) | ||||
|             ?: throw SodaException("결제정보에 오류가 있습니다.") | ||||
|         val member = memberRepository.findByEmail(user.username) | ||||
|             ?: throw SodaException("로그인 정보를 확인해주세요.") | ||||
|  | ||||
|         if (charge.payment!!.paymentGateway == PaymentGateway.PG) { | ||||
|             val bootpay = Bootpay(bootpayApplicationId, bootpayPrivateKey) | ||||
|  | ||||
|             try { | ||||
|                 bootpay.accessToken | ||||
|                 val verifyResult = objectMapper.convertValue( | ||||
|                     bootpay.getReceipt(verifyRequest.receiptId), | ||||
|                     VerifyResult::class.java | ||||
|                 ) | ||||
|  | ||||
|                 if (verifyResult.status == 1 && verifyResult.price == charge.payment!!.price.toInt()) { | ||||
|                     charge.payment?.receiptId = verifyResult.receiptId | ||||
|                     charge.payment?.method = verifyResult.method | ||||
|                     charge.payment?.status = PaymentStatus.COMPLETE | ||||
|                     member.charge(charge.chargeCan, charge.rewardCan, "pg") | ||||
|                 } else { | ||||
|                     throw SodaException("결제정보에 오류가 있습니다.") | ||||
|                 } | ||||
|             } catch (e: Exception) { | ||||
|                 throw SodaException("결제정보에 오류가 있습니다.") | ||||
|             } | ||||
|         } else { | ||||
|             throw SodaException("결제정보에 오류가 있습니다.") | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user