Search code examples
c#windows-storedesktop-bridgeavaloniauimicrosoft-store

In app purchasing with Microsoft Store in an Avalonia app


I'm trying to implement in app purchasing for an Avalonia application. I am using the Windows.Services.Store namespace. Whenever I try to use the GetAssociatedStoreProductsAsync method to get information about the product, I get an error code in the ExtendedError of the StoreProductyQueryResult of 0x803F6107.

The application is an Avalonia app with a few projects. In addition to the application projects, there is a Windows Application Packaging Project.

  • I have associated the app's packaging project with the store.
  • I have created and had approved the store listing for the app.
  • I have downloaded the app from the store and run it (this is supposed to get the license info I need to run the app in development according to this page: https://learn.microsoft.com/en-au/windows/uwp/monetize/in-app-purchases-and-trials#testing)
  • I have created and had approved a subscription addon.
  • The packaging project is the startup project for the solution.
  • I have deployed the app (right click the packaging project and press deploy) and then run using visual studio's standard Start Debugging.

Now, some code! Here is where I set up the store context - notice that I am using the Avalonia specific method for getting the window handle - I am unsure if this is the correct method in this context (though I have used this method in other parts of the application to get a handle to successfully launch windows printer driver settings windows)

            if (storeContext == null)
            {
                storeContext = StoreContext.GetDefault();
                _context = storeContext;
            }
            var window = Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop ? desktop.MainWindow : null;
            var handle = window.PlatformImpl.Handle.Handle;
            WinRT.Interop.InitializeWithWindow.Initialize(_context, handle);

Here is a function I am trying to use to check if the license is valid:

       public async Task<bool> CheckIfUserHasSubscriptionAsync()
        {
            StoreAppLicense appLicense = await _context.GetAppLicenseAsync();

            foreach (var addOnLicense in appLicense.AddOnLicenses)
            {
                StoreLicense license = addOnLicense.Value;
                if (license.SkuStoreId.StartsWith(_subscriptionStoreId))
                {
                    if (license.IsActive)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

When running this function, the length of AddOnLicenses is always 0 and the function goes straight to the return false.

Here is the function I am trying to use to get the list of products:

        public async Task<IEnumerable<StoreProduct>> GetSubscriptionProductsAsync()
        {
            StoreProductQueryResult result =
                await _context.GetAssociatedStoreProductsAsync(new string[] { "Durable" });

            if (result.ExtendedError != null)
            {
                System.Diagnostics.Debug.WriteLine("Something went wrong while getting the add-ons. " +
                    "ExtendedError:" + result.ExtendedError);
                return null;
            }

            return result.Products.Values;
        }

This function returns null with Extended error having a value of 0x803F6107.

Is anyone able to shed any light on this issue?


Solution

  • OK - what finally solved it was going through the wapproj file and comparing line by line against the wapproj file of the Avalonia test project I had created. The original project had in a previous life been deployed using sideloading and had certificates etc. left in the config from that. I removed all of those and set it to use temporary store generated certificates instead. Then I downloaded the app from the store and ran it once before doing a deploy and run of the app from visual studio. This time, the store context was populated correctly and the errors are gone! Phew!