jamesmontemagno / InAppBillingPlugin

Cross-platform In App Billing Plugin for .NET
MIT License
651 stars 152 forks source link

iOS/Android I don't know if a user has already consumed his purchase #188

Closed Fox9MonoGame closed 6 years ago

Fox9MonoGame commented 6 years ago

I don't know if I need to restore a consumable in-app purchase(50 coins) because I don't know if the user has already consumed his 50 coins in my game. If the user has consumed the 50 coins, then I don't need to restore the in-app purchase. But on the other hand, if he hasn't consumed his in-app purchase yet, then I need to restore his 50 coins.

How can I check the ConsumptionState of an in-app purchase? I need to be sure that he has already consumed the in-app purchase.

` public async Task WasItemPurchased(string productId) { var billing = CrossInAppBilling.Current; try { var connected = await billing.ConnectAsync(ItemType.InAppPurchase);

    if (!connected)
    {
        //Couldn't connect
        return;
    }

    //check purchases
    var purchases = await billing.GetPurchasesAsync(ItemType.InAppPurchase);

    //check for null just incase
    if(purchases?.Any(p => p.ProductId == productId) ?? false)
    {         
     //Restore or not?
     //I want to check if the in-app purchase was already consumed, but I don't know how to get the ConsumptionState here??
        return true;
    }
    else
    {
        //no purchases found
        return false;
    }
}    
catch (InAppBillingPurchaseException purchaseEx)
{
    //Billing Exception handle this based on the type
    Debug.WriteLine("Error: " + purchaseEx);
}
catch (Exception ex)
{
    //Something has gone wrong
}
finally
{    
    await billing.DisconnectAsync();
}

return false;
}`
jamesmontemagno commented 6 years ago

On iOS items are immediately consumed. On Android you need to track if they consumed it or when you get your purchases there is a Consumed state. https://github.com/jamesmontemagno/InAppBillingPlugin/blob/master/src/Plugin.InAppBilling.Abstractions/InAppBillingPurchase.cs#L61