Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -package com.abuk.abook.data
- -
- -import android.app.Activity
- -import com.abuk.abook.data.rx.BuyPurchaseOnSubscriber
- -import com.abuk.abook.extension.applySchedulers
- -import com.abuk.abook.extension.isInternetAvailable
- -import com.android.billingclient.api.*
- -import io.reactivex.Completable
- -import io.reactivex.Observable
- -import io.reactivex.Single
- -import io.reactivex.subjects.PublishSubject
- -import java.net.UnknownHostException
- -
- -
- -@Deprecated("d")
- -class RxBillings private constructor(val activity: Activity, private val sku: String) : PurchasesUpdatedListener {
- -
- -
- - private var publishSubject: PublishSubject<Purchase>? = null
- - private val billingClient: BillingClient = BillingClient.newBuilder(activity.applicationContext).setListener(this).build()
- -
- -
- - override fun onPurchasesUpdated(billingResult:BillingResult, purchases: MutableList<Purchase?>?) {
- - if (publishSubject == null) return
- - if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && purchases != null && purchases.isNotEmpty() && purchases[0] != null) {
- - publishSubject?.onNext(purchases[0]!!)
- - publishSubject?.onComplete()
- - } else {
- - publishSubject?.let {
- - if (!it.hasComplete() && !it.hasThrowable()) {
- - it.onError(RxBillingException(billingResult.responseCode))
- - }
- - }
- - }
- - publishSubject = null
- - }
- -
- - companion object {
- - fun buyBook(activity: Activity, bookSku: String, bookId: Int): Completable {
- - return Observable.create(BuyPurchaseOnSubscriber(activity,bookSku,null!!)).ignoreElements()
- -// return buy(activity, bookSku).flatMap { pur ->
- -// val ver = VerifyPurchase(bookId, pur.signature, pur.sku, pur.purchaseToken)
- -// return@flatMap Injector.appComponent.authApi()
- -// .verifyPurchase(ver)
- -// .singleOrError()
- -// .applySchedulers()
- -// .doOnSubscribe {
- -// (activity as? BaseView)?.showProgress(message = "Виконується запит")
- -// }
- -// .doFinally {
- -// (activity as? BaseView)?.hideProgress()
- -// }
- -// .onErrorResumeNext {
- -// val err = Error.parseError(it)
- -// return@onErrorResumeNext if (!err.hasError(Error.PURCHASE_ALREADY_CONFIRMED)) {
- -// Single.error(it)
- -// } else {
- -// val billings = RxBillings(activity, bookSku)
- -// billings.connect()
- -// .flatMap { it.launchBillingFlowWithConsume() }
- -// .doFinally {
- -// billings.billingClient.endConnection()
- -// }
- -// .flatMap { verPur ->
- -// val verRe = VerifyPurchase(bookId, verPur.signature, verPur.sku, verPur.purchaseToken)
- -// Injector.appComponent.authApi().verifyPurchase(verRe)
- -// .singleOrError()
- -// .applySchedulers()
- -// .doOnSubscribe {
- -// (activity as? BaseView)?.showProgress(message = "Виконується запит")
- -// }
- -// .doFinally {
- -// (activity as? BaseView)?.hideProgress()
- -// }
- -// }
- -// }
- -// }
- -// }.toCompletable()
- - }
- -
- - private fun buy(activity: Activity, sku: String): Single<Purchase> {
- - val billings = RxBillings(activity, sku)
- - return billings.connect()
- - .flatMap { rxBillings ->
- - rxBillings.getPurchase()
- - .onErrorResumeNext {
- - if (it is RxBillingNoPurchaseException) {
- - rxBillings.launchBillingFlow()
- - } else {
- - Single.error(it)
- - }
- - }
- - }.doFinally {
- - billings.billingClient.endConnection()
- - }
- - }
- - }
- -
- - fun connect(): Single<RxBillings> {
- - return Single.create<RxBillings> {
- - if (!activity.isInternetAvailable() && !it.isDisposed) {
- - it.onError(UnknownHostException())
- - return@create
- - }
- -// billingClient.startConnection(object : BillingClientStateListener {
- -// override fun onBillingSetupFinished(@BillingClient.BillingResponse billingResponseCode: Int) {
- -// if (it.isDisposed) return
- -// if (billingResponseCode == BillingClient.BillingResponse.OK) {
- -// it.onSuccess(this@RxBillings)
- -// } else {
- -// it.onError(RxBillingException(billingResponseCode))
- -// }
- -// }
- -//
- -// override fun onBillingServiceDisconnected() {
- -// }
- -// })
- - }.applySchedulers()
- - }
- -
- - fun launchBillingFlowWithConsume(): Single<Purchase> {
- - return getPurchase().flatMap { purchaseToConsume ->
- - return@flatMap Single.create<Purchase> {
- - if (it.isDisposed) return@create
- - val cp = ConsumeParams.newBuilder().setPurchaseToken(purchaseToConsume.purchaseToken).build()
- - billingClient.consumeAsync(cp) { responseCode, purchaseToken ->
- - if (it.isDisposed) return@consumeAsync
- - if (responseCode.responseCode == 0 || responseCode.responseCode == 8) {
- - it.onError(RxBillingNoPurchaseException())
- - } else {
- - it.onError(RxBillingException(responseCode.responseCode))
- - }
- -
- - }
- - }.applySchedulers()
- - }.onErrorResumeNext {
- - return@onErrorResumeNext if (it is RxBillingNoPurchaseException) {
- - launchBillingFlow()
- - } else {
- - Single.error(it)
- - }
- - }
- - }
- -
- - fun getPurchase(): Single<Purchase> {
- - return Single.create<Purchase> { singleEmitter ->
- - val response = billingClient.queryPurchases(BillingClient.SkuType.INAPP)
- -
- Add a comment to this line
- - billingClient.isReady
- - if (singleEmitter.isDisposed) return@create
- - if (response.responseCode == BillingClient.BillingResponseCode.OK) {
- - val purchase = response.purchasesList.firstOrNull { it.sku == sku }
- - if (purchase == null) {
- - billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.INAPP) { responseCode, purchasesList ->
- - if (singleEmitter.isDisposed) return@queryPurchaseHistoryAsync
- - if (responseCode.responseCode == BillingClient.BillingResponseCode.OK) {
- - val historyPurchase = purchasesList?.firstOrNull { it.sku == sku }
- - if (historyPurchase != null) {
- -// singleEmitter.onSuccess(historyPurchase.)
- - singleEmitter.onError(RxBillingNoPurchaseException())
- - } else {
- - singleEmitter.onError(RxBillingNoPurchaseException())
- - }
- - } else {
- - singleEmitter.onError(RxBillingException(responseCode.responseCode))
- Add a comment to this line
- - }
- - }
- - } else {
- - singleEmitter.onSuccess(purchase)
- - }
- - } else {
- - singleEmitter.onError(RxBillingException(response.responseCode))
- - }
- - }.applySchedulers()
- - }
- -
- - fun launchBillingFlow(): Single<Purchase> {
- -
- - val flowParams = BillingFlowParams.newBuilder()
- - .setOldSku(sku)
- -// .setType(BillingClient.SkuType.INAPP)
- - .build()
- - billingClient.launchBillingFlow(activity, flowParams)
- - publishSubject = PublishSubject.create()
- - return publishSubject!!.singleOrError().applySchedulers()
- - }
- -
- - class RxBillingException(var code: Int) : Exception()
- - class RxBillingNoPurchaseException : Exception()
- -
- -
- -}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement