Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
---------------
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<activity android:name=".items.ItemActivity"/>
<activity android:name=".plan.PlansActivity"/>
<activity android:name=".subscription.SubscriptionActivity"/>
<activity android:name=".plan.UpgradePlanActivity"/>
<activity android:name="com.chargebee.example.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/chargebee/example/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" )
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,6 +26,8 @@ class BillingViewModel : ViewModel() {
var subscriptionList: MutableLiveData<ArrayList<SubscriptionDetailsWrapper>?> = MutableLiveData()
var error: MutableLiveData<String?> = MutableLiveData()
private var subscriptionId: String = ""
var updateProductPurchaseResult: MutableLiveData<String> = MutableLiveData()
var priceChangeLiveData: MutableLiveData<String> = MutableLiveData()

fun purchaseProduct(product: CBProduct, customerID: String) {

Expand Down Expand Up @@ -102,4 +105,80 @@ class BillingViewModel : ViewModel() {
}
}
}

fun updatePurchase(productIdList: ArrayList<String>, oldPurchaseToken: String, context: Context){
CBPurchase.retrieveProducts(context,
productIdList,object : CBCallback.ListProductsCallback<ArrayList<CBProduct>>{
override fun onSuccess(productIDs: ArrayList<CBProduct>) {
if (productIDs.size > 0) {
CBPurchase.updateProduct(context, productIDs.first(),oldPurchaseToken, object : CBCallback.PurchaseCallback<String>{
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<String>, context: Context){
CBPurchase.retrieveProducts(
context,
productIdList,
object : CBCallback.ListProductsCallback<ArrayList<CBProduct>> {
override fun onSuccess(productIDs: ArrayList<CBProduct>) {
if (productIDs.size > 0) {
CBPurchase.priceChangeConfirmation(productIDs.first(), object: CBCallback.PriceChangeCallback<String>{
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}")
}
}
})
}
}
Original file line number Diff line number Diff line change
@@ -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<String>(productId)
this.mBillingViewModel!!.updatePurchase(array, purchaseToken, context)
}

mPriceChangeButton.setOnClickListener{
var productId = mProductIdInput.text.toString()
val productIdList = ArrayList<String>()
productIdList.add(productId)
this.mBillingViewModel!!.priceChangeUpdate(productIdList, context)

}

}
}
4 changes: 3 additions & 1 deletion app/src/main/java/com/chargebee/example/util/CBMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

1 change: 1 addition & 0 deletions app/src/main/java/com/chargebee/example/util/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package com.chargebee.example.util

object Constants {
const val PRODUCTS_LIST_KEY = "products"
const val PRICE_KEY = "priceChange"
}
78 changes: 78 additions & 0 deletions app/src/main/res/layout/activity_upgradeplan.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".plan.UpgradePlanActivity">

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/productInputLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:hint="Product ID"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/productIdInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="15"
android:inputType="textPersonName"
android:text="" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/purchaseTokenInputLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:hint="Purchase Token"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/productInputLayout">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/purchaseTokenInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="15"
android:inputType="textPersonName"
android:text="" />
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="Plan Upgrade/Downgrade"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/buyBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Change Plan"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/purchaseTokenInputLayout" />
<Button
android:id="@+id/btnpricechange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Price Change"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buyBtn" />

</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 1 addition & 1 deletion chargebee/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0.8"
versionName "1.0.9"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
Loading