Skip to content

Step 1. Set up the integration

Register the credentials you prepared in the Google Play Console in the Hive Console, and set up the environment for implementing Google payments with the dedicated plugin.

1. Configure the store and console settings

To use Google payments, you must set up a payment account in the Google Play Console, get a license key, and then register the key in the Hive Console.

1.1. Issue a license key in the Google Play Console

In the Google Play Console, prepare the license key and market account information required for payments. For details, see Prerequisites.

1.2. Enter the license key in the Hive Console

Register the license key and the market account JSON file you got from the Google Play Console in the store settings of the Hive Console. For details, see Google Play credentials.

2. Install the Hive Axyl Google payment plugin

The Google payment plugin required for integrating the Google Play payment window is provided as the com.com2usplatform.hiveaxyl.payments.addon.google package. Add this package to your project dependencies in the same way you did in Install the Hive Axyl SDK.

"dependencies": {
  "com.com2usplatform.hiveaxyl.core": "1.0.0",
  "com.com2usplatform.hiveaxyl.payments": "1.0.0",
  "com.com2usplatform.hiveaxyl.payments.addon.google": "1.0.0"
}

The plugin is Android-only and configures the Google Play Billing Library dependency along with it, so you do not need to install the Billing Library separately.

Platform requirements

The Google payment plugin runs on Android API 29 or later and internally wraps the BillingClient of Google Play Billing Library 9.

Register and retrieve the plugin

To use the Google payment plugin, register it with builder.AddPlayBilling() during Hive Axyl initialization, and get the plugin instance with HiveCore.TryResolve<IGooglePlayBillingPlugin>() when you use it.

using Hive.Axyl.Core;
using Hive.Axyl.Core.Unity;
using Hive.Axyl.Payments;
using Hive.Axyl.Payments.Addon.Google;

// Register the Google payment plugin during Axyl initialization
HiveBootstrap.Initialize(config, builder =>
{
    builder.AddPayments(sandbox: true);  // Payment (IPaymentsService). Development and test environment. For production builds, use AddPayments()
    builder.AddPlayBilling();            // Google payment plugin (IGooglePlayBillingPlugin)
});
// Retrieve the plugin when you use it
if (!HiveCore.TryResolve<IGooglePlayBillingPlugin>(out var googlePlugin))
{
    Debug.LogError("The Google Play Billing plugin is not registered.");
    return;
}

BillingClient connection

All payment features of the Google payment plugin (QueryProductDetailsAsync, LaunchBillingFlowAsync, QueryPurchasesAsync, ConsumeAsync, AcknowledgePurchaseAsync) work only while the BillingClient is connected. The plugin does not connect the BillingClient automatically, so you must call StartConnectionAsync() before using payment features.

When you branch on the result of StartConnectionAsync(), see Common error handling for how to distinguish the Failure common to plugins, HiveError, and the domain results (Outcome) of each method.

using Hive.Axyl.Payments.Addon.Google;
using System.Threading;

// Connect the BillingClient (required before calling any payment feature)
GooglePlayBillingServiceStartConnectionResult connResult =
    await googlePlugin.StartConnectionAsync(CancellationToken.None);

switch (connResult)
{
    case GooglePlayBillingServiceStartConnectionResult.Success:
        // Connection succeeded. Payment features are available
        break;

    case GooglePlayBillingServiceStartConnectionResult.AlreadyConnected:
        // Already connected. No further action needed
        break;

    case GooglePlayBillingServiceStartConnectionResult.UnknownOutcome:
        Debug.LogWarning("Unknown connection result.");
        break;

    case GooglePlayBillingServiceStartConnectionResult.Failure failure:
        Debug.LogError($"BillingClient connection failed: {failure}");
        break;

    default:
        Debug.LogWarning($"Unhandled result: {connResult.GetType().Name}");
        break;
}
Handle disconnection events

The Google Play BillingClient raises the BillingServiceDisconnected event when the service connection is lost. The plugin does not reconnect automatically, so subscribe to this event and call StartConnectionAsync() again when needed.

googlePlugin.BillingServiceDisconnected += () =>
{
    Debug.LogWarning("BillingClient is disconnected. Reconnect it.");
    // Call StartConnectionAsync() again if needed
};

When the app exits or no longer uses payment features, call EndConnectionAsync() to close the BillingClient connection.

// Close the BillingClient connection (when the app exits or no longer uses payment features)
await googlePlugin.EndConnectionAsync(CancellationToken.None);

3. Optional: Set price tiers for each product in the Hive Console

In Google Play payments, the actual payment price is managed in the Google Play Store. The price information you enter in the Hive Console is used only for viewing in the Hive Console, so this step is optional.

To set price tiers, see Price tier.

4. Register products in the Hive Console

To learn how to register products to sell, see Product registration.

Caution

The Product IDs of the products registered in Google Play and the products registered in the Hive Console must match. We recommend using the information entered in the Google Play Console.

Next steps

Get the list of products registered in Google Play.