Created
July 25, 2019 07:47
-
-
Save mikeedwards83/3709f5a54a041993da3853ad7bcef1ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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