123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package cn.yyxx.eyuangame.core.impl.iab
- import android.app.Activity
- import android.content.Context
- import cn.yyxx.eyuangame.base.utils.Logger
- import cn.yyxx.eyuangame.core.impl.SdkBridgeImpl
- import cn.yyxx.eyuangame.core.ui.dialog.ScaleLoadingDialog
- import com.android.billingclient.api.BillingClient
- import com.android.billingclient.api.BillingClientStateListener
- import com.android.billingclient.api.BillingResult
- import com.android.billingclient.api.Purchase
- import com.google.android.gms.common.ConnectionResult
- import com.google.android.gms.common.GoogleApiAvailability
- /**
- * @author #Suyghur.
- * Created on 2021/06/17
- */
- abstract class InAppBilling {
- protected var billingClient: BillingClient? = null
- private var loadingDialog: ScaleLoadingDialog? = null
- protected fun checkGoogleApiAvailability(activity: Activity): Boolean {
- return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity) == ConnectionResult.SUCCESS
- }
- /**
- * 初始化IAB收银台客户端
- */
- protected fun initializeBillingClient(activity: Activity) {
- billingClient = BillingClient.newBuilder(activity).setListener { billingResult, list ->
- //谷歌支付结果在这里回调
- if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
- if (list.isNullOrEmpty()) {
- purchasesUpdatedFailed()
- } else {
- for (purchase in list) {
- Logger.d(purchase.toString())
- purchase.skus.forEach {
- if (it.equals(SdkBridgeImpl.initBean.rewardId)) {
- //预注册
- preRewardPurchasesUpdated(activity, purchase)
- } else {
- //支付
- chargePurchasesUpdated(activity, purchase)
- }
- }
- }
- }
- } else {
- purchasesUpdatedFailed()
- }
- }.enablePendingPurchases().build()
- }
- /**
- * 连接谷歌商店
- */
- protected fun connectGooglePlay(activity: Activity, isPreReward: Boolean = false) {
- billingClient?.apply {
- if (!isReady) {
- Logger.d("start connection Google Play ...")
- startConnection(object : BillingClientStateListener {
- override fun onBillingSetupFinished(billingResult: BillingResult) {
- logBillingResult("onBillingSetupFinished", billingResult)
- if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
- if (isPreReward) {
- queryRewardInfo(activity)
- } else {
- queryChargeInfo(activity)
- }
- } else {
- connectGooglePlayFailed()
- }
- }
- override fun onBillingServiceDisconnected() {
- Logger.e("onBillingServiceDisconnected")
- connectGooglePlay(activity, isPreReward)
- }
- })
- } else {
- dismissDialog()
- disConnection()
- }
- }
- }
- // protected fun handlePurchase(list)
- protected fun disConnection() {
- billingClient?.apply {
- if (isReady) {
- Logger.d("断开谷歌收银台连接,以清空被消耗或者失败的缓存订单")
- endConnection()
- }
- }
- }
- protected fun logBillingResult(callbackFuncName: String, billingResult: BillingResult) {
- val code = billingResult.responseCode
- val msg = billingResult.debugMessage
- Logger.d("$callbackFuncName , code : $code , msg : $msg")
- }
- protected fun showDialog(context: Context) {
- loadingDialog?.apply {
- dismiss()
- loadingDialog = null
- }
- loadingDialog = ScaleLoadingDialog(context, "")
- loadingDialog?.show()
- }
- protected fun dismissDialog() {
- loadingDialog?.apply {
- dismiss()
- loadingDialog = null
- }
- }
- /**
- * charge
- * 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}
- */
- protected abstract fun chargePurchasesUpdated(activity: Activity, purchase: Purchase)
- /**
- * pre reward
- * Purchase{"packageName":"com.flyfun.demo","productId":"com.flyfun.demo70","purchaseTime":1618367980837,"purchaseState":0,"purchaseToken":"cdphbkooagehckdcmkhmbhmd.AO-J1OzHTtCRDhd8k1Qfp3FU0GgYQUSaCx6I6W6Oi_P2tiS3LWKIdb6jTkc-tTqfcCagPxw2nPqujl9s10KuCunTl3OLSiIN9A"}
- */
- protected abstract fun preRewardPurchasesUpdated(activity: Activity, purchase: Purchase)
- protected abstract fun queryRewardInfo(activity: Activity)
- protected abstract fun queryChargeInfo(activity: Activity)
- protected abstract fun purchasesUpdatedFailed()
- protected abstract fun connectGooglePlayFailed()
- }
|