Skip to content

Commit

Permalink
Merge pull request #2010 from Hafizzle/Fiz/gem-package-purchase-fix
Browse files Browse the repository at this point in the history
Handle non-HTTPExceptions & continue flow if consume fails & no error
  • Loading branch information
phillipthelen committed Jul 7, 2023
2 parents bfcd425 + 43a14f4 commit 057ecd7
Showing 1 changed file with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
}
Expand Down

0 comments on commit 057ecd7

Please sign in to comment.