When developing live video software, developers often add some functions to the live software at the request of customers, such as developing lucky gifts, adding microblog login function, or accessing Google Payment function which is not commonly used in China. What should we do?This article talks about the implementation of Google Payment by Android Client for Live Software.
First, add dependencies to the gradle as follows:
compile 'com.android.billingclient:billing:1.1'
1. Initialize related variables
private Context mContext;//Context Object private CoinBean mBean;//Purchased Goods Information Class private googlePayCallback mGooglePayCallback;//Payment Callback private BillingClient mBillingClient; public GooglePayTask(Context context , CoinBean bean, googlePayCallback callback) { mContext = context; mBean = bean; mGooglePayCallback = callback; mBillingClient=BillingClient.newBuilder(mContext).setListener(this).build(); }
2. Connect to the Google market with the following code
mBillingClient.startConnection(new BillingClientStateListener() { @Override public void onBillingSetupFinished(int responseCode) { L.e("googlePay","Connect to Google Market"); } @Override public void onBillingServiceDisconnected() { //Unable to connect to Google Play ToastUtil.show(WordUtil.getString(R.string.cannot_conn_google)); if (mGooglePayCallback!=null){ mGooglePayCallback.onServiceDissconnected(); } } });
3. When the live video software connects to the Google market successfully, you need to query if there is any commodity information in the app at this time, the specific code is as follows
List<String> skuList = new ArrayList<>(); L.e("googlePlay",mBean.getId()); skuList.add(mBean.getId());//id of the goods to be queried SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder(); params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP); mBillingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() { @Override public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) { //L.e("googlePay", "Error querying commodity information, code ="+responseCode+skuDetailsList); if (responseCode== BillingClient.BillingResponse.OK && skuDetailsList != null){ L.e("googlePay","There is commodity information"); ToastUtil.show("There is commodity information"); }else { L.e("googlePay","Error querying commodity information, code = "+responseCode); if (mGooglePayCallback!=null){ mGooglePayCallback.onFailed(); release(); } } } });
4. When the commodity information is queried, the payment will be initiated according to the id of the commodity queried in the previous step.
BillingFlowParams flowParams = BillingFlowParams.newBuilder() .setSku(mBean.getId())//Commodity id .setType(BillingClient.SkuType.INAPP) .build(); int responseCode = mBillingClient.launchBillingFlow(((MyCoinActivity)mContext),flowParams);
After listening for successful callbacks to pay, request the relevant interface, go to the service to pay successful callbacks, increase the corresponding purchases, at this time also need to consume the purchased goods through BillingClient's consumeAsync method, then the whole Google payment purchase process will really end.
That's how Google pays to connect to the live video software android client. Refer to the official documentation for other configurations.More dry articles related to live video software development will be released gradually in the future. Please look forward to them. If you need any friends, please pay attention to me.
Statement: The article is original content, please note CSDN link and author for reprinting