AptoideBillingSDKManager Unity Documentation

Overview

The AptoideBillingSDKManager is a Unity MonoBehaviour class that acts as a bridge between the Unity application and the Aptoide Billing Native SDK. It provides methods to initialize the SDK, manage billing operations, and handle callbacks from the native SDK. This manager is designed to simplify the integration of in-app purchases and billing-related features in Unity applications.


Features

Initialization

  • Initializes the Aptoide Billing SDK with the required parameters.
  • Establishes a connection to the billing service.

Billing Operations

  • Query SKU details asynchronously.
  • Launch the billing flow for in-app purchases.
  • Consume purchased items.
  • Query purchases made by the user.
  • Check if specific features are supported by the SDK.

App Update Management

  • Check if an app update is available.
  • Launch dialogs or redirect users to the store for updates.

Callback Handling

  • Handles callbacks from the native SDK for billing setup, purchase updates, SKU details, and consumption responses.

Methods

Initialization

InitializePlugin

Initializes the SDK with listeners, a public key, and the game object name.
Parameters:

  • IAppCoinsBillingStateListener _appCoinsBillingStateListener
  • IConsumeResponseListener _consumeResponseListener
  • IPurchasesUpdatedListener _purchasesUpdatedListener
  • ISkuDetailsResponseListener _skuDetailsResponseListener
  • string publicKey
  • string gameObjectName

Initialize

Calls the native SDK to initialize with the provided public key and game object name.

StartConnection

Starts the connection to the billing service.

EndConnection

Ends the connection to the billing service.

Billing Operations

IsReady

Checks if the billing service is ready.
Returns: bool

QuerySkuDetailsAsync

Queries SKU details asynchronously.
Parameters:

  • string[] skus
  • string skuType

LaunchBillingFlow

Launches the billing flow for a specific SKU.
Parameters:

  • string sku
  • string skuType
  • string developerPayload
    Returns: int (response code)

ConsumeAsync

Consumes a purchased item.
Parameters:

  • string purchaseToken

IsFeatureSupported

Checks if a specific feature is supported by the SDK.
Parameters:

  • string feature
    Returns: int (response code)

QueryPurchases

Queries purchases made by the user.
Parameters:

  • string skuType
    Returns: PurchasesResult

App Update Management

IsAppUpdateAvailable

Checks if an app update is available.
Returns: bool

LaunchAppUpdateDialog

Launches a dialog to prompt the user for an app update.

LaunchAppUpdateStore

Redirects the user to the store for an app update.

Callback Handlers

OnBillingSetupFinished

Handles the callback when the billing setup is finished.
Parameters:

  • string responseCode

OnBillingServiceDisconnected

Handles the callback when the billing service is disconnected.

OnPurchasesUpdated

Handles the callback when purchases are updated.
Parameters:

  • string purchasesResultJson

OnSkuDetailsResponse

Handles the callback when SKU details are received.
Parameters:

  • string skuDetailsResultJson

OnConsumeResponse

Handles the callback when a consumption response is received.
Parameters:

  • string consumeResultJson

Integration Notes

To use the AptoideBillingSDKManager, you must create a separate class to handle the game logic for the following operations:

  1. Purchase:
    Implement logic to initiate purchases by calling LaunchBillingFlow and handle the results via the OnPurchasesUpdated callback.

  2. Server-Side Check:
    Perform server-side validation of purchases to ensure security and prevent fraud.

  3. Consumption:
    Manage the consumption of purchased items by calling ConsumeAsync and handling the OnConsumeResponse callback.

  4. Query Purchases:
    Use QueryPurchases to retrieve the list of purchases made by the user.

  5. Query SKUs:
    Use QuerySkuDetailsAsync to fetch details about available SKUs.


Example Workflow

  1. Initialize the SDK using InitializePlugin.
  2. Query available SKUs using QuerySkuDetailsAsync.
  3. Launch the billing flow for a selected SKU using LaunchBillingFlow.
  4. Validate the purchase on the server side.
  5. Consume the purchase if applicable using ConsumeAsync.
  6. Query purchases to update the game state using QueryPurchases.

Extra - Unity Import

Import Unity Package Manager

  1. Open the top menu bar: Window > Package Manager.
  2. Click the + sign and select Import via git URL.
  3. Paste the following link:
    https://github.com/Catappult/appcoins-sdk-unity.git#v0.9.0.7

Android Manifest

Update your AndroidManifest.xml file to include the necessary permissions and queries for SDK communication:

<manifest>
  ...
  <queries>
    <package android:name="com.appcoins.wallet" />
    ...
  </queries>
  <uses-permission android:name="com.appcoins.BILLING" />
  <uses-permission android:name="android.permission.INTERNET" />
  ...
</manifest>

 

Gradle Files

Include the following dependency in your Gradle files to ensure proper integration of the Aptoide Billing SDK:

dependencies {
  implementation("io.catappult:android-appcoins-billing:0.9.0.8") // Check the latest version in mvnrepository
}

 

If you don't already have build.gradle files, you can use the approach outlined below:

Module (baseProjectTemplate.gradle)

allprojects {
    buildscript {
        repositories {
            google()
            jcenter()
        }

        dependencies {
            // Ensure compatibility with the Android Gradle Plugin version preinstalled with Unity
            classpath 'com.android.tools.build:gradle:3.4.3'
        }
    }

    repositories {
        google()
        jcenter()
        flatDir {
            dirs "${project(':unityLibrary').projectDir}/libs"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

 

Application (mainTemplate.gradle)

apply plugin: 'com.android.library'

repositories {
    google()
    mavenCentral()
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation("io.catappult:android-appcoins-billing:0.9.0.8")
    implementation('org.json:json:20210307')
}

android {
    compileSdkVersion 33 // Adjust as required
    buildToolsVersion '33.0.2'

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 33
        versionCode 1
        versionName '1.0'
    }

    lintOptions {
        abortOnError false
    }

    aaptOptions {
        noCompress = ['.unity3d', '.ress', '.resource', '.obb']
    }
}

Additional Tools

Android Logcat

To debug and verify the SDK's behavior, use the Android Logcat plugin.
Steps:

  1. Access Windows > Analysis > Android Logcat from the Unity top bar.
    2. Select the active APK and filter logs using the tag "APPC".
    This helps track detailed information regarding initialization, purchases, and consumption.

Backend Validation

Always perform server-side validation for purchases to ensure security and prevent fraud.


Diceroll Demo App

A full demo app, Diceroll Aptoide, is available as an example to showcase SDK integration. Steps in the workflow:

  1. Initialize the Billing SDK.
  2. Load SKU information.
  3. Query purchases.
  4. Trigger billing flow for purchases or subscriptions.
  5. Validate purchases on the server side.
  6. Deliver items to the user.
  7. Consume the purchase when applicable.

FAQ

What is the recommended Unity version?

The most suitable Unity version for integration is 2021.3.8f1.