Connecting to The Service

You need to make a few simple setup Action's before you will start using the Vending API with Android Native.

Enable Vending Service

Just make sure that Vending service is enabled under the Android Native services list. See the screenshot below:

Include RSA public key

Copy the Base64-encoded RSA public key from google play console:

And set it inside the Vending android Native Settings

Define In-App products via Editor UI

This step isn't required. Because once you connected to the billing service you can get all the information about the in-app products available for your user. But the reason we give you this ability is the following:

  • Not all users what to bother to retrieve product details after the connection. 
  • In some cases, you want to be able to show user products options to purchase before you connected to the billing service.

So for you confines you may fill products list under the same services tab:

The products you define inside this UI will be available in the runtime with AN_Billing.Inventory.Products. See the example bellow:

using SA.Android.Vending.Billing;;
...

foreach (AN_Product product in AN_Billing.Inventory.Products) {
    AN_Logger.Log("product.ProductId" + product.ProductId);
    AN_Logger.Log("product.Type" + product.Type);
    AN_Logger.Log("product.Price" + product.Price);
    AN_Logger.Log("product.Title" + product.Title);
    AN_Logger.Log("product.Description" + product.Description);
    AN_Logger.Log("product.PriceAmountMicros" + product.PriceAmountMicros);
    AN_Logger.Log("product.PriceCurrencyCode" + product.PriceCurrencyCode);
    AN_Logger.Log("product.SubscriptionPeriod" + product.SubscriptionPeriod);
    AN_Logger.Log("product.FreeTrialPeriod" + product.FreeTrialPeriod);
    AN_Logger.Log("product.SubscriptionPeriod" + product.SubscriptionPeriod);
    AN_Logger.Log("product.FreeTrialPeriod" + product.FreeTrialPeriod);
    AN_Logger.Log("product.IntroductoryPrice" + product.IntroductoryPrice);
    AN_Logger.Log("product.IntroductoryPriceAmountMicros" + product.IntroductoryPriceAmountMicros);
    AN_Logger.Log("product.IntroductoryPricePeriod" + product.IntroductoryPricePeriod);
    AN_Logger.Log("product.IntroductoryPriceCycles" + product.IntroductoryPriceCycles);
    AN_Logger.Log("product.OriginalJson" + product.OriginalJson);
    AN_Logger.Log("----------------------------------------------------");
}

Once the connection to billing is established. This data will be overridden with data received from Google Play Billing Service.

Define In-App products via C# 

As an alternative, you may define all the Vending settings with the plugin C# API. You can set everything up using the code, or Editor UI, or you even may combine those approaches. The example below will demonstrate how to fulfill vending settings with C# API.

using SA.Android;
...

//Getting link to the plugin settings object.
var settings = AN_Settings.Instance;

//Enable Vending
settings.Vending = true;
//Enable Licensing
settings.Licensing = true;
//Define Public Key
settings.RSAPublicKey = "MIIBIjANBg......kqhkiG9w0B";

//It's importent to clear the products list. If you want to define all the prodcuts from scratch.
settings.InAppProducts.Clear();

var p1 = new AN_Product("example.product.1", AN_ProductType.inapp);
p1.Title = "Example 1";
settings.InAppProducts.Add(p1);

var p2 = new AN_Product("example.product.2", AN_ProductType.subs);
p2.Title = "Example 2";
settings.InAppProducts.Add(p2);

Connecting to The Service

Before you can make Google Play Billing requests, you must first establish a connection to Google Play. Once the connection is established you can already get information about user purchases and available in-app products for the current app.

The following code sample demonstrates how to start a connection and then test that it's ready to use:

using SA.Android.Vending.Billing;;
...

AN_Billing.Connect((result) => {
    AN_Logger.Log("Connect result.IsSucceeded: " + result.IsSucceeded);
    if(result.IsSucceeded) {
        AN_Logger.Log("AN_Billing.Inventory.Purchases.Count: " + AN_Billing.Inventory.Purchases.Count);
        AN_Logger.Log("AN_Billing.Inventory.Products.Count: " + AN_Billing.Inventory.Products.Count);

        //Let's print all purchases info
        foreach(AN_Purchase purchase in AN_Billing.Inventory.Purchases) {
            AN_Logger.Log("purchase.OrderId" + purchase.OrderId);
            AN_Logger.Log("purchase.ProductId" + purchase.ProductId);
            AN_Logger.Log("purchase.PackageName" + purchase.PackageName);
            AN_Logger.Log("purchase.PurchaseState" + purchase.PurchaseState);
            AN_Logger.Log("purchase.PurchaseTime" + purchase.PurchaseTime);
            AN_Logger.Log("purchase.Signature" + purchase.Signature);
            AN_Logger.Log("purchase.Token" + purchase.Token);
            AN_Logger.Log("purchase.Type" + purchase.Type);
            AN_Logger.Log("purchase.DeveloperPayload" + purchase.DeveloperPayload);
            AN_Logger.Log("purchase.AutoRenewing" + purchase.AutoRenewing);
            AN_Logger.Log("purchase.OriginalJson" + purchase.OriginalJson);
            AN_Logger.Log("----------------------------------------------------");
        }

        //And products info as well
        foreach (AN_Product product in AN_Billing.Inventory.Products) {
            AN_Logger.Log("product.ProductId" + product.ProductId);
            AN_Logger.Log("product.Type" + product.Type);
            AN_Logger.Log("product.Price" + product.Price);
            AN_Logger.Log("product.Title" + product.Title);
            AN_Logger.Log("product.Description" + product.Description);
            AN_Logger.Log("product.PriceAmountMicros" + product.PriceAmountMicros);
            AN_Logger.Log("product.PriceCurrencyCode" + product.PriceCurrencyCode);
            AN_Logger.Log("product.SubscriptionPeriod" + product.SubscriptionPeriod);
            AN_Logger.Log("product.FreeTrialPeriod" + product.FreeTrialPeriod);
            AN_Logger.Log("product.SubscriptionPeriod" + product.SubscriptionPeriod);
            AN_Logger.Log("product.FreeTrialPeriod" + product.FreeTrialPeriod);
            AN_Logger.Log("product.IntroductoryPrice" + product.IntroductoryPrice);
            AN_Logger.Log("product.IntroductoryPriceAmountMicros" + product.IntroductoryPriceAmountMicros);
            AN_Logger.Log("product.IntroductoryPricePeriod" + product.IntroductoryPricePeriod);
            AN_Logger.Log("product.IntroductoryPriceCycles" + product.IntroductoryPriceCycles);
            AN_Logger.Log("product.OriginalJson" + product.OriginalJson);
            AN_Logger.Log("----------------------------------------------------");

        }
    } else {
        AN_Logger.Log("Billing service connection failed: " + result.Error.FullMessage);
    }
});

Some Android phones might have an older version of the Play Store app that doesn't support certain products types, such as subscriptions. Therefore, before your app enters the billing flow, and after you connected to the billing service you may check if all the feature you need is supported by the current device:

AN_Billing.Connect((result) => {
    AN_Logger.Log("Connect result.IsSucceeded: " + result.IsSucceeded);
    AN_Logger.Log("Connect result.IsInAppsAPIAvalible: " + result.IsInAppsAPIAvalible);
    AN_Logger.Log("Connect result.IsSubsAPIAvalible: " + result.IsSubsAPIAvalible);
    ....
    ///Code from pervious example 
});

Products Availability

This can be very useful for an experienced developer who wishes to finetune their projects. Let's say you set 3 products for your app. It doesn't matter if you defined your products via Editor UI or via C# code. So you have defined:

  • Product A
  • Product B
  • Product C

Once you connected to billing service as you know the information you defined for your products will be overridden by values received from billing service. What you want to know is if all of those products were available on billing service. since if a billing device doesn't know about this product, most likely you would not able to purchase it. So it probably would be better to hide the product if it isn't available. Once product values are overridden from billing service the IsValid flag will be set to true. See the example below:

using SA.Android.Vending.Billing;
...

AN_Billing.Connect((result) => {
    AN_Logger.Log("Connect result.IsSucceeded: " + result.IsSucceeded);
    if(result.IsSucceeded) {
        //And products info as well
        foreach (AN_Product product in AN_Billing.Inventory.Products) {
			Debug.Log("product.ProductId" + product.ProductId);
			if(product.IsValid) {
				Debug.Log("This product is available for purchasing"); 
			} else {
				Debug.Log("This product can't be purchased"); 
			}
            Debug.Log("----------------------------------------------------");
        }
    } else {
        Debug.Log("Billing service connection failed: " + result.Error.FullMessage);
    }
});