In order to use IAP with Google Play Store, you must have an active Google Session, to do this read our Google Sign in Guide or take a look at our in-house feature UPA where you can start a Remote Login Session for Google!
It is presumed that you have at least made an Application in Google Play Console!
Some important information to find is your application name; it generally follows this style "com.company.app_name", this name can be found under the overview of all your applications, or inside the application page beneath the title.
Underneath the Monetization page in the left menu list, you will see Products which contains In-app products. This is where you set up your products to use IAP with.
Create new in-app product, supply all the required fields.
Creating Purchases In Engine
To create Purchases in Unity, you must inherit from IDetailedStoreListener and import the required methods that follows.
usingUnity.Services.Core;usingUnity.Services.Core.Environments;usingUnityEngine;usingUnityEngine.Purchasing;usingUnityEngine.Purchasing.Extension;publicclassIAPManager:MonoBehaviour,IDetailedStoreListener{publicIStoreController controller;privateIExtensionProvider extensions;publicasyncvoidAwake() {try {var options =newInitializationOptions() .SetEnvironmentName("production");awaitUnityServices.InitializeAsync(options); }catch (Exception exception) {Debug.Log(exception); } //Specifically creates IAP for Google Play Store, using ifdefs to determine which platform is being run is preferable.
var builder =ConfigurationBuilder.Instance(StandardPurchasingModule.Instance(AppStore.GooglePlay));builder.AddProduct("<replace_with_product_id>",ProductType.Consumable);StandardPurchasingModule.Instance().useFakeStoreAlways=true;UnityPurchasing.Initialize(this, builder); }publicvoidOnPurchaseClicked(string productId) {controller.InitiatePurchase(productId); }publicvoidPurchase() {var product =controller.products.WithID("<replace_with_product_id>");if (product is { availableToPurchase:true }) {controller.InitiatePurchase(product); } } /// <summary> /// Called when Unity IAP is ready to make purchases. /// </summary>publicvoidOnInitialized(IStoreController controller,IExtensionProvider extensions) {this.controller= controller;this.extensions= extensions; } /// <summary> /// Called when Unity IAP encounters an unrecoverable initialization error. /// /// Note that this will not be called if Internet is unavailable; Unity IAP /// will attempt initialization until it becomes available. /// </summary>publicvoidOnInitializeFailed(InitializationFailureReason error) { } /// <summary> /// Called when a purchase completes. /// /// May be called at any time after OnInitialized(). /// </summary>publicPurchaseProcessingResultProcessPurchase(PurchaseEventArgs e) {LootLockerIAPManager.RedeemPurchase(e.purchasedProduct);returnPurchaseProcessingResult.Complete; } /// <summary> /// Called when a purchase fails. /// </summary>publicvoidOnPurchaseFailed(Product i,PurchaseFailureReason p) { }publicvoidOnPurchaseFailed(Product product,PurchaseFailureDescription failureDescription) { }publicvoidOnInitializeFailed(InitializationFailureReason error,string message) {Debug.Log("Error: "+ message); }}
A guide is currently in the making as this requires a lot of set up! Once the guide is up, it will be linked here.
Redeem Purchase For Player
Product is from using UnityEngine.Purchasing;
Which is a Package called "In-App Purchasing", read more here.
To be able to redeem a Google purchase you've made, you need to grab the product id (the same you used to make the purchase) and the purchase token from a successful purchase as input for the redeem.
On a successful redemption, the player will have been rewarded with whatever catalog item that this product id has been set up to reward.
To be able to redeem a Google purchase you've made, you need to grab the product id (the same you used to make the purchase) and the purchase token from a successful purchase as input for the redeem.
On a successful redemption, the player will have been rewarded with whatever catalog item that this product id has been set up to reward.