I am using subscription based billing in my app where users can subscribe to premium membership on a monthly payment basis. I am writing the code where I check the payment status of the premium user every 30 days and accordingly cancel his premium membership in case the monthly payment has not been made. My question is that what will "inventory.getPurchase(my_prod_id) return in case of failed payment? Also will it be the same for a cancelled order as well.
My code for checking the payment status is below
/**
* query for the product for premium membership from inventory,
*/
public void queryInventory(){
IabHelper.QueryInventoryFinishedListener mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory)
{
if (result.isFailure()) {
// handle error
Toast.makeText(getApplicationContext(), "Error : "+result.toString(), Toast.LENGTH_LONG).show();
}
else{
try{
//check if user has existing subscription purchase
Purchase membershipPurchase = inventory.getPurchase
if (membershipPurchase !=null){
if (Integer.parseInt(membershipPurchase.getOrderId())>Integer.parseInt(jsonPurchase.getString("OrderId"))){
//means membership has been renewed, hence update the backend database with new purchase data
updateUserTable();
}
}
else{
//subscription has not been renewed or has expired. Alert user and update user status back to free member.
Toast.makeText(ctx.getApplicationContext(), "Your Premium User membership has expired or not renewed. Please go to 'MyApps' in Google Play and verify", Toast.LENGTH_LONG).show();
//update backend database with status change to free member
updatetoFree();
}
}
catch(Exception e){
Toast.makeText(ctx.getApplicationContext(), "Error in membership verification: "+e.toString(), Toast.LENGTH_LONG).show();
}
}
}
};
List<String> additionalSkuList = new ArrayList<String>();
additionalSkuList.add(sku_premium_membership);
mHelper.queryInventoryAsync(true, additionalSkuList, mQueryFinishedListener);
}