How to Implement In-App Purchases in Windows 10 Apps?

前端 未结 2 636
暗喜
暗喜 2020-12-31 19:35

I want to integrate in-app purchasing in my windows universal app. I do the following thing before coding.

  • Make App on Windows Dev Center

  • Ad

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-31 20:10

    WindowsStoreProxy.xml to c# code and serialize to xml file

    public static CurrentApp LoadCurrentApp(string productKey = "Premium", bool isActive = false, bool isTrial = false)
        {
            CurrentApp currentApp = new CurrentApp();
            currentApp.ListingInformation = new ListingInformation()
            {
                App = new App()
                {
                    AgeRating = "3",
                    AppId = BasicAppInfo.AppId,
                    CurrentMarket = "en-us",
                    LinkUri = "",
                    MarketData = new MarketData()
                    {
                        Name = "In-app purchases",
                        Description = "AppDescription",
                        Price = "5.99",
                        CurrencySymbol = "$",
                        CurrencyCode = "USD",
                    }
                },
                Product = new Product()
                {
                    ProductId = productKey,
                    MarketData = new MarketData()
                    {
                        Lang = "en-us",
                        Name = productKey,
                        Description = "AppDescription",
                        Price = "5.99",
                        CurrencySymbol = "$",
                        CurrencyCode = "USD",
                    }
                }
            };
            currentApp.LicenseInformation = new LicenseInformation()
            {
                App = new App()
                {
                    IsActive = isActive.ToString(),
                    IsTrial = isTrial.ToString(),
                },
                Product = new Product()
                {
                    ProductId = productKey,
                    IsActive = isActive.ToString(),
                }
            };
            return currentApp;
        }
    

    Base xml model

    [XmlRoot(ElementName = "MarketData")]
    public class MarketData
    {
        [XmlElement(ElementName = "Name")]
        public string Name { get; set; }
        [XmlElement(ElementName = "Description")]
        public string Description { get; set; }
        [XmlElement(ElementName = "Price")]
        public string Price { get; set; }
        [XmlElement(ElementName = "CurrencySymbol")]
        public string CurrencySymbol { get; set; }
        [XmlElement(ElementName = "CurrencyCode")]
        public string CurrencyCode { get; set; }
        [XmlAttribute(AttributeName = "lang", Namespace = "http://www.w3.org/XML/1998/namespace")]
        public string Lang { get; set; }
    }
    
    [XmlRoot(ElementName = "App")]
    public class App
    {
        [XmlElement(ElementName = "AppId")]
        public string AppId { get; set; }
        [XmlElement(ElementName = "LinkUri")]
        public string LinkUri { get; set; }
        [XmlElement(ElementName = "CurrentMarket")]
        public string CurrentMarket { get; set; }
        [XmlElement(ElementName = "AgeRating")]
        public string AgeRating { get; set; }
        [XmlElement(ElementName = "MarketData")]
        public MarketData MarketData { get; set; }
        [XmlElement(ElementName = "IsActive")]
        public string IsActive { get; set; }
        [XmlElement(ElementName = "IsTrial")]
        public string IsTrial { get; set; }
    }
    
    [XmlRoot(ElementName = "Product")]
    public class Product
    {
        [XmlElement(ElementName = "MarketData")]
        public MarketData MarketData { get; set; }
        [XmlAttribute(AttributeName = "ProductId")]
        public string ProductId { get; set; }
        [XmlElement(ElementName = "IsActive")]
        public string IsActive { get; set; }
    }
    
    [XmlRoot(ElementName = "ListingInformation")]
    public class ListingInformation
    {
        [XmlElement(ElementName = "App")]
        public App App { get; set; }
        [XmlElement(ElementName = "Product")]
        public Product Product { get; set; }
    }
    
    [XmlRoot(ElementName = "LicenseInformation")]
    public class LicenseInformation
    {
        [XmlElement(ElementName = "App")]
        public App App { get; set; }
        [XmlElement(ElementName = "Product")]
        public Product Product { get; set; }
    }
    
    [XmlRoot(ElementName = "CurrentApp")]
    public class CurrentApp
    {
        [XmlElement(ElementName = "ListingInformation")]
        public ListingInformation ListingInformation { get; set; }
        [XmlElement(ElementName = "LicenseInformation")]
        public LicenseInformation LicenseInformation { get; set; }
    }
    

    Get XmlFile

    public async static Task GetWindowsStoreProxyXmlAsync(string productKey, bool isActive = false, bool isTrial = false)
        {
            StorageFile xmlFile = null;
            var currentApp = LoadCurrentApp(productKey, isActive, isTrial);
            var xml = StorageHelper.SerializeToXML(currentApp);
            if (!string.IsNullOrEmpty(xml))
            {
                xmlFile = await StorageHelper.LocalFolder.CreateFileAsync("MarketData.xml", CreationCollisionOption.ReplaceExisting);
                await FileIO.WriteTextAsync(xmlFile, xml);
            }
            return xmlFile;
        }
    

提交回复
热议问题