Buy In-app Billing Products
Once your app is connected to Google Play, you can initialize your purchase request for in-store items. Google Play provides a billing interface that allows users to access their payment methods, so your program doesn't have to deal with payment transactions directly.
When a item is purchased, Google Play will assume that the user already owns the product when a product is purchased and will not allow the user to purchase a product with the same product ID until the item is "consumed". You can control how the product is consumed in your program, and then tell Google Play,google Play to make the product available again after consumption.
You can also quickly get a list of items that a user has already purchased by querying Google Play. This method is useful. For example, when a user launches your app, you want to give the user what they have already purchased. NOTE 1
Buy a product
By invoking IabHelper thelaunchPurchaseFlow(Activity, String, int, OnIabPurchaseFinishedListener, String) 方法从你的应用中开始一个购买请求。你必须在你Activity的主线程里调用这个方法。下面是对launchPurchaseFlow方法中使用参数的解释:
- The first argument is the one called
Activity。
- The second parameter is the product ID (also known as the SKU of the commodity) to be purchased. Make sure you use an ID instead of a product name. You should have already defined and activated the product in the developer console, otherwise the product will not be recognized.
- The third parameter is a request code. This code can be any positive integer. Google Play will return this code along with the purchase response to the calling
Activity的 onActivityResult method.
- The fourth parameter is a listener (listener). This listener is notified when the purchase operation is complete and when the purchase response to Google Play is processed.
- The fifth parameter contains a ' developer payload ' string that you can use to send additional information about an order (which can be an empty string). Typically this parameter is used with a string token that uniquely identifies the purchase request. If you specify a value for a string type, Google play will send you the value along with the purchase response. Then, when you search for this product, Google play will return the string along with the product details.
Security advice: a good practice is to send a string that helps your program identify which user purchased the product, so that you can later verify that the user's purchase is real. For consumable items, you can use a randomly generated string, and for non-expendable items, you should use a string that uniquely identifies the user.
The following example uses a request code 10001 and an encrypted developer Playload string to show how to purchase a product ID ofSKU_GAS的商品。
Mhelper.launchpurchaseflow (This, Sku_gas, 10001, "Bgoa+v7g/yqdxvkrqq+jtfn4uqzbpiqjo4pf9rzj ");
If the purchase order is successful, if the purchase is successful, Google play will save the returned data to aPurchase对象中,这个对象会传给listener。
The following example shows the processing done in listener based on the success of the purchase and whether the user purchased gas or Premiun upgrade. In this case, gas is an in-store item that can be purchased multiple times, so you should consume the item before allowing the user to buy it again. Learn how to consume goods and see the consumable parts. This premium upgrade is a one-time purchase, so you don't have to consume it. It's a good idea to update the UI immediately after a successful purchase so that your users can see their new purchases.
Iabhelper.oniabpurchasefinishedlistener Mpurchasefinishedlistener=NewIabhelper.oniabpurchasefinishedlistener () { Public voidoniabpurchasefinished (iabresult result, Purchase Purchase) {if(Result.isfailure ()) {LOG.D (TAG,"Error Purchasing:" +result); return; } Else if(Purchase.getsku (). Equals (Sku_gas)) {//consume The gas and update the UI } Else if(Purchase.getsku (). Equals (Sku_premium)) {//give user access to premium content and update the UI } }};
Security Advice: When you receive a purchase return message from Google play, be sure to check that the return Purchase对象中的数据签名,订单号和developerPayload,确保是你想要的值。你应该验证订单号 orderId 是你先前没有处理过的独一无二的值,以及 developerPayload string matches the token you previously sent for the purchase request (Translator Note: The Developerpayload sent is encrypted, It may be MD5, or maybe Base64, because this payload is returned to you as it is, and you decrypt the comparison is not the original value). As a longer-term security measure, you should do the verification on your own secure server. P.S. This content is similar to the advice I wrote earlier on the blog.
Search for purchased items
After each successful purchase, Google play's in-store service will cache the user's purchase data locally. A good practice is to more through the In-app Billing service to query the user's purchase, for example, whenever the program starts or resumes, go to inquire, so that the user currently owns the situation of the purchase of goods can be reflected in your program.
IabHelper queryInventoryAsync(QueryInventoryFinishedListener) Gets the user's purchase information by invoking the method in the.
这个QueryInventoryFinishedListenerParameter sets a listener that will be notified when the query operation completes and the query response results are processed. It is safe to call this method on the main thread of your program.
Mhelper. Queryinventoryasync(mgotinventorylistener);
If the query succeeds, the query results are stored Inventory in an object and passed back to the listener. The In-app billing service will only return the purchase information completed by the current logged on device user.
Iabhelper.queryinventoryfinishedlistener Mgotinventorylistener=NewIabhelper.queryinventoryfinishedlistener () { Public voidonqueryinventoryfinished (iabresult result, Inventory Inventory) {if(Result.isfailure ()) {//Handle Error here } Else { //does the user have the premium upgrade?Mispremium =inventory.haspurchase (Sku_premium); //Update UI accordingly } }};
Consumption of goods
You can use the In-app billing Version 3 API to track your purchases with ownership information on Google Play. Once an item is purchased, it is considered "owned" and cannot be purchased again from Google Play when in this state. For this item, you must send a consumption request to Google Play so that Google Play can re-enable the item to be purchased. All managed in-store items are consumable. How you use this consumption mechanism in your program depends on you. In general, you will be consuming (for example, in-game currency or reusable game tokens) for those short-term effects that users may want to buy more than once, whereas products that offer a lasting effect once they are purchased do not need to be consumed (for example, an additional upgrade).
You are responsible for controlling and tracking how the goods are purchased to the user. For example, if a user buys in-game currency, you should update the amount of money purchased to the player's property.
Security advice: When a user buys a consumable item, before giving it to the user, you need to send a consumption request to make sure that you receive a successful response from Google Play before you can send the product to the user.
In order to record the purchase consumption, you can call IabHelper the consumeAsync(Purchase, OnConsumeFinishedListener) method in. The first parameter in this method is the Purchase object, which represents the consumed item. The second argument is OnConsumeFinishedListener,当消耗操作完成以及处理来自Google Play的消耗回应时,都会通知这个listener。 that this method can be called in the main thread of the program.
In the following code, you want to consume the gas items that the user previously purchased in your program.
Mhelper.consumeasync (Inventory.getpurchase (Sku_gas), Mconsumefinishedlistener);
The following code shows how to implement it OnConsumeFinishedListener .
Iabhelper.onconsumefinishedlistener Mconsumefinishedlistener =NewIabhelper.onconsumefinishedlistener () { Public voidonconsumefinished (Purchase Purchase, iabresult result) {if(Result.issuccess ()) {//provision the In-app purchase to the user//(for example, credits gold coins to player ' s character) } Else { //Handle Error } }};
Check consumable items when the program starts
One important thing is to check for consumable items when the user launches your app. In general, you will first query the In-app billing service that the user has purchased the product information (v through queryInventoryAsync ) and then get the consumable object through inventory Purchase . If your program detects that a user has consumable items, either way, you should immediately send a consumption request to Google Play and then send the item to the user. See TrivialDrive How the example is implemented when the application starts.
Note 1: The original is useful, for example, when you want to restore the user's purchases when your user launches your app.
Buy In-app Billing Products