Skip to content

Commit

Permalink
Merge pull request #35 from epegasus/master
Browse files Browse the repository at this point in the history
Removed old methods
  • Loading branch information
hypersoftdev committed Mar 1, 2024
2 parents 7ac9bf8 + 5a8d214 commit 3f4b8c2
Show file tree
Hide file tree
Showing 32 changed files with 193 additions and 1,431 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ private val billingManager by lazy { BillingManager(context) }

### Step # 2 (Billing Connection)

Retrieve a debugging ID for testing purposes by utilizing the `debugProductId` method. Ensure that the parameter `purchaseDetailList` represents a list containing all active purchases along with their respective details.
Retrieve a debugging ID for testing purposes by utilizing the `getDebugProductIDList()` method. Ensure that the parameter `purchaseDetailList` represents a list containing all active purchases along with their respective details.

```
val subsProductIdList = listOf("subs_product_id_1", "subs_product_id_2", "subs_product_id_3")
val inAppProductIdList = when (BuildConfig.DEBUG) {
true -> listOf(billingManager.debugProductId)
true -> listOf(billingManager.getDebugProductIDList())
false -> listOf("inapp_product_id_1", "inapp_product_id_2")
}
Expand Down Expand Up @@ -110,7 +110,7 @@ This observer monitors all active in-app and subscription products.
val subsProductIdList = listOf("subs_product_id_1", "subs_product_id_2", "subs_product_id_3")
val subsPlanIdList = listOf("subs_plan_id_1", "subs_plan_id_2", "subs_plan_id_3")
billingManager.observeQueryProducts().observe(viewLifecycleOwner) { productDetailList ->
billingManager.productDetailsLiveData.observe(viewLifecycleOwner) { productDetailList ->
Log.d(TAG, "Billing: initObservers: $productDetailList")
productDetailList.forEach { productDetail ->
Expand Down
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ android {
kotlinOptions {
jvmTarget = '17'
}
buildFeatures{
buildConfig true
}
}

dependencies {
Expand Down
143 changes: 15 additions & 128 deletions app/src/main/java/com/hypersoft/inappbilling/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,27 @@ package com.hypersoft.inappbilling
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.hypersoft.billing.BillingManager
import com.hypersoft.billing.common.interfaces.OnPurchaseListener
import com.hypersoft.billing.latest.dataClasses.PurchaseDetail
import com.hypersoft.billing.latest.interfaces.BillingListener
import com.hypersoft.billing.oldest.constants.SubscriptionPlans
import com.hypersoft.billing.oldest.constants.SubscriptionProductIds
import com.hypersoft.billing.oldest.dataClasses.ProductDetail
import com.hypersoft.billing.oldest.interfaces.OnConnectionListener
import com.hypersoft.billing.oldest.status.State
import com.hypersoft.billing.dataClasses.PurchaseDetail
import com.hypersoft.billing.interfaces.BillingListener
import com.hypersoft.billing.interfaces.OnPurchaseListener

const val TAG = "MyTag"

class MainActivity : AppCompatActivity() {

private val billingManager by lazy { BillingManager(this) }

// Old way
private lateinit var tvTitle: TextView
private val productId: String = "Paste your original Product ID"

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

newWay()
oldWay()
initBilling()
initObserver()

findViewById<Button>(R.id.btn_purchase).setOnClickListener { onPurchaseClick() }
}

/* -------------------------------------------------- New Way -------------------------------------------------- */
Expand Down Expand Up @@ -60,9 +52,9 @@ class MainActivity : AppCompatActivity() {
* -- Yearly
*/

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

Expand All @@ -71,11 +63,10 @@ class MainActivity : AppCompatActivity() {
productSubscriptions = listOf("Bronze", "Silver", "Gold", "Yearly"),
billingListener = billingListener
)
initNewObserver()
}

private fun initNewObserver() {
billingManager.observeQueryProducts().observe(this) { productDetailList ->
private fun initObserver() {
billingManager.productDetailsLiveData.observe(this) { productDetailList ->
Log.d(TAG, "initNewObserver: --------------------------------------")
productDetailList.forEach { productDetail ->
Log.d(TAG, "---: $productDetail")
Expand All @@ -100,15 +91,15 @@ class MainActivity : AppCompatActivity() {
// your code here...
}

private fun onPurchaseNewClick() {
private fun onPurchaseClick() {
// In-App
billingManager.makeInAppPurchase(this, billingManager.debugProductId, newPurchaseListener)
billingManager.makeInAppPurchase(this, billingManager.getDebugProductIDList(), onPurchaseListener)

// Subscription
billingManager.makeSubPurchase(this, "product_abc", "plan_abc", newPurchaseListener)
//billingManager.makeSubPurchase(this, "product_abc", "plan_abc", onPurchaseListener)
}

private val newPurchaseListener = object : OnPurchaseListener {
private val onPurchaseListener = object : OnPurchaseListener {
override fun onPurchaseResult(isPurchaseSuccess: Boolean, message: String) {
showMessage(message)
}
Expand All @@ -119,110 +110,6 @@ class MainActivity : AppCompatActivity() {
billingManager.destroyBilling()
}

/* -------------------------------------------------- Old Way -------------------------------------------------- */

private fun oldWay() {
initBilling()
initOldObserver()

tvTitle = findViewById(R.id.tv_title)
findViewById<Button>(R.id.btn_purchase).setOnClickListener {
onPurchaseClick()
}
}

private fun initOldObserver() {
State.billingState.observe(this) {
Log.d("BillingManager", "initObserver: $it")
tvTitle.text = it.toString()
}

//Observe Your Products
billingManager.productDetailsLiveData.observe(this) { list ->
var month = 0L
var year = 0L
list.forEach { productDetail: ProductDetail ->
Log.d(TAG, "initObservers: $productDetail")
when (productDetail.productId) {
SubscriptionProductIds.basicProductMonthly -> {
//binding.mtvOfferPrice1.text = productDetail.price
month = productDetail.priceAmountMicros / 1000000
}

SubscriptionProductIds.basicProductYearly -> {
//binding.mtvOfferPrice2.text = productDetail.price
year = productDetail.priceAmountMicros / 1000000
}

SubscriptionProductIds.basicProductSemiYearly -> {
//binding.mtvOfferPrice3Premium.text = productDetail.price
}

productId -> {
//binding.mtvOfferPrice3Premium.text = productDetail.price
}
}
}
// Best Offer
if (month == 0L || year == 0L) return@observe
val result = 100 - (year * 100 / (12 * month))
val text = "Save $result%"
//binding.mtvBestOffer.text = text

val perMonth = (year / 12L).toString()
//binding.mtvOffer.text = perMonth
}
}

private fun initBilling() {
billingManager.setCheckForSubscription(true)
val productIds = when (BuildConfig.DEBUG) {
true -> billingManager.getDebugProductIDList()
false -> listOf(productId)
}

if (BuildConfig.DEBUG) {
billingManager.startConnection(productIds, object : OnConnectionListener {
override fun onConnectionResult(isSuccess: Boolean, message: String) {
showMessage(message)
Log.d("TAG", "onConnectionResult: $isSuccess - $message")
}

override fun onOldPurchaseResult(isPurchased: Boolean, purchaseDetail: com.hypersoft.billing.oldest.dataClasses.PurchaseDetail?) {
// Update your shared-preferences here!
Log.d("TAG", "onOldPurchaseResult: $isPurchased")
}
})
} else {
billingManager.startConnection(productIds, object : OnConnectionListener {
override fun onConnectionResult(isSuccess: Boolean, message: String) {
showMessage(message)
Log.d("TAG", "onConnectionResult: $isSuccess - $message")
}

override fun onOldPurchaseResult(isPurchased: Boolean, purchaseDetail: com.hypersoft.billing.oldest.dataClasses.PurchaseDetail?) {
// Update your shared-preferences here!
Log.d("TAG", "onOldPurchaseResult: $isPurchased")
}
})
}
}


private fun onPurchaseClick() {
// In-App
billingManager.makeInAppPurchase(this, oldPurchaseListener)

// Subscription
billingManager.makeSubPurchase(this, SubscriptionPlans.basicPlanMonthly, oldPurchaseListener)
}

private val oldPurchaseListener = object : OnPurchaseListener {
override fun onPurchaseResult(isPurchaseSuccess: Boolean, message: String) {
showMessage(message)
}
}

private fun showMessage(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
Expand Down
11 changes: 5 additions & 6 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:padding="10dp"
android:layout_marginHorizontal="10dp"
android:padding="10dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -22,12 +22,11 @@
android:id="@+id/btn_purchase"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Purchase"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:text="Purchase"
app:layout_constraintBottom_toBottomOf="parent"
/>
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Empty file removed billing/consumer-rules.pro
Empty file.
71 changes: 11 additions & 60 deletions billing/src/main/java/com/hypersoft/billing/BillingManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@ package com.hypersoft.billing

import android.app.Activity
import android.content.Context
import androidx.lifecycle.LiveData
import com.hypersoft.billing.latest.BillingController
import com.hypersoft.billing.latest.interfaces.BillingListener
import com.hypersoft.billing.oldest.helper.BillingHelper
import com.hypersoft.billing.oldest.interfaces.OnConnectionListener
import com.hypersoft.billing.common.interfaces.OnPurchaseListener
import com.hypersoft.billing.latest.dataClasses.ProductDetail
import com.hypersoft.billing.controller.BillingController
import com.hypersoft.billing.interfaces.BillingListener
import com.hypersoft.billing.interfaces.OnPurchaseListener

/**
* @param context: Context can be of Application class
*/

class BillingManager(private val context: Context) : BillingHelper(context) {

private val billingController by lazy { BillingController(context) }

val debugProductId = billingController.debugProductId
class BillingManager(private val context: Context) : BillingController(context) {

/**
* @param productInAppPurchases: Pass list of in-app product's ID
Expand All @@ -30,69 +22,28 @@ class BillingManager(private val context: Context) : BillingHelper(context) {
productSubscriptions: List<String>,
billingListener: BillingListener? = null
) {
billingController.startBillingConnection(
startBillingConnection(
userInAppPurchases = productInAppPurchases,
userSubsPurchases = productSubscriptions,
billingListener = billingListener
)
}

fun observeQueryProducts(): LiveData<List<ProductDetail>> {
return billingController.productsObserver
}

fun makeInAppPurchase(activity: Activity?, productId: String, onPurchaseListener: OnPurchaseListener) {
billingController.makePurchaseInApp(activity = activity, productId = productId, onPurchaseListener = onPurchaseListener)
makePurchaseInApp(activity = activity, productId = productId, onPurchaseListener = onPurchaseListener)
}

fun makeSubPurchase(activity: Activity?, productId: String, planId: String, onPurchaseListener: OnPurchaseListener) {
billingController.makePurchaseSub(activity = activity, productId = productId, planId = planId, onPurchaseListener = onPurchaseListener)
makePurchaseSub(activity = activity, productId = productId, planId = planId, onPurchaseListener = onPurchaseListener)
}

fun updateSubPurchase(activity: Activity?, oldProductId: String, oldPlanId: String, productId: String, planId: String, onPurchaseListener: OnPurchaseListener) {
billingController.updatePurchaseSub(activity = activity, oldProductId = oldProductId, oldPlanId = oldPlanId, productId = productId, planId = planId, onPurchaseListener = onPurchaseListener)
updatePurchaseSub(activity = activity, oldProductId = oldProductId, oldPlanId = oldPlanId, productId = productId, planId = planId, onPurchaseListener = onPurchaseListener)
}

fun destroyBilling() = billingController.cleanBilling()
fun destroyBilling() = cleanBilling()

@Deprecated(
"Skip this method with new approach",
ReplaceWith("null"),
DeprecationLevel.WARNING
)
override fun setCheckForSubscription(isCheckRequired: Boolean) {
checkForSubscription = isCheckRequired
companion object {
const val TAG = "BillingManager"
}


/**
* Deprecated. Use [initialize] with individual productId parameters instead.
*/
@Deprecated(
"Use initialize with individual productId and plan ids parameters instead.",
ReplaceWith("initialize(productInAppPurchases, productSubscriptions, billingListener)"),
DeprecationLevel.WARNING
)
override fun startConnection(productIdsList: List<String>, onConnectionListener: OnConnectionListener) = startBillingConnection(productIdsList, onConnectionListener)


/**
* Deprecated. Use [makeInAppPurchase] with individual productId parameters instead.
*/
@Deprecated(
"Use makeInAppPurchase with individual productId parameters instead.",
ReplaceWith("makeInAppPurchase(activity, productId, onPurchaseListener)"),
DeprecationLevel.WARNING
)
fun makeInAppPurchase(activity: Activity?, onPurchaseListener: OnPurchaseListener) = purchaseInApp(activity, onPurchaseListener)

/**
* Deprecated. Use [makeSubPurchase] with individual productId and planId parameters instead.
*/
@Deprecated(
"Use makeSubPurchase with individual productId and planId parameters instead.",
ReplaceWith("makeSubPurchase(activity, productId, planId, onPurchaseListener)"),
DeprecationLevel.WARNING
)
fun makeSubPurchase(activity: Activity?, subscriptionPlans: String, onPurchaseListener: OnPurchaseListener) = purchaseSub(activity, subscriptionPlans, onPurchaseListener)
}
Loading

0 comments on commit 3f4b8c2

Please sign in to comment.