Integrating in-app billing

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.

Getting ready

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).

How to do it...

Our first requirement before adding in-app billing to our app is the app's public key from Google Play:

  1. We need to copy the Base64-encoded public key from the console under the app's Services & APIs section:
    How to do it...

    The public app license key

  2. Then, we paste that key into our app. For testing purposes, we can just store it in a field, but we should always obfuscate this key:
    private const string PublicKey = "<app-license-key>";

Next, we will integrate the in-app billing library into our app:

  1. To integrate the in-app billing library into our app, we first install the Xamarin.InAppBilling Component from the Xamarin Component Store.
  2. Then, we create an instance of the InAppBillingServiceConnection instance, passing in the public key from Google Play and the current Activity instance:
    connection = new InAppBillingServiceConnection(
      this, PublicKey);
  3. If there are any errors during the connection attempt, we would want to be notified. Use the following code to enable error messages:
    connection.OnInAppBillingError += (error, message) => {
      // there was an error with the service
    };
  4. We would also want to be notified when the connection succeeds:
    connection.OnConnected += () => {
      // app is connected
    };
  5. Finally, we will initiate the connection using the Connect() method:
    connection.Connect();
  6. Once connected, all operations are performed through the BillingHandler property; thus, we need to get the following instance:
    connection.OnConnected += () => {
      billing = connection.BillingHandler;
    };
  7. If an unknown error occurs while using the InAppBillingHandler instance, the InAppBillingProcesingError event is raised using the following code:
    billing.InAppBillingProcesingError += (message) => {
      // unknown error processing a request
    };
  8. As soon as we are finished with our products and purchases, we will ensure that we release resources using the Disconnect() method:
    connection.Disconnect();

How it works...

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.

Note

Google Play signs the responses with a private key, and the public key is used to verify the signature.

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.

Tip

Connecting to Google Play requires a visible Activity instance because this is where the purchase events are sent to.

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.

Note

Even though the InAppBillingProcesingError event exists, specific events for specific processing errors will be raised, such as the BuyProductError event.

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.210.17