管理 Google Play 订阅无效

Managing Google Play subscriptions not working

我的应用有应用内订阅购买功能,但我对整个实施过程感到非常困惑。

当应用程序打开时,我正在调用 onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) 来检查用户是否有任何活动购买。如果购买列表为空,则应用认为用户没有购买任何东西。

稍后当用户决定购买应用调用的东西时:

mBillingClient = BillingClient.newBuilder(view.getContext()).setListener(new PurchasesUpdatedListener()

再次购买后 onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) 被调用,但是,一旦我重新打开应用程序,一切都不起作用,一切都恢复正常(免费版),就像用户什么也没购买一样。

此外,用户购买数据也没有存储在云端(firebase 实时数据库)。已经有 3 位用户购买了产品,目前处于三天试用期。

你看过the sample on GitHub了吗?当用户打开您的应用程序时,您应该调用 queryPurchases。这是一个例子:

fun queryPurchasesAsync() {
        Log.d(LOG_TAG, "queryPurchasesAsync called")
        val purchasesResult = HashSet<Purchase>()
        var result = playStoreBillingClient.queryPurchases(BillingClient.SkuType.INAPP)
        Log.d(LOG_TAG, "queryPurchasesAsync INAPP results: ${result?.purchasesList?.size}")
        result?.purchasesList?.apply { purchasesResult.addAll(this) }
        if (isSubscriptionSupported()) {
            result = playStoreBillingClient.queryPurchases(BillingClient.SkuType.SUBS)
            result?.purchasesList?.apply { purchasesResult.addAll(this) }
            Log.d(LOG_TAG, "queryPurchasesAsync SUBS results: ${result?.purchasesList?.size}")
        }
        processPurchases(purchasesResult)
    }

您应该在与 BillingClient 服务建立连接后立即进行此调用:

/**
     * This is the callback for when connection to the Play [BillingClient] has been successfully
     * established. It might make sense to get [SkuDetails] and [Purchases][Purchase] at this point.
     */
    override fun onBillingSetupFinished(billingResult: BillingResult) {
        when (billingResult.responseCode) {
            BillingClient.BillingResponseCode.OK -> {
                Log.d(LOG_TAG, "onBillingSetupFinished successfully")
                querySkuDetailsAsync(BillingClient.SkuType.INAPP, GameSku.INAPP_SKUS)
                querySkuDetailsAsync(BillingClient.SkuType.SUBS, GameSku.SUBS_SKUS)
                queryPurchasesAsync()
            }
            BillingClient.BillingResponseCode.BILLING_UNAVAILABLE -> {
                //Some apps may choose to make decisions based on this knowledge.
                Log.d(LOG_TAG, billingResult.debugMessage)
            }
            else -> {
                //do nothing. Someone else will connect it through retry policy.
                //May choose to send to server though
                Log.d(LOG_TAG, billingResult.debugMessage)
            }
        }
    }