Skip to content

Commit

Permalink
Merge pull request #43 from dietmap/improved-response-logging
Browse files Browse the repository at this point in the history
Improve logging for AppStore responses
  • Loading branch information
koziolk authored Apr 28, 2020
2 parents 5cf196c + 5e79aeb commit aedd53e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ data class ReceiptResponse(@get:JsonProperty("status") val status: Int,

@JsonIgnore
fun shouldRetry() : Boolean = (status != 0 && isRetryable)

override fun toString(): String {
return "ReceiptResponse(status=$status, environment='$environment', responseStatusCode=$responseStatusCode)"
}
}

/**
Expand Down
17 changes: 10 additions & 7 deletions src/main/kotlin/com/dietmap/yaak/domain/appstore/AppStoreClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,36 @@ class AppStoreClient {
@Recover
fun recoverVerifyReceipt(runtimeException: RuntimeException, receiptRequest: ReceiptRequest) : ReceiptResponse {

logger.debug { "Handling recovery ReceiptRequest $receiptRequest for exception $runtimeException" }
logger.debug { "recoverVerifyReceipt: ReceiptRequest $receiptRequest for exception $runtimeException" }

val receiptResponseStatus: ReceiptResponseStatus =
productionRestTemplate.postForObject("/verifyReceipt", prepareHttpHeaders(receiptRequest), ReceiptResponseStatus::class.java)!!

logger.debug { "Handling recovery ReceiptResponseStatus $receiptResponseStatus" }
logger.debug { "recoverVerifyReceipt: ReceiptResponseStatus $receiptResponseStatus" }

if (receiptResponseStatus.responseStatusCode!! == ResponseStatusCode.CODE_21007) {
return sandboxRestTemplate.postForObject("/verifyReceipt", prepareHttpHeaders(receiptRequest), ReceiptResponse::class.java)!!
} else {
throw RuntimeException("Cannot process ReceiptRequest due to exception: ", runtimeException)
val message = "Cannot process ReceiptRequest due to exception $runtimeException";
logger.error { message }
throw ReceiptValidationException(message)
}
}

private fun processRequest(receiptRequest: ReceiptRequest): ReceiptResponse {
receiptRequest.password = password

logger.debug { "Processing ReceiptRequest $receiptRequest" }
logger.debug { "processRequest: ReceiptRequest $receiptRequest" }

val receiptResponse: ReceiptResponse =
productionRestTemplate.postForObject("/verifyReceipt", prepareHttpHeaders(receiptRequest), ReceiptResponse::class.java)!!

logger.debug { "Getting ReceiptResponse $receiptResponse" }
logger.debug { "processRequest: ReceiptResponse $receiptResponse" }

if (receiptResponse.shouldRetry()) {
logger.warn { "Retrying due to ${receiptResponse.responseStatusCode} status code" }
throw RuntimeException("Retrying due to ${receiptResponse.responseStatusCode}")
val message = "Retrying due to ${receiptResponse.responseStatusCode} status code"
logger.warn { message }
throw RetryableException(message)
}
return receiptResponse
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ class AppStoreSubscriptionService(val userAppClient: UserAppClient, val appStore
fun handleInitialPurchase(subscriptionPurchaseRequest: SubscriptionPurchaseRequest) : UserAppSubscriptionOrder? {
val receiptResponse = appStoreClient.verifyReceipt(ReceiptRequest(subscriptionPurchaseRequest.receipt))

logger.debug { "handleInitialPurchase: ReceiptResponse: $receiptResponse" }

if (receiptResponse.isValid()) {

// TODO find out which one of latestReceiptInfo
val latestReceiptInfo = receiptResponse.latestReceiptInfo!!.stream().findFirst().get()

val notification = UserAppSubscriptionNotification(
Expand All @@ -40,17 +41,19 @@ class AppStoreSubscriptionService(val userAppClient: UserAppClient, val appStore

return userAppClient.sendSubscriptionNotification(notification)
} else {
throw ReceiptValidationException("The ${receiptResponse.receipt} is not a valid receipt. " +
"Response code ${receiptResponse.responseStatusCode}")
val message = "The ${receiptResponse.receipt} is not a valid receipt. Response code ${receiptResponse.responseStatusCode}"
logger.error { message }
throw ReceiptValidationException(message)
}
}

fun handleAutoRenewal(subscriptionRenewRequest: SubscriptionRenewRequest) : UserAppSubscriptionOrder? {
val receiptResponse = appStoreClient.verifyReceipt(ReceiptRequest(subscriptionRenewRequest.receipt))

logger.debug { "handleAutoRenewal: ReceiptResponse: $receiptResponse" }

if (receiptResponse.isValid()) {

// TODO find out which one of latestReceiptInfo
val latestReceiptInfo = receiptResponse.latestReceiptInfo!!.stream().findFirst().get()

val notification = UserAppSubscriptionNotification(
Expand All @@ -68,8 +71,9 @@ class AppStoreSubscriptionService(val userAppClient: UserAppClient, val appStore

return userAppClient.sendSubscriptionNotification(notification)
} else {
throw ReceiptValidationException("The ${receiptResponse.receipt} is not a valid receipt. " +
"Response code ${receiptResponse.responseStatusCode}")
val message = "The ${receiptResponse.receipt} is not a valid receipt. Response code ${receiptResponse.responseStatusCode}"
logger.error { message }
throw ReceiptValidationException(message)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package com.dietmap.yaak.domain.appstore
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.ResponseStatus

@ResponseStatus(value = HttpStatus.BAD_REQUEST)
class ReceiptValidationException(message : String) : RuntimeException() {
@ResponseStatus(value = HttpStatus.UNPROCESSABLE_ENTITY)
class ReceiptValidationException(message : String) : RuntimeException(message) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.dietmap.yaak.domain.appstore

class RetryableException(message : String) : RuntimeException(message) {
}

0 comments on commit aedd53e

Please sign in to comment.