Access to Google Billing-Inapp Payment for Android Development

1. Prerequisites for using Google Pay:

1. Google Play can be installed on the phone and can be opened.

2. Publish your own app on the Google Play Console (with payment permission, which will be discussed below), you can only publish the Beta version (you can also publish the app that has not been developed, and the app needs to be signed). As shown in the figure below: (If the result in the figure below is achieved, and the app status is released, you can test Google payment, but you need to bind a bank card, which can be Googled. Add a test gmail account in the Develop Console, so that you can not There will be a real deduction!):

 

2. Related configuration on Android Studio

1. Add permissions:

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

2. Need to import in the tool class:

   a. Open the Android SDK Manager in Android Studio (in Tools-->Android), find and install the Google Play Billing Library item.

   b. Find the thing downloaded above, in the path of your Android Studio SDK installation ----> \extras\google\play_billingsamples\TrivialDrive\src\com\example\android\trivialdrivesample\util, copy the util file Folder all the classes in it to your Android Studio, as shown below: (all the classes in it are used).

   

   c. Find the IInAppBillingService.aidl file under \extras\google\play_billing under the path of your Android Studio SDK installation, and copy it to the location of your Android Studio project as shown below: (make sure the location is the same)

3. Start testing Google Pay

   1. Initialize the IabHelper class (the parameter base64EncodedPublicKey is as shown below, found in the Google Play Console, provided the app has been released):

mHelper = new IabHelper(context, base64EncodedPublicKey);

Then initialize, the relevant parameters can refer to the official documentation:

mHelper.startSetup

The failure of initialization depends on the successful installation of Google Service on the test device, and finally whether it can successfully open Google Play 2. Set the type of in-app product in Google Play (including its ID, which will be used when purchasing):

     a. Managed goods---single purchases, such as gold coins in the game

     b. Subscription --- eg, VIP purchase

If it is a managed commodity, it must be consumed after buying it. If it is not consumed after purchasing, it cannot be purchased next time. This is very important.

3. Some methods related to Google payment

  a.购买--launchPurchaseFlow(Activity, String, int, OnIabPurchaseFinishedListener, String) 

  b.consumeAsync(Purchase, OnConsumeFinishedListener) Consume the item (if your item is repurchasable, you need to consume the item after the purchase is successful).

  c.queryInventoryAsync(boolean, List, QueryInventoryFinishedListener) Query the product information owned by the user

 4. Call mHelper.onActivityResult(requestCode,resultCode,data) in the onActivityResult method of the Activity, otherwise the purchase end monitoring method will not be called after the purchase.

 

Summary: I just talked about the core things, some details were not mentioned, but they are all small problems, which can be found by Google (such as purchase parameters, consumption parameters, etc.), but there is a pit that Google is difficult to solve, that is, if the mobile phone does not give The Google Play app allows background pop-up permissions. When the purchase is executed, nothing happens, the purchase is unsuccessful, and it does not return, which is very annoying. This point needs attention .

 

 

. . . . . . . . . . . . Finally, I hope that you will be able to show a purchase order similar to the one below in the test:

That means that the payment in your google app has been completed, and then you will feel that it is actually very simple.

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325649107&siteId=291194637