public async override Task <AppProduct> UpdateProduct(AppProduct appProduct)
        {
            await Task.Run(() =>
            {
                // Attempt to get the license
                var license = GetLicense(appProduct.ProductID);
                if (license == null)
                {
                    return;
                }

                // Update the product
                appProduct.ExpiryDate = license.ExpirationDate.DateTime;
                appProduct.Purchased  = license.IsActive;
                if (license.IsConsumable)
                {
                    appProduct.ProductType = ProductKind.Consumable;
                }
                else
                {
                    appProduct.ProductType = ProductKind.Durable;
                }

                // Invoke the event
                InvokeAppProductsChanged();
            });

            return(appProduct);
        }
        public async override Task RefreshAppProducts()
        {
            AppProducts.Clear();

            var listingInfo = await GetListingInformationAsync();

            foreach (var product in listingInfo.ProductListings)
            {
                var license = GetLicense(product.Value.ProductId);

                AppProduct appProduct = new AppProduct(license.ProductId, license.ProductId, false);
                appProduct.Name                  = product.Value.Name;
                appProduct.Description           = product.Value.Description;
                appProduct.CurrentFormattedPrice = product.Value.FormattedPrice;
                appProduct.BaseFormattedPrice    = product.Value.FormattedBasePrice;
                appProduct.ExpiryDate            = license.ExpirationDate.DateTime;
                appProduct.Purchased             = license.IsActive;
                if (license.IsConsumable)
                {
                    appProduct.ProductType = ProductKind.Consumable;
                }
                else
                {
                    appProduct.ProductType = ProductKind.Durable;
                }

                AppProducts.Add(appProduct);
            }

            // Invoke the event
            InvokeAppProductsChanged();
        }
        public async override Task RefreshAppProducts()
        {
            var kinds = GetProductKinds();

            var appLicense = await Context.GetAppLicenseAsync();

            var result = await Context.GetAssociatedStoreProductsAsync(kinds.Select(x => x.ToString()));

            AppProducts.Clear();
            foreach (var item in result.Products)
            {
                Debug.WriteLine($"Key: {item.Key} | StoreID: {item.Value.StoreId} | ProductID: {item.Value.InAppOfferToken}");

                // Cache the value
                var storeProduct = item.Value;

                // Create the App Product
                AppProduct appProduct = new AppProduct(storeProduct.InAppOfferToken, storeProduct.StoreId);
                appProduct.Name                  = storeProduct.Title;
                appProduct.Description           = storeProduct.Description;
                appProduct.CurrentFormattedPrice = storeProduct.Price.FormattedPrice;
                appProduct.BaseFormattedPrice    = storeProduct.Price.FormattedBasePrice;

                // Parse the Product Type
                if (Enum.TryParse <ProductKind>(storeProduct.ProductKind, out ProductKind parsedProductKind))
                {
                    appProduct.ProductType = parsedProductKind;
                }
                else
                {
                    appProduct.ProductType = ProductKind.Unknown;
                }

                // Add License Info
                appProduct.Purchased = storeProduct.IsInUserCollection;
                foreach (var license in appLicense.AddOnLicenses)
                {
                    Debug.WriteLine($"Licence Key: {license.Key} == Product Key: {item.Key}");
                    if (license.Key.Contains(item.Key))
                    {
                        appProduct.Purchased  = license.Value.IsActive;
                        appProduct.ExpiryDate = license.Value.ExpirationDate.DateTime;
                        Debug.WriteLine($"Licence Found - Purchased: {appProduct.Purchased} | Expiry Date: {appProduct.ExpiryDate}");
                        break;
                    }
                }

                // Finally, add to the main list
                Debug.WriteLine(appProduct);
                AppProducts.Add(appProduct);
            }

            InvokeAppProductsChanged();
        }
        public async override Task <AppProduct> PurchaseProduct(AppProduct appProduct)
        {
            if (appProduct == null)
            {
                Debug.WriteLine($"{nameof(appProduct)} is Null");
                throw new ArgumentNullException(nameof(appProduct));
                //return null;
            }

            await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                                                                                        async() =>
            {
                try
                {
                    StorePurchaseProperties properties = new StorePurchaseProperties(appProduct.Name);
                    var purchaseResult = await Context.RequestPurchaseAsync(appProduct.StoreID);

                    switch (purchaseResult.Status)
                    {
                    case StorePurchaseStatus.Succeeded:
                        Debug.WriteLine("User now owns this product");
                        MarkProductPurchased(appProduct);
                        break;

                    case StorePurchaseStatus.AlreadyPurchased:
                        Debug.WriteLine("User already owns this product");
                        MarkProductPurchased(appProduct);
                        break;

                    case StorePurchaseStatus.NotPurchased:
                        Debug.WriteLine("User chose to not purchase the product");
                        break;

                    case StorePurchaseStatus.NetworkError:
                        Debug.WriteLine("A network error occurred. Please try again later");
                        break;

                    case StorePurchaseStatus.ServerError:
                        Debug.WriteLine("A server error occurred. Please try again later");
                        break;

                    default:
                        Debug.WriteLine("An unknown response occurred. Please try again later");
                        break;
                    }
                }
                catch (Exception ex)
                {
                    DebugTools.PrintOutException(ex, $"Product Purchase Error ({appProduct.ProductID})");
                }
            });

            return(appProduct);
        }
        public async override Task <AppProduct> PurchaseProduct(AppProduct appProduct)
        {
            await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                                                                                        async() =>
            {
                var results = await Purchase(appProduct.ProductID);
                appProduct  = results.Item1;
            });

            return(appProduct);
        }
        public async override Task <AppProduct> UpdateProduct(AppProduct appProduct)
        {
            var kinds = GetProductKinds();

            // Get the Store product
            var result = await Context.GetAssociatedStoreProductsAsync(kinds.Select(x => x.ToString()));

            var storeProduct = result.Products.Where(x => x.Value.StoreId == appProduct.StoreID).FirstOrDefault().Value;

            if (storeProduct == null)
            {
                return(null);
            }

            // Get the License Information
            var appLicense = await Context.GetAppLicenseAsync();

            var storeProductLicense = appLicense.AddOnLicenses.Where(x => x.Value.InAppOfferToken == storeProduct.InAppOfferToken).FirstOrDefault().Value;

            if (storeProductLicense == null)
            {
                return(null);
            }

            // Update the product
            appProduct.Name                  = storeProduct.Title;
            appProduct.Description           = storeProduct.Description;
            appProduct.CurrentFormattedPrice = storeProduct.Price.FormattedPrice;
            appProduct.BaseFormattedPrice    = storeProduct.Price.FormattedBasePrice;
            appProduct.ExpiryDate            = storeProductLicense.ExpirationDate.DateTime;
            appProduct.Purchased             = storeProductLicense.IsActive;

            // Invoke Changed and Return
            Debug.WriteLine(appProduct);
            InvokeAppProductsChanged();
            return(appProduct);
        }
        public async Task <(AppProduct, PurchaseResults)> Purchase(string id)
        {
            var license = GetLicense(id);

            // If the product does not exist, exit with null
            if (license == null)
            {
                Debug.WriteLine("This product does not exist");
                throw new ArgumentException($"A product by ProductID of {id} does not exist", nameof(AppProduct.ProductID));
                //return (null, null);
            }

            // If the license is valid, but we don't have the product, create the product
            var product = GetAppProductByProductID(id);

            if (product == null)
            {
                product = new AppProduct(id, id, false);
                AppProducts.Add(product);
            }

            // If the license is already active, simply return the product
            if (license.IsActive)
            {
                Debug.WriteLine("User already owns this product");
                return(product, null);
            }

            try
            {
                PurchaseResults purchaseResults;

                if (DebugTools.DebugMode)
                {
                    Debug.WriteLine("Debug mode active, Simulating product purchase");
                    purchaseResults = await CurrentAppSimulator.RequestProductPurchaseAsync(id);

                    Debug.WriteLine("Finished Simulating");
                }
                else
                {
                    Debug.WriteLine("Requesting Product Purchase");
                    purchaseResults = await CurrentApp.RequestProductPurchaseAsync(id);

                    Debug.WriteLine("User finished interacting with purchase screen");
                }

                switch (purchaseResults.Status)
                {
                case ProductPurchaseStatus.AlreadyPurchased:
                    Debug.WriteLine("User already owns this product");
                    MarkProductPurchased(product);
                    break;

                case ProductPurchaseStatus.Succeeded:
                    Debug.WriteLine("User now owns this product");
                    MarkProductPurchased(product);
                    break;

                case ProductPurchaseStatus.NotPurchased:
                    Debug.WriteLine("User chose to not purchase the product");
                    product.Purchased = false;
                    if (DebugTools.DebugMode)
                    {
                        Debug.WriteLine("Simulating Purchase");
                        MarkProductPurchased(product);
                    }
                    break;

                case ProductPurchaseStatus.NotFulfilled:
                    Debug.WriteLine("A previous purchase was not fulfilled");
                    MarkProductPurchased(product);
                    break;

                default:
                    Debug.WriteLine("An unknown response occurred. Please try again later");
                    break;
                }

                return(product, purchaseResults);
            }
            catch (Exception ex)
            {
                DebugTools.PrintOutException(ex, $"Product Purchase Error ({id})");
            }

            return(product, null);
        }
 protected void MarkProductPurchased(AppProduct appProduct)
 {
     appProduct.Purchased = true;
     InvokeAppProductsChanged();
 }
 public abstract Task <AppProduct> PurchaseProduct(AppProduct appProduct);
 public abstract Task <AppProduct> UpdateProduct(AppProduct appProduct);