Forked from jonathanpeppers/GooglePlayPurchaseService.cs
Created
June 7, 2019 19:15
-
-
Save Kasiviswanathan3876/ef0adf09811bdac1cb84120068a332b9 to your computer and use it in GitHub Desktop.
GooglePlayPurchaseService - GetPrices
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
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