UnityPurchasing.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine.Purchasing.Extension;
  5. namespace UnityEngine.Purchasing
  6. {
  7. /// <summary>
  8. /// The core abstract implementation for Unity Purchasing.
  9. /// </summary>
  10. public abstract class UnityPurchasing
  11. {
  12. /// <summary>
  13. /// The main initialization call for Unity Purchasing.
  14. /// </summary>
  15. /// <param name="listener"> The <c>IStoreListener</c> to receive callbacks for future transactions </param>
  16. /// <param name="builder"> The <c>ConfigurationBuilder</c> containing the product definitions mapped to stores </param>
  17. public static void Initialize(IStoreListener listener, ConfigurationBuilder builder)
  18. {
  19. Initialize(listener, builder, UnityEngine.Debug.unityLogger, Application.persistentDataPath, new UnityAnalytics(), builder.factory.GetCatalogProvider());
  20. }
  21. /// <summary>
  22. /// This is useful in certain test scenarios, such as repeatedly testing
  23. /// an App's behaviour when purchases are restored.
  24. ///
  25. /// This is a static method since developers may wish to clear the log before
  26. /// initialising IAP.
  27. /// </summary>
  28. public static void ClearTransactionLog()
  29. {
  30. var log = new TransactionLog(UnityEngine.Debug.unityLogger, Application.persistentDataPath);
  31. log.Clear();
  32. }
  33. /// <summary>
  34. /// Created for integration testing.
  35. /// </summary>
  36. internal static void Initialize(IStoreListener listener, ConfigurationBuilder builder,
  37. ILogger logger, string persistentDatapath, IUnityAnalytics analytics, ICatalogProvider catalog)
  38. {
  39. var transactionLog = new TransactionLog(logger, persistentDatapath);
  40. var manager = new PurchasingManager(transactionLog, logger, builder.factory.service, builder.factory.storeName);
  41. var analyticsReporter = new AnalyticsReporter(analytics);
  42. // Proxy the PurchasingManager's callback interface to forward Transactions to Analytics.
  43. var proxy = new StoreListenerProxy(listener, analyticsReporter, builder.factory);
  44. FetchAndMergeProducts(builder.useCatalogProvider, builder.products, catalog, response =>
  45. {
  46. manager.Initialize(proxy, response);
  47. });
  48. }
  49. internal static void FetchAndMergeProducts(bool useCatalog,
  50. HashSet<ProductDefinition> localProductSet, ICatalogProvider catalog, Action<HashSet<ProductDefinition>> callback)
  51. {
  52. if (useCatalog && catalog != null)
  53. {
  54. catalog.FetchProducts(cloudProducts => {
  55. var updatedProductSet = new HashSet<ProductDefinition>(localProductSet);
  56. foreach (var product in cloudProducts)
  57. {
  58. // Products are hashed by id, so this should remove the local product with the same id before adding the cloud product
  59. updatedProductSet.Remove(product);
  60. updatedProductSet.Add(product);
  61. }
  62. callback(updatedProductSet);
  63. });
  64. }
  65. else
  66. {
  67. callback(localProductSet);
  68. }
  69. }
  70. }
  71. }