In-App Purchases
Links
Unity In-App Purchases Setup Log #1
Link your Unity Project to a Unity Dashboard Project.
Note
We had already done this when setting up unity authentication.
Sign your project up for for Unity In-App Purchases product in https://cloud.unity.com/
Sign into https://cloud.unity.com/
Click on the Products tab on the left hand side of the screen.
Click on the In-App Purchases product.
Click on the Lauch button. This will take you to the Unity IAP Documentation
From here I got the information I needed to proceed with implementing In App Purchases. I will document the next steps I took below.
Activate In-App Purchasing in Unity
In unity go to Window > General > Services
Click on the In-App Purchasing section. This should open In-App Purchasing Settings in the Project Settings window.
Click the Off Button to toggle In-App purchasing on. This will install the IAP package from the package manager.
Initialize Unity Gaming Services first then In-App Purchasing second
Below is example code for initializing Unity Gaming Services
using System; using Unity.Services.Core; using Unity.Services.Core.Environments; using UnityEngine; public class InitializeUnityServices : MonoBehaviour { public string environment = "production"; async void Start() { try { var options = new InitializationOptions() .SetEnvironmentName(environment); await UnityServices.InitializeAsync(options); } catch (Exception exception) { // An error occurred during services initialization. } } }
Below is example code for initializing In App Purchases
using UnityEngine; using UnityEngine.Purchasing; using UnityEngine.Purchasing.Extension; public class MyIAPManager : IDetailedStoreListener { private IStoreController controller; private IExtensionProvider extensions; public MyIAPManager () { var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); builder.AddProduct("100_gold_coins", ProductType.Consumable, new IDs { {"100_gold_coins_google", GooglePlay.Name}, {"100_gold_coins_mac", MacAppStore.Name} }); UnityPurchasing.Initialize (this, builder); } /// <summary> /// Called when Unity IAP is ready to make purchases. /// </summary> public void OnInitialized (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> public void OnInitializeFailed (InitializationFailureReason error) { } /// <summary> /// Called when a purchase completes. /// /// May be called at any time after OnInitialized(). /// </summary> public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs e) { return PurchaseProcessingResult.Complete; } /// <summary> /// Called when a purchase fails. /// IStoreListener.OnPurchaseFailed is deprecated, /// use IDetailedStoreListener.OnPurchaseFailed instead. /// </summary> public void OnPurchaseFailed (Product i, PurchaseFailureReason p) { } /// <summary> /// Called when a purchase fails. /// </summary> public void OnPurchaseFailed (Product i, PurchaseFailureDescription p) { } }
Define Products that users can purchase.
In the above example script there was a section of code that defined products that users can purchase in your game.
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); builder.AddProduct("100_gold_coins", ProductType.Consumable, new IDs { {"100_gold_coins_google", GooglePlay.Name}, {"100_gold_coins_mac", MacAppStore.Name} }); UnityPurchasing.Initialize (this, builder);
Look to the documentation to determine how to construct the products you plan to put in your game.
Enable users to purchase items
The below script references the
InitiatePurchasemethod of theIStoreControllerdefined in theMyIAPManagerclass in the example above.
// Example method called when the user presses a 'buy' button // to start the purchase process. public void OnPurchaseClicked(string productId) { controller.InitiatePurchase(productId); }
-
Use the
ProcessPurchasefunction of yourIStoreListener(in theMyIAPManagerabove) to respond to purchase requests. Look at the documentation to see what to return from this function as it depends on whether the user is purchasing consumables or not.Note that when restoring purchases, the
ProcessPurchasefunction will be called for each product the user owns.
-
Gogle Play automatically restores any products the user owns during the first initialization following reinstallation.
The
ProcessPurchasemethod of yourIStoreListener(in theMyIAPManagerexample script above) will be called for each owned item.
-
In the
MyIAPManagerexample script above, when initializing theStandardPurchasingModule, setStandardPurchasingModule.Instance().useFakeStoreUIModetoFakeStoreUIMode.DeveloperUser. In game a window will pop up that asks you how to initialize the store as well as choose how to respond to each purchase request that is initiated.
Add In-App Purchases to Apple App Store
Set Up In-App Purchases on iOS
Go to https://appstoreconnect.apple.com/ and sign in
Select your app and click on the App Store Tab
On the left hand side of the screen look under Features and select In-App Purchases
On the In-App Purchases screen click the Plus button to create an In-App purchase. For each purchase you will have to enter a type, reference name, and product ID. Note that the product ID is the same ID used in Unity.
After entering all the information click the Create button.
On your new in app purchase product page scroll down to Availability and click the Set Up Avaliability button. By default Apple makes your product available to all possible countries.
Next, scroll down to the Price Schedule section and click the Add Pricing button.
Select your base country and price for the base country.
After clicking the next button you will be able to set prices across all other countries.
Look at the rest of the in app purchase page for other in app purchase options.
-
Go to https://appstoreconnect.apple.com/ and sign in
Go to the Users and Access section and select the Sandbox tab.
Click a button to Add Test Account and enter in the testers information.
Note
From Apple’s sandbox documentation
Use an email address that hasn’t been used as an Apple ID or to purchase iTunes or App Store content. Consider creating a dedicated email address for each sandbox tester.
If your email service provider supports email subaddressing with a plus sign (+), you can use subaddresses of a sandbox-specific address for a sandbox tester. For example, if your base sandbox email is billjames2@icloud.com, you can use billjames2+UK@icloud.com, billjames2+US@icloud.com, and billjames2+JP@icloud.com as email addresses for additional testers. All communications sent to the subaddresses are also sent to the base address.
Complete the pre requisites required by apple before sandbox testing can commence.
Make sure you are also test on a development build: (On your build settings check the box for Development Build)
When all that setup is done build and run your app on xcode.
I followed the rest of the instructions for testing in app purchases listed by apple here: https://developer.apple.com/documentation/storekit/in-app_purchase/testing_in-app_purchases_with_sandbox#3524803 here is a log of what I did:
I built the app in unity with the Development Build checkbox selected
In Xcode I clicked Product > Clean Build Folder and then i built the app to my device
I was prompted to sign into my apple id as soon as the app opened.
Afterwards i began purchasing items from inside my app. When you make the purchase it should ask you to enter your sandbox password and presents the text [Environment: Sandbox] on the page.
Once the password is input the purchase should complete.
To clear my purchase history, on my Iphone I went to Settings > AppStore I clicked on my sandbox account at the bottom of the list and hit the Manage button. After entering my password on the next scree I clicked clear purchase history.
To test interrupted purchases, on my Iphone I went to Settings > AppStore I clicked on my sandbox account at the bottom of the list and hit the Manage button. After entering my password on the next scree I enabled the Test Interrupted Purchases toggle.
Add In-App Purchases to Google Play Store
Set Up In-App Purchases on Google Play Store
Visit https://play.google.com/console/ and sign in
Select your app.
On the left hand side of the window scroll down to Products > In-app products
If necessary, set up a Google Payments merchant account.
On the In-app products page click the button for Create product
Define the product ID, product details, and price. Note that the product ID is the same ID used in Unity.
Click the Activate button after saving.
Test In-App Purchases on Android
Visit https://play.google.com/console/ and sign in
Click the Settings section on the right and on the settings page click License Testing
Choose the email list you want to test with.
Click the Save changes button.
Next go back to the play console homepage and select yoru app.
Under testing click your test track and select the testers tab.
Share the test url with the testers so that they can install the test release with in app purchases.
To test IAP make a purchase on a device logged in with a tester google account.