IAP IN UNITY Easy Integration for BEGINNER! 2023 (UNITY, GOOGLE PLAY AND TESTING!!)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome back to another video in this video we are going to implement in-app purchase in unity so let's get started [Music] inner purchase are basically some extra content or feature that player likes to buy in exchange for real world money now these in-app purchase are basically divided into three categories which is consumable non-consumable and subscription now consumable are the items that can be purchased again and again for example coins gems basically the in-game currency and many more things like that now the non-consumable items are the opposite of the consumable these items cannot be purchased again and again for example a no ads option in which the player pays a specific amount of money to remove the ads from the game forever now the last is the subscription item and these are the items that can be purchased but for a specific period of time and when the subscription period is over then player has to repurchase the subscription to regain the access and example would be a VIP member or Royal Pass or a light pass these passes provide additional benefits but these passes needed to be recharged every month now before starting the implementation we need two things first we need to create an app into our Google Play developer console account and second we have to upload a build into our newly created app because we cannot directly integrate in-app purchase into our very first build and even if you try to do it Google Play will not allow you to create any in-app product on their server so for that reason I have already created an app and its name is gpgs testing which basically stands for Google Play games Services testing and I have uploaded some builds too now don't worry you only have to upload a single build I have uploaded like four but these were for the testing purpose the time is for some Google Play policies now if your game has more than one million dollar every year the Google Play will take a 30 commission from each transaction that your game makes now if your game makes less than one million dollar then Google Play will take only 15 and if you talk about the subscription product Google Play always takes a fixed 15 commission no matter how much your game earns and if you want to bring this percentage further down you can participate in different programs offered by Google Play and if you want to know more about this the link is in the description now this video is divided into three parts in the unity path we will set up project download package Implement SDK will read receipt and do whatever is needed to be done inside the unity and in the Google Play part you will create in-app products on the Google Play server of each category that means we will create in-app product for consumable non-consumable and subscription and then finally we will test everything inside our Android device and we'll see that everything works perfectly or not now let's open unity and see how to implement this so now I'm inside unity and I have a very simple scene setup I have like three items to sell which is 50 coins no ads and a light pass so now let me quickly just run this and show you guys how this works right now so now when I try to purchase 50 coins it gives me the 50 coins that means all the basic functionality has been done and similarly if I try to purchase no ads it removes the ad and if I try to purchase a light pass it makes me the VIP member but as you can see in case of the Elite pass there are several important features are missing for example there is no expiration date cancellation time or when the product is going to be expired because these features are very important from a player perspective and we literally needs to fix this but before this there is one more big problem and let me restart the game to show you what that problem is so now I have restarted the unity and as you can see everything is gone like it does not remember that I have already purchased the no ads and I am already subscribed to an Elite pass so that means we have to fix a lot of issue but before that let's talk about the solution now codeless IP is one of the latest thing that Unity has to offer but in this video we are not going to use it because the cordless IPS is still under development and as of the current version of the cordless IP it is hard to extract the information of a subscription item for example their cancellation date their expiration date free trial period introductory price and many more things so for that reason we are not going to use the codeless IB instead we are going to use the more traditional method which is coding now don't worry the traditional method is not as hard as you may have thought let's understand that by the help of a workflow now in our game we may have different items to sell for example some may have more consumable than the non-consumable and some may have more subscription items to sell than the others too so the first step is to decide what you want to sell now for this example let's just take one items in each category that means one for consumable one for non-consumable and one for subscription so when we have decided like what we want to sell we need three classes now these classes are used to store all the data of the in-app products for example their ID their name their description price time period and etc etc now once we have stored all the data of the in-app products inside the class then we need to feed that data inside the Builder now don't worry this Builder comes from the unity package itself so we don't have to care about it now this Builder basically helps in referencing to the specific in-app purchase that we have created into the Google Play Store now once we have feed all the data inside the Builder the third step is to initialize the unity purchasing now once we have initialized the unity purchasing Unity will do all its work and when the initialization is done then we are ready to click some button and make some payments which will then result in success or failure so with keeping that in mind let's get started now the first step is to set up the project for that click on file build setting player setting and check your company name and product name and inside the resolution and presentation check your device orientation and I would like to have my apps orientation to landscape left and Landscape right so I will change it to that and inside the other setting change your scripting backend to il-2 cpb and pick the arm 64. so now the last step is to set up the key store for that click on the key store manager here you can create a new key store or you can just fill in the password if you already have one so in my case I have already created a key store so I will just fill in the passwords and we also have to add the scene into the build settings so with that our setup is complete and now we are ready to download the package now to download the package click on Windows package manager now inside the unity registry search for in-app purchase now here it is click on it and just click install now it will take some time so let me just fast forward the clip so now it is downloaded so we'll go and check inside the package manager and uh so here we have a IAP package so that means the package is successfully downloaded now there is one last thing that we have to do before we can start coding and that is linking our Unity project with unity dashboard so for that click on edit project settings and now under the services panel we will select our organization and after that we can create a new project ID or we can select if we already have one so in my case I have already created a project ID so I will just select it and then I will just click on the link project ID now with this it will link our Unity project with the unity dashboard now we have all our information and yes we do not appeal to children under age 13. now go to the in-app purchasing and turn it on and then here you have to add the Google licensing key and you can easily find it on your Google Play developer console account now just open your account and select your app and under the monetization setup you can see a licensing key now just simply copy it and paste it inside the unity now just click on submit or update and with that we have successfully linked our Unity project with unity dashboard now if you face any problem in linking the project with the dashboard then it's completely normal you can try restarting Unity or you can double check all your information on the dashboard or you can just give it some time because Unity server needs some time to reflect the changes and after like two hours I believe it will definitely connect to the dashboard so with that we have successfully completed our setup and now we are ready for some coding now let's open shop script and here we can see that we have a start which basically reflects the coin that we currently have and other than that we have three function one for consumable one for non-consumable and the last one is for subscription so whenever the consumable button is pressed then add 50 coin function get called which basically adds 50 coins and similarly when the no ads button is pressed then remove ads gets called and same thing happens with the subscription item whenever the subscription item is pressed then activate the Royal Pass get called now according to our workflow we have to decide like what we have to sell and yes we have decided that we have to sell three items one for each category one is 50 coins then the no ads and last is the Light Pass then in addition to that we have to create classes that will hold our in-app products data for example like their ID their name and etc etc now let's just quickly create the classes that will hold our in-app products data so here we have created a class with the name consumable item and this class will be used to store the data of the consumable products for example it will store their name their ID their description and the price and now all we have to do is just to copy paste this for the non-consumable and for the subscription and one last thing that we have to do for the subscription class is to add the time duration now let's create the objects now here I am creating like a single object but you can create like an array of an object if you have more than one item in any of the category now with that our first step is done and now for the second step we have to feed all the data inside the Builder so for that we will use Unity engine dot purchasing and we will derive from one more class and its name is iStore listener and this class is basically provides us some callbacks that are very useful for handling the purchasing so let me just implement the interface which will automatically create all the callbacks for me and let me just find it I think it's inside the extra here it is let me just bring them out now don't worry about the extra it basically handles all the basic functionality like adding coin removing ads or showing ads and things like that and these function are very useful for handling the purchasing but for now let's just stick to our second step which is adding data inside the Builder so for that we will create a new function and the name of the function is setup Builder now let me just call it from the start now to set up a builder we will need to have a builder itself so let's just create a builder so where Builder equals to configuration Builder dot instance and pass in the standard purchasing module dot instance now we have our Builder and all we have to do is just to add the data inside the Builder so for that type Builder dot add product and then we have to pass the ID of the product so we'll write C item dot ID then we have to declare its type which is consumable now we just have to copy paste this for the non-consumable and subscription so let me just quickly do it so now we have successfully faded the data inside the Builder and now it's time for the third step which is to initialize the unity purchasing so for that type Unity purchasing Dot initialize and pass in the Builder now with that our third step is done and now Unity will initialize all the things that it has to offer but how will we know that the initialization is done or the process is completed so here comes the importance of the Callback functions so from the five callback function one will get automatically called whose name is on initialized so let me just quickly copy it and paste right below the initialization line so here you can see on initialized callback do come with the controller and this controller is basically very useful for making any payment or doing anything with the products so let me just quickly create a global variable for that because we are going to use it throughout this script so let me just create a variable globally and under the on initialized callback let me just quickly assign my Global variable so from now on we will use M store controller for all the things that we have to do in the future so with that we have now completed our third step which is initializing the unity and now we can move on to our fourth step which is clicking some button and making some payments so as here you can see we have three function which directly distributes the reward which we definitely don't want so what we are going to do now is to comment the add 50 coin function and instead of this we will write the M store controller dot initialize purchase and we will pass the our consumable ID which is C item dot ID now this will show a pop-up in the Android with the option to purchase the consumable item so now let's just quickly do it for the consumable item and the subscription item so all we have to do is just copy paste and replace the IDS so until now there is an IIT button user can click on it and the pop-up will appear on this screen and now we need to know that user successfully purchased it or user decline to purchase and if user purchased it then what is the item that user purchased so for all that we will use the process purchase callback so let me just quickly copy it and paste it right below when we are initializing the purchase this process purchased is used to distribute the rewards because it automatically get called whenever uh item is successfully purchased now to distribute the rewards all we need to know is the ID of the product that has been purchased so for that first we will get the reference of the product itself and then and now let me just print the ID of the product by the help of product.definition.id and now our job is very simple all we have to do is to compare this ID and distribute the rewards so here I have compared the ID with the consumable id non-consumable id and subscription ID and I have just distributed the rewards accordingly and now to close off this function we have to return purchase processing result dot complete and for other callbacks we will just pin these statements now everything is done so let's just fill in the in-app products data in the inspector now here the only thing we care about is the product ID as we have to use the exact same product ID in future in the Google Play Store now for IDs try to use all small letters numbers and underscores now I have just filled all the data so let's run this and check the code is working or not so let's try to purchase 50 coins and yes we got the print statement so that means it is working perfectly so let me quickly check the others too so now everything is working perfectly I got all the print statements now before creating the product in the Google Play server we have to deal with one more error and that error is short-term memory loss because this code does not automatically remembers that someone has already purchased a non-consumable item or someone has already subscribed to a subscription now to deal with this problem is fairly very simple all we have to do is just to check the receipt if we have a receipt of a particular item that means we have purchased it or else we haven't purchased it now to implement this let's create a new function and its name should be check consumable so let me quickly implement this function and then I will explain it to you guys so first thing that we are checking inside this function is the controller should not be equal to null then we take a reference of the product from its ID then we are checking the product should not be equal to null and if we have a product we are checking the product dot has received Now product dot has received is true if player owns a receipt and it's false if player doesn't owns one now if we have a receipt then we are removing the ad or else if we don't have a receipt then we are showing the ads now just like remove ads function I have also created a show at function which basically displays the ad now with this check non-consumable is completed and we have to do the exact same thing for the subscription product now we cannot do the exact same thing for the subscription product because someone may have a subscription receipt but it does not means the subscription is active on that particular period of time so let's create a new function to check the subscription product and we will call it just right after when you are checking the non-consumable product now inside this function we will first take the reference of the product itself just like we did in the non-consumable product then we will check the subscription product should not be equal to null if the subscription product is null then the product is not found now if the subscription product is not equal to null then we have to make a try catch statement now inside the try blocks we will check that subscription product should has received because if we do not find a receipt we don't want to move further and if we find a receipt then we have to take the help of a subscription manager class that is provided by the unity so for that we have to write where subscription manager is equals to new subscription manager and we will pass in the product and then once we get the subscription manager we can get all the subscription info so for that we will write where info is equals to subscription manager dot getsubscription info now this info holds all the information about the subscription product for example their cancel time their expiration time their free trial period and many more things as you can see here so for now let me just print get expired date then after that now we will just simply check inside a if statement that info dot is subscribed is equal equal to true then we are currently subscribed or else we are not subscribed so let me just print a statement for each of them and now if we are subscribed I would like to call the activity Light Pass or else I would like to call the deactivity Light Pass so with this our all main work for a subscription product is done now let me just quickly print the else statement now it will throw a exception when we will try to run it inside the unity because checking the subscription product only works for the Google Apple or Amazon store so if we run this inside the unity it will throw exception so for that we will just write inside the cat statement that we cannot run it inside the fake store that is running inside the unity now with that we have completed everything we are handling exceptions we are handling the failed callbacks we are handling the successful purchase here we are handling the button clicks and here we are setting up the Builder and initializing the unity oh and here we have to put a capital i so with that all the unity work is now completed and now it's time for creating some in-app products on the Google Play servers now in your Google Play account open your app and click on in-app products so I was testing the in-app purchase couple of days ago so I created this remove ads at that time but don't worry about this you just have to click on this create product now here fill in your product ID and this ID should match exactly with the ID that we created inside the unity then fill in your name and description now let's just fill in the amount so this is in INR so for now I will just write like 100 inrs because we don't really care about the amount right now and after that we will just click on this multiple quantity because we want the user to purchase a couple of them together and now we will just hit save and activate so now over 50 coin product is ready and is in active state and now we have to create a one more product for the no ads option and now the only difference is that we will not click on this multiple quantity now just click on Save and activate and now with that our both of the products are ready now before moving on to the subscription product let's understand the subscription product by the help of a symbol diagram so for a subscription product we have a subscription then we have multiple base plan under it and then we have multiple offers inside the base plans now subscription basically represents the overall time period like one year one month one week like that whereas the base plan represents the type of the subscription like it is auto renewable or prepaid and the last is the offer which basically means the offers for example buy for 12 month and get one month for free and you can create many more offers just like that but for now let's just focus on creating simple subscription products so for that click on subscription and here I have some testing subscriptions but don't worry about that just click on create subscriptions enter your exact subscription product ID and give it a name then enter some benefits and add your description then click on Save changes now we have to create base plans now enter the base plan ID and click on auto renew and here I think I cannot use the underscore so I will replace it with the hyphens now here you can change the billing period And I would like to have it on weekly basis and I think I would not like to change anything else so now fill in the price now select all countries and click on set price and I would like to change the currency to USD and I think 1.99 dollars are great then click on update let's save it now let me check everything is perfect or not and now click on activate and here we have our newly created subscription product and I think everything is now completed so now we are ready for testing now if you're doing it for the very first time then you have to upload your build into Google Play developer console account under internal testing and if you are doing it for the second or third time then you can use a USB cable and side load the APK into your Android so now let's assume you are doing it for the very first time if so then click on the internal testing now before uploading the build we have to create some testers so click on create email list then give it a name then enter tester's email and obviously it is a wrong email so it will not gonna work but you got the idea then once you entered your email then click on Save changes now click on create new release here you can upload a build from your computer and for the release name and release notes you can just simply write like new release or anything that you like and after that you will get a link and by the help of the link you can simply download the APK into your Android so now I'm inside my Android device now if I try to purchase 50 coins it works perfectly now let's try nomads it also works perfectly it removes the ad now let's try the Elite pass subscribe and yeah it makes me the VIP member now let's close the app and then we will reopen it and then we will check it remembers the thing that we have already purchased or not so now I have restarted the unity and yeah it works perfectly it remembers that we have purchased the no ads and we are currently subscribed to the Elight pass so that's great but wait guys this is not the end because while testing the APK into the Android device I found out that when we tried to purchase 50 coins and if we increase the quantity to like two or three and then when we click on purchase then instead of giving me the three times the price it only gives me the one time as you can see before the transaction I had 100 coin and now I only have 150 so I think we need some way to access this quantity and this quantity basically sits inside the Google Play receipt now this is a Google Play receipt which is provided after each successful payment and and we can clearly see your field for quantity now all we have to do is just to convert this nested Json into its equivalent class structure so that we can easily access this quantity and use it inside our script so now let's just quickly implement this so first I will just paste in the equivalent class structure now don't worry the script is in the description in these classes we have multiple subclasses now inside this payload data class we have a variable quantity that we have to use so let's create objects for these classes and inside the Callback function where we were Distributing our Rewards we will comment the add coins 50 and by the help of the Json utility class we will pass the receipt that we get from the Google Play and we have to do the same thing for the other classes as well and finally we can get the quantity from the payload data class now all we have to do is just run a loop until it reaches the quantity and then we will paste the add coins 50 function inside it and with that it's time for final testing so let me try to purchase coins and I will increase its quantity to 3. and yeah it gives me the 150 coins so as you can see the quantity is now perfectly working and we have implemented the consumable non-consumable and subscription product so that's it guys we have successfully implemented the IAP in unity I hope you guys like this video If you guys like this video then do like this video and let me know if you have any doubt or any topic for future videos but till then goodbye and thanks for watching please
Info
Channel: Coco 3D
Views: 37,432
Rating: undefined out of 5
Keywords: iap unity, iap unity google play, in app purchase unity android, unity iap, unity iap tutorial, unity iap google play, unity iap 2023, unity iap implementation, unity iap subscription, unity iap subscription tutorial, unity iap receipt validation, unity iap non consumable, unity iap consumable
Id: PthFk5je350
Channel Id: undefined
Length: 27min 54sec (1674 seconds)
Published: Sat Jul 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.