|
@@ -9,7 +9,8 @@ import cn.yyxx.eyuangame.core.impl.SdkBridgeImpl
|
|
|
import cn.yyxx.eyuangame.core.internal.IImplCallback
|
|
|
import cn.yyxx.eyuangame.core.internal.IRequestCallback
|
|
|
import cn.yyxx.eyuangame.core.network.SdkRequest
|
|
|
-import cn.yyxx.support.JsonUtils
|
|
|
+import cn.yyxx.eyuangame.core.utils.MMKVUtils
|
|
|
+import cn.yyxx.support.ResUtils
|
|
|
import cn.yyxx.support.hawkeye.ToastUtils
|
|
|
import com.android.billingclient.api.*
|
|
|
import org.json.JSONException
|
|
@@ -23,7 +24,6 @@ class ChargeImpl : InAppBilling() {
|
|
|
|
|
|
//初始化IAB收银台客户端
|
|
|
//连接谷歌商店
|
|
|
-// private var payLoadingDialog: CircleProgressLoadingDialog? = null
|
|
|
private lateinit var implCallback: IImplCallback
|
|
|
private lateinit var chargeInfo: SdkChargeInfo
|
|
|
|
|
@@ -36,16 +36,14 @@ class ChargeImpl : InAppBilling() {
|
|
|
}
|
|
|
this.implCallback = callback
|
|
|
this.chargeInfo = chargeInfo
|
|
|
- //dismissDialog()
|
|
|
- //showDialog()
|
|
|
+ showDialog(activity)
|
|
|
|
|
|
- checkLocalNotifyFailedOrder(activity)
|
|
|
+// checkLocalNotifyFailedOrder(activity)
|
|
|
getOrderId(activity)
|
|
|
-
|
|
|
}
|
|
|
|
|
|
override fun chargePurchasesUpdated(activity: Activity, purchase: Purchase) {
|
|
|
- notifyOrder2Backend(activity, chargeInfo.orderId, purchase.originalJson, purchase.signature, false)
|
|
|
+ notifyOrder2Backend(activity, chargeInfo.orderId, purchase, false)
|
|
|
}
|
|
|
|
|
|
override fun preRewardPurchasesUpdated(activity: Activity, purchase: Purchase) {
|
|
@@ -61,6 +59,7 @@ class ChargeImpl : InAppBilling() {
|
|
|
billingClient?.apply {
|
|
|
queryPurchasesAsync(BillingClient.SkuType.INAPP, object : PurchasesResponseListener {
|
|
|
override fun onQueryPurchasesResponse(billingResult: BillingResult, list: MutableList<Purchase>) {
|
|
|
+ logBillingResult("onQueryPurchasesResponse", billingResult)
|
|
|
if (list.isNullOrEmpty()) {
|
|
|
//正常发起支付流程
|
|
|
Logger.d("正常发起支付流程")
|
|
@@ -69,14 +68,13 @@ class ChargeImpl : InAppBilling() {
|
|
|
Logger.e("存在未消耗订单,发起补单流程,size : ${list.size}")
|
|
|
run breaking@{
|
|
|
list.forEach { purchase ->
|
|
|
- Logger.d(purchase.toString())
|
|
|
purchase.skus.forEach {
|
|
|
if (it.equals(SdkBridgeImpl.initBean.rewardId)) {
|
|
|
Logger.e("存在阻塞的预注册奖励,停止支付流程")
|
|
|
if (list.size == 1) {
|
|
|
- ToastUtils.toastInfo(activity, "In-app Billing has some error , please restart app and try again")
|
|
|
dismissDialog()
|
|
|
disConnection()
|
|
|
+ ToastUtils.toastInfo(activity, "In-app Billing has some error , please restart app and try again")
|
|
|
implCallback.onResult(-1, "存在阻塞的预注册奖励,停止支付流程")
|
|
|
}
|
|
|
//跳出支付流程
|
|
@@ -89,17 +87,14 @@ class ChargeImpl : InAppBilling() {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
|
|
|
override fun purchasesUpdatedFailed() {
|
|
|
- TODO("Not yet implemented")
|
|
|
}
|
|
|
|
|
|
override fun connectGooglePlayFailed() {
|
|
|
- TODO("Not yet implemented")
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -126,9 +121,24 @@ class ChargeImpl : InAppBilling() {
|
|
|
connectGooglePlay(activity, false)
|
|
|
} catch (e: JSONException) {
|
|
|
e.printStackTrace()
|
|
|
- //dismissDialog()
|
|
|
+ dismissDialog()
|
|
|
+ val msg = if (TextUtils.isEmpty(resultInfo.msg)) {
|
|
|
+ ResUtils.getResString(activity, "yyxx_charge_tv_error")
|
|
|
+ } else {
|
|
|
+ resultInfo.msg
|
|
|
+ }
|
|
|
+ ToastUtils.toastInfo(activity, msg)
|
|
|
implCallback.onResult(-1, "获取订单异常")
|
|
|
}
|
|
|
+ } else {
|
|
|
+ dismissDialog()
|
|
|
+ val msg = if (TextUtils.isEmpty(resultInfo.msg)) {
|
|
|
+ ResUtils.getResString(activity, "yyxx_charge_tv_error")
|
|
|
+ } else {
|
|
|
+ resultInfo.msg
|
|
|
+ }
|
|
|
+ ToastUtils.toastInfo(activity, msg)
|
|
|
+ implCallback.onResult(-1, "获取订单异常")
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -142,26 +152,27 @@ class ChargeImpl : InAppBilling() {
|
|
|
val skus = mutableListOf<String>()
|
|
|
skus.add(chargeInfo.productId)
|
|
|
val params = SkuDetailsParams.newBuilder().setType(BillingClient.SkuType.INAPP).setSkusList(skus).build()
|
|
|
- billingClient?.querySkuDetailsAsync(params, object : SkuDetailsResponseListener {
|
|
|
- override fun onSkuDetailsResponse(billingResult: BillingResult, list: MutableList<SkuDetails>?) {
|
|
|
- dismissDialog()
|
|
|
- if (list.isNullOrEmpty()) {
|
|
|
- //查询商品信息失败
|
|
|
- Logger.e("查询商品信息失败")
|
|
|
- implCallback.onResult(-1, "查询商品信息失败")
|
|
|
- disConnection()
|
|
|
+ billingClient?.querySkuDetailsAsync(params) { billingResult, list ->
|
|
|
+ logBillingResult("onSkuDetailsResponse", billingResult)
|
|
|
+ dismissDialog()
|
|
|
+ if (list.isNullOrEmpty()) {
|
|
|
+ //查询商品信息失败
|
|
|
+ Logger.e("查询商品信息失败")
|
|
|
+ ToastUtils.toastInfo(activity, ResUtils.getResString(activity, "yyxx_charge_tv_error"))
|
|
|
+ implCallback.onResult(-1, "查询商品信息失败")
|
|
|
+ disConnection()
|
|
|
+ } else {
|
|
|
+ if (list.size == 1) {
|
|
|
+ val skuDetails = list[0]
|
|
|
+ Logger.d("product id : ${skuDetails.sku}")
|
|
|
+ launchBillingFlow(activity, skuDetails)
|
|
|
} else {
|
|
|
- if (list.size == 1) {
|
|
|
- val skuDetails = list[0]
|
|
|
- Logger.d("product id : ${skuDetails.sku}")
|
|
|
- launchBillingFlow(activity, skuDetails)
|
|
|
- } else {
|
|
|
- implCallback.onResult(-1, "查询商品信息异常")
|
|
|
- disConnection()
|
|
|
- }
|
|
|
+ ToastUtils.toastInfo(activity, ResUtils.getResString(activity, "yyxx_charge_tv_error"))
|
|
|
+ implCallback.onResult(-1, "查询商品信息异常")
|
|
|
+ disConnection()
|
|
|
}
|
|
|
}
|
|
|
- })
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -172,15 +183,19 @@ class ChargeImpl : InAppBilling() {
|
|
|
billingClient?.apply {
|
|
|
if (isReady) {
|
|
|
val flowParams = BillingFlowParams.newBuilder().setObfuscatedAccountId(chargeInfo.orderId).setSkuDetails(skuDetails).build()
|
|
|
+
|
|
|
val billingResult = launchBillingFlow(activity, flowParams)
|
|
|
+ logBillingResult("launchBillingFlow", billingResult)
|
|
|
if (billingResult.responseCode != BillingClient.BillingResponseCode.OK) {
|
|
|
dismissDialog()
|
|
|
disConnection()
|
|
|
+ ToastUtils.toastInfo(activity, ResUtils.getResString(activity, "yyxx_charge_tv_error"))
|
|
|
implCallback.onResult(-1, "启动谷歌收银台失败")
|
|
|
}
|
|
|
} else {
|
|
|
dismissDialog()
|
|
|
disConnection()
|
|
|
+ ToastUtils.toastInfo(activity, ResUtils.getResString(activity, "yyxx_charge_tv_error"))
|
|
|
implCallback.onResult(-1, "启动谷歌收银台失败")
|
|
|
}
|
|
|
}
|
|
@@ -189,22 +204,24 @@ class ChargeImpl : InAppBilling() {
|
|
|
/**
|
|
|
* 通知服务端发货
|
|
|
*/
|
|
|
- private fun notifyOrder2Backend(activity: Activity, orderId: String, originalJson: String, signature: String, isCache: Boolean = false) {
|
|
|
- if (!isCache) {
|
|
|
- // saveOrder2Local()
|
|
|
- }
|
|
|
- SdkRequest.instance.notifyOrder(activity, orderId, originalJson, signature, object : IRequestCallback {
|
|
|
+ private fun notifyOrder2Backend(activity: Activity, orderId: String, purchase: Purchase, isCache: Boolean = false) {
|
|
|
+ SdkRequest.instance.notifyOrder(activity, orderId, purchase.originalJson, object : IRequestCallback {
|
|
|
override fun onResponse(resultInfo: ResultInfo) {
|
|
|
- try {
|
|
|
+ if (resultInfo.code == 1) {
|
|
|
//消耗订单
|
|
|
- val jsonObject = JSONObject(originalJson)
|
|
|
- if (JsonUtils.hasJsonKey(jsonObject, "purchaseToken")) {
|
|
|
- consumeAsync(activity, jsonObject.getString("purchaseToken"), isCache)
|
|
|
- }
|
|
|
- } catch (e: JSONException) {
|
|
|
- e.printStackTrace()
|
|
|
+ consumeAsync(activity, purchase, isCache)
|
|
|
+ } else {
|
|
|
+ //失败则缓存订单
|
|
|
+ saveOrderInfo(purchase.orderId, orderId)
|
|
|
dismissDialog()
|
|
|
disConnection()
|
|
|
+ val msg = if (TextUtils.isEmpty(resultInfo.msg)) {
|
|
|
+ ResUtils.getResString(activity, "yyxx_charge_tv_error")
|
|
|
+ } else {
|
|
|
+ resultInfo.msg
|
|
|
+ }
|
|
|
+ ToastUtils.toastInfo(activity, msg)
|
|
|
+ implCallback.onResult(-1, "发货失败")
|
|
|
}
|
|
|
}
|
|
|
})
|
|
@@ -215,45 +232,78 @@ class ChargeImpl : InAppBilling() {
|
|
|
*/
|
|
|
private fun consumeCacheOrder(activity: Activity, purchase: Purchase) {
|
|
|
//消耗完了再发起支付
|
|
|
- val cache = ""
|
|
|
- val orerId = ""
|
|
|
- val originalJson = purchase.originalJson
|
|
|
- val purchaseToken = ""
|
|
|
+ Logger.d("消耗缓存订单 : $purchase")
|
|
|
+ var orderId = ""
|
|
|
+ purchase.accountIdentifiers?.apply {
|
|
|
+ orderId = if (!TextUtils.isEmpty(obfuscatedAccountId)) {
|
|
|
+ obfuscatedAccountId!!
|
|
|
+ } else {
|
|
|
+ //TODO getLocalOrderId
|
|
|
+ getOrderInfo(purchase.orderId)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ notifyOrder2Backend(activity, orderId, purchase, true)
|
|
|
+// consumeAsync(activity, purchase.purchaseToken, true)
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 消耗订单
|
|
|
*/
|
|
|
- private fun consumeAsync(activity: Activity, purchaseToken: String, isCache: Boolean = false) {
|
|
|
- val consumeParams = ConsumeParams.newBuilder().setPurchaseToken(purchaseToken).build()
|
|
|
+ private fun consumeAsync(activity: Activity, purchase: Purchase, isCache: Boolean = false) {
|
|
|
+ val consumeParams = ConsumeParams.newBuilder().setPurchaseToken(purchase.purchaseToken).build()
|
|
|
billingClient?.apply {
|
|
|
if (isReady) {
|
|
|
- consumeAsync(consumeParams, object : ConsumeResponseListener {
|
|
|
- override fun onConsumeResponse(billingResult: BillingResult, purchaseToken: String) {
|
|
|
- dismissDialog()
|
|
|
- if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
|
|
|
- // saveOrder2Local()
|
|
|
- if (isCache) {
|
|
|
- querySkuDetails(activity)
|
|
|
- } else {
|
|
|
- disConnection()
|
|
|
- implCallback.onResult(0, "支付成功")
|
|
|
- }
|
|
|
+ consumeAsync(consumeParams) { billingResult, _ ->
|
|
|
+ logBillingResult("onConsumeResponse", billingResult)
|
|
|
+ dismissDialog()
|
|
|
+ if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
|
|
|
+ // saveOrder2Local()
|
|
|
+ if (isCache) {
|
|
|
+ querySkuDetails(activity)
|
|
|
} else {
|
|
|
disConnection()
|
|
|
- implCallback.onResult(-1, "消耗订单异常")
|
|
|
+ implCallback.onResult(0, "支付成功")
|
|
|
}
|
|
|
+ removeOrderInfo(purchase.orderId)
|
|
|
+ } else {
|
|
|
+ disConnection()
|
|
|
+ implCallback.onResult(-1, "消耗订单异常")
|
|
|
+ ToastUtils.toastInfo(activity, ResUtils.getResString(activity, "yyxx_charge_tv_error"))
|
|
|
+
|
|
|
}
|
|
|
- })
|
|
|
+ }
|
|
|
} else {
|
|
|
dismissDialog()
|
|
|
disConnection()
|
|
|
implCallback.onResult(-1, "消耗订单异常")
|
|
|
+ ToastUtils.toastInfo(activity, ResUtils.getResString(activity, "yyxx_charge_tv_error"))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private fun saveOrderInfo(googleOrderId: String, orderId: String) {
|
|
|
+ try {
|
|
|
+ MMKVUtils.instance.orderKV?.encode(googleOrderId, orderId)
|
|
|
+ } catch (e: JSONException) {
|
|
|
+ e.printStackTrace()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun getOrderInfo(googleOrderId: String): String {
|
|
|
+ val info = MMKVUtils.instance.orderKV?.decodeString(googleOrderId)
|
|
|
+ Logger.d("getOrderInfo : $info")
|
|
|
+ return if (TextUtils.isEmpty(info)) {
|
|
|
+ ""
|
|
|
+ } else {
|
|
|
+ info!!
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun removeOrderInfo(googleOrderId: String) {
|
|
|
+ MMKVUtils.instance.orderKV?.removeValueForKey(googleOrderId)
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
companion object {
|
|
|
val instance: ChargeImpl by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
|