GooglePlay Payment Usage Summary 2. googleplay payment Summary
Payment details
Next to the content in the previous article, this article describes the specific items in the payment.
I am using the payment function in fragment because of the company's project requirements. The official demo is called in Activity.
1. Construct an IabHelper instance.
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHelper = new IabHelper(getActivity(), MyApplication.GOOGLE_APP_BILLING_KEY); mHelper.enableDebugLogging(true); mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { public void onIabSetupFinished(IabResult result) { if (!result.isSuccess()) { // Oh noes, there was a problem. Log.d(TAG, "Problem setting up In-app Billing: " + result); return; } if (mHelper == null) return; // Hooray, IAB is fully set up! initQueryItemInfos(); } }); Log.d(TAG, mHelper.toString()); }
The IabHelper object needs to be instantiated. The input parameter is the base64 encoded value of the public_key of the current application, which is stored in the google play background.
Start debug
Add a listener and perform initialization. The code above indicates that the initQueryItemInfos () task is triggered after IabHelper is built.
Let's talk about our processing requirements. We need to present our products to users so that they can be easily selected. But how can we present them to users? We can configure the product details on the google play background, you can also modify it. However, because we may need to modify the product price, it is impossible to control the product information in the Code. In this way, the maintainability is poor and our solution is, store the product ID set on the server. The server opens an interface, and the client is responsible for calling this interface to obtain the relevant id data. Then, through the id data, call the google play interface to obtain the complete product information, such as the description, quantity, and price. (In this way, unified control on the server can control product changes at any time. Clients such as android and ios only need to follow the returned product id, you can call your own payment technology interface to achieve the effect .)
For convenience, I paste the printed information of a product id that is queried.
**
SkuDetails: {"productId": "cash_35", "type": "inapp", "price": "US $0.99", "price_amount_micros": 99, "price_currency_code ": "USD", "title": "Some infos", "description": "35 count "}
**
We focus on the product ID and price.
mAdditionalSkuList.add(CASH_1);mAdditionalSkuList.add(CASH_2);mAdditionalSkuList.add(CASH_5);mAdditionalSkuList.add(CASH_10);mAdditionalSkuList.add(CASH_20);mAdditionalSkuList.add(CASH_35);mAdditionalSkuList.add(CASH_50);mAdditionalSkuList.add(CASH_100);mHelper.queryInventoryAsync(true, mAdditionalSkuList, mQueryFinishedListener);
When we call this initQueryItemInfos () method, we add the product id to the list and callMHelper. queryInventoryAsyncThe method for querying product information is as follows:MQueryFinishedListenerCallback. Let's take a look at the content in this listener.
IabHelper.QueryInventoryFinishedListener mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() { public void onQueryInventoryFinished(IabResult result, Inventory inventory) { if (result.isFailure()) { --- handle error Log.d(TAG, "result error"); return; } List<Bean> beanList = new ArrayList<>(); for (int i = 0; i < mAdditionalSkuList.size(); i++) { SkuDetails skuDetails = inventory.getSkuDetails(mAdditionalSkuList.get(i)); if (skuDetails != null) { Log.d(TAG, skuDetails.toString()); shouCashList.add(new Gson().fromJson(skuDetails.getJson(), Bean.class)); } } --- update the UI mGridAdapter.setGridData(beanList); mGridView.setAdapter(mGridAdapter); mGridView.setOnItemClickListener(MyFragment.this); } };
The listener resolves the returned skuDetails object to our own object as listView or the data in the GridView. Then, we set the data to our BaseAdapter. Associate the GridView with Adapater for some processing.
The implementation class of BaseAdapter is to rewrite several methods. This should be an old topic. In some getView methods, store the object Bean value to our ImageView and TextView. The code in this place will not be pasted out.
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_dialog_list, container, false); mGridView = (GridView) view.findViewById(R.id.shoucash_grid_view); mGridAdapter = new GridAdapter(getActivity()); return view; }
The code in OnCreateView is to construct our Adapter. The data settings are placed in the callback listener of the preceding query excuse.
In this way, when our program loads fragment, we can see the product list on the page, including the product price, name and other information.
The next step is our purchase function. Let's talk about it in the next article. It's very late and it's time to rest.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.