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,80 +187,33 @@ 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 {
@Transactional
fun googleVerify(
memberId: Long,
chargeId: Long,
productId: String,
purchaseToken: String,
paymentGateway: PaymentGateway
) {
val charge = chargeRepository.findByIdOrNull(id = chargeId)
?: throw SodaException("결제정보에 오류가 있습니다.")
val member = memberRepository.findByIdOrNull(id = memberId)
?: throw SodaException("로그인 정보를 확인해주세요.")
if (paymentGateway == PaymentGateway.GOOGLE_IAP) {
val response = androidPublisher.purchases().products() val response = androidPublisher.purchases().products()
.get("kr.co.vividnext.sodalive", productId, purchaseToken) .get("kr.co.vividnext.sodalive", request.productId, request.purchaseToken)
.execute() ?: throw SodaException("결제정보에 오류가 있습니다.") .execute() ?: throw SodaException("결제정보에 오류가 있습니다.")
if ( if (
response.purchaseState == 0 && response.purchaseState == 0 &&
charge.payment!!.status == PaymentStatus.REQUEST response.consumptionState == 1 &&
payment.status == PaymentStatus.REQUEST
) { ) {
consumeWithRetry(productId, purchaseToken, charge, member) payment.status = PaymentStatus.COMPLETE
} else { member.charge(request.chargeCan, 0, "aos")
throw SodaException("결제정보에 오류가 있습니다.")
}
} else {
throw SodaException("결제정보에 오류가 있습니다.")
}
}
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( applicationEventPublisher.publishEvent(
ChargeSpringEvent( ChargeSpringEvent(
@ -268,19 +221,15 @@ class ChargeService(
memberId = member.id!! memberId = member.id!!
) )
) )
} else {
return throw SodaException("결제정보에 오류가 있습니다.")
}
} catch (e: Exception) { } catch (e: Exception) {
lastError = e e.printStackTrace()
attempt += 1 throw SodaException("결제정보에 오류가 있습니다.")
Thread.sleep(delay)
delay *= 2
} }
} } else {
lastError?.printStackTrace() throw SodaException("결제정보에 오류가 있습니다.")
if (attempt == retries) {
throw SodaException("구매를 하지 못했습니다.\n고객센터로 문의해 주세요")
} }
} }