|
|
@@ -5,6 +5,7 @@ import android.content.Context
|
|
|
import android.os.Handler
|
|
|
import android.os.Message
|
|
|
import android.util.Log
|
|
|
+import com.adealink.weparty.wallet.data.TAG_GOOGLE_BILLING
|
|
|
import com.android.billingclient.api.AcknowledgePurchaseParams
|
|
|
import com.android.billingclient.api.AcknowledgePurchaseResponseListener
|
|
|
import com.android.billingclient.api.BillingClient
|
|
|
@@ -57,7 +58,10 @@ class GooglePayUtil(context: Context, private var listener: OnPurchasesListener)
|
|
|
|
|
|
//购买状态监听器
|
|
|
private var mPurchasesUpdatedListener = PurchasesUpdatedListener { result, purchases ->
|
|
|
- log("PurchasesUpdatedListener result:${result.responseCode} purchases:$purchases")
|
|
|
+ Log.i(
|
|
|
+ TAG_GOOGLE_BILLING,
|
|
|
+ "PurchasesUpdatedListener result:${result.responseCode} purchases:$purchases"
|
|
|
+ )
|
|
|
if (result.responseCode == BillingResponseCode.OK && purchases != null) {
|
|
|
listener.onPurchasesSuccess(purchases)
|
|
|
} else if (result.responseCode == BillingResponseCode.USER_CANCELED) {
|
|
|
@@ -68,19 +72,25 @@ class GooglePayUtil(context: Context, private var listener: OnPurchasesListener)
|
|
|
}
|
|
|
|
|
|
private var mPurchaseResponseListener = PurchasesResponseListener { result, purchases ->
|
|
|
- log("mPurchaseResponseListener result:${result.responseCode} p1:$purchases")
|
|
|
+ Log.i(
|
|
|
+ TAG_GOOGLE_BILLING,
|
|
|
+ "mPurchaseResponseListener result:${result.responseCode} p1:$purchases"
|
|
|
+ )
|
|
|
listener.onQueryPurchasesResponse(result, purchases)
|
|
|
}
|
|
|
|
|
|
//连接状态监听器
|
|
|
private var mConnectListener = object : BillingClientStateListener {
|
|
|
override fun onBillingServiceDisconnected() {
|
|
|
- log("onBillingServiceDisconnected")
|
|
|
+ Log.i(TAG_GOOGLE_BILLING, "onBillingServiceDisconnected")
|
|
|
mHandler?.sendEmptyMessageDelayed(0, 1000)
|
|
|
}
|
|
|
|
|
|
override fun onBillingSetupFinished(result: BillingResult) {
|
|
|
- log("onBillingSetupFinished --> ${result.responseCode} ${result.debugMessage}")
|
|
|
+ Log.i(
|
|
|
+ TAG_GOOGLE_BILLING,
|
|
|
+ "onBillingSetupFinished --> ${result.responseCode} ${result.debugMessage}"
|
|
|
+ )
|
|
|
if (result.responseCode == BillingResponseCode.OK) {
|
|
|
listener.onSetupFinish()
|
|
|
}
|
|
|
@@ -121,8 +131,12 @@ class GooglePayUtil(context: Context, private var listener: OnPurchasesListener)
|
|
|
* @param productId Google Play 管理中心配置的商品 id
|
|
|
* @param type ProductType.SUBS | ProductType.INAPP
|
|
|
*/
|
|
|
- fun queryProductDetails(productId: String, type: String, listener: ProductDetailsResponseListener) {
|
|
|
- log("queryProductDetails productId:$productId type:$type")
|
|
|
+ fun queryProductDetails(
|
|
|
+ productId: String,
|
|
|
+ type: String,
|
|
|
+ listener: ProductDetailsResponseListener
|
|
|
+ ) {
|
|
|
+ Log.i(TAG_GOOGLE_BILLING, "queryProductDetails productId:$productId type:$type")
|
|
|
val params = QueryProductDetailsParams.newBuilder()
|
|
|
.setProductList(
|
|
|
arrayListOf(
|
|
|
@@ -142,15 +156,29 @@ class GooglePayUtil(context: Context, private var listener: OnPurchasesListener)
|
|
|
* @param product 从google 获取的商品信息
|
|
|
* @param personalized 是否自动化决策进行了个性化设置
|
|
|
*/
|
|
|
- fun purchases(activity: Activity, product: ProductDetails, userId: String, selfOrderId: String, offerToken: String? = null, personalized: Boolean? = null) {
|
|
|
- log("purchases\nproduct:${product.name}\nid:${product.productId}\ntype:${product.productType}")
|
|
|
+ fun purchases(
|
|
|
+ activity: Activity,
|
|
|
+ product: ProductDetails,
|
|
|
+ userId: String,
|
|
|
+ selfOrderId: String,
|
|
|
+ offerToken: String? = null,
|
|
|
+ personalized: Boolean? = null
|
|
|
+ ) {
|
|
|
+ Log.i(
|
|
|
+ TAG_GOOGLE_BILLING,
|
|
|
+ "purchases\nproduct:${product.name}\nid:${product.productId}\ntype:${product.productType}"
|
|
|
+ )
|
|
|
val list = arrayListOf(
|
|
|
ProductDetailsParams.newBuilder()
|
|
|
.setProductDetails(product)
|
|
|
.apply {
|
|
|
if (product.productType == ProductType.SUBS) {
|
|
|
if (offerToken == null) {
|
|
|
- throw Exception("purchases error, product -> ${product.productId} is type ProductType.SUBS, offerToken is request!!")
|
|
|
+ Log.e(
|
|
|
+ TAG_GOOGLE_BILLING,
|
|
|
+ "purchases error, product -> ${product.productId} is type ProductType.SUBS, offerToken is request!!"
|
|
|
+ )
|
|
|
+ return
|
|
|
}
|
|
|
setOfferToken(offerToken)
|
|
|
}
|
|
|
@@ -172,7 +200,7 @@ class GooglePayUtil(context: Context, private var listener: OnPurchasesListener)
|
|
|
* @param type ProductType.SUBS | ProductType.INAPP
|
|
|
*/
|
|
|
suspend fun queryPurchaseAsync(type: String) {
|
|
|
- log("queryPurchaseAsync type:$type")
|
|
|
+ Log.i(TAG_GOOGLE_BILLING, "queryPurchaseAsync type:$type")
|
|
|
val params = QueryPurchasesParams.newBuilder().setProductType(type)
|
|
|
mClient?.queryPurchasesAsync(params.build(), mPurchaseResponseListener)
|
|
|
}
|
|
|
@@ -200,9 +228,13 @@ class GooglePayUtil(context: Context, private var listener: OnPurchasesListener)
|
|
|
suspend fun acknowledgePurchases(purchase: Purchase) {
|
|
|
if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {
|
|
|
if (!purchase.isAcknowledged) {
|
|
|
- val acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.purchaseToken)
|
|
|
+ val acknowledgePurchaseParams =
|
|
|
+ AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.purchaseToken)
|
|
|
val ackPurchaseResult = withContext(Dispatchers.IO) {
|
|
|
- mClient?.acknowledgePurchase(acknowledgePurchaseParams.build(), mAcknowledgeListener)
|
|
|
+ mClient?.acknowledgePurchase(
|
|
|
+ acknowledgePurchaseParams.build(),
|
|
|
+ mAcknowledgeListener
|
|
|
+ )
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -256,8 +288,4 @@ class GooglePayUtil(context: Context, private var listener: OnPurchasesListener)
|
|
|
mClient?.endConnection()
|
|
|
mClient = null
|
|
|
}
|
|
|
-
|
|
|
- private fun log(msg: String) {
|
|
|
- Log.e("GooglePayUtil", msg)
|
|
|
- }
|
|
|
}
|