Search Unity

IAP Subscription setup question

Discussion in 'Unity IAP' started by Mars91, Jan 5, 2021.

  1. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    Hi all,
    I'm configuring subscription for my app, I created two subscription on Google Play Console (will setup for apple store too), and noted their Product ID.
    What I can't find is

    Code (CSharp):
    1.  
    2. // Google Play Store-specific product identifier subscription product.
    3. private static string kProductNameGooglePlaySubscription = "com.unity3d.subscription.original";
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  3. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    My question is.

    Code (CSharp):
    1.  
    2.         public static string kProductIDSubscriptionMonthly = "code_I_defined_in_google_play_console";
    Is the Product ID I created in the console and kProductNameGooglePlaySubscription is something I create to track the subscription in my code or viceversa?
     
    Mario4022 likes this.
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    The productID's need to be the same, on the dashboard and in the app. Did you write this code yourself? Where are you getting this code? Please review the documentation that I shared, it will explain the process. Also, review the Sample project.
     
  5. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    The code comes from UnityLearn
    https://learn.unity.com/tutorial/unity-iap#

    Here's something to better explain what I'm asking

    Code (CSharp):
    1.  
    2.         public static string kProductIDNonConsumable = "non_consumable"; // PRODUCT ID DEFINED in DEV CONSOLE
    3.         public static string kProductIDSubscriptionMonthly = "monthly"; // ???
    4.         public static string kProductIDSubscriptionYearly = "yearly_subscription";
    5.  
    6.         private static string kProductNameAppleSubscriptionMonthly = "monthly"; // ???
    7.         private static string kProductNameAppleSubscriptionYearly = "yearly_subscription";
    8.  
    9.         private static string kProductNameGooglePlaySubscriptionMonthly = "monthly";
    10.         private static string kProductNameGooglePlaySubscriptionYearly = "yearly_subscription";
    11.  
    12.  
    13.         void Start()
    14.         {
    15.             // If we haven't set up the Unity Purchasing reference
    16.             if (m_StoreController == null)
    17.             {
    18.                 // Begin to configure our connection to Purchasing
    19.                 InitializePurchasing();
    20.             }
    21.         }
    22.  
    23.         public void InitializePurchasing()
    24.         {
    25.             // If we have already connected to Purchasing ...
    26.             if (IsInitialized())
    27.             {
    28.                 // ... we are done here.
    29.                 return;
    30.             }
    31.  
    32.             var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    33.  
    34.             builder.AddProduct(kProductIDNonConsumable, ProductType.NonConsumable);
    35.  
    36.             builder.AddProduct(kProductIDSubscriptionMonthly, ProductType.Subscription, new IDs(){
    37.                 { kProductNameAppleSubscriptionMonthly, AppleAppStore.Name },
    38.                 { kProductNameGooglePlaySubscriptionMonthly, GooglePlay.Name },
    39.             });
    40.  
    41.             builder.AddProduct(kProductIDSubscriptionYearly, ProductType.Subscription, new IDs(){
    42.                 { kProductNameAppleSubscriptionYearly, AppleAppStore.Name },
    43.                 { kProductNameGooglePlaySubscriptionYearly, GooglePlay.Name }
    44.             });
    45.  
    46.             UnityPurchasing.Initialize(this, builder);
    47.         }
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Got it! You would replace all those products with your own, those are all just examples. You can also take a look at the Sample IAP project, it does the same thing and has products such as "gold50" and "noads"

    So delete all those private static string variables. Then look for the errors that it generates on the other lines, and make note where they are used. Then only add the ones you need, using similar syntax and replace all references with them. I personally don't start my variables names with a lower-case "k" but that's up to you.
     
  7. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    Yeas but what I don't userstand is the difference between

    kProductIDSubscriptionMonthly - kProductNameGooglePlaySubscriptionMonthly
    Which one is coming from google play console and which one is defined by me, are they both from the google play console?

    PS: I don't like to start var with lowercase k too xD
     
  8. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Delete all those from your code, as mentioned. You also need to review the documentation, and compare to the Sample IAP project. So the question is, what products do you want in your game? Gold coins? A potion? A subscription so users can choose not to see ads? Once you decide, you create each product in your Google Play developer console and give it a unique productID that you like. Then use the same ProductID in your code. All products are defined by you, your Google Play dashboard starts out with zero products. You need to add them.
     
  9. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    Yes and I understand that, I already have a game with inapp purchase and I know how to set up a non consumable, the "problem" comes with understanding subscription since in the code they rely on two static string while on google play console I only setup one product id per subscription.
     
  10. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    For example in the code you add a non consumable using only the product id while the subscription is created using the string var one is the product id and the other one is ???

    Code (CSharp):
    1.  
    2. // Non consumable, use the Product ID as expected
    3. builder.AddProduct(kProductIDNonConsumable, ProductType.NonConsumable);
    4.  
    5. // Subscription, rely on kProductIDSubscriptionMonthly (which is suppose is the Product ID)
    6. // and on kProductNameAppleSubscriptionMonthly which is??
    7. builder.AddProduct(kProductIDSubscriptionMonthly, ProductType.Subscription, new IDs(){
    8.     { kProductNameAppleSubscriptionMonthly, AppleAppStore.Name },
    9.     { kProductNameGooglePlaySubscriptionMonthly, GooglePlay.Name },
    10. });
    11.  
     
  11. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    That is not necessary in your case, I generally don't recommend productID overrides. That allows you to refer to one productID in your game logic but have a separate productID on Google and Apple. I just keep the productIDs the same on both Google and Apple. Our documentation mentions the same thing https://docs.unity3d.com/Manual/UnityIAPDefiningProducts.html
     
  12. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    This mean that I can do this (assuming that both GooglePlay and AppStore subscription has the same ids):

    Code (CSharp):
    1. builder.AddProduct(kProductIDSubscriptionMonthly, ProductType.Subscription);
     
  13. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Yes, please see the Sample IAP Project as an example.
     
  14. growsolutionsapp

    growsolutionsapp

    Joined:
    May 12, 2021
    Posts:
    16
    I Have Add Subscription in my game but no idea how to ad and subscription work any idea or sample code it work for me
     
  15. growsolutionsapp

    growsolutionsapp

    Joined:
    May 12, 2021
    Posts:
    16
    Not initialized Massage get how it code work or not

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Purchasing.Security;
    using UnityEngine.Purchasing;
    using System;


    public class SubScriptionManager : MonoBehaviour, IStoreListener
    {
    public static SubScriptionManager Instance;

    private static IStoreController m_StoreController; // The Unity Purchasing system.
    private static IExtensionProvider m_StoreExtensionProvider; // The store-specific Purchasing subsystems.

    public static string kProductIDSubscription = "com.vipclub";

    // Apple App Store-specific product identifier for the subscription product.
    private static string kProductNameAppleSubscription = "com.vipclub";

    // Google Play Store-specific product identifier subscription product.
    private static string kProductNameGooglePlaySubscription = "com.vipclub";

    public bool isSubscriptionActive;
    private void Awake()
    {
    if (Instance == null)
    {
    Instance = this;
    DontDestroyOnLoad(gameObject);
    }
    else
    {
    Destroy(gameObject);
    }
    }
    void Start()
    {
    if (m_StoreController == null)
    InitializePurchasing();
    }

    public void InitializePurchasing()
    {
    if (IsInitialized())
    return;
    var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    builder.AddProduct(kProductIDSubscription, ProductType.Subscription, new IDs(){
    { kProductNameAppleSubscription, AppleAppStore.Name },
    { kProductNameGooglePlaySubscription, GooglePlay.Name },
    });
    }

    private bool IsInitialized()
    {
    return m_StoreController != null && m_StoreExtensionProvider != null;
    }

    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    {
    m_StoreController = controller;
    m_StoreExtensionProvider = extensions;
    GetAndSetProductPriceLocal();

    }

    public void OnInitializeFailed(InitializationFailureReason error)
    {
    Debug.Log("Araj : OnInitializeFailed InitializationFailureReason:" + error);
    }

    private void GetAndSetProductPriceLocal()
    {
    PlayerPrefs.SetString("vip_club", m_StoreController.products.WithID(kProductIDSubscription).metadata.localizedPriceString.ToString());
    }
    public void BuySubscription()
    {
    BuyProductID(kProductIDSubscription);
    }

    void BuyProductID(string productId)
    {
    // If Purchasing has been initialized ...
    if (IsInitialized())
    {
    Product product = m_StoreController.products.WithID(productId);

    if (product != null && product.availableToPurchase)
    {
    Debug.Log(string.Format("Araj : Purchasing product asychronously: '{0}'", product.definition.id));
    m_StoreController.InitiatePurchase(product);
    }
    else
    {
    Debug.Log("Araj : BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
    }
    }
    else
    {
    Debug.Log("Araj : BuyProductID FAIL. Not initialized.");
    }
    }
    public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
    {
    // A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing
    // this reason with the user to guide their troubleshooting actions.
    Debug.Log(string.Format("Araj : OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
    }

    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    {

    if (String.Equals(args.purchasedProduct.definition.id, kProductIDSubscription, StringComparison.Ordinal))
    {
    //PlayerPrefs.SetInt("NoAds", 1);
    }
    else
    {
    Debug.Log(string.Format("Araj : ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
    }
    return PurchaseProcessingResult.Complete;
    }
    }
     
  16. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446