简体   繁体   中英

How do I properly implement the Google Play Billing Library

So I'm trying to implement the Google Play Billing Library into my app so that I can allow users to make a in app purchase. And so far I feel as if it's going alright I've managed to follow the guide quite well, but the last step is confusing me, I don't know how to implement the BillingFlowParams to be more exact I'ms truggleing with the setSkuDetails , I know it says

Retrieve a value for "skuDetails" by calling querySkuDetailsAsync()

but I have no idea what params to pass there, I tried looking at the documentation but that made me even more confused.

This is what I have so far

package com.my.name.ui;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.android.billingclient.api.SkuDetails;
import com.android.billingclient.api.SkuDetailsParams;
import com.android.billingclient.api.SkuDetailsResponseListener;
import com.my.name.R;

import java.util.ArrayList;
import java.util.List;

public class PremiumFragment extends Fragment implements PurchasesUpdatedListener {


    private String LICENSE_KEY = "removedforthesakeofthepost";
    private String ITEM_SKU_ADREMOVAL = "myadremoval";
    private String mAdRemovalPrice;

    private Button btnPurchase;


    private BillingClient mBillingClient;




    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mBillingClient = BillingClient.newBuilder(getContext()).enablePendingPurchases().setListener(this).build();
        mBillingClient.startConnection(new BillingClientStateListener() {

            @Override
            public void onBillingSetupFinished(BillingResult billingResult) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                    // The billing client is ready. You can query purchases here.

                    List skuList = new ArrayList<>();
                    skuList.add(ITEM_SKU_ADREMOVAL);
                    SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
                    params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
                    mBillingClient.querySkuDetailsAsync(params.build(),
                            new SkuDetailsResponseListener() {
                                @Override
                                public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {
                                    if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && skuDetailsList != null) {
                                        for (SkuDetails skuDetails : skuDetailsList) {
                                            String sku = skuDetails.getSku();
                                            String price = skuDetails.getPrice();
                                            if (ITEM_SKU_ADREMOVAL.equals(sku)) {
                                                mAdRemovalPrice = price;
                                            }
                                        }
                                    }
                                }


                            });

                }
            }

            @Override
            public void onBillingServiceDisconnected() {
                //TODO implement your own retry policy
                // Try to restart the connection on the next request to
                // Google Play by calling the startConnection() method.
            }
        });

        View rootView = inflater.inflate(R.layout.fragment_premium, container, false);

        btnPurchase = rootView.findViewById(R.id.purchasebtn);

        btnPurchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //No idea what to do here.

                // Retrieve a value for "skuDetails" by calling querySkuDetailsAsync().
                BillingFlowParams flowParams = BillingFlowParams.newBuilder()
                        .setSkuDetails(skuDetails)
                        .build();
                int responseCode = billingClient.launchBillingFlow(flowParams);

            }
        });

        return rootView;
    }







    @Override
    public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {

    }
}

You have to enable your button "btnPurchase" in onBillingSetupFinished and disable on onBillingServiceDisconnected (on start it disabled). So in "btnPurchase.setOnClickListene / onClick" you have to call "billing_client.querySkuDetailsAsync" (your code is right) and

for( SkuDetails skuDetails : sku_details_list ){
    if( ITEM_SKU_ADREMOVAL.equals( skuDetails.getSku() ) ){
        BillingFlowParams flow_params = BillingFlowParams.newBuilder()
                .setSkuDetails( skuDetails )
                .build();
        billing_client.launchBillingFlow( MyActivity.this, flow_params );
    }
}

Don't forget about "billing_client.acknowledgePurchase"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM