Search code examples
c#windows-store-appssubscriptionadd-on

How to determine whether add-on subscription of Windows Store app is in trial period or in paid period


I am working on adding add-on subscription to my Desktop Bridge app published in Windows store using Windows.Services.Store APIs.

I created a test add-on of subscription for 3 month period with 1 week trial period. I can get a StoreAppLicence instance by StoreContext.GetAppLicenseAsync method calling from my app and then from its AddOnLicenses property, find a StoreLicense instance whose SkuStoreId matches the StoreId of the test add-on in the beginning. But there is no clue whether this subscription is in trial period or in paid (full) period because it does not have IsTrial property like StoreAppLicence.

So I would like to how to determine whether the subscription is in trial period or in paid period for showing my users the status of subcription in my app.

Update

I was not clear enough but I am asking about the case after the current user has "purchased" add-on subscription as free trial. I would like to know how to determine whether the trial period has not finished yet or the trial period has passed already and the subscription has moved to paid (full) period.

Probably I can achieve it by storing the data on when the user "purchased" the subscription in local and comparing it with the current date but it seems not ideal because there might be a chance of inconsistency with the data managed by Windows Store server.


Solution

  • Probably I found a solution.

    IsTrial property of StoreCollectionData obtained by StoreContext.GetUserCollectionAsync method provides the information what I need. In addition, the StoreCollectionData also includes AcquiredDate property which indicates the purchase date of subscription add-on and is useful to calculate the expiration date on your own. In my experience, ExpirationDate property of StoreLicense obtained by StoreContext.GetAppLicenseAsync method seems not accurate (3 days later than actual expiration date).

    The sample code would be as follows.

    public enum LicenseStatus
    {
        Unknown = 0,
        Trial,
        Full
    }
    
    private static StoreContext _context;
    
    public static async Task<(string storeId, LicenseStatus status, DateTimeOffset acquiredDate)[]> GetSubscriptionAddonStatusesAsync()
    {
        if (_context is null)
            _context = StoreContext.GetDefault();
    
        StoreProductQueryResult queryResult = await _context.GetUserCollectionAsync(new[] { "Durable" });
        if (queryResult.ExtendedError != null)
            throw queryResult.ExtendedError;
    
        IEnumerable<(string, LicenseStatus, DateTimeOffset)> Enumerate()
        {
            foreach (KeyValuePair<string, StoreProduct> pair in queryResult.Products)
            {
                StoreSku sku = pair.Value.Skus.FirstOrDefault();
                StoreCollectionData data = sku?.CollectionData;
                if (data != null)
                {
                    LicenseStatus status = data.IsTrial ? LicenseStatus.Trial : LicenseStatus.Full;
                    yield return (pair.Key, status, data.AcquiredDate);
                }
            }
        }
    
        return Enumerate().ToArray();
    }
    

    On the other hand, there still exists a weird thing on StoreContext.GetUserCollectionAsync method. It only provides the information on the latest add-on while from its explanation, it should provide the information on all add-ons. Therefore, if you want to check multiple add-ons, this method will not be enough.