how to get subscription expire date in inapp v3 android
Asked Answered
H

3

13

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.

Hf answered 3/6, 2015 at 12:45 Comment(1)
Get subscription expiry date after subscribed. Refer this link : #38801437Unplaced
H
4

I want to give Ans to my own que's ans, So that if some one looking for the same can find the solution. After lots of google I did not find a exact solution I was looking for So I create a method in BillingProcessor class in iabv3 library project which returns a Bundle with Purchases details, in which i get the purchased date. Now I am able to find the expiry date with this. The method looks like below

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;
}
Hf answered 5/6, 2015 at 6:2 Comment(6)
What is that purchased date exactly? The date of the last payment?Divorcee
Yes, the purchased date is last payment date of product.Hf
how to call getPurchases() from BillingProcessor class? @maddydLaaland
its only show first purchase date after renewal. how can i get renewal date? @maddydLaaland
@AnilKanani did you get renewal date?Squatter
Purchase date is not the latest renewal date. It's subscription signup date - meaning the time when the subscription subscribed very first time.Valenba
K
6

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;
}
Kalsomine answered 17/7, 2019 at 18:51 Comment(3)
In this approach what if the user changed their device time and date to say a year in the future?Spermatogonium
Well of course it will fail, it's not meant to be used to grant / revoke access to paid stuff, but to display the next renewing date to the user. If he is trying to hack the app, he has to expect this kind of information to be inaccurate.Kalsomine
now = new Date(); this is where the problem comes in. It relies on phones time. I can set my phone time to be past or within certain period of subscription.Toughie
H
4

I want to give Ans to my own que's ans, So that if some one looking for the same can find the solution. After lots of google I did not find a exact solution I was looking for So I create a method in BillingProcessor class in iabv3 library project which returns a Bundle with Purchases details, in which i get the purchased date. Now I am able to find the expiry date with this. The method looks like below

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;
}
Hf answered 5/6, 2015 at 6:2 Comment(6)
What is that purchased date exactly? The date of the last payment?Divorcee
Yes, the purchased date is last payment date of product.Hf
how to call getPurchases() from BillingProcessor class? @maddydLaaland
its only show first purchase date after renewal. how can i get renewal date? @maddydLaaland
@AnilKanani did you get renewal date?Squatter
Purchase date is not the latest renewal date. It's subscription signup date - meaning the time when the subscription subscribed very first time.Valenba
P
1

I have used this code for getting the purchase details in the Billing Processor Library .

   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
Partite answered 1/6, 2017 at 5:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.