Skip to content

Commit

Permalink
- Added support for consumables.
Browse files Browse the repository at this point in the history
- Cleaned the code a bit.
- Added facility to support parameters more flexibly.
  • Loading branch information
akshaaatt committed Apr 17, 2021
1 parent 40c1892 commit 1d43099
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 27 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "com.alphelios.richierich"
minSdkVersion 21
targetSdkVersion 30
versionCode 9
versionName "1.0.8"
versionCode 12
versionName "1.1.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Binary file modified app/release/app-release.aab
Binary file not shown.
16 changes: 9 additions & 7 deletions app/src/main/java/com/alphelios/richierich/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {

binding.bottomnavview.itemIconTintList = null

val skuList = listOf("base", "moderate", "quite", "plenty", "yearly")
val nonConsumablesList = listOf("base")
val consumablesList = listOf("base", "moderate", "quite", "plenty", "yearly")
val subsList = listOf("subscription")

val iapConnector = IapConnector(
this,
skuList,
subsList,
"LICENSE KEY",
true
context = this,
nonConsumableKeys = nonConsumablesList,
consumableKeys = consumablesList,
subscriptionKeys = subsList,
key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAh09sfdDzMhCh3AG9mq2EsyFUN72FBKabPpMsJyUUwXVsVJRLDWQYKmWnr0bVGsdVHwQtEDi//EY1NubXjCmViAxFnnfbdUrAk9PbRRTaMQ4taifn9fCaQ6XAW70ju3/mXuL+1xX+r8B0O9B373sP0YqiUs0b8HTtTjQoOGtcGb2KlYpQlJjjtISfVpsSk2RdKatkDyeesv+6568O7xgb5zp/KNJk8d1fMqKGWJiveFkZvedDh1ECdi3rSz1aQB+z/aEf6AIiuLzu0V8NNKjvZnxUjVJgL+lcLDrL1YZuKx9h5BX7k8lPZI7fLIVE6b6iNx3msfVdiqPkZ3s49JxA1QIDAQAB",
enableLogging = true
)

iapConnector.addPurchaseListener(object : PurchaseServiceListener {
Expand Down Expand Up @@ -55,7 +57,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
}

override fun onProductRestored(sku: String?) {
// will be triggered fetching owned products using IAPManager.init();
// will be triggered fetching owned products using IapConnector;
}
})

Expand Down
4 changes: 2 additions & 2 deletions iap/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 17
targetSdkVersion 30
versionCode 12
versionName "1.1.1"
versionCode 13
versionName "1.1.2"
}

buildTypes {
Expand Down
32 changes: 27 additions & 5 deletions iap/src/main/java/com/alphelios/iap/BillingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import android.util.Log
import com.android.billingclient.api.*

class BillingService(private val context: Context,
private val inAppSkuKeys: List<String>,
private val nonConsumableKeys: List<String>,
private val consumableKeys: List<String>,
private val subscriptionSkuKeys: List<String>)
: IBillingService(), PurchasesUpdatedListener, BillingClientStateListener, AcknowledgePurchaseResponseListener {

Expand All @@ -29,9 +30,11 @@ class BillingService(private val context: Context,
override fun onBillingSetupFinished(billingResult: BillingResult) {
log("onBillingSetupFinished: billingResult: $billingResult")
if (billingResult.isOk()) {
inAppSkuKeys.querySkuDetails(BillingClient.SkuType.INAPP) {
subscriptionSkuKeys.querySkuDetails(BillingClient.SkuType.SUBS) {
queryPurchases()
nonConsumableKeys.querySkuDetails(BillingClient.SkuType.INAPP) {
consumableKeys.querySkuDetails(BillingClient.SkuType.INAPP) {
subscriptionSkuKeys.querySkuDetails(BillingClient.SkuType.SUBS) {
queryPurchases()
}
}
}
}
Expand Down Expand Up @@ -143,7 +146,26 @@ class BillingService(private val context: Context,
val skuDetails = skusDetails[purchase.sku]
when (skuDetails?.type) {
BillingClient.SkuType.INAPP -> {
productOwned(purchase.sku, isRestore)
/**
* Consume the purchase
*/
if(consumableKeys.contains(purchase.sku)){
mBillingClient.consumeAsync(
ConsumeParams.newBuilder().setPurchaseToken(purchase.purchaseToken).build()
) { billingResult, _ ->
when (billingResult.responseCode) {
BillingClient.BillingResponseCode.OK -> {
productOwned(purchase.sku, !isRestore)
}
else -> {
Log.d(TAG, "Handling consumables : Error during consumption attempt -> ${billingResult.debugMessage}")
}
}
}
}
else{
productOwned(purchase.sku, isRestore)
}
}
BillingClient.SkuType.SUBS -> {
subscriptionOwned(purchase.sku, isRestore)
Expand Down
4 changes: 2 additions & 2 deletions iap/src/main/java/com/alphelios/iap/IBillingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class IBillingService {
}

/**
* @param sku - product specificator
* @param sku - product specifier
* @param isRestore - a flag indicating whether it's a fresh purchase or restored product
*/
fun productOwned(sku: String?, isRestore: Boolean) {
Expand All @@ -47,7 +47,7 @@ abstract class IBillingService {
}

/**
* @param sku - subscription specificator
* @param sku - subscription specifier
* @param isRestore - a flag indicating whether it's a fresh purchase or restored subscription
*/
fun subscriptionOwned(sku: String, isRestore: Boolean) {
Expand Down
9 changes: 4 additions & 5 deletions iap/src/main/java/com/alphelios/iap/IapConnector.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.alphelios.iap

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context

Expand All @@ -15,18 +14,18 @@ class IapConnector @JvmOverloads constructor
*/
(
context: Context,
iapKeys: List<String>,
nonConsumableKeys: List<String>,
consumableKeys: List<String>,
subscriptionKeys: List<String> = emptyList(),
key: String? = null,
enableLogging: Boolean = false
) {

@SuppressLint("StaticFieldLeak")
private var mBillingService: IBillingService? = null

init {
val contextLocal = context.applicationContext ?: context
mBillingService = BillingService(contextLocal, iapKeys, subscriptionKeys)
mBillingService = BillingService(contextLocal, nonConsumableKeys, consumableKeys, subscriptionKeys)
getBillingService().init(key)
getBillingService().enableDebugLogging(enableLogging)
}
Expand Down Expand Up @@ -65,7 +64,7 @@ class IapConnector @JvmOverloads constructor

fun getBillingService(): IBillingService {
return mBillingService ?: let {
throw RuntimeException("Call IAPManager.build to initialize billing service")
throw RuntimeException("Call IapConnector to initialize billing service")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ interface PurchaseServiceListener : BillingServiceListener {
/**
* Callback will be triggered when a product purchased successfully
*
* @param sku - specificator of owned product
* @param sku - specifier of owned product
*/
fun onProductPurchased(sku: String?)

/**
* Callback will be triggered upon owned products restore
*
* @param sku - specificator of owned product
* @param sku - specifier of owned product
*/
fun onProductRestored(sku: String?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ interface SubscriptionServiceListener : BillingServiceListener {
/**
* Callback will be triggered upon owned subscription restore
*
* @param sku - specificator of owned subscription
* @param sku - specifier of owned subscription
*/
fun onSubscriptionRestored(sku: String?)

/**
* Callback will be triggered when a subscription purchased successfully
*
* @param sku - specificator of purchased subscription
* @param sku - specifier of purchased subscription
*/
fun onSubscriptionPurchased(sku: String?)
}

0 comments on commit 1d43099

Please sign in to comment.