Compare commits

..

No commits in common. "87765941eb64baba629094f88d5f198fb9d54431" and "1809862c166c018562746be060acdc4e51c531bd" have entirely different histories.

3 changed files with 31 additions and 103 deletions

View File

@ -42,6 +42,4 @@ data class Charge(
var useCan: UseCan? = null var useCan: UseCan? = null
var title: String? = null var title: String? = null
var googleProductId: String? = null
} }

View File

@ -59,25 +59,6 @@ class ChargeController(private val service: ChargeService) {
throw SodaException("로그인 정보를 확인해주세요.") throw SodaException("로그인 정보를 확인해주세요.")
} }
val chargeId = service.googleCharge( ApiResponse.ok(service.googleCharge(member, request))
member = member,
title = request.title,
chargeCan = request.chargeCan,
price = request.price,
currencyCode = request.currencyCode,
productId = request.productId,
purchaseToken = request.purchaseToken,
paymentGateway = request.paymentGateway
)
ApiResponse.ok(
service.googleVerify(
memberId = member.id!!,
chargeId = chargeId,
productId = request.productId,
purchaseToken = request.purchaseToken,
paymentGateway = request.paymentGateway
)
)
} }
} }

View File

@ -187,57 +187,45 @@ class ChargeService(
} }
@Transactional @Transactional
fun googleCharge( fun googleCharge(member: Member, request: GoogleChargeRequest) {
member: Member, val charge = Charge(request.chargeCan, 0)
title: String, charge.title = request.title
chargeCan: Int,
price: Double,
currencyCode: String,
productId: String,
purchaseToken: String,
paymentGateway: PaymentGateway
): Long {
val charge = Charge(chargeCan, 0)
charge.title = title
charge.googleProductId = productId
charge.member = member charge.member = member
val payment = Payment(paymentGateway = paymentGateway) val payment = Payment(paymentGateway = request.paymentGateway)
payment.locale = currencyCode payment.locale = request.currencyCode
payment.price = price payment.price = request.price
payment.receiptId = purchaseToken payment.receiptId = request.purchaseToken
payment.method = "구글(인 앱 결제)" payment.method = "구글(인 앱 결제)"
charge.payment = payment charge.payment = payment
chargeRepository.save(charge) chargeRepository.save(charge)
return charge.id!! if (request.paymentGateway == PaymentGateway.GOOGLE_IAP) {
} try {
val response = androidPublisher.purchases().products()
.get("kr.co.vividnext.sodalive", request.productId, request.purchaseToken)
.execute() ?: throw SodaException("결제정보에 오류가 있습니다.")
@Transactional if (
fun googleVerify( response.purchaseState == 0 &&
memberId: Long, response.consumptionState == 1 &&
chargeId: Long, payment.status == PaymentStatus.REQUEST
productId: String, ) {
purchaseToken: String, payment.status = PaymentStatus.COMPLETE
paymentGateway: PaymentGateway member.charge(request.chargeCan, 0, "aos")
) {
val charge = chargeRepository.findByIdOrNull(id = chargeId)
?: throw SodaException("결제정보에 오류가 있습니다.")
val member = memberRepository.findByIdOrNull(id = memberId)
?: throw SodaException("로그인 정보를 확인해주세요.")
if (paymentGateway == PaymentGateway.GOOGLE_IAP) { applicationEventPublisher.publishEvent(
val response = androidPublisher.purchases().products() ChargeSpringEvent(
.get("kr.co.vividnext.sodalive", productId, purchaseToken) chargeId = charge.id!!,
.execute() ?: throw SodaException("결제정보에 오류가 있습니다.") memberId = member.id!!
)
if ( )
response.purchaseState == 0 && } else {
charge.payment!!.status == PaymentStatus.REQUEST throw SodaException("결제정보에 오류가 있습니다.")
) { }
consumeWithRetry(productId, purchaseToken, charge, member) } catch (e: Exception) {
} else { e.printStackTrace()
throw SodaException("결제정보에 오류가 있습니다.") throw SodaException("결제정보에 오류가 있습니다.")
} }
} else { } else {
@ -245,45 +233,6 @@ class ChargeService(
} }
} }
private fun consumeWithRetry(productId: String, purchaseToken: String, charge: Charge, member: Member) {
var attempt = 0
var delay = 2000L
val retries = 3
var lastError: Exception? = null
while (attempt < retries) {
try {
androidPublisher.purchases().products().consume(
"kr.co.vividnext.sodalive",
productId,
purchaseToken
)
charge.payment!!.status = PaymentStatus.COMPLETE
member.charge(charge.chargeCan, 0, "aos")
applicationEventPublisher.publishEvent(
ChargeSpringEvent(
chargeId = charge.id!!,
memberId = member.id!!
)
)
return
} catch (e: Exception) {
lastError = e
attempt += 1
Thread.sleep(delay)
delay *= 2
}
}
lastError?.printStackTrace()
if (attempt == retries) {
throw SodaException("구매를 하지 못했습니다.\n고객센터로 문의해 주세요")
}
}
private fun requestRealServerVerify(verifyRequest: AppleVerifyRequest): Boolean { private fun requestRealServerVerify(verifyRequest: AppleVerifyRequest): Boolean {
val body = JSONObject() val body = JSONObject()
body.put("receipt-data", verifyRequest.receiptString) body.put("receipt-data", verifyRequest.receiptString)