Skip to content

Commit

Permalink
Merge pull request #44 from epegasus/master
Browse files Browse the repository at this point in the history
InAppBilling
  • Loading branch information
hypersoftdev authored May 21, 2024
2 parents 2505c92 + 096db95 commit 7c7ac0b
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 80 deletions.
50 changes: 26 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,35 @@ Retrieve a debugging ID for testing and ensure the `purchaseDetailList` paramete

```
val subsProductIdList = listOf("subs_product_id_1", "subs_product_id_2", "subs_product_id_3")
val inAppProductIdList = if (BuildConfig.DEBUG) {
listOf(billingManager.getDebugProductIDList())
} else {
listOf("inapp_product_id_1", "inapp_product_id_2")
val productInAppConsumable = when (BuildConfig.DEBUG) {
true -> listOf("product_id_1")
false -> listOf("product_id_1", "product_id_2")
}
val productInAppNonConsumable = when (BuildConfig.DEBUG) {
true -> listOf(billingManager.getDebugProductIDList())
false -> listOf("product_id_1", "product_id_2")
}
billingManager.initialize(
productInAppPurchases = inAppProductIdList,
productSubscriptions = subsProductIdList,
billingListener = object : BillingListener {
override fun onConnectionResult(isSuccess: Boolean, message: String) {
Log.d(
"BillingTAG",
"Billing: initBilling: onConnectionResult: isSuccess = $isSuccess - message = $message"
)
}
override fun purchasesResult(purchaseDetailList: List<PurchaseDetail>) {
if (purchaseDetailList.isEmpty()) {
// No purchase found, reset all sharedPreferences (premium properties)
}
purchaseDetailList.forEachIndexed { index, purchaseDetail ->
Log.d("BillingTAG", "Billing: initBilling: purchasesResult: $index) $purchaseDetail ")
}
}
billingManager.initialize(
productInAppConsumable = productInAppConsumable,
productInAppNonConsumable = productInAppNonConsumable,
productSubscriptions = subsProductIdList,
billingListener = object : BillingListener {
override fun onConnectionResult(isSuccess: Boolean, message: String) {
Log.d("BillingTAG", "Billing: initBilling: onConnectionResult: isSuccess = $isSuccess - message = $message")
}
override fun purchasesResult(purchaseDetailList: List<PurchaseDetail>) {
if (purchaseDetailList.isEmpty()) {
// No purchase found, reset all sharedPreferences (premium properties)
}
purchaseDetailList.forEachIndexed { index, purchaseDetail ->
Log.d("BillingTAG", "Billing: initBilling: purchasesResult: $index) $purchaseDetail ")
}
)
}
}
)
```
Access comprehensive details of the currently purchased item using the `PurchaseDetail` class:
Expand Down
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "com.hypersoft.inappbilling"
minSdk 23
targetSdk 34
versionCode 6
versionName "1.1.6"
versionCode = 1
versionName = "1.0.0"
}

buildTypes {
Expand All @@ -35,9 +35,9 @@ android {

dependencies {

implementation 'androidx.core:core-ktx:1.13.0'
implementation 'androidx.core:core-ktx:1.13.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.11.0'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

// Billing Library
Expand Down
36 changes: 21 additions & 15 deletions app/src/main/java/com/hypersoft/inappbilling/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,23 @@ class MainActivity : AppCompatActivity() {
*/

private fun initBilling() {
val productIds = when (BuildConfig.DEBUG) {
true -> listOf(billingManager.getDebugProductIDList())
val productInAppConsumable = when (BuildConfig.DEBUG) {
true -> listOf("test")
false -> listOf("abc", "def")
}
val productInAppNonConsumable = when (BuildConfig.DEBUG) {
true -> listOf(billingManager.getDebugProductIDList())
false -> listOf(packageName)
}

billingManager.initialize(
productInAppPurchases = productIds,
productSubscriptions = listOf("Bronze", "Silver", "Gold", "Yearly"),
productInAppConsumable = productInAppConsumable,
productInAppNonConsumable = productInAppNonConsumable,
productSubscriptions = listOf("Bronze", "Silver", "Gold"),
billingListener = billingListener
)
}

private fun initObserver() {
billingManager.productDetailsLiveData.observe(this) { productDetailList ->
Log.d(TAG, "initNewObserver: --------------------------------------")
productDetailList.forEach { productDetail ->
Log.d(TAG, "---: $productDetail")
}
}
}

private val billingListener = object : BillingListener {
override fun onConnectionResult(isSuccess: Boolean, message: String) {
Log.d(TAG, "onConnectionResult: isSuccess: $isSuccess, message: $message")
Expand All @@ -83,18 +79,28 @@ class MainActivity : AppCompatActivity() {
}

override fun purchasesResult(purchaseDetailList: List<PurchaseDetail>) {
purchaseDetailList[0].productType
Log.d(TAG, "onConnectionResult: purchaseDetailList: $purchaseDetailList")
//purchaseDetailList[0].productType
proceedApp()
}
}

private fun initObserver() {
billingManager.productDetailsLiveData.observe(this) { productDetailList ->
Log.d(TAG, "initNewObserver: --------------------------------------")
productDetailList.forEach { productDetail ->
Log.d(TAG, "---: $productDetail")
}
}
}

private fun proceedApp() {
// your code here...
}

private fun onPurchaseClick() {
// In-App
billingManager.makeInAppPurchase(this, billingManager.getDebugProductIDList(), onPurchaseListener)
billingManager.makeInAppPurchase(this, "test", onPurchaseListener)

// Subscription
//billingManager.makeSubPurchase(this, "product_abc", "plan_abc", onPurchaseListener)
Expand Down
9 changes: 6 additions & 3 deletions billing/src/main/java/com/hypersoft/billing/BillingManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@ import com.hypersoft.billing.interfaces.OnPurchaseListener
class BillingManager(private val context: Context) : BillingController(context) {

/**
* @param productInAppPurchases: Pass list of in-app product's ID
* @param productInAppConsumable: Pass list of in-app product's ID (consumable) or pass emptyList
* @param productInAppNonConsumable: Pass list of in-app product's ID
* @param productSubscriptions: Pass list of subscription product's ID
*/

fun initialize(
productInAppPurchases: List<String>,
productInAppConsumable: List<String>,
productInAppNonConsumable: List<String>,
productSubscriptions: List<String>,
billingListener: BillingListener? = null
) {
startBillingConnection(
userInAppPurchases = productInAppPurchases,
userInAppConsumable = productInAppConsumable,
userInAppNonConsumable = productInAppNonConsumable,
userSubsPurchases = productSubscriptions,
billingListener = billingListener
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ open class BillingController(context: Context) : BillingRepository(context) {
private var job: Job? = null

fun startBillingConnection(
userInAppPurchases: List<String>,
userInAppConsumable: List<String>,
userInAppNonConsumable: List<String>,
userSubsPurchases: List<String>,
billingListener: BillingListener? = null
) {
Expand All @@ -34,13 +35,13 @@ open class BillingController(context: Context) : BillingRepository(context) {
startConnection { isSuccess, message ->
billingListener?.onConnectionResult(isSuccess, message)
if (isSuccess) {
fetchData(userInAppPurchases, userSubsPurchases)
fetchData(userInAppConsumable, userInAppNonConsumable, userSubsPurchases)
}
}
}

private fun fetchData(userInAppPurchases: List<String>, userSubsPurchases: List<String>) {
setUserQueries(userInAppPurchases, userSubsPurchases)
private fun fetchData(userInAppConsumable: List<String>, userInAppNonConsumable: List<String>, userSubsPurchases: List<String>) {
setUserQueries(userInAppConsumable, userInAppNonConsumable, userSubsPurchases)
fetchPurchases()
fetchStoreProducts()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ enum class ResultState(val message: String) {
PURCHASING_ALREADY_OWNED("Already owned this product! No need to purchase"),
PURCHASING_SUCCESSFULLY("Successfully Purchased"),
PURCHASING_FAILURE("Failed to make transaction"),

PURCHASE_CONSUME("Successfully Consumed"),
PURCHASE_FAILURE("Failed to consume product")
}
Loading

0 comments on commit 7c7ac0b

Please sign in to comment.