Skip to content

Commit

Permalink
Merge pull request #76 from Skaldebane/master
Browse files Browse the repository at this point in the history
Fix PENDING state not reported
  • Loading branch information
akshaaatt authored Apr 25, 2022
2 parents 4ba4dc8 + 984dbba commit 771632d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
18 changes: 10 additions & 8 deletions iap/src/main/java/com/aemerse/iap/BillingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions iap/src/main/java/com/aemerse/iap/IBillingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ 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 {
productOwnedInternal(purchaseInfo, isRestore)
}
}

fun productOwnedInternal(purchaseInfo: DataWrappers.PurchaseInfo, isRestore: Boolean) {
private fun productOwnedInternal(purchaseInfo: DataWrappers.PurchaseInfo, isRestore: Boolean) {
for (purchaseServiceListener in purchaseServiceListeners) {
if (isRestore) {
purchaseServiceListener.onProductRestored(purchaseInfo)
Expand All @@ -58,16 +58,16 @@ 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 {
subscriptionOwnedInternal(purchaseInfo, isRestore)
}
}

fun subscriptionOwnedInternal(purchaseInfo: DataWrappers.PurchaseInfo, isRestore: Boolean) {
private fun subscriptionOwnedInternal(purchaseInfo: DataWrappers.PurchaseInfo, isRestore: Boolean) {
for (subscriptionServiceListener in subscriptionServiceListeners) {
if (isRestore) {
subscriptionServiceListener.onSubscriptionRestored(purchaseInfo)
Expand All @@ -85,18 +85,18 @@ abstract class IBillingService {
}
}

fun updatePrices(iapkeyPrices: Map<String, DataWrappers.SkuDetails>) {
fun updatePrices(iapKeyPrices: Map<String, DataWrappers.SkuDetails>) {
findUiHandler().post {
updatePricesInternal(iapkeyPrices)
updatePricesInternal(iapKeyPrices)
}
}

fun updatePricesInternal(iapkeyPrices: Map<String, DataWrappers.SkuDetails>) {
private fun updatePricesInternal(iapKeyPrices: Map<String, DataWrappers.SkuDetails>) {
for (billingServiceListener in purchaseServiceListeners) {
billingServiceListener.onPricesUpdated(iapkeyPrices)
billingServiceListener.onPricesUpdated(iapKeyPrices)
}
for (billingServiceListener in subscriptionServiceListeners) {
billingServiceListener.onPricesUpdated(iapkeyPrices)
billingServiceListener.onPricesUpdated(iapKeyPrices)
}
}

Expand Down

0 comments on commit 771632d

Please sign in to comment.