Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Purchasing;
  5.  
  6. // Placing the Purchaser class in the CompleteProject namespace allows it to interact with ScoreManager,
  7. // one of the existing Survival Shooter scripts.
  8.  
  9.     // Deriving the Purchaser class from IStoreListener enables it to receive messages from Unity Purchasing.
  10.     public class subscribe : MonoBehaviour, IStoreListener
  11.     {
  12.         private static IStoreController m_StoreController;          // The Unity Purchasing system.
  13.         private static IExtensionProvider m_StoreExtensionProvider; // The store-specific Purchasing subsystems.
  14.     //must be saved in memory
  15.         private long lastTimeFromPay;
  16.         private long lastTimeEnterInGame;
  17.         private bool canBuySubscription = true;
  18.                                     // Product identifiers for all products capable of being purchased:
  19.                                     // "convenience" general identifiers for use with Purchasing, and their store-specific identifier
  20.                                     // counterparts for use with and outside of Unity Purchasing. Define store-specific identifiers
  21.                                     // also on each platform's publisher dashboard (iTunes Connect, Google Play Developer Console, etc.)
  22.  
  23.     // General product identifiers for the consumable, non-consumable, and subscription products.
  24.     // Use these handles in the code to reference which product to purchase. Also use these values
  25.     // when defining the Product Identifiers on the store. Except, for illustration purposes, the
  26.     // kProductIDSubscription - it has custom Apple and Google identifiers. We declare their store-
  27.     // specific mapping to Unity Purchasing's AddProduct, below.
  28.     public static string kProductIDSubscription = "subscription";
  29.  
  30.         // Apple App Store-specific product identifier for the subscription product.
  31.         private static string kProductNameAppleSubscription = "com.unity3d.subscription.new";
  32.  
  33.         // Google Play Store-specific product identifier subscription product.
  34.         private static string kProductNameGooglePlaySubscription = "com.unity3d.subscription.original";
  35.         void Update()
  36.         {
  37.         if (Input.GetKeyDown(KeyCode.W))
  38.         {
  39.             InitializePurchasing();
  40.         }
  41.         if (Input.GetKeyDown(KeyCode.C))
  42.         {
  43.             InitializePurchasing();
  44.         }
  45.     }
  46.         void Start()
  47.         {
  48.         if(canBuySubscription==false)
  49.             ResultFromSubscribe();
  50.         }
  51.         public void ResultFromSubscribe()
  52.         {
  53.             if(lastTimeEnterInGame-lastTimeFromPay>=30)
  54.             {
  55.             lastTimeEnterInGame = 0;
  56.             lastTimeFromPay = 0;
  57.             }
  58.             else
  59.             {
  60.             lastTimeEnterInGame = System.DateTime.Now.Day;
  61.             }
  62.         }
  63.         public void InitializePurchasing()
  64.         {
  65.             // If we have already connected to Purchasing ...
  66.             if (IsInitialized())
  67.             {
  68.                 // ... we are done here.
  69.                 return;
  70.             }
  71.  
  72.             // Create a builder, first passing in a suite of Unity provided stores.
  73.             var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
  74.  
  75.             // Add a product to sell / restore by way of its identifier, associating the general identifier
  76.             // with its store-specific identifiers.
  77.             // And finish adding the subscription product. Notice this uses store-specific IDs, illustrating
  78.             // if the Product ID was configured differently between Apple and Google stores. Also note that
  79.             // one uses the general kProductIDSubscription handle inside the game - the store-specific IDs
  80.             // must only be referenced here.
  81.             builder.AddProduct(kProductIDSubscription, ProductType.Subscription, new IDs(){
  82.                 { kProductNameAppleSubscription, AppleAppStore.Name },
  83.                 { kProductNameGooglePlaySubscription, GooglePlay.Name },
  84.             });
  85.  
  86.             // Kick off the remainder of the set-up with an asynchrounous call, passing the configuration
  87.             // and this class' instance. Expect a response either in OnInitialized or OnInitializeFailed.
  88.             UnityPurchasing.Initialize(this, builder);
  89.         }
  90.    
  91.         private bool IsInitialized()
  92.         {
  93.             // Only say we are initialized if both the Purchasing references are set.
  94.             return m_StoreController != null && m_StoreExtensionProvider != null;
  95.         }
  96.  
  97.         public void BuySubscription()
  98.         {
  99.             // Buy the subscription product using its the general identifier. Expect a response either
  100.             // through ProcessPurchase or OnPurchaseFailed asynchronously.
  101.             // Notice how we use the general product identifier in spite of this ID being mapped to
  102.             // custom store-specific identifiers above.
  103.             BuyProductID(kProductIDSubscription);
  104.         }
  105.  
  106.  
  107.         void BuyProductID(string productId)
  108.         {
  109.             // If Purchasing has been initialized ...
  110.             if (IsInitialized())
  111.             {
  112.                 // ... look up the Product reference with the general product identifier and the Purchasing
  113.                 // system's products collection.
  114.                 Product product = m_StoreController.products.WithID(productId);
  115.  
  116.                 // If the look up found a product for this device's store and that product is ready to be sold ...
  117.                 if (product != null && product.availableToPurchase)
  118.                 {
  119.                     Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
  120.                     // ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed
  121.                     // asynchronously.
  122.                     m_StoreController.InitiatePurchase(product);
  123.                 }
  124.                 // Otherwise ...
  125.                 else
  126.                 {
  127.                 // ... report the product look-up failure situation  
  128.                     Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
  129.                 }
  130.             }
  131.             // Otherwise ...
  132.             else
  133.             {
  134.                 // ... report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or
  135.                 // retrying initiailization.
  136.                 Debug.Log("BuyProductID FAIL. Not initialized.");
  137.             }
  138.         }
  139.  
  140.  
  141.         // Restore purchases previously made by this customer. Some platforms automatically restore purchases, like Google.
  142.         // Apple currently requires explicit purchase restoration for IAP, conditionally displaying a password prompt.
  143.  
  144.         //  
  145.         // --- IStoreListener
  146.         //
  147.  
  148.         public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
  149.         {
  150.             // Purchasing has succeeded initializing. Collect our Purchasing references.
  151.             Debug.Log("OnInitialized: PASS");
  152.  
  153.             // Overall Purchasing system, configured with products for this application.
  154.             m_StoreController = controller;
  155.             // Store specific subsystem, for accessing device-specific store features.
  156.             m_StoreExtensionProvider = extensions;
  157.  
  158.             BuySubscription();
  159.         }
  160.  
  161.  
  162.         public void OnInitializeFailed(InitializationFailureReason error)
  163.         {
  164.             // Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.
  165.             Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
  166.         }
  167.  
  168.  
  169.         public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
  170.         {
  171.         if (canBuySubscription)
  172.         {
  173.             if (String.Equals(args.purchasedProduct.definition.id, kProductIDSubscription, StringComparison.Ordinal))
  174.             {
  175.                 // TODO: The subscription item has been successfully purchased, grant this to the player.
  176.                 print("PAY IS PASSED!");
  177.                 lastTimeFromPay = System.DateTime.Now.Day;
  178.                 lastTimeEnterInGame = System.DateTime.Now.Day;
  179.                 canBuySubscription = false;
  180.                 ResultFromSubscribe();
  181.             }
  182.             // Or ... an unknown product has been purchased by this user. Fill in additional products here....
  183.             else
  184.             {
  185.                 Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
  186.             }
  187.         }
  188.         else
  189.         {
  190.             print("ASASASASASASASASASASASASASASASASAA");
  191.         }
  192.      
  193.         // Return a flag indicating whether this product has completely been received, or if the application needs
  194.         // to be reminded of this purchase at next app launch. Use PurchaseProcessingResult.Pending when still
  195.         // saving purchased products to the cloud, and when that save is delayed.
  196.         return PurchaseProcessingResult.Complete;
  197.     }
  198.         public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
  199.         {
  200.             // A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing
  201.             // this reason with the user to guide their troubleshooting actions.
  202.             Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
  203.         }
  204.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement