Skip to content

Instantly share code, notes, and snippets.

@isaidamier
Created September 5, 2019 16:28
Show Gist options
  • Save isaidamier/044bb4714d73d8a258361314d44d0f6a to your computer and use it in GitHub Desktop.
Save isaidamier/044bb4714d73d8a258361314d44d0f6a to your computer and use it in GitHub Desktop.
fun queryPurchasesAsync() {
val purchasesResult = HashSet<Purchase>()
var result = playStoreBillingClient.queryPurchases(BillingClient.SkuType.INAPP)
if(nullOrEmpty(result)) {
queryPurchaseHistoryAsync()
} else {
result?.purchasesList?.let { purchasesResult.addAll(it) }
}
}
private fun queryPurchaseHistoryAsync() {
playStoreBillingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS) {
billingResult, purchasesList ->
when (billingResult.responseCode) {
BillingClient.BillingResponseCode.OK -> {
if(purchasesList.isNotEmpty()) {
purchasesList.forEach {
if(it.purchaseTime < oneDayOld()) {
processPurchase(it.sku, it.purchaseToken)
}
}
}
}
else -> {
//do nothing. Someone else will connect it through retry policy.
//May choose to send to server though
Log.d(LOG_TAG, billingResult.debugMessage)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment