mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-07-19 20:34:15 +00:00
Handle non-HTTPExceptions & continue flow if consume fails & no error
- Throw exception on consume if failed to consume (to continue flow/handle error) - Handle other potential errors (such as IOException or a exception thrown by billingClient.consumePurchase method that would not previously be handled
This commit is contained in:
parent
b3060267f7
commit
99871faff4
1 changed files with 22 additions and 10 deletions
|
|
@ -272,9 +272,12 @@ class PurchaseHandler(
|
|||
retryUntil { billingClientState.canMaybePurchase && billingClient.isReady }
|
||||
val params = ConsumeParams.newBuilder().setPurchaseToken(purchase.purchaseToken).build()
|
||||
val result = billingClient.consumePurchase(params)
|
||||
if (result.billingResult.responseCode != BillingClient.BillingResponseCode.OK) {
|
||||
if (result.billingResult.responseCode != BillingClient.BillingResponseCode.OK && retries > 0) {
|
||||
delay(500)
|
||||
consume(purchase, retries - 1)
|
||||
} else if (result.billingResult.responseCode != BillingClient.BillingResponseCode.OK) {
|
||||
//Throw an error to continue the flow
|
||||
throw Exception("Failed to consume purchase after multiple attempts")
|
||||
} else {
|
||||
userViewModel.userRepository.retrieveUser(false, true)
|
||||
}
|
||||
|
|
@ -393,16 +396,25 @@ class PurchaseHandler(
|
|||
}
|
||||
|
||||
private fun handleError(throwable : Throwable, purchase : Purchase) {
|
||||
(throwable as? HttpException)?.let { error ->
|
||||
if (error.code() == 401) {
|
||||
val res = apiClient.getErrorResponse(throwable)
|
||||
if (res.message != null && res.message == "RECEIPT_ALREADY_USED") {
|
||||
processedPurchase(purchase)
|
||||
removeGift(purchase.products.firstOrNull())
|
||||
CoroutineScope(Dispatchers.IO).launch(ExceptionHandler.coroutine()) {
|
||||
consume(purchase)
|
||||
when (throwable) {
|
||||
is HttpException -> {
|
||||
if (throwable.code() == 401) {
|
||||
val res = apiClient.getErrorResponse(throwable)
|
||||
if (res.message != null && res.message == "RECEIPT_ALREADY_USED") {
|
||||
processedPurchase(purchase)
|
||||
removeGift(purchase.products.firstOrNull())
|
||||
CoroutineScope(Dispatchers.IO).launch(ExceptionHandler.coroutine()) {
|
||||
consume(purchase)
|
||||
}
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
// Handles other potential errors such as IOException or an exception
|
||||
// thrown by billingClient.consumePurchase method that is not handled
|
||||
CoroutineScope(Dispatchers.IO).launch(ExceptionHandler.coroutine()) {
|
||||
consume(purchase)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue