1. What is it and why is it needed?In-App Purchase, roughly speaking, is a service of buying virtual goods within the application (for example, game currency, new levels, game items, etc.). It is used mainly in games, in cases when the question arises of the need to earn money on his creation, but I do not really want to distribute it for a fee (or there is no sense).
2. In-app purchase in android applicationsWhen I was faced with the need to use in-app purchase in the game being developed, it was very difficult to find detailed Russian-language information on how to tie this service to my game. The best service was described
in one of Habr's articles , but I would like to show my vision on this issue. Therefore, I decided to write a small manual for connecting the service to the application.
')
For starters, it would be nice to figure out how it all works.
In principle, the whole process of functioning was pretty well described in the article I referred to above, so repeating the same thing again does not make much sense. Therefore, I will get straight to the point.
It is assumed that you already have a developer account on the Android Market.
In order to work with the system of purchases within the application, we need a file called IMarketBillingService.aidl. You can find it in the sample 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 IMarketBillingService.aidl file into your project from the downloaded example. It is important that it lies in com.android.vending.billing. After that, add the extension to the manifest: <uses-android: name = "com.android.vending.BILLING" />.
In the sample application, in addition to the file described above, there are also several implemented classes for working with the payment system. Copy them into your project. What is each of them:
BillingReceiver - receives all asynchronous responses from the market and sends them further to the BillingService;
BillingService - sends requests to the market;
Consts - contains all the constants of the sample application;
Dungeons - provides UI and displays the history of purchases made;
Purchase Database - local database;
PurchaseObserver - monitoring changes in purchases;
ResponseHandler - update database and UI;
Security - security;
Base64 and Base64DecoderException - coding from binary to base64. Required for the operation of the Security class.
In the Security class, look for the string:
String base64EncodedPublicKey = “…”
and enter here your PublicKey, received when registering an account on the Android Market.
In the Dungeons class, look for a list of products that are supposed to be sold, and change them for your own. At the same time, the goods must be uploaded to the Android Market and published (the application itself is not necessary to publish it - if it is tested, however, you need to remember to add yourself (or someone else) to the developers (done in the profile settings)). Next, we add the interface and, in principle, the application is ready.
However, you should take care of security. The best way to secure purchases is to use the appropriate service on the server. After the user has made a purchase of goods, the market will send a JSON line with information about the purchased product:
{ "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.
At the server side of the application, you need to check the returned string with the signature and your key (RSA encryption) and return the result of the check to the client. For this there is a
small open library written in php, I used it.
For more information about security when working with in-app purchase is written
here .