public void QueryInventory() { // Are we connected to a network? ConnectivityManager connectivityManager = (ConnectivityManager)MainActivity.Instance.GetSystemService(MainActivity.ConnectivityService); NetworkInfo activeConnection = connectivityManager.ActiveNetworkInfo; if ((activeConnection != null) && activeConnection.IsConnected) { // Ok, carefully attempt to connect to the in-app service try { // Asynchronously get inventory Task.Factory.StartNew(() => MP.MpUtils.FetchPaymentData(Android.App.Application.Context, FortumoInAppService._serviceId, FortumoInAppService._inAppSecret)); // Asnchronously get purchases Task.Factory.StartNew(() => { var responses = MP.MpUtils.GetPurchaseHistory(Android.App.Application.Context, FortumoInAppService._serviceId, FortumoInAppService._inAppSecret, FortumoInAppService._timeOut); // Record what we purchased foreach (var response in responses) { var paymentResponse = response as PaymentResponse; // Sanity checks if (paymentResponse == null || paymentResponse.BillingStatus != MP.MpUtils.MessageStatusBilled) { continue; } // Ok to record payment this.UpdatePayment(paymentResponse); } }); // Get local inventory // IMPORTANT: First time this is called, result will be empty) var priceData = MP.MpUtils.GetFetchedPriceData(Android.App.Application.Context, FortumoInAppService._serviceId, FortumoInAppService._inAppSecret); // TODO: Process priceData // Update inventory var inAppSkuRepository = new InAppProduct(); /* foreach (var product in products) { var existingProduct = inAppSkuRepository.GetSkuByProductId(product.ProductId); if (existingProduct != null) { existingProduct.Type = ItemType.Product; existingProduct.Price = product.Price; existingProduct.Title = product.Title; existingProduct.Description = product.Description; existingProduct.PriceCurrencyCode = product.Price_Currency_Code; inAppSkuRepository.Update(existingProduct); } else { var newProduct = new InAppSkuEntity(); newProduct.ProductId = product.ProductId; newProduct.Type = ItemType.Product; newProduct.Price = product.Price; newProduct.Title = product.Title; newProduct.Description = product.Description; newProduct.PriceCurrencyCode = product.Price_Currency_Code; inAppSkuRepository.Create(newProduct); } } */ this.FireOnQueryInventory(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception trying to connect to in app service: " + ex); this.FireOnQueryInventoryError(-1, null); } } }
public void Initialize() { this._customPaymentObserver = new CustomPaymentObserver(this); SKPaymentQueue.DefaultQueue.AddTransactionObserver(this._customPaymentObserver); this._queryInventoryObserver = NSNotificationCenter.DefaultCenter.AddObserver(InAppService.InAppQueryInventoryNotification, (notification) => { var info = notification.UserInfo; if (info == null) { // TODO: Had to put this in so it wouldn't crash, needs a revisit return; } var practiceModeProductId = new NSString(this.PracticeModeProductId); var product = (SKProduct)info.ObjectForKey(practiceModeProductId); // Update inventory var newProduct = new InAppProduct(); newProduct.ProductId = this.PracticeModeProductId; newProduct.Type = "inapp"; newProduct.Price = this.LocalizedPrice(product); newProduct.PriceCurrencyCode = product.PriceLocale.CurrencyCode; newProduct.Title = product.LocalizedTitle; newProduct.Description = product.LocalizedDescription; App.ViewModel.Products.Add(newProduct); // Notify anyone who needed to know that our inventory is in if (this.OnQueryInventory != null) { this.OnQueryInventory(); } }); this._queryInventoryErrorObserver = NSNotificationCenter.DefaultCenter.AddObserver(InAppService.InAppQueryInventoryErrorNotification, (notification) => { // Notify anyone who needed to know that there was a query inventory error if (this.OnQueryInventoryError != null) { this.OnQueryInventoryError(0, null); } }); this._purchaseProductObserver = NSNotificationCenter.DefaultCenter.AddObserver(InAppService.InAppPurchaseProductNotification, (notification) => { // Notify anyone who needed to know that product was purchased if (this.OnPurchaseProduct != null) { this.OnPurchaseProduct(); } }); this._purchaseProductErrorObserver = NSNotificationCenter.DefaultCenter.AddObserver(InAppService.InAppPurchaseProductErrorNotification, (notification) => { // Notify anyone who needed to know that there was a product purchase error if (this.OnPurchaseProductError != null) { this.OnPurchaseProductError(0, string.Empty); } }); this._restoreProductsObserver = NSNotificationCenter.DefaultCenter.AddObserver(InAppService.InAppRestoreProductsNotification, (notification) => { // Notify anyone who needed to know that products were restored if (this.OnRestoreProducts != null) { this.OnRestoreProducts(); } }); this._restoreProductsErrorObserver = NSNotificationCenter.DefaultCenter.AddObserver(InAppService.InAppRestoreProductsErrorNotification, (notification) => { // Notify anyone who needed to know that there was an error in restoring products if (this.OnRestoreProductsError != null) { this.OnRestoreProductsError(0, null); } }); if (this.CanMakePayments()) { // Async request // StoreKit -> App Store -> ReceivedResponse (see below) this.QueryInventory(); } }
public async void QueryInventory() { var products = await this._serviceConnection.BillingHandler.QueryInventoryAsync( new List<string>() { this.PracticeModeProductId }, ItemType.Product); // Update inventory foreach (var product in products) { var newProduct = new InAppProduct(); newProduct.ProductId = product.ProductId; newProduct.Type = ItemType.Product; newProduct.Price = product.Price; newProduct.Title = product.Title; newProduct.Description = product.Description; newProduct.PriceCurrencyCode = product.Price_Currency_Code; App.ViewModel.Products.Add(newProduct); } if (this.OnQueryInventory != null) { this.OnQueryInventory(); } }