Files
Wino-Mail/Wino.Core.UWP/Services/StoreManagementService.cs

73 lines
2.2 KiB
C#
Raw Normal View History

2024-04-18 01:44:37 +02:00
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Services.Store;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Store;
2025-02-16 11:54:23 +01:00
namespace Wino.Core.UWP.Services;
public class StoreManagementService : IStoreManagementService
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
private StoreContext CurrentContext { get; }
private readonly Dictionary<StoreProductType, string> productIds = new Dictionary<StoreProductType, string>()
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
{ StoreProductType.UnlimitedAccounts, "UnlimitedAccounts" }
};
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private readonly Dictionary<StoreProductType, string> skuIds = new Dictionary<StoreProductType, string>()
{
{ StoreProductType.UnlimitedAccounts, "9P02MXZ42GSM" }
};
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public StoreManagementService()
{
CurrentContext = StoreContext.GetDefault();
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public async Task<bool> HasProductAsync(StoreProductType productType)
{
var productKey = productIds[productType];
var appLicense = await CurrentContext.GetAppLicenseAsync();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (appLicense == null)
return false;
2025-02-16 11:54:23 +01:00
// Access the valid licenses for durable add-ons for this app.
foreach (KeyValuePair<string, StoreLicense> item in appLicense.AddOnLicenses)
{
StoreLicense addOnLicense = item.Value;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (addOnLicense.InAppOfferToken == productKey)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
return addOnLicense.IsActive;
2024-04-18 01:44:37 +02:00
}
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
return false;
}
public async Task<Domain.Enums.StorePurchaseResult> PurchaseAsync(StoreProductType productType)
{
if (await HasProductAsync(productType))
return Domain.Enums.StorePurchaseResult.AlreadyPurchased;
else
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
var productKey = skuIds[productType];
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var result = await CurrentContext.RequestPurchaseAsync(productKey);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
switch (result.Status)
{
case StorePurchaseStatus.Succeeded:
return Domain.Enums.StorePurchaseResult.Succeeded;
case StorePurchaseStatus.AlreadyPurchased:
return Domain.Enums.StorePurchaseResult.AlreadyPurchased;
default:
return Domain.Enums.StorePurchaseResult.NotPurchased;
2024-04-18 01:44:37 +02:00
}
}
}
}