Skip to content

Commit

Permalink
rollback of purchase consuming changes
Browse files Browse the repository at this point in the history
  • Loading branch information
IrynaTsymbaliuk committed May 31, 2022
1 parent 7b407c4 commit 2636aac
Showing 1 changed file with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ class BillingDataSource(application: Application) : PurchasesUpdatedListener,
private var skuDetailsResponseTime = -SKU_DETAILS_REQUERY_TIME

private val knownInAppSKUs = mutableListOf("test_product_id", "product_id_2")
private val knownAutoConsumeSKUs = mutableListOf("test_product_id", "product_id_2")

private val skuStateMap: MutableMap<String, MutableStateFlow<SkuState>> = HashMap()
private val skuDetailsMap: MutableMap<String, MutableStateFlow<SkuDetails?>> = HashMap()
private val _skuDetailsList = MutableLiveData<List<SkuDetails>?>()
val skuDetailsList
get() = _skuDetailsList

private val purchaseConsumptionInProcess: MutableSet<Purchase> = HashSet()
private val newPurchaseFlow = MutableSharedFlow<List<String>>(extraBufferCapacity = 1)
private val purchaseConsumedFlow = MutableSharedFlow<List<String>>()
private val billingFlowInProcess = MutableStateFlow(false)

private val billingClient: BillingClient
Expand Down Expand Up @@ -234,7 +237,33 @@ class BillingDataSource(application: Application) : PurchasesUpdatedListener,
}
updatedSkus.add(sku)
}
setSkuStateFromPurchase(purchase)
val purchaseState = purchase.purchaseState
if (purchaseState == Purchase.PurchaseState.PURCHASED) {
setSkuStateFromPurchase(purchase)
var isConsumable = false
defaultScope.launch {
for (sku in purchase.skus) {
if (knownAutoConsumeSKUs.contains(sku)) {
isConsumable = true
} else {
if (isConsumable) {
Log.e(
TAG, "Purchase cannot contain a mixture of consumable" +
"and non-consumable items: " + purchase.skus.toString()
)
isConsumable = false
break
}
}
}
if (isConsumable) {
consumePurchase(purchase)
newPurchaseFlow.tryEmit(purchase.skus)
}
}
} else {
setSkuStateFromPurchase(purchase)
}
}
} else {
Log.e(TAG, "Empty purchase list.")
Expand All @@ -248,14 +277,40 @@ class BillingDataSource(application: Application) : PurchasesUpdatedListener,
}
}

private suspend fun consumePurchase(purchase: Purchase) {
if (purchaseConsumptionInProcess.contains(purchase)) {
return
}
purchaseConsumptionInProcess.add(purchase)
val consumePurchaseResult = billingClient.consumePurchase(
ConsumeParams.newBuilder()
.setPurchaseToken(purchase.purchaseToken)
.build()
)
purchaseConsumptionInProcess.remove(purchase)
if (consumePurchaseResult.billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
Log.e(TAG, "Consumption successful. Emitting sku.")
defaultScope.launch {
purchaseConsumedFlow.emit(purchase.skus)
}
for (sku in purchase.skus) {
setSkuState(sku, SkuState.SKU_STATE_UNPURCHASED)
}
} else {
Log.e(
TAG,
"Error while consuming: ${consumePurchaseResult.billingResult.debugMessage}"
)
}
}

fun launchBillingFlow(activity: Activity, sku: String, id: String) {
val skuDetails = skuDetailsMap[sku]?.value
if (null != skuDetails) {
val billingFlowParamsBuilder = BillingFlowParams.newBuilder()
billingFlowParamsBuilder
.setObfuscatedAccountId(id)
.setSkuDetails(skuDetails)
Log.e(TAG, "ObfuscatedAccountId = $id")
defaultScope.launch {
val br = billingClient.launchBillingFlow(
activity,
Expand Down

0 comments on commit 2636aac

Please sign in to comment.