diff --git a/iap/src/main/java/com/aemerse/iap/BillingService.kt b/iap/src/main/java/com/aemerse/iap/BillingService.kt index 9d67f88..d3d1d3a 100644 --- a/iap/src/main/java/com/aemerse/iap/BillingService.kt +++ b/iap/src/main/java/com/aemerse/iap/BillingService.kt @@ -58,9 +58,9 @@ class BillingService( * New purchases will be provided to the PurchasesUpdatedListener. */ private suspend fun queryPurchases() { - val inappResult: PurchasesResult = + val inAppResult: PurchasesResult = mBillingClient.queryPurchasesAsync(BillingClient.SkuType.INAPP) - processPurchases(inappResult.purchasesList, isRestore = true) + processPurchases(inAppResult.purchasesList, isRestore = true) val subsResult: PurchasesResult = mBillingClient.queryPurchasesAsync(BillingClient.SkuType.SUBS) processPurchases(subsResult.purchasesList, isRestore = true) @@ -149,7 +149,11 @@ class BillingService( if (!purchasesList.isNullOrEmpty()) { log("processPurchases: " + purchasesList.size + " purchase(s)") purchases@ for (purchase in purchasesList) { - if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED && purchase.skus[0].isSkuReady()) { + // The purchase is considered successful in both PURCHASED and PENDING states. + val purchaseSuccess = purchase.purchaseState == Purchase.PurchaseState.PURCHASED + || purchase.purchaseState == Purchase.PurchaseState.PENDING + + if (purchaseSuccess && purchase.skus[0].isSkuReady()) { if (!isSignatureValid(purchase)) { log("processPurchases. Signature is not valid for: $purchase") continue@purchases @@ -159,9 +163,7 @@ class BillingService( val skuDetails = skusDetails[purchase.skus[0]] when (skuDetails?.type) { BillingClient.SkuType.INAPP -> { - /** - * Consume the purchase - */ + // Consume the purchase if (consumableKeys.contains(purchase.skus[0])) { mBillingClient.consumeAsync( ConsumeParams.newBuilder() @@ -188,8 +190,8 @@ class BillingService( } } - // Acknowledge the purchase if it hasn't already been acknowledged. - if (!purchase.isAcknowledged) { + // If the state is PURCHASED, acknowledge the purchase if it hasn't been acknowledged yet. + if (!purchase.isAcknowledged && purchase.purchaseState == Purchase.PurchaseState.PURCHASED) { val acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder() .setPurchaseToken(purchase.purchaseToken).build() mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, this) diff --git a/iap/src/main/java/com/aemerse/iap/IBillingService.kt b/iap/src/main/java/com/aemerse/iap/IBillingService.kt index 875e152..864d5f7 100644 --- a/iap/src/main/java/com/aemerse/iap/IBillingService.kt +++ b/iap/src/main/java/com/aemerse/iap/IBillingService.kt @@ -38,8 +38,8 @@ abstract class IBillingService { } /** - * @param purchaseInfo - product specifier - * @param isRestore - a flag indicating whether it's a fresh purchase or restored product + * @param purchaseInfo Product specifier + * @param isRestore Flag indicating whether it's a fresh purchase or restored product */ fun productOwned(purchaseInfo: DataWrappers.PurchaseInfo, isRestore: Boolean) { findUiHandler().post { @@ -47,7 +47,7 @@ abstract class IBillingService { } } - fun productOwnedInternal(purchaseInfo: DataWrappers.PurchaseInfo, isRestore: Boolean) { + private fun productOwnedInternal(purchaseInfo: DataWrappers.PurchaseInfo, isRestore: Boolean) { for (purchaseServiceListener in purchaseServiceListeners) { if (isRestore) { purchaseServiceListener.onProductRestored(purchaseInfo) @@ -58,8 +58,8 @@ abstract class IBillingService { } /** - * @param purchaseInfo - subscription specifier - * @param isRestore - a flag indicating whether it's a fresh purchase or restored subscription + * @param purchaseInfo Subscription specifier + * @param isRestore Flag indicating whether it's a fresh purchase or restored subscription */ fun subscriptionOwned(purchaseInfo: DataWrappers.PurchaseInfo, isRestore: Boolean) { findUiHandler().post { @@ -67,7 +67,7 @@ abstract class IBillingService { } } - fun subscriptionOwnedInternal(purchaseInfo: DataWrappers.PurchaseInfo, isRestore: Boolean) { + private fun subscriptionOwnedInternal(purchaseInfo: DataWrappers.PurchaseInfo, isRestore: Boolean) { for (subscriptionServiceListener in subscriptionServiceListeners) { if (isRestore) { subscriptionServiceListener.onSubscriptionRestored(purchaseInfo) @@ -85,18 +85,18 @@ abstract class IBillingService { } } - fun updatePrices(iapkeyPrices: Map) { + fun updatePrices(iapKeyPrices: Map) { findUiHandler().post { - updatePricesInternal(iapkeyPrices) + updatePricesInternal(iapKeyPrices) } } - fun updatePricesInternal(iapkeyPrices: Map) { + private fun updatePricesInternal(iapKeyPrices: Map) { for (billingServiceListener in purchaseServiceListeners) { - billingServiceListener.onPricesUpdated(iapkeyPrices) + billingServiceListener.onPricesUpdated(iapKeyPrices) } for (billingServiceListener in subscriptionServiceListeners) { - billingServiceListener.onPricesUpdated(iapkeyPrices) + billingServiceListener.onPricesUpdated(iapKeyPrices) } }