Skip to content

Instantly share code, notes, and snippets.

@jonathanpeppers
Last active June 7, 2019 19:15
Show Gist options
  • Save jonathanpeppers/53527897a76bc294c4f600a328239af7 to your computer and use it in GitHub Desktop.
Save jonathanpeppers/53527897a76bc294c4f600a328239af7 to your computer and use it in GitHub Desktop.
GooglePlayPurchaseService - GetPrices
public class GooglePlayPurchaseService : PurchaseService
{
private BillingConnection _connection;
private sealed class Product
{
public string Title { get; set; }
public string Price { get; set; }
public string Type { get; set; }
public string Description { get; set; }
public string ProductId { get; set; }
}
private async Task Connect()
{
if (_connection == null)
{
_connection = new BillingConnection();
await _connection.Connect();
}
}
public async override Task<Purchase[]> GetPrices(params string[] ids)
{
await Connect();
var querySku = new Bundle();
querySku.PutStringArrayList(BillingConstants.SkuDetailsItemList, ids);
//NOTE: this is a blocking network call, so start a Task
var skuDetails = await Task.Factory.StartNew(() => _connection.Service.GetSkuDetails(BillingConstants.ApiVersion, Application.Context.PackageName, BillingConstants.ItemTypeInApp, querySku));
int response = skuDetails.GetInt(BillingConstants.ResponseCode);
if (response != BillingConstants.ResultOk)
{
throw new Exception("GetSkuDetails failed, code: " + response);
}
var productsAsJson = skuDetails.GetStringArrayList(BillingConstants.SkuDetailsList);
var products = new List<Purchase>(productsAsJson.Count);
foreach (string json in productsAsJson)
{
var nativeProduct = JsonConvert.DeserializeObject<Product>(json);
products.Add(new Purchase
{
Id = nativeProduct.ProductId,
Price = nativeProduct.Price,
NativeObject = nativeProduct,
});
}
return products.ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment