question

Jamie Hibbard avatar image
Jamie Hibbard asked

You subscription code checking is wrong

After waisting a lot of time. I found in the depth of the forum that if end date is null then user has purchased the item. I have seen some code: Receipt receipt = response.getReceipt(); String itemType = receipt.getItemType(); String sku = receipt.getSku(); String purchaseToken = receipt.getPurchaseToken(); SubscriptionPeriod subscriptionPeriod = receipt.getSubscriptionPeriod(); Date subscriptionStart = subscriptionPeriod.getStartDate(); Date subscriptionEnd = subscriptionPeriod.getEndDate(); Date today = new Date(); if (subscriptionStart <= today && (subscriptionEnd == null || today <=subscriptionEnd)) { // Entitle subscription } Eclipse is saying you can't perform <= on a date. So I used .after() and before() this wouldn't work as purchased today. etc... So.... Convert to longs. this code is now working. SubscriptionPeriod subscriptionPeriod = receipt.getSubscriptionPeriod(); Long subscriptionStart = subscriptionPeriod.getStartDate().getTime(); Date subscriptionEnd = subscriptionPeriod.getEndDate(); Long today = System.currentTimeMillis(); // if subscription end hasn't been set user is subscribed if (subscriptionStart <= today && subscriptionEnd == null) { // Entitle subscription Log.d("AMAZON", "USER SUBSCRIBED"); }
iap
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered
Hi Jamie, Thank you for writing to us. First thing, we do not check the end date to confirm the purchase, instead we do it to confirm whether the subscription is still valid and not expired. And we do it periodically in onPurchaseUpdatesResponse(). Below is async task which is started from mentioned callback. private class PurchaseUpdatesAsyncTask extends AsyncTask { @Override protected Boolean doInBackground(PurchaseUpdatesData... args) { PurchaseUpdatesData purchaseUpdatesData = args[0]; Set receipts = purchaseUpdatesData.getReceipts(); LatestSubscriptionPeriod latestSubscriptionPeriod = new LatestSubscriptionPeriod(); for (Receipt receipt : receipts) { String parentSku = receipt.getSku(); SubscriptionPeriod subscriptionPeriod = receipt .getSubscriptionPeriod(); // Store latest subscription period // (the one with the latest start date) for each Parent SKU latestSubscriptionPeriod.putIfLatest(parentSku, receipt); } Map latestSubPeriodPerSKU = latestSubscriptionPeriod.getAll(); for (Entry entry : latestSubPeriodPerSKU.entrySet()) { String parentSku = entry.getKey(); Receipt receiptWithLatestSubPeriod = entry.getValue(); SubscriptionPeriod latestSubPeriod = receiptWithLatestSubPeriod .getSubscriptionPeriod(); // If SubscriptionPeriod EndDate is NULL, // then subscription has NOT ended yet. Date now = new Date(); [b] boolean isSubscriptionActive = (latestSubPeriod.getStartDate().compareTo(now) < 0) && (latestSubPeriod.getEndDate() == null);[/b] String sku = MySKU.valueForParentSKU(parentSku).getSku(); //Notify the app the about subscription status of this sku } // Subscriptions are NOT revoked as customer would have paid for it // at beginning of subscription and is entitled to it for // subscription period. // Therefore, no need to call getRevokedSkus() for subscriptions return true; } } Please refer the subscription sample app for complete example. Apps-SDK.zip - > Android/InAppPurchasing/samples/SampleIAPSubscriptionsApp
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Jamie Hibbard avatar image
Jamie Hibbard answered
on https://developer.amazon.com/sdk/in-app-purchasing/documentation/quick-start.html Under Subscription section you have the following which i also used for onPurchaseUpdatesResponse to check the subscription is valid. The code won't work as it's a Date. Need to be updated to as you have posted here public class MyPurchasingObserver extends BasePurchasingObserver { ... public void onPurchaseResponse(PurchaseResponse response) { if (status == PurchaseResponse.getPurchaseRequestStatus.SUCCESSFUL) { Receipt receipt = response.getReceipt(); String itemType = receipt.getItemType(); String sku = receipt.getSku(); String purchaseToken = receipt.getPurchaseToken(); SubscriptionPeriod subscriptionPeriod = receipt.getSubscriptionPeriod(); Date subscriptionStart = subscriptionPeriod.getStartDate(); Date subscriptionEnd = subscriptionPeriod.getEndDate(); Date today = new Date(); [b] if (subscriptionStart <= today && (subscriptionEnd == null || today <=subscriptionEnd))[/b] { // Entitle subscription } } } ... } Message was edited by: Jamie Hibbard
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered
Thanks a lot Jamie for bringing this in our notice. We will get it corrected ASAP.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.