Skip to content

Commit

Permalink
Merge pull request #17 from epegasus/master
Browse files Browse the repository at this point in the history
Billing (subscription + inapp)
  • Loading branch information
hypersoftdev committed May 12, 2023
2 parents 880d898 + 939e142 commit eb58f6a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
10 changes: 10 additions & 0 deletions .idea/deploymentTargetDropDown.xml

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

4 changes: 2 additions & 2 deletions app/src/main/java/com/hypersoft/inappbilling/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ class MainActivity : AppCompatActivity() {

private fun onPurchaseClick() {
// In-App
billingManager.makeInAppPurchase(this, object : OnPurchaseListener {
/*billingManager.makeInAppPurchase(this, object : OnPurchaseListener {
override fun onPurchaseResult(isPurchaseSuccess: Boolean, message: String) {
showMessage(message)
}
})
})*/

// Subscription
billingManager.makeSubPurchase(this, SubscriptionTags.basicMonthly, object : OnPurchaseListener {
Expand Down
26 changes: 15 additions & 11 deletions billing/src/main/java/com/hypersoft/billing/helper/BillingHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ abstract class BillingHelper(private val context: Context) {
val queryPurchasesParams = QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.INAPP).build()
billingClient.queryPurchasesAsync(queryPurchasesParams) { _, purchases ->
onConnectionListener?.onOldPurchaseResult(false)

Log.d(TAG, " --------------------------- old purchase (In-App) --------------------------- ")
Log.d(TAG, "getSubscriptionOldPurchases: Object: $purchases")
purchases.forEach { purchase ->
Log.d(TAG, "getSubscriptionOldPurchases: Products: ${purchase.products}")
Log.d(TAG, "getSubscriptionOldPurchases: Original JSON: ${purchase.originalJson}")
Log.d(TAG, "getSubscriptionOldPurchases: Developer Payload: ${purchase.developerPayload}")

if (purchase.products.isEmpty()) {
setBillingState(BillingState.CONSOLE_OLD_PRODUCTS_INAPP_NOT_FOUND)
return@forEach
Expand Down Expand Up @@ -192,6 +199,7 @@ abstract class BillingHelper(private val context: Context) {
if (productDetailsResult.productDetailsList.isNullOrEmpty()) {
setBillingState(BillingState.CONSOLE_PRODUCTS_SUB_NOT_EXIST)
} else {
Log.d(TAG, "queryForAvailableSubProducts: ${productDetailsResult.productDetailsList}")
dataProviderSub.setProductDetailsList(productDetailsResult.productDetailsList!!)
setBillingState(BillingState.CONSOLE_PRODUCTS_SUB_AVAILABLE)
}
Expand All @@ -217,7 +225,7 @@ abstract class BillingHelper(private val context: Context) {

purchases.forEach { purchase ->

Log.d(TAG, " --------------------------- old purchase --------------------------- ")
Log.d(TAG, " --------------------------- old purchase (Sub) --------------------------- ")
Log.d(TAG, "getSubscriptionOldPurchases: Object: $purchase")
Log.d(TAG, "getSubscriptionOldPurchases: Products: ${purchase.products}")
Log.d(TAG, "getSubscriptionOldPurchases: Original JSON: ${purchase.originalJson}")
Expand Down Expand Up @@ -346,27 +354,33 @@ abstract class BillingHelper(private val context: Context) {
}

protected fun purchaseSub(activity: Activity?, subscriptionTags: String, onPurchaseListener: OnPurchaseListener) {
Log.d(TAG, "purchaseSub: in")
if (checkValidationsSub(activity)) return

this.onPurchaseListener = onPurchaseListener

Log.d(TAG, "purchaseSub: Starting")

val indexOf = when (subscriptionTags) {
SubscriptionTags.basicMonthly -> 0
SubscriptionTags.basicYearly -> 1
SubscriptionTags.premiumMonthly -> 2
SubscriptionTags.premiumYearly -> 3
else -> -1
}
Log.d(TAG, "purchaseSub: indexOf : $indexOf")

if (indexOf == -1) {
setBillingState(BillingState.CONSOLE_PRODUCTS_SUB_NOT_FOUND)
return
}

if (indexOf >= dataProviderSub.getProductDetailsList().size) {
Log.d(TAG, "purchaseSub: >= : ${dataProviderSub.getProductDetailsList().size}")
setBillingState(BillingState.CONSOLE_PRODUCTS_SUB_NOT_FOUND)
return
}
Log.d(TAG, "purchaseSub: >= : ${dataProviderSub.getProductDetailsList()}")

val productDetails = dataProviderSub.getProductDetailsList()[indexOf]

Expand Down Expand Up @@ -430,16 +444,6 @@ abstract class BillingHelper(private val context: Context) {
return true
}

dataProviderSub.productIdsList.forEach { id ->
dataProviderSub.getProductDetailsList().forEach { productDetails ->
if (id != productDetails.productId) {
setBillingState(BillingState.CONSOLE_PRODUCTS_SUB_NOT_FOUND)
onPurchaseListener?.onPurchaseResult(false, BillingState.CONSOLE_PRODUCTS_SUB_NOT_FOUND.message)
return true
}
}
}

if (billingClient.isFeatureSupported(BillingClient.FeatureType.PRODUCT_DETAILS).responseCode != BillingClient.BillingResponseCode.OK) {
setBillingState(BillingState.FEATURE_NOT_SUPPORTED)
return true
Expand Down

0 comments on commit eb58f6a

Please sign in to comment.