Benoit
Benoit

Reputation: 1

billingClient.queryProductDetailsAsync returning only the details for the first subscription, not for all

I have 3 subscription named: plana, planb, planc.

Each one has a 7-day free trial offer and a base plan (starting after the trial period).

My app should show the pricing for all the 3 plans in their respective textView (tv_priceplana, tv_priceplanb, tv_priceplanc), but only displays for the first one (see below scrrenshots)

What I get in the best case:

(what I get)

What I want:

(what I want)

Here below is the part of the code to show details of the plans. I don't understand why it works only for the details of the first subscription plan (see above sreenshot), not hte others. I have tried to move the if-part (see comment in the code) to different places, but it gives me in the best case, the phases of the first plan (free trial and base plan), but not the other ones. What is wrong? What is the right code to do it?

Please note that I also tried to clear cache and data, and consider that I want to keep my textViews (not a recycler) for my only 3 plans. And for information, I use the billing version 7.1.1

Thanks in avance for your help.

 private void getSubsDetails(){
        billingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
                if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.OK){
                    ImmutableList<QueryProductDetailsParams.Product> productList = ImmutableList.of(
                            // Plan A //
                            QueryProductDetailsParams.Product.newBuilder()
                                    .setProductId(productIdPlanA)
                                    .setProductType(BillingClient.ProductType.SUBS)
                                    .build(),
                            // Plan B //
                            QueryProductDetailsParams.Product.newBuilder()
                                    .setProductId(productIdPlanB)
                                    .setProductType(BillingClient.ProductType.SUBS)
                                    .build(),
                            // Plan C //
                            QueryProductDetailsParams.Product.newBuilder()
                                    .setProductId(productIdPlanC)
                                    .setProductType(BillingClient.ProductType.SUBS)
                                    .build()
                    );

                    QueryProductDetailsParams queryProductDetailsParams = QueryProductDetailsParams.newBuilder()
                            .setProductList(productList)
                            .build();

                    billingClient.queryProductDetailsAsync(queryProductDetailsParams, new ProductDetailsResponseListener() {
                        @Override
                        public void onProductDetailsResponse(@NonNull BillingResult billingResult, @NonNull List<ProductDetails> productDetailsList) {
                            for(ProductDetails productDetails:productDetailsList) {
                                List<ProductDetails.PricingPhase> pricingPhases = productDetails.getSubscriptionOfferDetails().get(0).getPricingPhases().getPricingPhaseList();
                                productId = productDetails.getProductId();
                                String formattedPrice = pricingPhases.get(0).getFormattedPrice();
                                String billingPeriod = pricingPhases.get(0).getBillingPeriod();
                                int recurrenceMode = pricingPhases.get(0).getRecurrenceMode();
                                String number, duration, bp;
                                bp = billingPeriod;
                                number = billingPeriod.substring(1, 2);
                                duration = billingPeriod.substring(2, 3);
                                if (recurrenceMode == 2) {
                                    if (duration.equals("M")) {
                                        offerDuration = "For " + number + " Month(s)";
                                    } else if (duration.equals("Y")) {
                                        offerDuration = "For " + number + " Year(s)";
                                    } else if (duration.equals("W")) {
                                        offerDuration = "For " + number + " Week(s)";
                                    } else if (duration.equals("D")) {
                                        offerDuration = "For " + number + " Days";
                                    }
                                } else {
                                    if (bp.equals("P1M")) {
                                        offerDuration = " Monthly";
                                    } else if (bp.equals("P6M")) {
                                        offerDuration = " every 6 Months";
                                    } else if (bp.equals("P1Y")) {
                                        offerDuration = " Yearly";
                                    } else if (bp.equals("P1W")) {
                                        offerDuration = " Weekly";
                                    }
                                }
                                phasesPlan = formattedPrice + " " + offerDuration;
                                for (int i = 0; i <= (pricingPhases.size()); i++) {
                                    if (i > 0) {
                                        String offrePeriod = pricingPhases.get(i).getBillingPeriod();
                                        String price = pricingPhases.get(i).getFormattedPrice();
                                        if (offrePeriod.equals("P1M")) {
                                            offerDuration = " Monthly";
                                        } else if (offrePeriod.equals("P6M")) {
                                            offerDuration = " every 6 Months";
                                        } else if (offrePeriod.equals("P1Y")) {
                                            offerDuration = " Yearly";
                                        } else if (offrePeriod.equals("P1W")) {
                                            offerDuration = " Weekly";
                                        } else {
                                            offerDuration = "";
                                        }
                                        phasesPlan += "\n" + price + offerDuration;
                                    }
                                }
                                // I moved this if part to different places, but it returns nothing or only the first plan details
                                if (productId.equals(productIdPlanA)) {
                                    phasesPlanA = phasesPlan;
                                } else if (productId.equals(productIdPlanB)){
                                    phasesPlanB = phasesPlan;
                                } else if (productId.equals(productIdPlanC)){
                                    phasesPlanC = phasesPlan;
                                }
                            }
                        }
                    });
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            // Showing in Layout
                            tv_priceplana.setText(phasesPlanA);
                            tv_priceplanb.setText(phasesPlanB);
                            tv_priceplanc.setText(phasesPlanC);
                        }
                    });
                }
            }

            @Override
            public void onBillingServiceDisconnected() {

            }
        });
    }

Upvotes: 0

Views: 23

Answers (0)

Related Questions