-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from cb-amutha/feature/otp_purchase_support
One Time Purchase Support
- Loading branch information
Showing
20 changed files
with
827 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,11 @@ import android.content.SharedPreferences | |
import android.util.Log | ||
import com.chargebee.android.billingservice.CBCallback | ||
import com.chargebee.android.billingservice.CBPurchase | ||
import com.chargebee.android.billingservice.OneTimeProductType | ||
import com.chargebee.android.billingservice.ProductType | ||
import com.chargebee.android.exceptions.CBException | ||
import com.chargebee.android.models.CBProduct | ||
import com.chargebee.android.models.NonSubscription | ||
import com.chargebee.android.network.CBCustomer | ||
import com.chargebee.android.network.ReceiptDetail | ||
import com.chargebee.example.util.NetworkUtil | ||
|
@@ -17,6 +20,12 @@ class ExampleApplication : Application(), NetworkUtil.NetworkListener { | |
private lateinit var networkUtil: NetworkUtil | ||
private lateinit var sharedPreference: SharedPreferences | ||
lateinit var mContext: Context | ||
private val customer = CBCustomer( | ||
id = "sync_receipt_android", | ||
firstName = "Test", | ||
lastName = "Purchase", | ||
email = "[email protected]" | ||
) | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
|
@@ -46,7 +55,10 @@ class ExampleApplication : Application(), NetworkUtil.NetworkListener { | |
productIdList, | ||
object : CBCallback.ListProductsCallback<ArrayList<CBProduct>> { | ||
override fun onSuccess(productIDs: ArrayList<CBProduct>) { | ||
validateReceipt(mContext, productIDs.first()) | ||
if (productIDs.first().productType == ProductType.SUBS) | ||
validateReceipt(mContext, productIDs.first()) | ||
else | ||
validateNonSubscriptionReceipt(mContext, productIDs.first()) | ||
} | ||
|
||
override fun onError(error: CBException) { | ||
|
@@ -56,12 +68,7 @@ class ExampleApplication : Application(), NetworkUtil.NetworkListener { | |
} | ||
|
||
private fun validateReceipt(context: Context, product: CBProduct) { | ||
val customer = CBCustomer( | ||
id = "sync_receipt_android", | ||
firstName = "Test", | ||
lastName = "Purchase", | ||
email = "[email protected]" | ||
) | ||
|
||
CBPurchase.validateReceipt( | ||
context = context, | ||
product = product, | ||
|
@@ -82,4 +89,27 @@ class ExampleApplication : Application(), NetworkUtil.NetworkListener { | |
} | ||
}) | ||
} | ||
|
||
private fun validateNonSubscriptionReceipt(context: Context, product: CBProduct) { | ||
CBPurchase.validateReceiptForNonSubscriptions( | ||
context = context, | ||
product = product, | ||
customer = customer, | ||
productType = OneTimeProductType.CONSUMABLE, | ||
completionCallback = object : CBCallback.OneTimePurchaseCallback { | ||
override fun onSuccess(result: NonSubscription, status: Boolean) { | ||
// Clear the local cache once receipt validation success | ||
val editor = sharedPreference.edit() | ||
editor.clear().apply() | ||
Log.i(javaClass.simpleName, "Subscription ID: ${result.invoiceId}") | ||
Log.i(javaClass.simpleName, "Plan ID: ${result.chargeId}") | ||
Log.i(javaClass.simpleName, "Customer ID: ${result.customerId}") | ||
Log.i(javaClass.simpleName, "Status: $status") | ||
} | ||
|
||
override fun onError(error: CBException) { | ||
Log.e(javaClass.simpleName, "Exception :$error") | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.