feat(charge): payverse pg - 충전/검증 API 추가
This commit is contained in:
@@ -20,6 +20,32 @@ class ChargeController(
|
||||
private val trackingService: AdTrackingService
|
||||
) {
|
||||
|
||||
@PostMapping("/payverse")
|
||||
fun payverseCharge(
|
||||
@RequestBody request: PayverseChargeRequest,
|
||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
|
||||
) = run {
|
||||
if (member == null) {
|
||||
throw SodaException("로그인 정보를 확인해주세요.")
|
||||
}
|
||||
|
||||
ApiResponse.ok(service.payverseCharge(member, request))
|
||||
}
|
||||
|
||||
@PostMapping("/payverse/verify")
|
||||
fun payverseVerify(
|
||||
@RequestBody verifyRequest: PayverseVerifyRequest,
|
||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
|
||||
) = run {
|
||||
if (member == null) {
|
||||
throw SodaException("로그인 정보를 확인해주세요.")
|
||||
}
|
||||
|
||||
val response = service.payverseVerify(memberId = member.id!!, verifyRequest)
|
||||
trackingCharge(member, response)
|
||||
ApiResponse.ok(Unit)
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
fun charge(
|
||||
@RequestBody chargeRequest: ChargeRequest,
|
||||
|
@@ -2,6 +2,7 @@ package kr.co.vividnext.sodalive.can.charge
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty
|
||||
import kr.co.vividnext.sodalive.can.payment.PaymentGateway
|
||||
import java.math.BigDecimal
|
||||
|
||||
data class ChargeRequest(val canId: Long, val paymentGateway: PaymentGateway)
|
||||
|
||||
@@ -44,3 +45,30 @@ data class GoogleChargeRequest(
|
||||
val purchaseToken: String,
|
||||
val paymentGateway: PaymentGateway
|
||||
)
|
||||
|
||||
data class PayverseChargeRequest(
|
||||
val canId: Long
|
||||
)
|
||||
|
||||
data class PayverseChargeResponse(
|
||||
val chargeId: Long,
|
||||
val payloadJson: String
|
||||
)
|
||||
|
||||
data class PayverseVerifyRequest(
|
||||
val transactionId: String,
|
||||
val orderId: String
|
||||
)
|
||||
|
||||
data class PayverseVerifyResponse(
|
||||
val resultStatus: String,
|
||||
val tid: String,
|
||||
val schemeCode: String,
|
||||
val transactionType: String,
|
||||
val transactionStatus: String,
|
||||
val transactionMessage: String,
|
||||
val orderId: String,
|
||||
val customerId: String,
|
||||
val processingCurrency: String,
|
||||
val processingAmount: BigDecimal
|
||||
)
|
||||
|
@@ -22,6 +22,7 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import org.apache.commons.codec.digest.DigestUtils
|
||||
import org.json.JSONObject
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.context.ApplicationEventPublisher
|
||||
@@ -34,6 +35,8 @@ import org.springframework.transaction.annotation.Transactional
|
||||
import java.math.BigDecimal
|
||||
import java.math.RoundingMode
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.UUID
|
||||
|
||||
@Service
|
||||
@Transactional(readOnly = true)
|
||||
@@ -63,7 +66,19 @@ class ChargeService(
|
||||
@Value("\${apple.iap-verify-sandbox-url}")
|
||||
private val appleInAppVerifySandBoxUrl: String,
|
||||
@Value("\${apple.iap-verify-url}")
|
||||
private val appleInAppVerifyUrl: String
|
||||
private val appleInAppVerifyUrl: String,
|
||||
|
||||
@Value("\${payverse.mid}")
|
||||
private val payverseMid: String,
|
||||
@Value("\${payverse.client-key}")
|
||||
private val payverseClientKey: String,
|
||||
@Value("\${payverse.secret-key}")
|
||||
private val payverseSecretKey: String,
|
||||
@Value("\${payverse.host}")
|
||||
private val payverseHost: String,
|
||||
|
||||
@Value("\${server.env}")
|
||||
private val serverEnv: String
|
||||
) {
|
||||
|
||||
@Transactional
|
||||
@@ -126,6 +141,129 @@ class ChargeService(
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun payverseCharge(member: Member, request: PayverseChargeRequest): PayverseChargeResponse {
|
||||
val can = canRepository.findByIdOrNull(request.canId)
|
||||
?: throw SodaException("잘못된 요청입니다\n앱 종료 후 다시 시도해 주세요.")
|
||||
|
||||
val charge = Charge(can.can, can.rewardCan)
|
||||
charge.title = can.title
|
||||
charge.member = member
|
||||
charge.can = can
|
||||
|
||||
val payment = Payment(paymentGateway = PaymentGateway.PAYVERSE)
|
||||
payment.price = can.price.toDouble()
|
||||
charge.payment = payment
|
||||
|
||||
val savedCharge = chargeRepository.save(charge)
|
||||
|
||||
val chargeId = savedCharge.id!!
|
||||
val amount = BigDecimal(savedCharge.payment!!.price)
|
||||
val reqDate = savedCharge.createdAt!!.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
|
||||
val sign = DigestUtils.sha512Hex(
|
||||
String.format("||%s||%s||%s||%s||%s||", payverseSecretKey, payverseMid, chargeId, amount, reqDate)
|
||||
)
|
||||
val customerId = UUID.nameUUIDFromBytes("${serverEnv}_user_${member.id!!}".toByteArray()).toString()
|
||||
val requestCurrency = "KRW"
|
||||
|
||||
val payload = linkedMapOf(
|
||||
"mid" to payverseMid,
|
||||
"clientKey" to payverseClientKey,
|
||||
"orderId" to chargeId.toString(),
|
||||
"customerId" to customerId,
|
||||
"productName" to can.title,
|
||||
"requestCurrency" to requestCurrency,
|
||||
"requestAmount" to amount.toString(),
|
||||
"reqDate" to reqDate,
|
||||
"billkeyReq" to "N",
|
||||
"mallReserved" to "",
|
||||
"sign" to sign
|
||||
)
|
||||
val payloadJson = objectMapper.writeValueAsString(payload)
|
||||
|
||||
return PayverseChargeResponse(chargeId = charge.id!!, payloadJson = payloadJson)
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun payverseVerify(memberId: Long, verifyRequest: PayverseVerifyRequest): ChargeCompleteResponse {
|
||||
val charge = chargeRepository.findByIdOrNull(verifyRequest.orderId.toLong())
|
||||
?: throw SodaException("결제정보에 오류가 있습니다.")
|
||||
val member = memberRepository.findByIdOrNull(memberId)
|
||||
?: throw SodaException("로그인 정보를 확인해주세요.")
|
||||
|
||||
// 결제수단 확인
|
||||
if (charge.payment?.paymentGateway != PaymentGateway.PAYVERSE) {
|
||||
throw SodaException("결제정보에 오류가 있습니다.")
|
||||
}
|
||||
|
||||
// 결제 상태에 따른 분기 처리
|
||||
when (charge.payment?.status) {
|
||||
PaymentStatus.REQUEST -> {
|
||||
try {
|
||||
val url = "$payverseHost/payment/search/transaction/${verifyRequest.transactionId}"
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.addHeader("mid", payverseMid)
|
||||
.addHeader("clientKey", payverseClientKey)
|
||||
.get()
|
||||
.build()
|
||||
|
||||
val response = okHttpClient.newCall(request).execute()
|
||||
if (!response.isSuccessful) {
|
||||
throw SodaException("결제정보에 오류가 있습니다.")
|
||||
}
|
||||
|
||||
val body = response.body?.string() ?: throw SodaException("결제정보에 오류가 있습니다.")
|
||||
val verifyResponse = objectMapper.readValue(body, PayverseVerifyResponse::class.java)
|
||||
|
||||
val isSuccess = verifyResponse.resultStatus == "SUCCESS" &&
|
||||
verifyResponse.transactionStatus == "SUCCESS" &&
|
||||
verifyResponse.orderId.toLongOrNull() == charge.id &&
|
||||
verifyResponse.processingAmount.compareTo(BigDecimal.valueOf(charge.can!!.price.toLong())) == 0
|
||||
|
||||
if (isSuccess) {
|
||||
// verify 함수의 232~248 라인과 동일 처리
|
||||
charge.payment?.receiptId = verifyResponse.tid
|
||||
charge.payment?.method = verifyResponse.schemeCode
|
||||
charge.payment?.status = PaymentStatus.COMPLETE
|
||||
// 통화코드 설정
|
||||
charge.payment?.locale = verifyResponse.processingCurrency
|
||||
|
||||
member.charge(charge.chargeCan, charge.rewardCan, "pg")
|
||||
|
||||
applicationEventPublisher.publishEvent(
|
||||
ChargeSpringEvent(
|
||||
chargeId = charge.id!!,
|
||||
memberId = member.id!!
|
||||
)
|
||||
)
|
||||
|
||||
return ChargeCompleteResponse(
|
||||
price = BigDecimal(charge.payment!!.price).setScale(2, RoundingMode.HALF_UP).toDouble(),
|
||||
currencyCode = charge.payment!!.locale?.takeLast(3) ?: "KRW",
|
||||
isFirstCharged = chargeRepository.isFirstCharged(memberId)
|
||||
)
|
||||
} else {
|
||||
throw SodaException("결제정보에 오류가 있습니다.")
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
throw SodaException("결제정보에 오류가 있습니다.")
|
||||
}
|
||||
}
|
||||
PaymentStatus.COMPLETE -> {
|
||||
// 이미 결제가 완료된 경우, 동일한 데이터로 즉시 반환
|
||||
return ChargeCompleteResponse(
|
||||
price = BigDecimal(charge.payment!!.price).setScale(2, RoundingMode.HALF_UP).toDouble(),
|
||||
currencyCode = charge.payment!!.locale?.takeLast(3) ?: "KRW",
|
||||
isFirstCharged = chargeRepository.isFirstCharged(memberId)
|
||||
)
|
||||
}
|
||||
else -> {
|
||||
throw SodaException("결제정보에 오류가 있습니다.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
fun charge(member: Member, request: ChargeRequest): ChargeResponse {
|
||||
val can = canRepository.findByIdOrNull(request.canId)
|
||||
|
@@ -1,5 +1,5 @@
|
||||
package kr.co.vividnext.sodalive.can.payment
|
||||
|
||||
enum class PaymentGateway {
|
||||
PG, GOOGLE_IAP, APPLE_IAP, POINT_CLICK_AD
|
||||
PG, PAYVERSE, GOOGLE_IAP, APPLE_IAP, POINT_CLICK_AD
|
||||
}
|
||||
|
@@ -13,6 +13,12 @@ weraser:
|
||||
apiUrl: ${WERASER_API_URL}
|
||||
apiKey: ${WERASER_API_KEY}
|
||||
|
||||
payverse:
|
||||
mid: ${PAYVERSE_MID}
|
||||
clientKey: ${PAYVERSE_CLIENT_KEY}
|
||||
secretKey: ${PAYVERSE_SECRET_KEY}
|
||||
host: ${PAYVERSE_HOST}
|
||||
|
||||
bootpay:
|
||||
applicationId: ${BOOTPAY_APPLICATION_ID}
|
||||
privateKey: ${BOOTPAY_PRIVATE_KEY}
|
||||
|
Reference in New Issue
Block a user