/// <summary> /// Populates the Mock Library with IAP items defined in XML /// </summary> /// <param name="Xml">XML to parse</param> public static void PopulateIAPItemsFromXml(string Xml) { //MessageBox.Show(Xml); CheckIfInitialized(); try { XElement xmlDoc = XElement.Parse(Xml); foreach (XElement element in xmlDoc.Elements()) { var pl = new ProductListing(); pl.Name = element.Element("Name").Value; pl.ProductId = element.Element("ProductId").Value; pl.Description = element.Element("Description").Value; pl.FormattedPrice = element.Element("FormattedPrice").Value; string uri = element.Element("ImageUri").Value; pl.ImageUri = string.IsNullOrEmpty(uri) ? null : new Uri(uri); pl.ProductType = (ProductType)Enum.Parse(typeof(ProductType), element.Element("ProductType").Value); string keywords = element.Element("Keywords").Value; pl.Keywords = string.IsNullOrEmpty(keywords) ? null : keywords.Split(';'); pl.Tag = element.Element("Tag").Value; if (pl.Tag.Length > 3000) { throw new Exception("Data stored in the 'Tag' can not exceed 3000 characters!"); } AddProductListing(element.Attribute("Key").Value, pl); bool purchased = bool.Parse(element.Attribute("Purchased").Value); bool fulfilled = bool.Parse(element.Attribute("Fulfilled").Value); if (!purchased && fulfilled) { throw new InvalidOperationException("Error in your XML definition: An item can't be marked as fulfilled but not purchased! Item: " + pl.Name); } if (purchased) { var store = new MockReceiptStore(); store.SaveReceipt(pl, false); StateInformation.SetState(pl.ProductId, fulfilled); _appLicenseInformation.ProductLicenses[pl.ProductId].IsActive = true; } } } catch (Exception ex) { // MessageBox.Show("load mock list error:" + ex.Message); } //MessageBox.Show("success"); }
internal static string SimulatePurchase(string ProductId, bool includeReceipt) { ProductListing listing = allProducts.Values.FirstOrDefault(p => p.ProductId.Equals(ProductId, StringComparison.InvariantCultureIgnoreCase)); if (listing == null) { throw new ArgumentException("Specified productId has no ProductListing"); } string receipt = string.Empty; bool OkToPurchase = true; bool?state = StateInformation.GetState(listing.ProductId); if (state != null && state.Value == false) { // This is an unfulfiled item MessageBox.Show("You have already purchased this but not fulfiled it yet"); OkToPurchase = false; } else { var rs = new MockReceiptStore(); if (listing.ProductType == ProductType.Durable && rs.EnumerateReceipts().ContainsKey(listing.ProductId)) { MessageBox.Show("You already purchased this durable"); OkToPurchase = false; } } if (OkToPurchase) { MessageBoxResult result = MessageBox.Show(string.Format("Simulating purchase. Do you want to buy this item ({0})?", listing.Name), "Mock UI", MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { var store = new MockReceiptStore(); receipt = store.SaveReceipt(listing, includeReceipt); StateInformation.SetState(listing.ProductId, listing.ProductType == ProductType.Durable); // Set as fulfilled for Durables only _appLicenseInformation.ProductLicenses[listing.ProductId].IsActive = true; } } return(receipt); }