【问题标题】:Android expiry time of In-app subscription item in androidAndroid中应用内订阅项目的Android到期时间
【发布时间】:2016-02-04 17:15:20
【问题描述】:

我正在尝试获取到期时间或订阅状态,以确保用户是否定期为我的商品付款。当我使用

查询时
Purchase monthlySubscription = inv.getPurchase("itemName");

ArrayList<String> ownedSkus = ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");

它返回以下数据

{  
       "packageName":"com.abcPackage",
       "productId":"auto1week",
       "purchaseTime":1453369299644,
       "purchaseState":0,
       "developerPayload":"PAY_LOAD",
       "purchaseToken":"TOKEN",
       "autoRenewing":true
}

问题是,purchaseTime 在几周后保持不变,这应该在每次购买后更改。

我尝试了 google Play 开发者 API

https://developers.google.com/android-publisher/#subscriptions

但我很难在我的 android 设备上实现它。

如果有人能指导我一步一步地在安卓设备上获取这些数据,我将不胜感激。

https://developers.google.com/android-publisher/api-ref/purchases/subscriptions

我们将非常感谢您在这方面的任何帮助。

【问题讨论】:

  • 删除开发者有效载荷和购买令牌。
  • 我在测试购买时注意到了同样的事情。似乎“购买时间”始终是首次购买订阅的时间。我找不到太多关于此更新永远购买的信息。我认为目前唯一真正的选择是连接到 API 并在那里验证购买。

标签: android in-app-billing subscriptions


【解决方案1】:

不确定这是否会有所帮助,但这是我的服务器端代码(在 java 中),它连接到开发人员 API 并返回订阅的到期时间。

我在 Google Developer Console 中创建了一个服务帐户,并按照有些晦涩的说明在 src/resources/keys.json 中创建了一个密钥文件。 APPLICATION_NAME 是我的应用程序包名称,PRODUCT_ID 是来自 Google PLAY 开发者控制台的订阅 ID。

抱歉,这并不是您要求的“逐步”,但我也在服务器端而不是客户端进行验证。我想在客户端上,您可以通过检查 purchaseState == 0(1=已取消,2=已退款)和 autoRenewing==true 来进行某种软验证。但是,如果他们取消,您可能会卡在那里,因为您仍然应该在订阅期间提供服务。

    public static Long doSomeWork(String token){
        log.debug("Google Validation: Doing some work:" + token);

        try{
            // Creating new Trusted Transport
            HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
            JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

            // Getting Auth Creds
            Credential cred = getAuthCredential();

            // Building Android Publisher API call
            AndroidPublisher ap = new AndroidPublisher.Builder(httpTransport, JSON_FACTORY, cred)
                    .setApplicationName(APPLICATION_NAME).build();

            // Get Subscription
            AndroidPublisher.Purchases.Subscriptions.Get get = ap.purchases().subscriptions().get(
                    APPLICATION_NAME,
                    PRODUCT_ID,
                    token);

            SubscriptionPurchase subscription = get.execute();
            log.debug(subscription.toPrettyString());

            log.debug("DONE (not null)");
            return subscription.getExpiryTimeMillis();

        } catch(IOException ex) {
            ex.printStackTrace();
        } catch (GeneralSecurityException ex2) {
            ex2.printStackTrace();
        }

        log.debug("DONE (failure) (0)");
        return 0L;

    }

    private static Credential getAuthCredential(){

        log.debug("getAuthCredential");
        try{
            //Read the credentials from the keys file.  This file is obtained from the
            // Google Developer Console (not the Play Developer Console
            InputStream is = GoogleReceiptValidation.class.getClassLoader().getResourceAsStream("keys.json");
            String str = IOUtils.toString(is);
            is.close();
            JSONObject obj = new JSONObject(str);
            InputStream stream = new ByteArrayInputStream(obj.toString().getBytes());

            //This is apparently "beta functionality".
            GoogleCredential creds = GoogleCredential.fromStream(stream);
            creds = creds.createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER));

            return creds;
        } catch (IOException ex){
            ex.printStackTrace();
        } catch (JSONException ex2){
            ex2.printStackTrace();
        }

        log.debug("No Creds found - returning null");

        return null;

    }

【讨论】:

  • 感谢您的输入,但我不需要服务器端代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
  • 2016-03-17
  • 2014-02-15
  • 1970-01-01
  • 2012-02-03
  • 2012-06-23
  • 1970-01-01
相关资源
最近更新 更多