Skip to content

Commit

Permalink
Merge pull request #20 from epegasus/master
Browse files Browse the repository at this point in the history
Subscription
  • Loading branch information
hypersoftdev committed May 12, 2023
2 parents 7454285 + 2e3f334 commit e338bdf
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 35 deletions.
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 @@ -9,7 +9,7 @@ import androidx.appcompat.app.AppCompatActivity
import com.hypersoft.billing.BillingManager
import com.hypersoft.billing.interfaces.OnPurchaseListener
import com.hypersoft.billing.status.State
import com.hypersoft.billing.status.SubscriptionTags
import com.hypersoft.billing.constants.SubscriptionPlans
import dev.epegasus.billinginapppurchases.interfaces.OnConnectionListener

class MainActivity : AppCompatActivity() {
Expand Down Expand Up @@ -79,7 +79,7 @@ class MainActivity : AppCompatActivity() {
})*/

// Subscription
billingManager.makeSubPurchase(this, SubscriptionTags.basicPlanMonthly, object : OnPurchaseListener {
billingManager.makeSubPurchase(this, SubscriptionPlans.basicPlanMonthly, object : OnPurchaseListener {
override fun onPurchaseResult(isPurchaseSuccess: Boolean, message: String) {
showMessage(message)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class BillingManager(context: Context) : BillingHelper(context) {

fun makeInAppPurchase(activity: Activity?, onPurchaseListener: OnPurchaseListener) = purchaseInApp(activity, onPurchaseListener)

fun makeSubPurchase(activity: Activity?, subscriptionTags: String, onPurchaseListener: OnPurchaseListener) = purchaseSub(activity, subscriptionTags, onPurchaseListener)
fun makeSubPurchase(activity: Activity?, subscriptionPlans: String, onPurchaseListener: OnPurchaseListener) = purchaseSub(activity, subscriptionPlans, onPurchaseListener)

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hypersoft.billing.status
package com.hypersoft.billing.constants

/**
* @Author: SOHAIB AHMED
Expand All @@ -8,16 +8,16 @@ package com.hypersoft.billing.status
* -> https://stackoverflow.com/users/20440272/sohaib-ahmed
*/

object SubscriptionTags {
object SubscriptionPlans {

const val basicPlanWeekly = "basic-plan-weekly"
const val basicPlanFourWeeks = "basic-plan-four-weeks"
const val basicPlanMonthly = "basic-plan-monthly"
const val basicPlanQuarterly = "basic-plan-quarterly"
const val basicPlanSemiYearly = "basic-plan-semi-yearly"
const val basicPlanYearly = "basic-plan-yearly"
const val basicPlanLifeTime = "basic-plan-lifetime"

/*const val basicPlanWeekly = "basic-weekly"
const val basicPlanQuarterly = "basic-quarterly"
const val basicPlanSemiYearly = "basic-semi-yearly"*/

/*const val premiumPlanWeekly = "premium-weekly"
const val premiumPlanMonthly = "premium-monthly"
const val premiumPlanQuarterly = "premium-quarterly"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.hypersoft.billing.constants

/**
* @Author: SOHAIB AHMED
* @Date: 12,May,2023
* @Accounts
* -> https://github.com/epegasus
* -> https://stackoverflow.com/users/20440272/sohaib-ahmed
*/

object SubscriptionProductIds {

const val basicProductWeekly = "basic_product_weekly"
const val basicProductFourWeeks = "basic_product_four_weeks"
const val basicProductMonthly = "basic_product_monthly"
const val basicProductQuarterly = "basic_product_quarterly"
const val basicProductSemiYearly = "basic_product_semi-yearly"
const val basicProductYearly = "basic_product_yearly"
const val basicProductLifeTime = "basic_product_lifetime"

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.hypersoft.billing.dataProvider
import com.android.billingclient.api.BillingClient
import com.android.billingclient.api.ProductDetails
import com.android.billingclient.api.QueryProductDetailsParams
import com.hypersoft.billing.constants.SubscriptionProductIds

internal class DataProviderSub {

Expand All @@ -15,9 +16,13 @@ internal class DataProviderSub {
*/

val productIdsList = listOf(
"basic_product_monthly",
"basic_product_yearly",
"basic_product_lifetime"
SubscriptionProductIds.basicProductWeekly,
SubscriptionProductIds.basicProductFourWeeks,
SubscriptionProductIds.basicProductMonthly,
SubscriptionProductIds.basicProductQuarterly,
SubscriptionProductIds.basicProductSemiYearly,
SubscriptionProductIds.basicProductYearly,
SubscriptionProductIds.basicProductLifeTime,
)

fun getProductList(): List<QueryProductDetailsParams.Product> {
Expand Down
54 changes: 31 additions & 23 deletions billing/src/main/java/com/hypersoft/billing/helper/BillingHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import com.android.billingclient.api.PurchasesUpdatedListener
import com.android.billingclient.api.QueryProductDetailsParams
import com.android.billingclient.api.QueryPurchasesParams
import com.android.billingclient.api.queryProductDetails
import com.hypersoft.billing.constants.SubscriptionPlans
import com.hypersoft.billing.dataProvider.DataProviderInApp
import com.hypersoft.billing.dataProvider.DataProviderSub
import com.hypersoft.billing.enums.BillingState
import com.hypersoft.billing.interfaces.OnPurchaseListener
import com.hypersoft.billing.status.State.getBillingState
import com.hypersoft.billing.status.State.setBillingState
import com.hypersoft.billing.status.SubscriptionTags
import dev.epegasus.billinginapppurchases.interfaces.OnConnectionListener
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -353,47 +353,55 @@ abstract class BillingHelper(private val context: Context) {
return false
}

protected fun purchaseSub(activity: Activity?, subscriptionTags: String, onPurchaseListener: OnPurchaseListener) {
protected fun purchaseSub(activity: Activity?, subscriptionPlans: 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.basicPlanMonthly -> 0
SubscriptionTags.basicPlanYearly -> 1
/*SubscriptionTags.premiumPlanMonthly -> 2
SubscriptionTags.premiumYearly -> 3*/
else -> -1
Log.d(TAG, "purchaseSub: Starting: ${dataProviderSub.getProductDetailsList()}")

var prodDetails: ProductDetails? = null

dataProviderSub.getProductDetailsList().forEach { productDetails ->
if (productDetails.productId == SubscriptionPlans.basicPlanWeekly && subscriptionPlans == SubscriptionPlans.basicPlanWeekly) {
prodDetails = productDetails
return@forEach
} else if (productDetails.productId == SubscriptionPlans.basicPlanFourWeeks && subscriptionPlans == SubscriptionPlans.basicPlanFourWeeks) {
prodDetails = productDetails
return@forEach
} else if (productDetails.productId == SubscriptionPlans.basicPlanMonthly && subscriptionPlans == SubscriptionPlans.basicPlanMonthly) {
prodDetails = productDetails
return@forEach
} else if (productDetails.productId == SubscriptionPlans.basicPlanQuarterly && subscriptionPlans == SubscriptionPlans.basicPlanQuarterly) {
prodDetails = productDetails
return@forEach
} else if (productDetails.productId == SubscriptionPlans.basicPlanSemiYearly && subscriptionPlans == SubscriptionPlans.basicPlanSemiYearly) {
prodDetails = productDetails
return@forEach
} else if (productDetails.productId == SubscriptionPlans.basicPlanYearly && subscriptionPlans == SubscriptionPlans.basicPlanYearly) {
prodDetails = productDetails
return@forEach
}
}
Log.d(TAG, "purchaseSub: indexOf : $indexOf")

if (indexOf == -1) {
setBillingState(BillingState.CONSOLE_PRODUCTS_SUB_NOT_FOUND)
return
}
Log.d(TAG, "purchaseSub: prodDetails : $prodDetails")

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

val productDetails = dataProviderSub.getProductDetailsList()[indexOf]

// Retrieve all offers the user is eligible for.
val offers = productDetails.subscriptionOfferDetails?.let {
retrieveEligibleOffers(offerDetails = it, tag = subscriptionTags)
val offers = prodDetails!!.subscriptionOfferDetails?.let {
retrieveEligibleOffers(offerDetails = it, tag = subscriptionPlans)
}

// Get the offer id token of the lowest priced offer.
val offerToken = offers?.let { leastPricedOfferToken(it) }

offerToken?.let { token ->
val productDetailsParamsList = listOf(BillingFlowParams.ProductDetailsParams.newBuilder().setProductDetails(productDetails).setOfferToken(token).build())
val productDetailsParamsList = listOf(BillingFlowParams.ProductDetailsParams.newBuilder().setProductDetails(prodDetails!!).setOfferToken(token).build())
val billingFlowParams = BillingFlowParams.newBuilder().setProductDetailsParamsList(productDetailsParamsList).build()

// Launch the billing flow
Expand Down

0 comments on commit e338bdf

Please sign in to comment.