InAppBilling.kt 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package cn.yyxx.eyuangame.core.impl.iab
  2. import android.app.Activity
  3. import android.content.Context
  4. import cn.yyxx.eyuangame.base.utils.Logger
  5. import cn.yyxx.eyuangame.core.impl.SdkBridgeImpl
  6. import cn.yyxx.eyuangame.core.ui.dialog.ScaleLoadingDialog
  7. import com.android.billingclient.api.BillingClient
  8. import com.android.billingclient.api.BillingClientStateListener
  9. import com.android.billingclient.api.BillingResult
  10. import com.android.billingclient.api.Purchase
  11. import com.google.android.gms.common.ConnectionResult
  12. import com.google.android.gms.common.GoogleApiAvailability
  13. /**
  14. * @author #Suyghur.
  15. * Created on 2021/06/17
  16. */
  17. abstract class InAppBilling {
  18. protected var billingClient: BillingClient? = null
  19. private var loadingDialog: ScaleLoadingDialog? = null
  20. protected fun checkGoogleApiAvailability(activity: Activity): Boolean {
  21. return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity) == ConnectionResult.SUCCESS
  22. }
  23. /**
  24. * 初始化IAB收银台客户端
  25. */
  26. protected fun initializeBillingClient(activity: Activity) {
  27. billingClient = BillingClient.newBuilder(activity).setListener { billingResult, list ->
  28. //谷歌支付结果在这里回调
  29. if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
  30. if (list.isNullOrEmpty()) {
  31. purchasesUpdatedFailed()
  32. } else {
  33. for (purchase in list) {
  34. Logger.d(purchase.toString())
  35. purchase.skus.forEach {
  36. if (it.equals(SdkBridgeImpl.initBean.rewardId)) {
  37. //预注册
  38. preRewardPurchasesUpdated(activity, purchase)
  39. } else {
  40. //支付
  41. chargePurchasesUpdated(activity, purchase)
  42. }
  43. }
  44. }
  45. }
  46. } else {
  47. purchasesUpdatedFailed()
  48. }
  49. }.enablePendingPurchases().build()
  50. }
  51. /**
  52. * 连接谷歌商店
  53. */
  54. protected fun connectGooglePlay(activity: Activity, isPreReward: Boolean = false) {
  55. billingClient?.apply {
  56. if (!isReady) {
  57. Logger.d("start connection Google Play ...")
  58. startConnection(object : BillingClientStateListener {
  59. override fun onBillingSetupFinished(billingResult: BillingResult) {
  60. logBillingResult("onBillingSetupFinished", billingResult)
  61. if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
  62. if (isPreReward) {
  63. queryRewardInfo(activity)
  64. } else {
  65. queryChargeInfo(activity)
  66. }
  67. } else {
  68. connectGooglePlayFailed()
  69. }
  70. }
  71. override fun onBillingServiceDisconnected() {
  72. Logger.e("onBillingServiceDisconnected")
  73. connectGooglePlay(activity, isPreReward)
  74. }
  75. })
  76. } else {
  77. dismissDialog()
  78. disConnection()
  79. }
  80. }
  81. }
  82. // protected fun handlePurchase(list)
  83. protected fun disConnection() {
  84. billingClient?.apply {
  85. if (isReady) {
  86. Logger.d("断开谷歌收银台连接,以清空被消耗或者失败的缓存订单")
  87. endConnection()
  88. }
  89. }
  90. }
  91. protected fun logBillingResult(callbackFuncName: String, billingResult: BillingResult) {
  92. val code = billingResult.responseCode
  93. val msg = billingResult.debugMessage
  94. Logger.d("$callbackFuncName , code : $code , msg : $msg")
  95. }
  96. protected fun showDialog(context: Context) {
  97. loadingDialog?.apply {
  98. dismiss()
  99. loadingDialog = null
  100. }
  101. loadingDialog = ScaleLoadingDialog(context, "")
  102. loadingDialog?.show()
  103. }
  104. protected fun dismissDialog() {
  105. loadingDialog?.apply {
  106. dismiss()
  107. loadingDialog = null
  108. }
  109. }
  110. /**
  111. * charge
  112. * Purchase{"orderId":"GPA.3325-4558-9050-03633","packageName":"com.flyfun.demo","productId":"com.flyfun.ylj.60","purchaseTime":1618367632652,"purchaseState":4,"purchaseToken":"mpfdmeeoemplddgfknnkgnlf.AO-J1Owpfua8OfxpU_sjOBTY_ZgRoN3Km-mNJdmie9Qpd1w-DDQjtkUfcWOGhScHHeJ9ogWqkP_01w5WHp9Pfnpvgx1_nffDYA","acknowledged":false}
  113. */
  114. protected abstract fun chargePurchasesUpdated(activity: Activity, purchase: Purchase)
  115. /**
  116. * pre reward
  117. * Purchase{"packageName":"com.flyfun.demo","productId":"com.flyfun.demo70","purchaseTime":1618367980837,"purchaseState":0,"purchaseToken":"cdphbkooagehckdcmkhmbhmd.AO-J1OzHTtCRDhd8k1Qfp3FU0GgYQUSaCx6I6W6Oi_P2tiS3LWKIdb6jTkc-tTqfcCagPxw2nPqujl9s10KuCunTl3OLSiIN9A"}
  118. */
  119. protected abstract fun preRewardPurchasesUpdated(activity: Activity, purchase: Purchase)
  120. protected abstract fun queryRewardInfo(activity: Activity)
  121. protected abstract fun queryChargeInfo(activity: Activity)
  122. protected abstract fun purchasesUpdatedFailed()
  123. protected abstract fun connectGooglePlayFailed()
  124. }