Skip to content

Commit

Permalink
billing v5
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-ostapovets committed Sep 28, 2022
1 parent 82f06b0 commit 4aea881
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ext {
rxAndroid2Ver = '2.1.1'

// Billing Client
billingClientVer = '4.0.0'
billingClientVer = '5.0.0'

// Developer-related
timberVer = '4.7.1'
Expand Down
2 changes: 1 addition & 1 deletion rxbilling/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ apply plugin: 'org.jetbrains.dokka-android'
android {

defaultConfig {
versionName "4.0.0"
versionName "5.0.0"
minSdkVersion minSdkVer
targetSdkVersion targetSdkVer
compileSdkVersion compileSdkVer
Expand Down
46 changes: 37 additions & 9 deletions rxbilling/src/main/java/com/gen/rxbilling/client/RxBilling.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ interface RxBilling : Connectable<BillingClient> {

fun observeUpdates(): Flowable<PurchasesUpdate>

fun getPurchases(@BillingClient.SkuType skuType: String): Single<List<Purchase>>
fun getPurchases(@BillingClient.ProductType skuType: String): Single<List<Purchase>>

fun getPurchaseHistory(@BillingClient.SkuType skuType: String): Single<List<PurchaseHistoryRecord>>
fun getPurchaseHistory(@BillingClient.ProductType skuType: String): Single<List<PurchaseHistoryRecord>>

fun getSkuDetails(params: SkuDetailsParams): Single<List<SkuDetails>>

/**
* do not mix subs and inapp types in the same params object
*/
fun getProductDetails(params: QueryProductDetailsParams): Single<List<ProductDetails>>

fun launchFlow(activity: Activity, params: BillingFlowParams): Completable

fun consumeProduct(params: ConsumeParams): Completable
Expand All @@ -35,7 +40,7 @@ interface RxBilling : Connectable<BillingClient> {
}

class RxBillingImpl(
billingFactory: BillingClientFactory
billingFactory: BillingClientFactory,
) : RxBilling {

private val updateSubject = PublishSubject.create<PurchasesUpdate>()
Expand Down Expand Up @@ -73,11 +78,11 @@ class RxBillingImpl(
}
}

override fun getPurchases(@BillingClient.SkuType skuType: String): Single<List<Purchase>> {
override fun getPurchases(@BillingClient.ProductType skuType: String): Single<List<Purchase>> {
return getBoughtItems(skuType)
}

override fun getPurchaseHistory(@BillingClient.SkuType skuType: String): Single<List<PurchaseHistoryRecord>> {
override fun getPurchaseHistory(@BillingClient.ProductType skuType: String): Single<List<PurchaseHistoryRecord>> {
return getHistory(skuType)
}

Expand All @@ -98,6 +103,23 @@ class RxBillingImpl(
}.firstOrError()
}

override fun getProductDetails(params: QueryProductDetailsParams): Single<List<ProductDetails>> {
return connectionFlowable
.flatMapSingle { client ->
Single.create<List<ProductDetails>> {
client.queryProductDetailsAsync(params) { billingResult, skuDetailsList ->
if (it.isDisposed) return@queryProductDetailsAsync
val responseCode = billingResult.responseCode
if (isSuccess(responseCode)) {
it.onSuccess(skuDetailsList)
} else {
it.onError(BillingException.fromResult(billingResult))
}
}
}
}.firstOrError()
}

override fun launchFlow(activity: Activity, params: BillingFlowParams): Completable {
return connectionFlowable
.flatMap {
Expand Down Expand Up @@ -152,11 +174,14 @@ class RxBillingImpl(
.ignoreElement()
}

private fun getBoughtItems(@BillingClient.SkuType type: String): Single<List<Purchase>> {
private fun getBoughtItems(@BillingClient.ProductType type: String): Single<List<Purchase>> {
return connectionFlowable
.flatMapSingle {
Single.create<List<Purchase>> { emitter ->
it.queryPurchasesAsync(type) { billingResult, mutableList ->
val params = QueryPurchasesParams.newBuilder()
.setProductType(type)
.build()
it.queryPurchasesAsync(params) { billingResult, mutableList ->
if (emitter.isDisposed) return@queryPurchasesAsync
if (isSuccess(billingResult.responseCode)) {
emitter.onSuccess(mutableList)
Expand All @@ -168,11 +193,14 @@ class RxBillingImpl(
}.firstOrError()
}

private fun getHistory(@BillingClient.SkuType type: String): Single<List<PurchaseHistoryRecord>> {
private fun getHistory(@BillingClient.ProductType type: String): Single<List<PurchaseHistoryRecord>> {
return connectionFlowable
.flatMapSingle { client ->
Single.create<List<PurchaseHistoryRecord>> {
client.queryPurchaseHistoryAsync(type) { billingResult: BillingResult, list: MutableList<PurchaseHistoryRecord>? ->
val params = QueryPurchaseHistoryParams.newBuilder()
.setProductType(type)
.build()
client.queryPurchaseHistoryAsync(params) { billingResult: BillingResult, list: MutableList<PurchaseHistoryRecord>? ->
if (it.isDisposed) return@queryPurchaseHistoryAsync
val responseCode = billingResult.responseCode
if (isSuccess(responseCode)) {
Expand Down

0 comments on commit 4aea881

Please sign in to comment.