Skip to content

Commit

Permalink
Merge pull request #83 from cb-haripriyan/fix/validate-receipt
Browse files Browse the repository at this point in the history
fix: fixes error when trying to valiate a non-purchased item
  • Loading branch information
cb-amutha committed Sep 1, 2023
2 parents 2610b5d + 8e18a5e commit 382ca73
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
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.21'
implementation 'com.chargebee:chargebee-android:1.0.22'
```

## Example project
Expand Down Expand Up @@ -474,7 +474,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.21'
implementation 'com.chargebee:chargebee-android:1.0.22'
```
Example project
---------------
Expand Down
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.21"
versionName "1.0.22"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,13 @@ class BillingClientManager(context: Context) : PurchasesUpdatedListener {
)
)

private fun itemNotOwnedException(): CBException {
return CBException(ErrorDetail(
message = BillingErrorCode.ITEM_NOT_OWNED.message,
httpStatusCode = BillingErrorCode.ITEM_NOT_OWNED.code
))
}

private fun queryPurchaseHistoryFromStore(
connectionStatus: Boolean
) {
Expand Down Expand Up @@ -517,15 +524,24 @@ class BillingClientManager(context: Context) : PurchasesUpdatedListener {
}
}

internal fun validateReceiptWithChargebee(product: CBProduct, completionCallback: CBCallback.PurchaseCallback<String> ) {
internal fun validateReceiptWithChargebee(
product: CBProduct,
completionCallback: CBCallback.PurchaseCallback<String>
) {
this.purchaseCallBack = completionCallback
onConnected({ status ->
if (status)
queryPurchaseHistory { purchaseHistoryList ->
val purchaseTransaction = purchaseHistoryList.filter {
it.productId.first() == product.productId
}
validateReceipt(purchaseTransaction.first().purchaseToken, product)
val transaction = purchaseTransaction.firstOrNull()
transaction?.let {
validateReceipt(transaction.purchaseToken, product)
} ?: run {
completionCallback.onError(itemNotOwnedException())
}

} else
completionCallback.onError(
connectionError
Expand Down Expand Up @@ -582,14 +598,24 @@ class BillingClientManager(context: Context) : PurchasesUpdatedListener {
}
}

internal fun validateNonSubscriptionReceiptWithChargebee(product: CBProduct, completionCallback: CBCallback.OneTimePurchaseCallback) {
internal fun validateNonSubscriptionReceiptWithChargebee(
product: CBProduct,
completionCallback: CBCallback.OneTimePurchaseCallback
) {
this.oneTimePurchaseCallback = completionCallback
onConnected({ status ->
if (status)
queryPurchaseHistory { purchaseHistoryList ->
val purchaseTransaction = purchaseHistoryList.filter {
it.productId.first() == product.productId
}
validateNonSubscriptionReceipt(purchaseTransaction.first().purchaseToken, product)
val transaction = purchaseTransaction.firstOrNull()
transaction?.let {
validateNonSubscriptionReceipt(transaction.purchaseToken, product)
} ?: run {
completionCallback.onError(itemNotOwnedException())
}

} else
completionCallback.onError(
connectionError
Expand Down

0 comments on commit 382ca73

Please sign in to comment.