Skip to content

Instantly share code, notes, and snippets.

@mikeedwards83
Created July 25, 2019 07:47
Show Gist options
  • Save mikeedwards83/3709f5a54a041993da3853ad7bcef1ea to your computer and use it in GitHub Desktop.
Save mikeedwards83/3709f5a54a041993da3853ad7bcef1ea to your computer and use it in GitHub Desktop.
protected async Task<PurchaseResult> WasItemPurchased(string productId, ItemType type)
{
var billing = CrossInAppBilling.Current;
try
{
var connected = await billing.ConnectAsync(type);
if (!connected)
{
//Couldn't connect
return new PurchaseResult(PurchaseResultState.ConnectionIssue, null);
}
//check purchases
var purchases = await billing.GetPurchasesAsync(type);
purchases = purchases.ToArray();
var purchase = purchases?.FirstOrDefault(p => p.ProductId == productId);
//check for null just incase
if (purchase != null)
{
//Purchase restored
return new PurchaseResult(PurchaseResultState.Success, purchase);
}
else
{
//no purchases found
return new PurchaseResult(PurchaseResultState.Failed, null);
}
}
catch (InAppBillingPurchaseException purchaseEx)
{
Logger.Error("StoreServiceBase.PurchaseInApp InAppBillingPurchaseException", purchaseEx);
throw purchaseEx;
}
catch (Exception ex)
{
Logger.Error("StoreServiceBase.PurchaseInApp Exception", ex);
throw ex;
//Something has gone wrong
}
finally
{
await billing.DisconnectAsync();
}
return new PurchaseResult(PurchaseResultState.Failed, null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment