Skip to content

Commit

Permalink
Merge pull request #29 from epegasus/master
Browse files Browse the repository at this point in the history
Old Purchase (Bug Fixed)
  • Loading branch information
hypersoftdev committed Jul 25, 2023
2 parents 6be36d1 + 00bc4a0 commit 6809da8
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 20 deletions.
6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ billingManager.makeSubPurchase(activity, SubscriptionPlans.basicPlanMonthly, obj
Add plans and tags on Play Console
There are few subsciption tags as follow, to generate plans.

For Weekly Subscription

-> Product ID: basic_product_weekly
-> Plan ID: basic-plan-weekly

For Montly Subscription

-> Product ID: basic_product_monthly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.hypersoft.billing.dataClasses.ProductDetail
import com.hypersoft.billing.helper.BillingHelper.Companion.TAG
import com.hypersoft.billing.interfaces.OnPurchaseListener
import com.hypersoft.billing.status.State
import dev.epegasus.billinginapppurchases.interfaces.OnConnectionListener
import com.hypersoft.billing.interfaces.OnConnectionListener

class MainActivity : AppCompatActivity() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.app.Activity
import android.content.Context
import com.hypersoft.billing.helper.BillingHelper
import com.hypersoft.billing.interfaces.OnPurchaseListener
import dev.epegasus.billinginapppurchases.interfaces.OnConnectionListener
import com.hypersoft.billing.interfaces.OnConnectionListener

/**
* @param context: Context can be of Application class
Expand Down
51 changes: 36 additions & 15 deletions billing/src/main/java/com/hypersoft/billing/helper/BillingHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import com.hypersoft.billing.enums.ProductType
import com.hypersoft.billing.interfaces.OnPurchaseListener
import com.hypersoft.billing.status.State.getBillingState
import com.hypersoft.billing.status.State.setBillingState
import dev.epegasus.billinginapppurchases.interfaces.OnConnectionListener
import com.hypersoft.billing.interfaces.OnConnectionListener
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -90,6 +90,7 @@ abstract class BillingHelper(private val context: Context) {

if (billingClient.isReady) {
setBillingState(BillingState.CONNECTION_ALREADY_ESTABLISHING)
proceedBilling()
return
}

Expand All @@ -104,11 +105,7 @@ abstract class BillingHelper(private val context: Context) {
override fun onBillingSetupFinished(billingResult: BillingResult) {
val isBillingReady = billingResult.responseCode == BillingClient.BillingResponseCode.OK
if (isBillingReady) {
setBillingState(BillingState.CONNECTION_ESTABLISHED)
getInAppOldPurchases()
Handler(Looper.getMainLooper()).post {
onConnectionListener.onConnectionResult(true, BillingState.CONNECTION_ESTABLISHED.message)
}
proceedBilling()
} else {
setBillingState(BillingState.CONNECTION_FAILED)
onConnectionListener.onConnectionResult(false, billingResult.debugMessage)
Expand All @@ -117,11 +114,21 @@ abstract class BillingHelper(private val context: Context) {
})
}

private fun proceedBilling() {
setBillingState(BillingState.CONNECTION_ESTABLISHED)
getInAppOldPurchases()
Handler(Looper.getMainLooper()).post {
onConnectionListener?.onConnectionResult(true, BillingState.CONNECTION_ESTABLISHED.message)
}
}

private fun getInAppOldPurchases() = CoroutineScope(Dispatchers.Main).launch {
setBillingState(BillingState.CONSOLE_OLD_PRODUCTS_INAPP_FETCHING)
val queryPurchasesParams = QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.INAPP).build()
billingClient.queryPurchasesAsync(queryPurchasesParams) { _, purchases ->
onConnectionListener?.onOldPurchaseResult(false)
CoroutineScope(Dispatchers.Main).launch {
onConnectionListener?.onOldPurchaseResult(false)
}

Log.d(TAG, " --------------------------- old purchase (In-App) --------------------------- ")
Log.d(TAG, "getInAppOldPurchases: Object: $purchases")
Expand All @@ -142,13 +149,16 @@ abstract class BillingHelper(private val context: Context) {
if (purchase.isAcknowledged) {
dataProviderInApp.getProductIdsList().forEach {
if (it.contains(compareSKU, true)) {
isPurchasedFound = true
setBillingState(BillingState.CONSOLE_OLD_PRODUCTS_INAPP_OWNED)
Handler(Looper.getMainLooper()).post {
isPurchasedFound = true
onConnectionListener?.onOldPurchaseResult(true)
CoroutineScope(Dispatchers.Main).launch {
onConnectionListener?.onOldPurchaseResult(true)
}
}
}
}
checkForSubscriptionIfAvailable()
} else {
if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {
for (i in 0 until dataProviderInApp.getProductIdsList().size) {
Expand All @@ -162,19 +172,24 @@ abstract class BillingHelper(private val context: Context) {
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK || purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {
setBillingState(BillingState.CONSOLE_OLD_PRODUCTS_INAPP_OWNED_AND_ACKNOWLEDGE)
isPurchasedFound = true
onConnectionListener?.onOldPurchaseResult(true)
CoroutineScope(Dispatchers.Main).launch {
onConnectionListener?.onOldPurchaseResult(true)
}
} else {
setBillingState(BillingState.CONSOLE_OLD_PRODUCTS_INAPP_OWNED_AND_FAILED_TO_ACKNOWLEDGE)
}
checkForSubscriptionIfAvailable()
}
}
} else {
checkForSubscriptionIfAvailable()
}
}
}
if (purchases.isEmpty()) {
setBillingState(BillingState.CONSOLE_OLD_PRODUCTS_INAPP_NOT_FOUND)
checkForSubscriptionIfAvailable()
}
checkForSubscriptionIfAvailable()
queryForAvailableInAppProducts()
queryForAvailableSubProducts()
}
Expand All @@ -193,10 +208,12 @@ abstract class BillingHelper(private val context: Context) {

billingClient.queryPurchasesAsync(queryPurchasesParams) { billingResult, purchases ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
onConnectionListener?.onOldPurchaseResult(false)
CoroutineScope(Dispatchers.Main).launch {
onConnectionListener?.onOldPurchaseResult(false)
}

Log.d(TAG, " --------------------------- old purchase (Sub) --------------------------- ")
Log.d(TAG, "getSubscriptionOldPurchases: Object: $purchases")
Log.d(TAG, "getSubscriptionOldPurchases: List: $purchases")
purchases.forEach { purchase ->
Log.d(TAG, "getSubscriptionOldPurchases: Object: $purchase")
Log.d(TAG, "getSubscriptionOldPurchases: Products: ${purchase.products}")
Expand All @@ -216,7 +233,9 @@ abstract class BillingHelper(private val context: Context) {
for (i in 0 until dataProviderSub.productIdsList.size) {
if (dataProviderSub.productIdsList[i].contains(compareSKU)) {
setBillingState(BillingState.CONSOLE_OLD_PRODUCTS_SUB_OWNED)
onConnectionListener?.onOldPurchaseResult(true)
CoroutineScope(Dispatchers.Main).launch {
onConnectionListener?.onOldPurchaseResult(true)
}
return@forEach
}
}
Expand All @@ -230,7 +249,9 @@ abstract class BillingHelper(private val context: Context) {
billingClient.acknowledgePurchase(acknowledgePurchaseParams) { billingResult: BillingResult ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK || purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {
setBillingState(BillingState.CONSOLE_OLD_PRODUCTS_SUB_OWNED_AND_ACKNOWLEDGE)
onConnectionListener?.onOldPurchaseResult(true)
CoroutineScope(Dispatchers.Main).launch {
onConnectionListener?.onOldPurchaseResult(true)
}
} else {
setBillingState(BillingState.CONSOLE_OLD_PRODUCTS_SUB_OWNED_AND_FAILED_TO_ACKNOWLEDGE)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.epegasus.billinginapppurchases.interfaces
package com.hypersoft.billing.interfaces

/**
* @Author: SOHAIB AHMED
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Jul 17 15:59:31 PKT 2023
#Mon Jul 24 15:53:47 PKT 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

0 comments on commit 6809da8

Please sign in to comment.