Skip to content

Instantly share code, notes, and snippets.

@igorkulman
Last active December 24, 2016 14:58
Show Gist options
  • Save igorkulman/9571908 to your computer and use it in GitHub Desktop.
Save igorkulman/9571908 to your computer and use it in GitHub Desktop.
using MockIAPLib;
using Store = MockIAPLib;
namespace YourAPP
{
/// <summary>
/// Service for mocking in-app purchases in debug mode
/// </summary>
public class MockWindowsPhoneStoreService: IWindowsPhoneStoreService
{
/// <summary>
/// Checks if a product is purchased
/// </summary>
/// <param name="productId">Product id</param>
/// <returns>True if the product is purchased</returns>
public bool IsPurchased(string productId)
{
if (String.IsNullOrEmpty(productId)) return false;
var licenseInformation = CurrentApp.LicenseInformation;
return licenseInformation.ProductLicenses[productId].IsActive;
}
/// <summary>
/// Tries to purchase a product
/// </summary>
/// <param name="productId">Product id</param>
/// <returns>True on success, false otherwise</returns>
public async Task<bool> Purchase(string productId)
{
if (String.IsNullOrEmpty(productId))
{
return false;
}
try
{
await CurrentApp.RequestProductPurchaseAsync(productId, false);
try
{
var licenses = CurrentApp.LicenseInformation.ProductLicenses;
if (licenses[productId].IsConsumable && licenses[productId].IsActive)
{
CurrentApp.ReportProductFulfillment(productId);
}
}
catch (Exception e)
{
}
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// Gets the price of a product
/// </summary>
/// <param name="productId">Product id</param>
/// <returns>Product price</returns>
public async Task<string> GetPrice(string productId)
{
try
{
var products = await CurrentApp.LoadListingInformationAsync();
var product = products.ProductListings.SingleOrDefault(l => l.Value.ProductId == productId);
if (product.Value == null) return string.Empty;
return product.Value.FormattedPrice;
}
catch
{
return string.Empty;
}
}
}
}
#if DEBUG
container.RegisterSingleton(typeof(IWindowsPhoneStoreService), "windowsPhoneStoreService", typeof(MockWindowsPhoneStoreService));
#else
container.RegisterSingleton(typeof(IWindowsPhoneStoreService), "windowsPhoneStoreService", typeof(WindowsPhoneStoreService));
#endif
/// <summary>
/// Interface for Windows Phone Store service
/// </summary>
public interface IWindowsPhoneStoreService
{
/// <summary>
/// Checks if a product is purchased
/// </summary>
/// <param name="productId">Product id</param>
/// <returns>True if the product is purchased</returns>
bool IsPurchased(string productId);
/// <summary>
/// Tries to purchase a product
/// </summary>
/// <param name="productId">Product id</param>
/// <returns>True on success, false otherwise</returns>
Task<bool> Purchase(string productId);
/// <summary>
/// Gets the price of a product
/// </summary>
/// <param name="productId">Product id</param>
/// <returns>Product price</returns>
Task<string> GetPrice(string productId);
}
private void SetupMockIAP()
{
MockIAP.Init();
MockIAP.RunInMockMode(true);
MockIAP.SetListingInformation(1, "en-us", "A description", "1", "TestApp");
// Add some more items manually.
ProductListing p = new ProductListing
{
Name = "img.2",
ImageUri = new Uri("/Res/Image/2.jpg", UriKind.Relative),
ProductId = "img.2",
ProductType = Windows.ApplicationModel.Store.ProductType.Durable,
Keywords = new string[] { "image" },
Description = "An image",
FormattedPrice = "1.0",
Tag = string.Empty
};
MockIAP.AddProductListing("img.2", p);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment