In-App Purchases

Unity In-App Purchases Setup Log #1

  1. Link your Unity Project to a Unity Dashboard Project.

    Note

    We had already done this when setting up unity authentication.

  2. Sign your project up for for Unity In-App Purchases product in https://cloud.unity.com/

    1. Sign into https://cloud.unity.com/

    2. Click on the Products tab on the left hand side of the screen.

    3. Click on the In-App Purchases product.

    4. Click on the Lauch button. This will take you to the Unity IAP Documentation

    5. From here I got the information I needed to proceed with implementing In App Purchases. I will document the next steps I took below.

  3. Activate In-App Purchasing in Unity

    1. In unity go to Window > General > Services

    2. Click on the In-App Purchasing section. This should open In-App Purchasing Settings in the Project Settings window.

    3. Click the Off Button to toggle In-App purchasing on. This will install the IAP package from the package manager.

  4. Initialize Unity Gaming Services first then In-App Purchasing second

    1. 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.
              }
          }
      }
      
    2. 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)
          {
          }
      }
      
  5. Define Products that users can purchase.

    1. 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);
      
    2. Look to the documentation to determine how to construct the products you plan to put in your game.

  6. Enable users to purchase items

    1. The below script references the InitiatePurchase method of the IStoreController defined in the MyIAPManager class 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);
    }
    
  7. Process Purchase Requests

    1. Use the ProcessPurchase function of your IStoreListener (in the MyIAPManager above) 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.

    2. Note that when restoring purchases, the ProcessPurchase function will be called for each product the user owns.

  8. Restore Transactions

    1. Gogle Play automatically restores any products the user owns during the first initialization following reinstallation.

    2. The ProcessPurchase method of your IStoreListener (in the MyIAPManager example script above) will be called for each owned item.

  9. Testing Purchases

    1. In the MyIAPManager example script above, when initializing the StandardPurchasingModule, set StandardPurchasingModule.Instance().useFakeStoreUIMode to FakeStoreUIMode.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

  1. Set Up In-App Purchases on iOS

    1. Go to https://appstoreconnect.apple.com/ and sign in

    2. Select your app and click on the App Store Tab

    3. On the left hand side of the screen look under Features and select In-App Purchases

    4. 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.

    5. After entering all the information click the Create button.

    6. 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.

    7. Next, scroll down to the Price Schedule section and click the Add Pricing button.

    8. Select your base country and price for the base country.

    9. After clicking the next button you will be able to set prices across all other countries.

    10. Look at the rest of the in app purchase page for other in app purchase options.

  2. Test In-App Purchases on iOS

    1. Go to https://appstoreconnect.apple.com/ and sign in

    2. Go to the Users and Access section and select the Sandbox tab.

    3. 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.

    4. 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)

Add In-App Purchases to Google Play Store

  1. Set Up In-App Purchases on Google Play Store

    1. Visit https://play.google.com/console/ and sign in

    2. Select your app.

    3. On the left hand side of the window scroll down to Products > In-app products

    4. If necessary, set up a Google Payments merchant account.