Search Unity

[Solved] How to handle a second purchase attempt of a non-consumable product

Discussion in 'Unity IAP' started by TGKG, Jun 22, 2017.

Thread Status:
Not open for further replies.
  1. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    180
    I have consumable and non-consumable and subscription product ID's in my android store.

    During the "builder.AddProduct" I have identified the non-consumables as "ProductType.NonConsumable". What does the Unity IAP do with the ProductType.NonConsumable information?

    What happens if the player tries to purchase the non-consumable product a second time? Does the Unity IAP trigger something that indicates that this non-consumable has already been purchased?

    Code (CSharp):
    1.  
    2. //Initializes the IAP builder, adds products that are available for sale and supplies a listener to handle purchasing events.
    3.     public void InitializePurchasing()
    4.     {
    5.        // If we have already connected to Purchasing ...
    6.         if (IsInitialized())
    7.         {
    8.             // ... we are done here.
    9.             return;
    10.         }
    11.         // Create a builder
    12.         var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    13.  
    14.         // Add a product to sell by way of its identifier, associating the general identifier with its store-specific identifiers.
    15.         //*********** CONSUMABLE PRODUCTS ******************      
    16.         builder.AddProduct(Product_100_coin, ProductType.Consumable);
    17.         builder.AddProduct(Product_500_coin, ProductType.Consumable);
    18.         builder.AddProduct(Product_1000_coin, ProductType.Consumable);
    19.         builder.AddProduct(Product_10_scatter, ProductType.Consumable);
    20.         builder.AddProduct(Product_5_splash, ProductType.Consumable);
    21.  
    22.        
    23.         //*********** NON CONSUMABLE PRODUCTS ******************
    24.         builder.AddProduct(Product_plane_jumbo, ProductType.NonConsumable)
    25.  
    26.         //*********** SUBSCRIPTION PRODUCTS ******************
    27.         builder.AddProduct(Product_Level5, ProductType.Subscription);
    28.        
    29.         // Kick off the remainder of the set-up with an asynchrounous call, passing the configuration
    30.         // and this class' instance. Expect a response either in OnInitialized or OnInitializeFailed.
    31.         UnityPurchasing.Initialize(this, builder);
    32.     }
    33.  
     
  2. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,519
    @TGKG

    Unity IAP does not have any inventory management features, so it would be best for you to manage that on your own. (e.g. disable buttons for items that can no longer be purchased.)

    I did a quick test on Android and it looks like trying to purchases a non-consumable twice will result in a purchase failure with the reason DuplicatePurchase. (This may be different on different platforms.)
     
  3. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    180
    So please explain the purpose of identifying the product as "ProductType.NonConsumable". (ie what does Unity do with that information)
     
  4. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    180
    Thank you for your quick check about trying to purchase a non-consumable twice.

    I guess I can either keep track of all of the non-consumables that a player has purchased (and save that info in a secure data file) OR retrieve that information from the Android store each time the game is started.

    So, is there a way to get from the Play store a list of non-consumables that a particular player has already purchased?
     
  5. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,519
    After you have initialized the purchase system, you can loop through all the products in the controller.
    This code should work for Android:
    Code (CSharp):
    1. public void OnInitialized(IStoreController controller, IExtensionProvider extensions) {
    2.     Debug.Log("Initialized");
    3.     this.controller = controller;
    4.     this.extensions = extensions;
    5.  
    6.     foreach (Product p in controller.products.all) {
    7.         //Non-consumables that have been purchased, should have the purchase info in the receipt.
    8.         Debug.Log(p.receipt);
    9.     }
    10. }
    For iOS, you can parse the App Receipt. There's an example at the bottom of this page:
    https://docs.unity3d.com/Manual/UnityIAPValidatingReceipts.html
     
    LandonC likes this.
Thread Status:
Not open for further replies.