hkyaaa
hkyaaa

Reputation: 100

launchBillingFlow() couldn't display the Google Play purchase screen

I'm using Google Play Billing Library in my app. App relase in internal test relase type. But when I clicked product button, launchBillingFlow() couldn't display the Google Play purchase screen.After click, code follows directly "//DO SOMETHING AND RETURN MENU OPERATIONS" part of block. My apps config lines: firstly in my manifest has a "com.android.vending.BILLING". Secondly; my gradle files has a current version of billing library; "implementation 'com.android.billingclient:billing:2.0.3'". But I dont know why still couldn't display purchase screen. My code is as follows;

 import android.content.Intent;
import android.content.SharedPreferences;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

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.PurchaseHistoryResponseListener;
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.ynapp.yesornoapp.R;

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

public class MyProductActivity extends AppCompatActivity  implements PurchasesUpdatedListener {
    //PREMIUM PRODUCTS
    Button btProduct;
    public static final String PR_PREFS_PRODUCT_ONE = "ProductOne";
    SharedPreferences prefs_product_one;
    //Will be used for in-app purchases.
    List<SkuDetails> skuDetailsList;
    BillingClient billingClient;
    Intent itMain;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        themeUtils.onActivityCreateSetTheme(this);
        setContentView(R.layout.activity_my_product);
        setupBillingClient();
        btProduct = (Button)findViewById(R.id.buttonforest);
    }

    public void onClickTheme(View view){
        SharedPreferences sharedPref = getSharedPreferences("MyData",MODE_PRIVATE);//MODE_PRIVATE
        final SharedPreferences.Editor editor = sharedPref.edit();
        switch (view.getId()){
            case R.id.buttonproductone:
                //önceden ödeme yapılıp yapılmadığını sorguluyoruz
                prefs_product_one= getSharedPreferences(PR_PREFS_PRODUCT_ONE,MODE_PRIVATE);
                final int product_one_flag = prefs_product_one.getInt("productone",0);
                List<String> skuList = new ArrayList<>();
                skuList.add("purch_product_one");

                if(billingClient.isReady() && product_one_flag==0){
                    SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
                    params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP).build();

                    billingClient.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("prefs_product_one".equals(sku)){
                                        Toast.makeText(MyProductActivity.this, sku + ": " + price, Toast.LENGTH_SHORT).show();
                                        BillingFlowParams flowParams = BillingFlowParams.newBuilder()
                                                .setSkuDetails(skuDetails)
                                                .build();
                                        BillingResult billingResponseCode = billingClient.launchBillingFlow(MyProductActivity.this, flowParams);
                                        if (billingResponseCode.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                                            //DO SOMETHING AND RETURN MENU OPERATIONS
                                        }
                                    }
                                }
                            }

                        }

                    });
                }
                else{
                    if(product_one_flag ==0)
                        Toast.makeText(MyProductActivity.this, "Cannot query product", Toast.LENGTH_SHORT).show();
                    else{
                        Toast.makeText(getApplicationContext(),btProduct.getText().toString(),Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
    }

    public  void setupBillingClient(){
        billingClient = BillingClient.newBuilder(this).enablePendingPurchases().setListener(this).build();
        billingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(BillingResult billingResult) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {

                    Toast.makeText(MyProductActivity.this, "Success to connect Billing", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onBillingServiceDisconnected() {

            }
        });
    }


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

    }
}

Upvotes: 0

Views: 1812

Answers (1)

DrPower
DrPower

Reputation: 445

Why do you start purchase when parsing results of calling querySkuDetailsAsync? You should have a button and launchBillingFlow in the button's onClick event or something triggered by user's action.

In querySkuDetailsAsync's callback, you can parse the results and get some SKU metadata to render your paywall.

Upvotes: 0

Related Questions