简体   繁体   中英

Managing Google Play subscriptions not working

My app has in-app subscription purchases but I am feeling really confused regarding the whole implementation process.

When the app opens I am calling onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) to check if the user has any active purchases. If the purchase list is null, the app assumes the user has not purchased anything.

Later when user decides to purchase something app calls:

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

Once the purchase is made again onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) is called, however, once I reopen the app nothing works, its all back to normal (free version) like user purchased nothing.

Also, user purchase data wasn't stored in the cloud (firebase real-time database). Already three users have made their purchase and it's in Three Day Trial period.

Have you looked at the sample on GitHub ? When user opens your app you are supposed to call queryPurchases . Here is an example:

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)
    }

And you should make this call as soon as you establish connection with the BillingClient service:

/**
     * 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)
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM