1. What is it and why is it needed?

In-app purchase is a service for buying virtual goods within an application (for example, game currency, new levels, game items, etc.). It is used mainly in games, in those cases when the question arises about the need to earn money on your creation, but you don’t really want to distribute it for a fee (or it makes no sense).

2. In-app purchase in android applications

When I was faced with the need to use in-app purchase in the game under development, it was very difficult to find detailed Russian-language information on how to tie this service to my game. Some services were described in one of our previous articles, but I would like to show my new vision on this issue. Therefore, I decided to write a small manual on how to connect the service to the application.

To begin with, it would be nice to figure out how all of this works.

It is assumed that you already have a developer account on the Android Market.

In order to work with the shopping system inside the application, we need a file called IMarketBillingService.aidl. You can find it in the example application for working with in-app purchase, which in turn is a downloadable component of the Android SDK. Launch the Android SDK and AVD Manager and select Available Packages. Next, you will need to select Third party add-ons -> Google Inc. add-ons -> Google Market Billing package.

Now you need to copy the IMarketBillingService.aidl file to your project from the downloaded example. It is important that it is in com.android.vending.billing. After that, in the manifest, add the extension: <uses-permission android: name = “com.android.vending.BILLING” />.

In the application example, in addition to the file described above, there are also several classes implemented for working with the payment system. Copy them to your project.

What is each of them:

In the Security class, look for the line:

and enter here your PublicKey obtained when registering an account on the Android Market.

In the Dungeons class, we look for a list of products that are supposed to be sold, and change them to our own. At the same time, the goods must be uploaded to the Android Market and published (the application itself is not required to be published – if it is tested, however, do not forget to add yourself (or someone else) to the developers (done in the profile settings)). Next, we add the interface and, in general, the application is ready.

However, you should take care of security. The best way to ensure shopping safety is to use the appropriate service on the server. After the user has completed the purchase of goods, the market will send a JSON line with information about the purchased goods:

{“nonce”: 1836535032137741465,

“orders”:

{“notificationId”: “android.test.purchased”,

“orderId”: “transactionId.android.test.purchased”,

“packageName”: “com.example.dungeons”,

“productId”: “android.test.purchased”,

“developerPayload”: “bGoa + V7g / yqDXvKRqq + JTFn4uQZbPiQJo4pf9RzJ”,

“purchaseTime”: 1290114783411,

“purchaseState”: 0}

}

and signature to authenticate the request.

On the server side of the application, you need to check the returned string using the signature and your key (RSA encryption) and return the verification result to the client. There is a small open library written in php for this, I used it.

Tags:

  • Android
  • in-app billing
  • in-app purchases
  • BillingReceiver – receives all asynchronous responses from the market and sends them further to BillingService;
  • BillingService – sends requests to the market;
    Consts – contains all the constants of the sample application;
    Dungeons – provides a UI and displays the history of completed purchases;
    PurchaseDatabase – a local database;
    PurchaseObserver – monitoring changes related to purchases;
    ResponseHandler – updating the database and UI;
    Security – security;

Base64 and Base64DecoderException – encoding from the binary system to base64. Required for the functioning of the Security class.