Jump to content

C# UWP Windows.Services.Store issues with checking Active Subscription

Hi guys,

 

So I've been trying C# UWP subscriptions in an app with Windows.Services.Store and whilst I can launch the purchase UI, I can't seem to get information about the Subscription such as if it is active or not.

 

The issue is that the StorePrice object is never set and the two methods never complete.

 

I've added the files required from the Windows SDK and .NET Core for UWP v4.5

 

I've removed the StoreID of the product in the code.

 

What's wrong with this code?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Services.Store;
using System.Runtime.InteropServices;
using System.Windows;

namespace ExampleDesktopBridge.UWPWrappers{
   public class SubscriptionWrapper{

        StorePrice subscriptionPrice;
     
      public StorePrice GetSubscriptionPrice(){
            return subscriptionPrice;
        }
     
      //Declare the IInitializeWithWindow interface in your app's code with the ComImport attribute
        [ComImport]
        [Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IInitializeWithWindow
        {
            void Initialize(IntPtr hwnd);
        }

        private StoreContext storeContext = StoreContext.GetDefault();
        StoreProduct subscriptionStoreProduct;
     	
      // Assign this variable to the Store ID of your subscription add-on.
        private string StoreId = "_________";
     
      public async Task<bool> CheckIfUserHasSubscriptionAsync(){
            StoreAppLicense appLicense = await storeContext.GetAppLicenseAsync();

          //  System.Threading.Thread.Sleep(5000);

            // Check if the customer has the rights to the subscription.
            foreach (var addOnLicense in appLicense.AddOnLicenses)
            {
                StoreLicense license = addOnLicense.Value;
                if (license.SkuStoreId.StartsWith(StoreId))
                {
                    if (license.IsActive)
                    {
                        // The expiration date is available in the license.ExpirationDate property.
                        return true;
                    }
                }
            }

            // The customer does not have a license to the subscription.
            return false;
        }

        public async Task<StoreProduct> GetSubscriptionProductAsync()
        {
            // Load the sellable add-ons for this app and check if the trial is still 
            // available for this customer. If they previously acquired a trial they won't 
            // be able to get a trial again, and the StoreProduct.Skus property will 
            // only contain one SKU.
            StoreProductQueryResult result =
                await storeContext.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;
            }

            // Look for the product that represents the subscription.
            foreach (var item in result.Products)
            {
                StoreProduct product = item.Value;
                subscriptionPrice = product.Price;
                if (product.StoreId == StoreId)
                {
                    return product;
                }
            }

            System.Diagnostics.Debug.WriteLine("The subscription was not found.");
            return null;
        }

Please help.

 

Thanks :).

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×