12

嗨,我已经使用android-inapp-billing-v3为一个项目实施了一年订阅的 inapp billing V3 。我想在我的应用程序中显示剩余天数。我打电话getSubscriptionTransactionDetails来获取产品的交易详细信息,但它总是返回null。这是我的代码。

  private BillingProcessor startInappCheck(){

         bp = new BillingProcessor(mContext, BASE64ENCODEDPUBLICKEY, new BillingProcessor.IBillingHandler() {
                @Override
                public void onProductPurchased(String productId, TransactionDetails details) {
                    LogUtils.e(TAG, "onProductPurchased :" +productId);
    //              showToast("onProductPurchased: " + productId);

                }
                @Override
                public void onBillingError(int errorCode, Throwable error) {

                    LogUtils.e(TAG, "onBillingError :" +errorCode);


                }
                @Override
                public void onBillingInitialized() {
  //                showToast("onBillingInitialized");
                    readyToPurchase = true;



                    try{
                        SkuDetails subs = bp.getSubscriptionListingDetails(SUBSCRIPTION_ID);


                        LogUtils.d(TAG, "Owned Subscription: " + subs.toString());
                       TransactionDetails tr = bp.getSubscriptionTransactionDetails(SUBSCRIPTION_ID);
                      LogUtils.d(TAG, "Owned Subscription: " + tr.toString());

                    }catch (Exception e) {
                        // TODO: handle exception
                    }


                }
                @Override
                public void onPurchaseHistoryRestored() {
   //                   showToast("onPurchaseHistoryRestored");
                    for(String sku : bp.listOwnedSubscriptions()){
                        LogUtils.d(TAG, "Owned Subscription: " + sku);
                    }
   //                showToast("onPurchaseHistoryRestored");

                }
            });
         return bp;
    }

我从onCreate.

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
     if (!bp.handleActivityResult(requestCode, resultCode, data))
            super.onActivityResult(requestCode, resultCode, data);

}

我的订阅项目按钮在fragment. 还有一个问题,我发现成功订阅后 onProductPurchased没有被调用,但我已经实现了onResume更新 UI 如果bp.isSubscribed(SUBSCRIPTION_ID)返回的逻辑true。请告诉我如何获取订阅开始日期和到期日期。

4

3 回答 3

5

我想给我自己的 que 的 ans 一个答案,这样如果有人在寻找相同的答案,就可以找到解决方案。经过大量的谷歌搜索,我没有找到我正在寻找的确切解决方案所以我BillingProcessor在项目的类中创建了一个方法,iabv3 library它返回一个Bundle带有购买详细信息的方法,其中我得到了购买日期。现在我可以用这个找到到期日期。该方法如下所示

public Bundle getPurchases(){
    if (!isInitialized())
        return null;
    try{
        return  billingService.getPurchases(Constants.GOOGLE_API_VERSION, contextPackageName, Constants.PRODUCT_TYPE_SUBSCRIPTION, null);
    }catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
于 2015-06-05T06:02:32.067 回答
5

我正在使用这个:

@Nullable
public Date getSubscriptionRenewingDate(String sku) {

    // Get the Purchase object:
    Purchase purchase = null;
    Purchase.PurchasesResult purchasesResult = _billingClient.queryPurchases(BillingClient.SkuType.SUBS);
    if (purchasesResult.getPurchasesList() != null) {
        for (Purchase p : purchasesResult.getPurchasesList()) {
            if (p.getSku().equals(sku) && p.getPurchaseState() == Purchase.PurchaseState.PURCHASED && p.isAutoRenewing()) {
                purchase = p;
                break;
            }
        }
    }

    // Get the SkuDetails object:
    SkuDetails skuDetails = null;
    for (SkuDetails s : _skuDetails) { // _skuDetails is an array of SkuDetails retrieved with querySkuDetailsAsync
        if (s.getSku().equals(sku)) {
            skuDetails = s;
            break;
        }
    }

    if (purchase != null && skuDetails != null) {

        Date purchaseDate = new Date(purchase.getPurchaseTime());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(purchaseDate);

        Date now = new Date();

        while (calendar.getTime().before(now)) {

            switch (skuDetails.getSubscriptionPeriod()) {

                case "P1W": calendar.add(Calendar.HOUR, 7*24); break;
                case "P1M": calendar.add(Calendar.MONTH, 1); break;
                case "P3M": calendar.add(Calendar.MONTH, 3); break;
                case "P6M": calendar.add(Calendar.MONTH, 6); break;
                case "P1Y": calendar.add(Calendar.YEAR, 1); break;
            }
        }

        return calendar.getTime();
    }

    return null;
}
于 2019-07-17T18:51:56.243 回答
1

我已使用此代码在计费处理器库中获取购买详细信息。

   TransactionDetails transactionDetails = bp.getSubscriptionTransactionDetails(channelModel.getAndroidProductId());
        Log.d(TAG, "initializePaymentSetup: " + transactionDetails.toString());
        Log.d(TAG, "initializePaymentSetup: " + transactionDetails.purchaseInfo.toString());

 transactionDetails.purchaseInfo.purchaseData// this will return the purchase date
于 2017-06-01T05:58:01.177 回答