Search Unity

UnityIP not working on IOS and Android.

Discussion in 'Unity IAP' started by A_never_kill, Oct 15, 2018.

  1. A_never_kill

    A_never_kill

    Joined:
    Jan 7, 2014
    Posts:
    81
    Code (CSharp):
    1. Hello ,
    2.  
    3. I am using below mentioned code for Iap in iOS and android.It response on unity editor console that product is purchased .
    4.  
    5. using System;
    6. using System.Collections.Generic;
    7. using UnityEngine;
    8. using UnityEngine.Purchasing;
    9.  
    10. // Placing the Purchaser class in the CompleteProject namespace allows it to interact with ScoreManager,
    11. // one of the existing Survival Shooter scripts.
    12. namespace CompleteProject
    13. {
    14.     // Deriving the Purchaser class from IStoreListener enables it to receive messages from Unity Purchasing.
    15.     public class Purchaser : MonoBehaviour, IStoreListener
    16.     {
    17.         private static IStoreController m_StoreController;          // The Unity Purchasing system.
    18.         private static IExtensionProvider m_StoreExtensionProvider; // The store-specific Purchasing subsystems.
    19.  
    20.         // Product identifiers for all products capable of being purchased:
    21.         // "convenience" general identifiers for use with Purchasing, and their store-specific identifier
    22.         // counterparts for use with and outside of Unity Purchasing. Define store-specific identifiers
    23.         // also on each platform's publisher dashboard (iTunes Connect, Google Play Developer Console, etc.)
    24.  
    25.         // General product identifiers for the consumable, non-consumable, and subscription products.
    26.         // Use these handles in the code to reference which product to purchase. Also use these values
    27.         // when defining the Product Identifiers on the store. Except, for illustration purposes, the
    28.         // kProductIDSubscription - it has custom Apple and Google identifiers. We declare their store-
    29.         // specific mapping to Unity Purchasing's AddProduct, below.
    30.         public static string kProductIDConsumable = "Aman";
    31.    
    32.  
    33.         public static string kProductIDNonConsumable = "noncons";
    34.         public static string kProductIDNonConsumable1 = "nonconsumable2";
    35.         public static string kProductIDNonConsumable2 = "nonconsumable3";
    36.         public static string kProductIDSubscription = "subscription";
    37.  
    38.         // Apple App Store-specific product identifier for the subscription product.
    39.         private static string kProductNameAppleSubscription = "com.unity3d.subscription.new";
    40.  
    41.         // Google Play Store-specific product identifier subscription product.
    42.         private static string kProductNameGooglePlaySubscription = "com.unity3d.subscription.original";
    43.  
    44.         void Start()
    45.         {
    46.             // If we haven't set up the Unity Purchasing reference
    47.             if (m_StoreController == null)
    48.             {
    49.                 // Begin to configure our connection to Purchasing
    50.                 InitializePurchasing();
    51.             }
    52.         }
    53.  
    54.         public void InitializePurchasing()
    55.         {
    56.             // If we have already connected to Purchasing ...
    57.             if (IsInitialized())
    58.             {
    59.                 // ... we are done here.
    60.                 return;
    61.             }
    62.  
    63.             // Create a builder, first passing in a suite of Unity provided stores.
    64.             var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    65.  
    66.             // Add a product to sell / restore by way of its identifier, associating the general identifier
    67.             // with its store-specific identifiers.
    68.             builder.AddProduct(kProductIDConsumable, ProductType.Consumable);
    69.          
    70.             // Continue adding the non-consumable product.
    71.             builder.AddProduct(kProductIDNonConsumable, ProductType.NonConsumable);
    72.             builder.AddProduct(kProductIDNonConsumable1, ProductType.NonConsumable);
    73.             builder.AddProduct(kProductIDNonConsumable2, ProductType.NonConsumable);
    74.  
    75.             // And finish adding the subscription product. Notice this uses store-specific IDs, illustrating
    76.             // if the Product ID was configured differently between Apple and Google stores. Also note that
    77.             // one uses the general kProductIDSubscription handle inside the game - the store-specific IDs
    78.             // must only be referenced here.
    79.             //builder.AddProduct(kProductIDSubscription, ProductType.Subscription, new IDs(){
    80.             //    { kProductNameAppleSubscription, AppleAppStore.Name },
    81.             //    { kProductNameGooglePlaySubscription, GooglePlay.Name },
    82.             //});
    83.  
    84.             // Kick off the remainder of the set-up with an asynchrounous call, passing the configuration
    85.             // and this class' instance. Expect a response either in OnInitialized or OnInitializeFailed.
    86.             UnityPurchasing.Initialize(this, builder);
    87.         }
    88.  
    89.  
    90.         private bool IsInitialized()
    91.         {
    92.             // Only say we are initialized if both the Purchasing references are set.
    93.             return m_StoreController != null && m_StoreExtensionProvider != null;
    94.         }
    95.  
    96.  
    97.         public void BuyConsumable()
    98.         {
    99.             // Buy the consumable product using its general identifier. Expect a response either
    100.             // through ProcessPurchase or OnPurchaseFailed asynchronously.
    101.          
    102.             BuyProductID(kProductIDConsumable);
    103.         }
    104.         //public void BuyConsumable1()
    105.         //{
    106.         //    // Buy the consumable product using its general identifier. Expect a response either
    107.         //    // through ProcessPurchase or OnPurchaseFailed asynchronously.
    108.  
    109.         //    BuyProductID(kProductIDConsumable1);
    110.         //}
    111.         //public void BuyConsumable2()
    112.         //{
    113.         //    // Buy the consumable product using its general identifier. Expect a response either
    114.         //    // through ProcessPurchase or OnPurchaseFailed asynchronously.
    115.  
    116.         //    BuyProductID(kProductIDConsumable2);
    117.         //}
    118.  
    119.  
    120.         public void BuyNonConsumable()
    121.         {
    122.             // Buy the non-consumable product using its general identifier. Expect a response either
    123.             // through ProcessPurchase or OnPurchaseFailed asynchronously.
    124.             BuyProductID(kProductIDNonConsumable);
    125.         }
    126.         public void BuyNonConsumable1()
    127.         {
    128.             // Buy the non-consumable product using its general identifier. Expect a response either
    129.             // through ProcessPurchase or OnPurchaseFailed asynchronously.
    130.             BuyProductID(kProductIDNonConsumable1);
    131.         }
    132.         public void BuyNonConsumable2()
    133.         {
    134.             // Buy the non-consumable product using its general identifier. Expect a response either
    135.             // through ProcessPurchase or OnPurchaseFailed asynchronously.
    136.             BuyProductID(kProductIDNonConsumable2);
    137.         }
    138.  
    139.  
    140.         public void BuySubscription()
    141.         {
    142.             // Buy the subscription product using its the general identifier. Expect a response either
    143.             // through ProcessPurchase or OnPurchaseFailed asynchronously.
    144.             // Notice how we use the general product identifier in spite of this ID being mapped to
    145.             // custom store-specific identifiers above.
    146.             BuyProductID(kProductIDSubscription);
    147.         }
    148.  
    149.  
    150.         void BuyProductID(string productId)
    151.         {
    152.             // If Purchasing has been initialized ...
    153.             if (IsInitialized())
    154.             {
    155.                 // ... look up the Product reference with the general product identifier and the Purchasing
    156.                 // system's products collection.
    157.                 Debug.Log(productId);
    158.                 Product product = m_StoreController.products.WithID(productId);
    159.  
    160.                 // If the look up found a product for this device's store and that product is ready to be sold ...
    161.                 if (product != null && product.availableToPurchase)
    162.                 {
    163.                     Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
    164.                     // ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed
    165.                     // asynchronously.
    166.                     m_StoreController.InitiatePurchase(product);
    167.                 }
    168.                 // Otherwise ...
    169.                 else
    170.                 {
    171.                     // ... report the product look-up failure situation
    172.                     Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
    173.                 }
    174.             }
    175.             // Otherwise ...
    176.             else
    177.             {
    178.                 // ... report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or
    179.                 // retrying initiailization.
    180.                 Debug.Log("BuyProductID FAIL. Not initialized.");
    181.             }
    182.         }
    183.  
    184.  
    185.         // Restore purchases previously made by this customer. Some platforms automatically restore purchases, like Google.
    186.         // Apple currently requires explicit purchase restoration for IAP, conditionally displaying a password prompt.
    187.         public void RestorePurchases()
    188.         {
    189.             // If Purchasing has not yet been set up ...
    190.             if (!IsInitialized())
    191.             {
    192.                 // ... report the situation and stop restoring. Consider either waiting longer, or retrying initialization.
    193.                 Debug.Log("RestorePurchases FAIL. Not initialized.");
    194.                 return;
    195.             }
    196.  
    197.             // If we are running on an Apple device ...
    198.             if (Application.platform == RuntimePlatform.IPhonePlayer ||
    199.                 Application.platform == RuntimePlatform.OSXPlayer)
    200.             {
    201.                 // ... begin restoring purchases
    202.                 Debug.Log("RestorePurchases started ...");
    203.  
    204.                 // Fetch the Apple store-specific subsystem.
    205.                 var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
    206.                 // Begin the asynchronous process of restoring purchases. Expect a confirmation response in
    207.                 // the Action<bool> below, and ProcessPurchase if there are previously purchased products to restore.
    208.                 apple.RestoreTransactions((result) => {
    209.                     // The first phase of restoration. If no more responses are received on ProcessPurchase then
    210.                     // no purchases are available to be restored.
    211.                     Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
    212.                 });
    213.             }
    214.             // Otherwise ...
    215.             else
    216.             {
    217.                 // We are not running on an Apple device. No work is necessary to restore purchases.
    218.                 Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
    219.             }
    220.         }
    221.  
    222.  
    223.         //
    224.         // --- IStoreListener
    225.         //
    226.  
    227.         public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    228.         {
    229.             // Purchasing has succeeded initializing. Collect our Purchasing references.
    230.             Debug.Log("OnInitialized: PASS");
    231.  
    232.             // Overall Purchasing system, configured with products for this application.
    233.             m_StoreController = controller;
    234.             // Store specific subsystem, for accessing device-specific store features.
    235.             m_StoreExtensionProvider = extensions;
    236.         }
    237.  
    238.  
    239.         public void OnInitializeFailed(InitializationFailureReason error)
    240.         {
    241.             // Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.
    242.             Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
    243.         }
    244.  
    245.  
    246.         public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    247.         {
    248.             // A consumable product has been purchased by this user.
    249.             if (String.Equals(args.purchasedProduct.definition.id, kProductIDConsumable, StringComparison.Ordinal))
    250.             {
    251.                 Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
    252.                 // The consumable item has been successfully purchased, add 100 coins to the player's in-game score.
    253.                 // ScoreManager.score += 100;
    254.                 print("You are purchased an item");
    255.                 PlayerPrefs.SetInt("patterpurchased", 1);
    256.             }
    257.             //else if (String.Equals(args.purchasedProduct.definition.id, kProductIDConsumable1, StringComparison.Ordinal))
    258.             //{
    259.             //    Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
    260.             //    // The consumable item has been successfully purchased, add 100 coins to the player's in-game score.
    261.             //    // ScoreManager.score += 100;
    262.             //    print("You are purchased an item");
    263.             //}
    264.             //else if (String.Equals(args.purchasedProduct.definition.id, kProductIDConsumable2, StringComparison.Ordinal))
    265.             //{
    266.             //    Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
    267.             //    // The consumable item has been successfully purchased, add 100 coins to the player's in-game score.
    268.             //    // ScoreManager.score += 100;
    269.             //    print("You are purchased an item");
    270.             //}
    271.             // Or ... a non-consumable product has been purchased by this user.
    272.             if (String.Equals(args.purchasedProduct.definition.id, kProductIDNonConsumable, StringComparison.Ordinal))
    273.             {
    274.                 Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
    275.                 // TODO: The non-consumable item has been successfully purchased, grant this item to the player.
    276.                 PlayerPrefs.SetInt("cookiepurchased", 1);
    277.                 //print(PlayerPrefs.SetInt("cookiepurchased"));
    278.             }
    279.            if (String.Equals(args.purchasedProduct.definition.id, kProductIDNonConsumable1, StringComparison.Ordinal))
    280.             {
    281.                 Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
    282.                 // TODO: The non-consumable item has been successfully purchased, grant this item to the player.
    283.              
    284.                 PlayerPrefs.SetInt("Patternpurchased", 1);
    285.             }
    286.             if (String.Equals(args.purchasedProduct.definition.id, kProductIDNonConsumable2, StringComparison.Ordinal))
    287.             {
    288.                 Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
    289.                 // TODO: The non-consumable item has been successfully purchased, grant this item to the player.
    290.  
    291.                 PlayerPrefs.SetInt("Toopingpurchased", 1);
    292.             }
    293.             // Or ... a subscription product has been purchased by this user.
    294.             else if (String.Equals(args.purchasedProduct.definition.id, kProductIDSubscription, StringComparison.Ordinal))
    295.             {
    296.                 Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
    297.                 // TODO: The subscription item has been successfully purchased, grant this to the player.
    298.             }
    299.             // Or ... an unknown product has been purchased by this user. Fill in additional products here....
    300.             else
    301.             {
    302.                 Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
    303.             }
    304.  
    305.             // Return a flag indicating whether this product has completely been received, or if the application needs
    306.             // to be reminded of this purchase at next app launch. Use PurchaseProcessingResult.Pending when still
    307.             // saving purchased products to the cloud, and when that save is delayed.
    308.             return PurchaseProcessingResult.Complete;
    309.         }
    310.  
    311.  
    312.         public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
    313.         {
    314.             // A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing
    315.             // this reason with the user to guide their troubleshooting actions.
    316.             Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
    317.         }
    318.     }
    319. }
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446