Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

in app purchase - how to get subscription expire date in inapp v3 android

Hi I have I implemented inapp billing V3 for one year subscription for a item using android-inapp-billing-v3. I want to show remaining days in my app. I am calling getSubscriptionTransactionDetails to get Transaction details for the product but it always returns null. here is my code.

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

I called this method from 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);

}

My subscription item button implemented in a fragment . One more problem I found that after successful subscription the onProductPurchased not get called but I have implemented the logic in onResume to update UI if bp.isSubscribed(SUBSCRIPTION_ID) returns true. Please tell me how to get subscription initiated date and expiry date.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm using this:

@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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...