When Google Play is set up to handle in-app billing, the app must first connect to the Google Play service before it can request product details and make purchases.
Before the app can connect to Google Play, we need to create an app listing on the Google Play Developer Console (http://play.google.com/apps/publish).
Our first requirement before adding in-app billing to our app is the app's public key from Google Play:
private const string PublicKey = "<app-license-key>";
Next, we will integrate the in-app billing library into our app:
InAppBillingServiceConnection
instance, passing in the public key from Google Play and the current Activity
instance:connection = new InAppBillingServiceConnection( this, PublicKey);
connection.OnInAppBillingError += (error, message) => { // there was an error with the service };
connection.OnConnected += () => { // app is connected };
Connect()
method:connection.Connect();
BillingHandler
property; thus, we need to get the following instance:connection.OnConnected += () => {
billing = connection.BillingHandler;
};
InAppBillingHandler
instance, the InAppBillingProcesingError
event is raised using the following code:billing.InAppBillingProcesingError += (message) => { // unknown error processing a request };
Disconnect()
method:connection.Disconnect();
To support in-app billing, we need to have created the products and/or subscriptions on the Google Play Developer Console. This will require that the com.android.vending.BILLING
permission be specified and the app be published to one of the channels.
Once this is done, we can start integrating the connection to the Google Play service. To do this, we will need the license key from the console. This allows the service connection to verify that the communication is really from the store and has not been tampered with. This key is found on the console under the app's Services & APIs section. The key should be obfuscated to prevent a malicious user from replacing the public key with another key and thereby removing one form of security.
All logic needed to interact with Google Play is in the Xamarin.InAppBilling
Component from the Xamarin Component Store. This library handles product listing, purchases, and purchase management. The library does not actually perform the tasks itself, but rather delegates the requests to the running Google Play service.
Once we have the key and library installed, we can now connect to the Google Play service. To do this, we create an instance of the InAppBillingServiceConnection
type, passing in the Activity
instance and the public key. Since the connection is asynchronously established, we must attach an event handler to the OnConnected
event. This event is raised when a connection is successful. If there are any errors, the OnInAppBillingError
event is raised instead. To actually initiate the connection, we invoke the Connect()
method.
As soon as the connection is successfully completed, the OnConnected
event is raised and we can get hold of the InAppBillingHandler
instance through the BillingHandler
property on the connection. This handler is then used to perform all further purchases and requests.
If there are any errors while making requests or processing responses, the InAppBillingProcesingError
event on InAppBillingHandler
will be raised. We can then handle these cases.
As soon as the interaction with the store is complete, the purchases are made and the products are consumed, we must disconnect from the service. This requests that any resources that are being held be released and is done through the Disconnect()
method on the connection. Disconnecting also destroys the billing handler; thus, we have to reconnect before interacting with it again.