Make IAP Purchases In-Game

In this guide, we will walk through how to implement the in-game purchase flow for Google Play Store using LootLocker, from initiating a purchase to validating and granting rewards to the player.

Prerequisites

circle-exclamation

To implement in-app purchases in your game, you need to:

  1. Initialize Unity's IAP system

  2. Register your Google Play products in the IAP system

  3. Handle the purchase flow when users buy items

  4. Redeem purchases with LootLocker

Overview

Unity's IAP system handles the communication with Google Play Store. To use it, you must:

Step 1: Initialize the IAP System

In your Awake() method, initialize Unity Services and create an IAP configuration builder:

public async void Awake()
{
    try
    {
        var options = new InitializationOptions()
            .SetEnvironmentName("production");
        await UnityServices.InitializeAsync(options);
    }
    catch (Exception exception)
    {
        Debug.Log(exception);
    }

    // Create a configuration builder specifically for Google Play Store
    var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance(AppStore.GooglePlay));
}

Step 2: Register Your Products

For each product you created in Google Play Console, you must register it with the IAP system using AddProduct(). This tells Unity which products are available for purchase.

The AddProduct() method takes two parameters:

  • Product ID: The ID you created in Google Play Console (e.g., "com.company.game.coins100")

  • Product Type: The type of product - Consumable (can be purchased multiple times) or NonConsumable (one-time purchase)

Replace com.company.game.coins100 with your actual product IDs from Google Play Console.

Step 3: Initialize the Purchasing System

After configuring all your products, initialize the IAP system:

This call registers your IDetailedStoreListener (typically your class implementing the interface) to receive purchase notifications.

Step 4: Handle Purchase Initialization Callback

When the IAP system is ready, OnInitialized() is called. Store the controller for later use:

Step 5: Trigger a Purchase

When a user clicks a "Buy" button, call InitiatePurchase() with the product ID. First, verify the product exists and is available:

Step 6: Handle Purchase Success

When a purchase completes successfully, ProcessPurchase() is called. In this method, send the purchase data to LootLocker for validation and redemption:

Step 7: Handle Purchase Failures

If a purchase fails, OnPurchaseFailed() is called:

Complete Example Script

chevron-rightComplete implementation of the IAP managerhashtag

Redeem Purchase For Class

Product is from using UnityEngine.Purchasing;

It is possible to redeem a purchase for a class instead of a player, the flow is the same just that you use the function called RedeemGooglePlayStorePurchaseForClass instead.

Conclusion

You now have the full Google Play Store purchase flow implemented: initiating a purchase using Google Play Billing, validating it through LootLocker, and granting rewards to the player.

With this setup, you can securely support Google Play in-app purchases while keeping your game logic centralized through LootLocker.

Last updated

Was this helpful?