Skip to content

Commit

Permalink
Merge pull request #16 from emzy07/develop
Browse files Browse the repository at this point in the history
Several design changes, features and Improvements
  • Loading branch information
akshaaatt authored Apr 4, 2021
2 parents 590f142 + 2418f05 commit 762f9ef
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ iapConnector.setOnInAppEventsListener(object : InAppEventsListener {
}

override fun onProductsPurchased(purchases: List<DataWrappers.PurchaseInfo>) {
/*provides recent purchases*/
/*provides recent purchases and will be triggered on first connection establishment on app launch to get already purchased products*/
}

override fun onError(inAppConnector: IapConnector, result: DataWrappers.BillingResponse?) {
Expand Down
71 changes: 63 additions & 8 deletions iap/src/main/java/com/alphelios/iap/IapConnector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.android.billingclient.api.BillingClient.BillingResponseCode.*
import com.android.billingclient.api.BillingClient.FeatureType.SUBSCRIPTIONS
import com.android.billingclient.api.BillingClient.SkuType.INAPP
import com.android.billingclient.api.BillingClient.SkuType.SUBS
import java.lang.IllegalArgumentException

/**
* Wrapper class for Google In-App purchases.
Expand All @@ -22,14 +23,41 @@ class IapConnector(context: Context, private val base64Key: String) {

private var inAppIds: List<String>? = null
private var subIds: List<String>? = null
private var consumableIds: List<String> = listOf()
private var consumableIds: List<String>? = null

private lateinit var iapClient: BillingClient

private var connected = false
private var checkedForPurchasesAtStart = false

init {
init(context)
}


fun isReady(debugLog: Boolean = false): Boolean {
if (!connected) {
Log.d(tag, "Billing client : is not ready because no connection is established yet")
}

if (!iapClient.isReady) {
Log.d(tag, "Billing client : is not ready because iapClient is not ready yet")
}

if (fetchedSkuDetailsList.isEmpty()) {
Log.d(
tag,
"Billing client : is not ready because fetchedSkuDetailsList is empty or not fetched yet"
)
}

if (!connected) {
Log.d(tag, "Billing client : is not ready because no connection is established yet")
}

return connected && iapClient.isReady && fetchedSkuDetailsList.isNotEmpty()
}

/**
* To set INAPP product IDs.
*/
Expand Down Expand Up @@ -118,26 +146,44 @@ class IapConnector(context: Context, private val base64Key: String) {
* Connects billing client with Play console to start working with IAP.
*/
fun connect(): IapConnector {

// Before we start, check input params we set empty list to null so we only have to deal with lists who are null (not provided) or not empty.
if (inAppIds.isNullOrEmpty())
inAppIds = null
if (subIds.isNullOrEmpty())
subIds = null
if (consumableIds.isNullOrEmpty())
consumableIds = null

Log.d(tag, "Billing service : Connecting...")
if (!iapClient.isReady) {
iapClient.startConnection(object : BillingClientStateListener {
override fun onBillingServiceDisconnected() {
connected = false
inAppEventsListener?.onError(
this@IapConnector,
DataWrappers.BillingResponse("Billing service : Disconnected")
)
Log.d(tag, "Billing service : Trying to establish to reconnect...")
iapClient.startConnection(this)
}

override fun onBillingSetupFinished(billingResult: BillingResult) {
connected = false
when (billingResult.responseCode) {
OK -> {
connected = true
Log.d(tag, "Billing service : Connected")
inAppIds?.let {
querySku(INAPP, it)
}
subIds?.let {
querySku(SUBS, it)
}
if (!checkedForPurchasesAtStart) {
getAllPurchases()
checkedForPurchasesAtStart = true
}
}
BILLING_UNAVAILABLE -> Log.d(tag, "Billing service : Unavailable")
else -> Log.d(tag, "Billing service : Setup error")
Expand All @@ -159,7 +205,7 @@ class IapConnector(context: Context, private val base64Key: String) {
when (billingResult.responseCode) {
OK -> {
if (skuDetailsList!!.isEmpty()) {
Log.d(tag, "Query SKU : Data not found (List empty)")
Log.d(tag, "Query SKU : Data not found (List empty) seems like no SkuIDs are configured on Google Playstore!")
inAppEventsListener?.onError(
this,
billingResult.run {
Expand All @@ -179,11 +225,14 @@ class IapConnector(context: Context, private val base64Key: String) {

if (skuType == SUBS)
inAppEventsListener?.onSubscriptionsFetched(fetchedSkuInfo)
else
else if (skuType == INAPP) {
inAppEventsListener?.onInAppProductsFetched(fetchedSkuInfo.map {
it.isConsumable = consumableIds.contains(it.sku)
it.isConsumable = isSkuIdConsumable(it.sku)
it
})
} else {
throw IllegalStateException("Not implemented SkuType")
}
}
}
else -> {
Expand All @@ -202,6 +251,11 @@ class IapConnector(context: Context, private val base64Key: String) {
}
}

private fun isSkuIdConsumable(skuId: String): Boolean {
if (consumableIds.isNullOrEmpty()) return false
return consumableIds!!.contains(skuId)
}

private fun getSkuInfo(skuDetails: SkuDetails): DataWrappers.SkuInfo {
return DataWrappers.SkuInfo(
skuDetails.sku,
Expand All @@ -225,9 +279,9 @@ class IapConnector(context: Context, private val base64Key: String) {
}

/**
* Returns all the **non-consumable** purchases of the user.
* Returns all the **non-consumable** purchases of the user and trigger the listener.
*/
fun getAllPurchases() {
private fun getAllPurchases() {
if (iapClient.isReady) {
val allPurchases = mutableListOf<Purchase>()
allPurchases.addAll(iapClient.queryPurchases(INAPP).purchasesList!!)
Expand Down Expand Up @@ -285,7 +339,7 @@ class IapConnector(context: Context, private val base64Key: String) {
*/
private fun acknowledgePurchase(purchase: DataWrappers.PurchaseInfo) {
purchase.run {
if (consumableIds.contains(sku))
if (isSkuIdConsumable(this.sku)) {
iapClient.consumeAsync(
ConsumeParams.newBuilder()
.setPurchaseToken(purchaseToken).build()
Expand All @@ -308,7 +362,8 @@ class IapConnector(context: Context, private val base64Key: String) {
)
}
}
} else
}
} else
iapClient.acknowledgePurchase(
AcknowledgePurchaseParams.newBuilder().setPurchaseToken(
purchaseToken
Expand Down

0 comments on commit 762f9ef

Please sign in to comment.