diff --git a/README.md b/README.md index fde8c79..eee687e 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ The following requirements must be set up before installing Chargebee’s Androi The `Chargebee-Android` SDK can be installed by adding below dependency to the `build.gradle` file: ```kotlin -implementation 'com.chargebee:chargebee-android:1.0.8' +implementation 'com.chargebee:chargebee-android:1.0.9' ``` ## Example project @@ -347,7 +347,7 @@ Chargebee is available under the [MIT license](https://opensource.org/licenses/M To install Chargebee's Android SDK, add the following dependency to the build.gradle file. ``` - implementation 'com.chargebee:chargebee-android:1.0.8' + implementation 'com.chargebee:chargebee-android:1.0.9' ``` Example project --------------- diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 89f51ce..36a7f0b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -22,6 +22,7 @@ + diff --git a/app/src/main/java/com/chargebee/example/MainActivity.kt b/app/src/main/java/com/chargebee/example/MainActivity.kt index 87700ba..ba2a8f8 100644 --- a/app/src/main/java/com/chargebee/example/MainActivity.kt +++ b/app/src/main/java/com/chargebee/example/MainActivity.kt @@ -27,6 +27,8 @@ import com.chargebee.example.items.ItemActivity import com.chargebee.example.items.ItemsActivity import com.chargebee.example.plan.PlanInJavaActivity import com.chargebee.example.plan.PlansActivity +import com.chargebee.example.plan.UpgradePlanActivity +import com.chargebee.example.util.Constants.PRICE_KEY import com.chargebee.example.subscription.SubscriptionActivity import com.chargebee.example.token.TokenizeActivity import com.chargebee.example.util.CBMenu @@ -124,6 +126,15 @@ class MainActivity : BaseActivity(), ListItemsAdapter.ItemClickListener { val intent = Intent(this, SubscriptionActivity::class.java) startActivity(intent) } + CBMenu.UpgradePlan.value -> { + val intent = Intent(this, UpgradePlanActivity::class.java) + startActivity(intent) + } + CBMenu.PriceChange.value -> { + val intent = Intent(this, UpgradePlanActivity::class.java) + intent.putExtra(PRICE_KEY, "priceChange") + startActivity(intent) + } else ->{ Log.i(javaClass.simpleName, " Not implemented" ) } diff --git a/app/src/main/java/com/chargebee/example/billing/BillingViewModel.kt b/app/src/main/java/com/chargebee/example/billing/BillingViewModel.kt index f175266..f48a647 100644 --- a/app/src/main/java/com/chargebee/example/billing/BillingViewModel.kt +++ b/app/src/main/java/com/chargebee/example/billing/BillingViewModel.kt @@ -1,5 +1,6 @@ package com.chargebee.example.billing +import android.content.Context import android.util.Log import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel @@ -25,6 +26,8 @@ class BillingViewModel : ViewModel() { var subscriptionList: MutableLiveData?> = MutableLiveData() var error: MutableLiveData = MutableLiveData() private var subscriptionId: String = "" + var updateProductPurchaseResult: MutableLiveData = MutableLiveData() + var priceChangeLiveData: MutableLiveData = MutableLiveData() fun purchaseProduct(product: CBProduct, customerID: String) { @@ -102,4 +105,80 @@ class BillingViewModel : ViewModel() { } } } + + fun updatePurchase(productIdList: ArrayList, oldPurchaseToken: String, context: Context){ + CBPurchase.retrieveProducts(context, + productIdList,object : CBCallback.ListProductsCallback>{ + override fun onSuccess(productIDs: ArrayList) { + if (productIDs.size > 0) { + CBPurchase.updateProduct(context, productIDs.first(),oldPurchaseToken, object : CBCallback.PurchaseCallback{ + override fun onSuccess(purchaseToken: String, status:Boolean) { + Log.i(TAG, "purchaseToken: $purchaseToken") + updateProductPurchaseResult.postValue(purchaseToken) + } + override fun onError(error: CBException) { + try { + cbException.postValue(error.message) + }catch (exp: Exception){ + Log.i(TAG, "Exception :${exp.message}") + } + } + + }) + } else { + Log.i(javaClass.simpleName,"Product id not found in Google Play") + cbException.postValue("Product id not found in Google Play") + } + } + + override fun onError(error: CBException) { + Log.e(javaClass.simpleName, "Error: ${error.message}") + try { + cbException.postValue(error.message) + }catch (exp: Exception){ + Log.e(TAG, "Exception :${exp.message}") + } + } + + }) + + } + + fun priceChangeUpdate(productIdList: ArrayList, context: Context){ + CBPurchase.retrieveProducts( + context, + productIdList, + object : CBCallback.ListProductsCallback> { + override fun onSuccess(productIDs: ArrayList) { + if (productIDs.size > 0) { + CBPurchase.priceChangeConfirmation(productIDs.first(), object: CBCallback.PriceChangeCallback{ + override fun onSuccess(response: String) { + priceChangeLiveData.postValue(response) + } + + override fun onError(error: CBException) { + Log.e(javaClass.simpleName,"Error in Price Change: ${error.message}") + try { + cbException.postValue(error.message) + }catch (exp: Exception){ + Log.i(TAG, "Exception :${exp.message}") + } + } + + }) + } else { + Log.i(javaClass.simpleName,"Product id not found in Google Play") + cbException.postValue("Product id not found in Google Play") + } + } + override fun onError(error: CBException) { + Log.e(javaClass.simpleName, "Error: ${error.message}") + try { + cbException.postValue(error.message) + }catch (exp: Exception){ + Log.e(TAG, "Exception :${exp.message}") + } + } + }) + } } \ No newline at end of file diff --git a/app/src/main/java/com/chargebee/example/plan/UpgradePlanActivity.kt b/app/src/main/java/com/chargebee/example/plan/UpgradePlanActivity.kt new file mode 100644 index 0000000..165c521 --- /dev/null +++ b/app/src/main/java/com/chargebee/example/plan/UpgradePlanActivity.kt @@ -0,0 +1,88 @@ +package com.chargebee.example.plan + +import android.content.Context +import android.os.Bundle +import android.text.TextUtils +import android.util.Log +import android.view.View +import android.widget.Button +import android.widget.TextView +import androidx.lifecycle.Observer +import com.chargebee.example.BaseActivity +import com.chargebee.example.R +import com.chargebee.example.billing.BillingActivity +import com.chargebee.example.billing.BillingViewModel +import com.chargebee.example.util.Constants +import com.google.android.material.textfield.TextInputEditText +import com.google.android.material.textfield.TextInputLayout + +class UpgradePlanActivity : BaseActivity() { + + private var mBillingViewModel : BillingViewModel? = null + private lateinit var mProductIdInput: TextInputEditText + private lateinit var mPurchaseTokenInput: TextInputEditText + private lateinit var mBuyProductButton: Button + private lateinit var mPriceChangeButton: Button + private lateinit var mPurchaseTokenInputLayout: TextInputLayout + private lateinit var mTvPriceChange: TextView + lateinit var context: Context + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_upgradeplan) + context = this + mBillingViewModel = BillingViewModel() + mProductIdInput = findViewById(R.id.productIdInput) + mPurchaseTokenInput = findViewById(R.id.purchaseTokenInput) + mBuyProductButton = findViewById(R.id.buyBtn) + mPriceChangeButton = findViewById(R.id.btnpricechange) + mPurchaseTokenInputLayout = findViewById(R.id.purchaseTokenInputLayout) + mTvPriceChange = findViewById(R.id.textView) + + val priceChange = intent.getStringExtra(Constants.PRICE_KEY) + if(priceChange !=null){ + mPurchaseTokenInputLayout.visibility = View.GONE + mBuyProductButton.visibility = View.GONE + mTvPriceChange.text = "Plan Price Change" + + }else{ + mPurchaseTokenInputLayout.visibility = View.VISIBLE + mPriceChangeButton.visibility = View.GONE + mTvPriceChange.text = "Plan Upgrade/Downgrade" + + } + + this.mBillingViewModel!!.updateProductPurchaseResult.observeForever{ + hideProgressDialog() + if (!TextUtils.isEmpty(it)) + alertSuccess(it) + } + + this.mBillingViewModel!!.priceChangeLiveData.observeForever{ + hideProgressDialog() + if (!TextUtils.isEmpty(it)) + alertSuccess(it) + } + this.mBillingViewModel!!.cbException.observeForever{ + if (!TextUtils.isEmpty(it)) + alertSuccess(it) + } + + mBuyProductButton.setOnClickListener{ + var productId = mProductIdInput.text.toString() + val purchaseToken = mPurchaseTokenInput.text.toString() + + val array = arrayListOf(productId) + this.mBillingViewModel!!.updatePurchase(array, purchaseToken, context) + } + + mPriceChangeButton.setOnClickListener{ + var productId = mProductIdInput.text.toString() + val productIdList = ArrayList() + productIdList.add(productId) + this.mBillingViewModel!!.priceChangeUpdate(productIdList, context) + + } + + } +} \ No newline at end of file diff --git a/app/src/main/java/com/chargebee/example/util/CBMenu.kt b/app/src/main/java/com/chargebee/example/util/CBMenu.kt index 630d5f5..cc3ed1e 100644 --- a/app/src/main/java/com/chargebee/example/util/CBMenu.kt +++ b/app/src/main/java/com/chargebee/example/util/CBMenu.kt @@ -11,6 +11,8 @@ enum class CBMenu(val value: String) { ProductIDs("Get Google Play Product Identifiers"), GetProducts("Get Products"), SubsStatus("Get Subscription Status"), - SubsList("Get Subscriptions List") + SubsList("Get Subscriptions List"), + UpgradePlan("Upgrade/Downgrade Plan"), + PriceChange("Price Change") } diff --git a/app/src/main/java/com/chargebee/example/util/Constants.kt b/app/src/main/java/com/chargebee/example/util/Constants.kt index 700be5d..01f4dbe 100644 --- a/app/src/main/java/com/chargebee/example/util/Constants.kt +++ b/app/src/main/java/com/chargebee/example/util/Constants.kt @@ -2,4 +2,5 @@ package com.chargebee.example.util object Constants { const val PRODUCTS_LIST_KEY = "products" + const val PRICE_KEY = "priceChange" } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_upgradeplan.xml b/app/src/main/res/layout/activity_upgradeplan.xml new file mode 100644 index 0000000..ecf3901 --- /dev/null +++ b/app/src/main/res/layout/activity_upgradeplan.xml @@ -0,0 +1,78 @@ + + + + + + + + + + + +