// This is the entry point method for the example.检查加载项信息,是否有可试用 public async Task SetupSubscriptionInfoAsync() { if (context == null) { context = StoreContext.GetDefault(); // If your app is a desktop app that uses the Desktop Bridge, you // may need additional code to configure the StoreContext object. // For more info, see https://aka.ms/storecontext-for-desktop. } //检查用户是否有订阅的许可证传给userOwnsSubscription值 userOwnsSubscription = await CheckIfUserHasSubscriptionAsync(); if (userOwnsSubscription) { Loading.IsActive = false; var messageDig = new MessageDialog("您已订阅此服务!"); //展示窗口,获取按钮是否退出 var result = await messageDig.ShowAsync(); // 解锁所有加载项订阅的特性功能 return; } //获取订阅信息.传给subscriptionStoreProduct值。 subscriptionStoreProduct = await GetSubscriptionProductAsync(); if (subscriptionStoreProduct == null) { Loading.IsActive = false; var messageDig = new MessageDialog("此订阅暂不提供!"); //展示窗口,获取按钮是否退出 var result = await messageDig.ShowAsync(); return; } //检查第一个SKU是否是试用版,并通知客户试用版可用。 //如果试用可用,Skus数组将始终具有两个可购买的SKU,以及 // first one is the trial. Otherwise,第一个是审判。否则,该数组将只有一个SKU。 StoreSku sku = subscriptionStoreProduct.Skus[0]; if (sku.SubscriptionInfo.HasTrialPeriod) { //如果存在试用 //您可以在这里向客户显示订阅购买信息。你可以使用 // sku.SubscriptionInfo.BillingPeriod and sku.SubscriptionInfo.BillingPeriodUnit //提供续约详情。 } else { //不存在试用 //您可以在这里向客户显示订阅购买信息。你可以使用 // sku.SubscriptionInfo.BillingPeriod and sku.SubscriptionInfo.BillingPeriodUnit //提供续约详情。 } // Prompt the customer to purchase the subscription. await PromptUserToPurchaseAsync(); }
/// <summary> /// /// </summary> /// <param name="subscriptionStoreId"></param> /// <returns></returns> private async Task <PurchaseResult> SetupSubscriptionInfoAsync(string subscriptionStoreId) { if (context == null) { context = StoreContext.GetDefault(); // If your app is a desktop app that uses the Desktop Bridge, you // may need additional code to configure the StoreContext object. // For more info, see https://aka.ms/storecontext-for-desktop. } bool userOwnsSubscription = await CheckIfUserHasActiveSubscriptionAsync(subscriptionStoreId); if (userOwnsSubscription) { // Unlock all the subscription add-on features here. return(new PurchaseResult() { PurchaseState = PurchaseState.Purchased, Sku = subscriptionStoreId }); } // Get the StoreProduct that represents the subscription add-on. await GetSubscriptionProductAsync(subscriptionStoreId); if (storeProduct == null) { return(new PurchaseResult() { PurchaseState = PurchaseState.Failed, Sku = subscriptionStoreId }); } // 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 = storeProduct.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. //} // Prompt the customer to purchase the subscription. return(await PromptUserToPurchaseAsync(sku)); }
public async Task SetupAdFreeSubscriptionInfoAsync() { if (storeContext == null) { storeContext = StoreContext.GetDefault(); //IInitializeWithWindow initWindow = (IInitializeWithWindow)(object)storeContext; //initWindow.Initialize(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle); } bool userOwnsSubscription = await CheckIfUserHasAdFreeSubscriptionAsync(); if (userOwnsSubscription) { // Unlock all the subscription add-on features here. return; } // Get the StoreProduct that represents the subscription add-on. subscriptionStoreProduct = await GetAdFreeSubscriptionProductAsync(); 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. } // Prompt the customer to purchase the subscription. await PromptUserToPurchaseAdFreeAsync(); }
/// <summary> /// Prompt for purchase popup /// </summary> /// <param name="sku"></param> /// <returns></returns> private async Task <PurchaseResult> PromptUserToPurchaseAsync(StoreSku sku) { string productId = sku.StoreId; // Request a purchase of the subscription product. If a trial is available it will be offered // to the customer. Otherwise, the non-trial SKU will be offered. StorePurchaseResult result = await storeProduct.RequestPurchaseAsync(); // Capture the error message for the operation, if any. string extendedError = string.Empty; if (result.ExtendedError != null) { extendedError = result.ExtendedError.Message; } return(result.Status switch { StorePurchaseStatus.Succeeded => new PurchaseResult() { PurchaseState = PurchaseState.Purchased, Sku = productId }, // Show a UI to acknowledge that the customer has purchased your subscription // and unlock the features of the subscription. StorePurchaseStatus.NotPurchased => new PurchaseResult() { PurchaseState = PurchaseState.Failed, Sku = productId }, StorePurchaseStatus.ServerError => new PurchaseResult() { PurchaseState = PurchaseState.Failed, Sku = productId }, StorePurchaseStatus.NetworkError => new PurchaseResult() { PurchaseState = PurchaseState.Failed, Sku = productId }, StorePurchaseStatus.AlreadyPurchased => new PurchaseResult() { PurchaseState = PurchaseState.Purchased, Sku = productId }, _ => new PurchaseResult() { PurchaseState = PurchaseState.Failed, Sku = productId }, });