I have been trying to build an android application with in app payments, and am having trouble setting it up. I am calling the following:
Activity activity = new Activity();
billingClient = BillingClient.newBuilder(this).setListener(this).build();
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@BillingResponse int billingResponseCode) {
if (billingResponseCode == BillingResponse.OK) {
List<String> skuList = new ArrayList<> ();
skuList.add("android.test.purchased");
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(SkuType.INAPP);
billingClient.querySkuDetailsAsync(params.build(),
(responseCode, skuDetailsList) -> {
if (responseCode == BillingResponse.OK
&& skuDetailsList != null) {
for (SkuDetails skuDetails : skuDetailsList) {
System.out.println("The price is "+skuDetails.getPrice());
BillingFlowParams flowParams = BillingFlowParams.newBuilder().setSkuDetails(skuDetails).build();
int code = billingClient.launchBillingFlow(activity,flowParams);
System.out.println("Code is: "+code);
}
}
});
}
}
@Override
public void onBillingServiceDisconnected() {
System.out.print("Billing disconnected?");
}
});
When I run this, I get that the code is -1, and I get the error: Exception while launching billing flow: ; for sku: android.test.purchased; try to reconnect
I found that the error code -1 is "Play Store service is not connected now - potentially transient state" (https://developer.android.com/reference/com/android/billingclient/api/BillingClient.BillingResponse). I have tried both the testing productIDs (android.test.purchased
), as well as a real one that I created, with the same results. I have also restarted the (physical) device. I can access the Play Store app like normal, so I can't figure out what I am doing wrong here.