[英]How to determine whether add-on subscription of Windows Store app is in trial period or in paid period
我正在使用Windows.Services.Store API向Windows商店中發布的Desktop Bridge應用程序添加附加訂閱。
我創建了3個月訂閱期和1周試用期的訂閱測試附件。 我可以得到一個StoreAppLicence通過實例StoreContext.GetAppLicenseAsync
方法從我的應用程序調用,然后從AddOnLicenses
屬性,找到一個StoreLicense實例,其SkuStoreId
的STOREID匹配測試插件在開始。 但是無法確定此訂閱是處於試用期還是處於付費(完整)期,因為它沒有像StoreAppLicence這樣的IsTrial
屬性。
因此,我想確定訂閱是處於試用期還是處於付費期,以便向用戶顯示我的應用程序中的訂閱狀態。
更新
我還不太清楚,但是我正在詢問當前用戶在“購買”了附加訂閱作為免費試用后的情況。 我想知道如何確定試用期是否尚未結束或試用期是否已過,並且訂閱已移至付費(完整)期。
大概可以通過在用戶在本地“購買”訂閱時存儲數據,並將其與當前日期進行比較來實現此目的,但這似乎並不理想,因為可能與Windows Store服務器管理的數據不一致。
C#示例:
subscriptionStoreProduct = await GetSubscriptionProductAsync();
if (subscriptionStoreProduct == null)
{
return;
}
// Check if the first SKU is a trial and notify the customer that a trial is available.
// If a trial is available, the Skus array will always have 2 purchasable SKUs and the
// first one is the trial. Otherwise, this array will only have one SKU.
StoreSku sku = subscriptionStoreProduct.Skus[0];
if (sku.SubscriptionInfo.HasTrialPeriod)
{
// You can display the subscription trial info to the customer here. You can use
// sku.SubscriptionInfo.TrialPeriod and sku.SubscriptionInfo.TrialPeriodUnit
// to get the trial details.
}
else
{
// You can display the subscription purchase info to the customer here. You can use
// sku.SubscriptionInfo.BillingPeriod and sku.SubscriptionInfo.BillingPeriodUnit
// to provide the renewal details.
}
從樣本購買訂閱附件和Class StoreSku Class派生
更新
如果用戶可以使用訂閱,則只有兩種可能性,一種是試用期,另一種是他們已經購買。 代碼中的方法( GetSubscriptionProductAsync )獲取用戶可以使用的訂閱,您可以在示例( 購買Cubscription附加組件 )中看到詳細信息。 屬性HasTrialPeriod獲取一個值,該值指示預訂是否包含試用期。
可能我找到了解決方案。
IsTrial
財產StoreCollectionData所獲得StoreContext.GetUserCollectionAsync
方法提供了我需要的信息。 此外,StoreCollectionData還包括AcquiredDate
屬性,該屬性指示訂閱附件的購買日期,並且對自己計算失效日期很有用。 以我的經驗, StoreContext.GetAppLicenseAsync
方法獲得的StoreLicense的ExpirationDate
屬性似乎不准確(比實際到期日期晚3天)。
示例代碼如下。
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();
}
另一方面, StoreContext.GetUserCollectionAsync
方法上仍然存在怪異的東西。 它僅提供有關最新加載項的信息,而從其解釋中,應提供有關所有加載項的信息。 因此,如果您要檢查多個加載項,此方法將是不夠的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.