📜 ⬆️ ⬇️

Shopping in the Android application - Play Billing Library

image

And how is it still on Habré no article about it? No matter, you need to fix it.

There are 2 ways to add in-app purchases in an Android app - old and new. Until 2017, everyone used the library from anjlab, but since June 2017 the situation has changed, Google has released its own library for internal purchases and subscriptions - Play Billing Library. Now the latter is considered standard.

Play Billing Library is very simple.
')
Connect the dependency.

implementation 'com.android.billingclient:billing:1.2' 

Add permission in the manifest.

 <uses-permission android:name="com.android.vending.BILLING"/> 

Create a BillingClient instance and start the connection.

 private BillingClient mBillingClient; ... mBillingClient = BillingClient.newBuilder(this).setListener(new PurchasesUpdatedListener() { @Override public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) { if (responseCode == BillingClient.BillingResponse.OK && purchases != null) { //       } } }).build(); mBillingClient.startConnection(new BillingClientStateListener() { @Override public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) { if (billingResponseCode == BillingClient.BillingResponse.OK) { //         } } @Override public void onBillingServiceDisconnected() { //    -    } }); 

We get to the onPurchasesUpdated () method when a purchase is made, in the onBillingSetupFinished () method you can request information about goods and purchases.

Request information about products. Place the querySkuDetails () in onBillingSetupFinished () .

 private Map<String, SkuDetails> mSkuDetailsMap = new HashMap<>(); private String mSkuId = "sku_id_1"; ... @Override public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) { if (billingResponseCode == BillingClient.BillingResponse.OK) { //         querySkuDetails(); //   } } ... private void querySkuDetails() { SkuDetailsParams.Builder skuDetailsParamsBuilder = SkuDetailsParams.newBuilder(); List<String> skuList = new ArrayList<>(); skuList.add(mSkuId); skuDetailsParamsBuilder.setSkusList(skuList).setType(BillingClient.SkuType.INAPP); mBillingClient.querySkuDetailsAsync(skuDetailsParamsBuilder.build(), new SkuDetailsResponseListener() { @Override public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) { if (responseCode == 0) { for (SkuDetails skuDetails : skuDetailsList) { mSkuDetailsMap.put(skuDetails.getSku(), skuDetails); } } } }); } 

In the code, you might notice the concept of SKU, what is it? SKU - from the English Stock Keeping Unit (commodity item identifier).

Now in mSkuDetailsMap , we have all the information about the goods (name, description, price) registered in the Play Console of this application (more on that later). Pay attention to this line skuList.add (mSkuId); , here we have added the product id from the Play Console, list here all the products you want to interact with. We have one product —sku_id_1.

Everything is ready to fulfill the purchase request. We transfer the product id. Run this method, for example, by clicking on the button.

 public void launchBilling(String skuId) { BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder() .setSkuDetails(mSkuDetailsMap.get(skuId)) .build(); mBillingClient.launchBillingFlow(this, billingFlowParams); } 

Now, running this method, you will see this dialog box (note. Pictures from the Internet).

image

Now, if the user buys the product - he must provide it. Add the payComplete () method and perform actions in it that give access to the purchased product. For example, if a user bought an ad disconnect, make this method so that the ad no longer appears.

 ... @Override public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) { if (responseCode == BillingClient.BillingResponse.OK && purchases != null) { //       payComplete(); } } ... 

Everything is good, but if the user restarts the application, our program knows nothing about purchases. It is necessary to request information about them. Do this in onBillingSetupFinished () .

 @Override public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) { if (billingResponseCode == BillingClient.BillingResponse.OK) { //         querySkuDetails(); //   List<Purchase> purchasesList = queryPurchases(); //   //   ,    for (int i = 0; i < purchasesList.size(); i++) { String purchaseId = purchasesList.get(i).getSku(); if(TextUtils.equals(mSkuId, purchaseId)) { payComplete(); } } } } ... private List<Purchase> queryPurchases() { Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.INAPP); return purchasesResult.getPurchasesList(); } 

In purchasesList the list of all purchases made by the user is displayed.

We make a check: if the item is purchased, perform payComplete () .

Is done. It remains to publish this application in the Play Console and add products. How to add a product: Application page description > Content for sale > Create limited content .

Note 1 : You will not be able to add the product until you download the build of the application in the Play Console.

Note 2 : To see the purchase dialog box, you need to load the build in the Play Console, add the product and wait a while (~ 30 minutes - 1 hour - 3 hours) until the product is updated, only after that the dialog box appears and you can will make a purchase.

Note 3 : Error Please fix the input params. SKU can't be null - the product in the Play Console has not yet been updated, wait.

Note 4 : You may encounter Error Error “Your transaction cannot be completed” , in the logs as response code 6 while you are testing. For what reasons this is exactly unknown to me, but according to my observations this happens after frequent manipulations with the purchase and return of goods. To fix this, go to the bank card menu and re-add your card. How to avoid it? Add your Play Console account as a tester and only buy from a test card.

GitHub demo

Buy me a coffee

(By the way, on Habré the donate system works on the button under the article - comment of the moderator).

Source: https://habr.com/ru/post/444072/


All Articles