Hi I am integrating Google Play Billing Library on my project on a fragment
in android studio to sell a subscription, I alrready make the set up in the Google Play Console uploading my app in internal test and making the subscription basic plan, then I continued to make the logic process in my app following the steps in the documentation, everything is fine, I checked the product list and it has the subscription´s information, but when the app tries to show up the google play window it appears the next error message:
I searched in internet and they said to clear the data, cache of the google play store and google play service and restart the phone I did it but it did not work, I open another app with subscription and it shows the window of the google play subscription without problem, so I dont think the problem is the google play store
I have tried a lot of things, download the app from google play store (internal test), changing a fragment for an activity, using the downgrade version of the libray and nothing work, I share you my code, thanks in advance
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_sin_anuncios, container, false);
PurchasesUpdatedListener purchasesUpdatedListener = new PurchasesUpdatedListener() {
@Override
public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
// To be implemented in a later section.
}
};
billingClient = BillingClient.newBuilder(getContext())
.setListener(purchasesUpdatedListener)
.enablePendingPurchases()
.build();
connectToGooglePlayBilling();
return view;
}
private void connectToGooglePlayBilling(){
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
// The BillingClient is ready. You can query purchases here.
getProductDeatils();
}
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Toast.makeText(getContext(), "No se pudo conectar a GooglePlay, revisa tu conexion a internet.\nIntentando conectarse nuevamente.", Toast.LENGTH_SHORT).show();
connectToGooglePlayBilling();
}
});
}
private void getProductDeatils(){
QueryProductDetailsParams queryProductDetailsParams =
QueryProductDetailsParams.newBuilder()
.setProductList(
ImmutableList.of(
QueryProductDetailsParams.Product.newBuilder()
.setProductId("version_sin_anuncios")
.setProductType(BillingClient.ProductType.SUBS)
.build()))
.build();
Activity activity = getActivity();
billingClient.queryProductDetailsAsync(
queryProductDetailsParams,
new ProductDetailsResponseListener() {
public void onProductDetailsResponse(BillingResult billingResult,
List<ProductDetails> productDetailsList) {
// check billingResult
// process returned productDetailsList
if(billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && productDetailsList != null){
TextView descripcion = view.findViewById(R.id.textViewSinAnuncios);
descripcion.setText(productDetailsList.get(0).getDescription());
Button BotonSinAnuncios = (Button) view.findViewById(R.id.buttonSinAnuncios);
BotonSinAnuncios.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Log.d("GoogleBillingService", "onBillingSetupFinished: billingResult: " + billingResult);
Log.d("GoogleBillingService", "Product detatils: " + productDetailsList.get(0));
ImmutableList productDetailsParamsList =
ImmutableList.of(
BillingFlowParams.ProductDetailsParams.newBuilder()
// retrieve a value for "productDetails" by calling queryProductDetailsAsync()
.setProductDetails(productDetailsList.get(0))
// to get an offer token, call ProductDetails.getSubscriptionOfferDetails()
// for a list of offers that are available to the user
.setOfferToken(String.valueOf(productDetailsList.get(0).getSubscriptionOfferDetails()))
.build()
);
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setProductDetailsParamsList(productDetailsParamsList)
.build();
// Launch the billing flow
billingClient.launchBillingFlow(activity, billingFlowParams);
}
});
}
}
}
);
}
}
This error often occurs when incorrect data is passed to the Billing Library.
In your case, please find the relevant offerToken
:
String offerToken = productDetailsList.get(0).getSubscriptionOfferDetails().get(0).getOfferToken();
(Of course, this is just an example, and I'm omitting various checks. I'm assuming that the first product is a subscription and that your desired offer is the first in the list returned by getSubscriptionOfferDetails()
.You should use your custom logic to find the right offer.)
Afterward, try removing the line .setOfferToken(String.valueOf(productDetailsList.get(0).getSubscriptionOfferDetails()))
with setOfferToken(offerToken)