简体   繁体   English

Android:如何检查用户是否有活动订阅

[英]Android: How to check if user have active subscription or not

I'm using Google Billing and trying to understand if subscription is expired or not with two solutions:我正在使用Google Billing并尝试通过两种解决方案了解订阅是否已过期:

  1. billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS) { billingResult, purchasesList -> }

  2. val purchasesList = billingClient.queryPurchases(BillingClient.SkuType.SUBS).purchasesList

And they returns something like that:他们返回这样的东西:

{
  "productId": "weekly_with_trial",
  "purchaseToken": "someToken",
  "purchaseTime": 1563305764597,
  "developerPayload": null
}
{
  "orderId": "GPA.3380-7704-5235-01361",
  "packageName": "com.myapp",
  "productId": "weekly_trial_trial",
  "purchaseTime": 1563305764597,
  "purchaseState": 0,
  "purchaseToken": "someToken",
  "autoRenewing": false,
  "acknowledged": false
}

Both of them return me a list of purchases without any info about if it expired or not.他们都给我发了一份购买清单,但没有任何关于它是否过期的信息。 How to check it ?如何检查?

As the documentation of Google play billing clearly suggest's that there are two function to get Purchases List由于 Google play billing 的文档清楚地表明有两个函数可以获取购买列表

val purchasesResult: PurchasesResult =
        billingClient.queryPurchases(SkuType.INAPP)

When you do a query purchase only active subscriptions appear on this list.当您进行查询购买时,此列表中只会显示有效订阅。 As long as the in-app product is on this list, the user should have access to it .只要应用内产品在此列表中,用户就应该有权访问它

This means that if the purchase is expired then you will not get that purchase item in the response for queryPurchase.这意味着如果购买已过期,那么您将不会在 queryPurchase 的响应中获得该购买项目。 However you have to keep in mind that it returns the local cached result of google play store.但是您必须记住,它返回 google play 商店的本地缓存结果。

So in order to get the purchase request from network call you need to use an asynchronous method, which is queryPurchaseHistoryAsync()因此,为了从网络调用中获取购买请求,您需要使用异步方法,即queryPurchaseHistoryAsync()

billingClient.queryPurchaseHistoryAsync(SkuType.INAPP, { billingResult, purchasesList ->
   if (billingResult.responseCode == BillingResponse.OK && purchasesList != null) {
       for (purchase in purchasesList) {
           // Process the result.
       }
   }
})

However there is a catch in object that contains info about the most recent purchase made by the user for each product ID, even if that purchase is expired, cancelled, or consumed.但是,有一个捕获对象包含有关用户为每个产品 ID 进行的最近一次购买的信息,即使该购买已过期、取消或消费。

So you can use queryPurchase() to get all active purchases.所以你可以使用queryPurchase()来获取所有活跃的购买。

Here is the code that I use to check for active subscription :这是我用来检查活动订阅的代码:

private void isUserHasSubscription(Context context) {
        billingClient = BillingClient.newBuilder(context).enablePendingPurchases().setListener(this).build();
        billingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(BillingResult billingResult) {
                Purchase.PurchasesResult purchasesResult=billingClient.queryPurchases(BillingClient.SkuType.SUBS);

                billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS,(billingResult1, list) -> {
                    
                    Log.d("billingprocess","purchasesResult.getPurchasesList():"+purchasesResult.getPurchasesList());
                    
                    if (billingResult1.getResponseCode() == BillingClient.BillingResponseCode.OK &&
                            !Objects.requireNonNull(purchasesResult.getPurchasesList()).isEmpty()) {

                            //here you can pass the user to use the app because he has an active subscription  
                            Intent myIntent=new Intent(context,MainActivity.class);
                            startActivity(myIntent);
                    }
                });
            }
            @Override
            public void onBillingServiceDisconnected() {
                // Try to restart the connection on the next request to
                // Google Play by calling the startConnection() method.
                Log.d("billingprocess","onBillingServiceDisconnected");
            }
        });
    }

Please follow the steps below to check if your subscription auto renewal is canceled or not. 请按照以下步骤检查您的订阅自动续订是否被取消。

Go to phone Settings. 转到手机设置。

Click iTunes & App Store. 单击iTunes&App Store。

Click Apple ID at top of screen. 单击屏幕顶部的Apple ID。

Click View Apple ID. 单击查看苹果ID。

Click Subscriptions. 单击订阅。

Click desired app. 点击所需的应用。

View subscription status. 查看订阅状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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