Advertisement
andyshon

RxBilling

Jun 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 9.25 KB | None | 0 0
  1. -package com.abuk.abook.data
  2. -
  3. -import android.app.Activity
  4. -import com.abuk.abook.data.rx.BuyPurchaseOnSubscriber
  5. -import com.abuk.abook.extension.applySchedulers
  6. -import com.abuk.abook.extension.isInternetAvailable
  7. -import com.android.billingclient.api.*
  8. -import io.reactivex.Completable
  9. -import io.reactivex.Observable
  10. -import io.reactivex.Single
  11. -import io.reactivex.subjects.PublishSubject
  12. -import java.net.UnknownHostException
  13. -
  14. -
  15. -@Deprecated("d")
  16. -class RxBillings private constructor(val activity: Activity, private val sku: String) : PurchasesUpdatedListener {
  17. -
  18. -
  19. -    private var publishSubject: PublishSubject<Purchase>? = null
  20. -    private val billingClient: BillingClient = BillingClient.newBuilder(activity.applicationContext).setListener(this).build()
  21. -
  22. -
  23. -    override fun onPurchasesUpdated(billingResult:BillingResult, purchases: MutableList<Purchase?>?) {
  24. -        if (publishSubject == null) return
  25. -        if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && purchases != null && purchases.isNotEmpty() && purchases[0] != null) {
  26. -            publishSubject?.onNext(purchases[0]!!)
  27. -            publishSubject?.onComplete()
  28. -        } else {
  29. -            publishSubject?.let {
  30. -                if (!it.hasComplete() && !it.hasThrowable()) {
  31. -                    it.onError(RxBillingException(billingResult.responseCode))
  32. -                }
  33. -            }
  34. -        }
  35. -        publishSubject = null
  36. -    }
  37. -
  38. -    companion object {
  39. -        fun buyBook(activity: Activity, bookSku: String, bookId: Int): Completable {
  40. -            return Observable.create(BuyPurchaseOnSubscriber(activity,bookSku,null!!)).ignoreElements()
  41. -//            return buy(activity, bookSku).flatMap { pur ->
  42. -//                val ver = VerifyPurchase(bookId, pur.signature, pur.sku, pur.purchaseToken)
  43. -//                return@flatMap Injector.appComponent.authApi()
  44. -//                        .verifyPurchase(ver)
  45. -//                        .singleOrError()
  46. -//                        .applySchedulers()
  47. -//                        .doOnSubscribe {
  48. -//                            (activity as? BaseView)?.showProgress(message = "Виконується запит")
  49. -//                        }
  50. -//                        .doFinally {
  51. -//                            (activity as? BaseView)?.hideProgress()
  52. -//                        }
  53. -//                        .onErrorResumeNext {
  54. -//                            val err = Error.parseError(it)
  55. -//                            return@onErrorResumeNext if (!err.hasError(Error.PURCHASE_ALREADY_CONFIRMED)) {
  56. -//                                Single.error(it)
  57. -//                            } else {
  58. -//                                val billings = RxBillings(activity, bookSku)
  59. -//                                billings.connect()
  60. -//                                        .flatMap { it.launchBillingFlowWithConsume() }
  61. -//                                        .doFinally {
  62. -//                                            billings.billingClient.endConnection()
  63. -//                                        }
  64. -//                                        .flatMap { verPur ->
  65. -//                                            val verRe = VerifyPurchase(bookId, verPur.signature, verPur.sku, verPur.purchaseToken)
  66. -//                                            Injector.appComponent.authApi().verifyPurchase(verRe)
  67. -//                                                    .singleOrError()
  68. -//                                                    .applySchedulers()
  69. -//                                                    .doOnSubscribe {
  70. -//                                                        (activity as? BaseView)?.showProgress(message = "Виконується запит")
  71. -//                                                    }
  72. -//                                                    .doFinally {
  73. -//                                                        (activity as? BaseView)?.hideProgress()
  74. -//                                                    }
  75. -//                                        }
  76. -//                            }
  77. -//                        }
  78. -//            }.toCompletable()
  79. -        }
  80. -
  81. -        private fun buy(activity: Activity, sku: String): Single<Purchase> {
  82. -            val billings = RxBillings(activity, sku)
  83. -            return billings.connect()
  84. -                    .flatMap { rxBillings ->
  85. -                        rxBillings.getPurchase()
  86. -                                .onErrorResumeNext {
  87. -                                    if (it is RxBillingNoPurchaseException) {
  88. -                                        rxBillings.launchBillingFlow()
  89. -                                    } else {
  90. -                                        Single.error(it)
  91. -                                    }
  92. -                                }
  93. -                    }.doFinally {
  94. -                        billings.billingClient.endConnection()
  95. -                    }
  96. -        }
  97. -    }
  98. -
  99. -    fun connect(): Single<RxBillings> {
  100. -        return Single.create<RxBillings> {
  101. -            if (!activity.isInternetAvailable() && !it.isDisposed) {
  102. -                it.onError(UnknownHostException())
  103. -                return@create
  104. -            }
  105. -//            billingClient.startConnection(object : BillingClientStateListener {
  106. -//                override fun onBillingSetupFinished(@BillingClient.BillingResponse billingResponseCode: Int) {
  107. -//                    if (it.isDisposed) return
  108. -//                    if (billingResponseCode == BillingClient.BillingResponse.OK) {
  109. -//                        it.onSuccess(this@RxBillings)
  110. -//                    } else {
  111. -//                        it.onError(RxBillingException(billingResponseCode))
  112. -//                    }
  113. -//                }
  114. -//
  115. -//                override fun onBillingServiceDisconnected() {
  116. -//                }
  117. -//            })
  118. -        }.applySchedulers()
  119. -    }
  120. -
  121. -    fun launchBillingFlowWithConsume(): Single<Purchase> {
  122. -        return getPurchase().flatMap { purchaseToConsume ->
  123. -            return@flatMap Single.create<Purchase> {
  124. -                if (it.isDisposed) return@create
  125. -                val cp = ConsumeParams.newBuilder().setPurchaseToken(purchaseToConsume.purchaseToken).build()
  126. -                billingClient.consumeAsync(cp) { responseCode, purchaseToken ->
  127. -                    if (it.isDisposed) return@consumeAsync
  128. -                    if (responseCode.responseCode == 0 || responseCode.responseCode == 8) {
  129. -                        it.onError(RxBillingNoPurchaseException())
  130. -                    } else {
  131. -                        it.onError(RxBillingException(responseCode.responseCode))
  132. -                    }
  133. -
  134. -                }
  135. -            }.applySchedulers()
  136. -        }.onErrorResumeNext {
  137. -            return@onErrorResumeNext if (it is RxBillingNoPurchaseException) {
  138. -                launchBillingFlow()
  139. -            } else {
  140. -                Single.error(it)
  141. -            }
  142. -        }
  143. -    }
  144. -
  145. -    fun getPurchase(): Single<Purchase> {
  146. -        return Single.create<Purchase> { singleEmitter ->
  147. -            val response = billingClient.queryPurchases(BillingClient.SkuType.INAPP)
  148. -
  149. Add a comment to this line
  150. -            billingClient.isReady
  151. -            if (singleEmitter.isDisposed) return@create
  152. -            if (response.responseCode == BillingClient.BillingResponseCode.OK) {
  153. -                val purchase = response.purchasesList.firstOrNull { it.sku == sku }
  154. -                if (purchase == null) {
  155. -                    billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.INAPP) { responseCode, purchasesList ->
  156. -                        if (singleEmitter.isDisposed) return@queryPurchaseHistoryAsync
  157. -                        if (responseCode.responseCode == BillingClient.BillingResponseCode.OK) {
  158. -                            val historyPurchase = purchasesList?.firstOrNull { it.sku == sku }
  159. -                            if (historyPurchase != null) {
  160. -//                                singleEmitter.onSuccess(historyPurchase.)
  161. -                                singleEmitter.onError(RxBillingNoPurchaseException())
  162. -                            } else {
  163. -                                singleEmitter.onError(RxBillingNoPurchaseException())
  164. -                            }
  165. -                        } else {
  166. -                            singleEmitter.onError(RxBillingException(responseCode.responseCode))
  167. Add a comment to this line
  168. -                        }
  169. -                    }
  170. -                } else {
  171. -                    singleEmitter.onSuccess(purchase)
  172. -                }
  173. -            } else {
  174. -                singleEmitter.onError(RxBillingException(response.responseCode))
  175. -            }
  176. -        }.applySchedulers()
  177. -    }
  178. -
  179. -    fun launchBillingFlow(): Single<Purchase> {
  180. -
  181. -        val flowParams = BillingFlowParams.newBuilder()
  182. -                .setOldSku(sku)
  183. -//                .setType(BillingClient.SkuType.INAPP)
  184. -                .build()
  185. -        billingClient.launchBillingFlow(activity, flowParams)
  186. -        publishSubject = PublishSubject.create()
  187. -        return publishSubject!!.singleOrError().applySchedulers()
  188. -    }
  189. -
  190. -    class RxBillingException(var code: Int) : Exception()
  191. -    class RxBillingNoPurchaseException : Exception()
  192. -
  193. -
  194. -}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement