I am trying to integrate Google Play Billing.
Here is my onCreate :
private final static String TAG = "MainActivity" ;
private final static String ITEM_SKU_SUBSCRIBE = "sub_example" ;
private final static String PREF_FILE = "shared_prefs" ;
private final static String SUBSCRIBE_KEY = "subscribe" ;
private BillingClient billingClient ;
private TextView premiumContent ;
private TextView subscriptionStatus ;
private Button purchaseButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
premiumContent = findViewById(R.id.premium_content) ;
subscriptionStatus = findViewById(R.id.subscription_status) ;
purchaseButton = findViewById(R.id.button) ;
billingClient = BillingClient.newBuilder(this)
.enablePendingPurchases()
.setListener(this)
.build() ;
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
Log.i(TAG, "onBillingSetupFinished ; billing response code == " + billingResult.getResponseCode());
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
Purchase.PurchasesResult queryPurchase = billingClient.queryPurchases(SUBS);
List<Purchase> queryPurchases = queryPurchase.getPurchasesList();
if (queryPurchases != null && queryPurchases.size() > 0) {
handlePurchases(queryPurchases);
} else {
saveSubscribeValueToPref(false);
}
} else {
Log.e(TAG, "ELSE : " + billingResult.getDebugMessage()) ;
}
}
@Override
public void onBillingServiceDisconnected() {
Toast.makeText(getApplicationContext(), "Service disconnected",
Toast.LENGTH_SHORT).show() ;
}
});
if (getSubscribeValueFromPref()) {
purchaseButton.setVisibility(View.GONE);
premiumContent.setVisibility(View.VISIBLE);
subscriptionStatus.setText("Subscription Status : Subscribed");
} else {
premiumContent.setVisibility(View.GONE);
purchaseButton.setVisibility(View.VISIBLE);
subscriptionStatus.setText("Subscription Status : Not Subscribed");
}
purchaseButton.setText("Load products");
purchaseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
subscribe();
}
});
}
When I click on the purchaseButton
, I call subscribe
function.
Basically it just call a function initiatePurchase
if billingClient.isReady()
returns true
, and initialize billingClient
in the other case.
Here is my initiatePurchase
function :
private void initiatePurchase() {
List<String> skuList = new ArrayList<>() ;
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder() ;
skuList.add(ITEM_SKU_SUBSCRIBE);
params.setSkusList(skuList).setType(SUBS);
BillingResult billingResult = billingClient.isFeatureSupported(BillingClient.FeatureType.SUBSCRIPTIONS) ;
Log.i(TAG, "Billing response code == " + billingResult.getResponseCode()) ;
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
Log.i(TAG, "Billing response code is OK");
billingClient.querySkuDetailsAsync(params.build(),
new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {
Log.i(TAG, "onSkuDetailsResponse ; billing result response code == " + billingResult.getResponseCode());
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
Log.i(TAG, "Billing response code OK") ;
if (skuDetailsList != null) {
for (int i = 0 ; i < skuDetailsList.size() ; i++) {
Log.i(TAG, "Loop INDEX " + i + " ; SkuDetails == " + skuDetailsList.get(i).getTitle()) ;
}
}
if (skuDetailsList != null && skuDetailsList.size() > 0) {
Log.i(TAG, "skuDetailsList is not null or empty");
BillingFlowParams flowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetailsList.get(0))
.build();
Log.i(TAG, "Flow Params = " + flowParams.getAccountId() + " | " + flowParams.getSku());
BillingResult billingFlowResult = billingClient
.launchBillingFlow(MainActivity.this, flowParams);
Log.i(TAG, "billingFlowResult == " + billingFlowResult.getResponseCode() + " | " + billingFlowResult.getDebugMessage()) ;
} else {
Toast.makeText(getApplicationContext(),
"Item not found", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(),
"Error - " + billingResult.getDebugMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
} else {
Toast.makeText(getApplicationContext(),
"Sorry, subscription not supported. Please update Play Store",
Toast.LENGTH_SHORT).show();
}
}
It seems to go to onSkuDetailsResponse
, with a billing response code OK.
I loop in the skuDetailsList
and I get sub_example
, which is my ITEM_SKU_SUBSCRIBE
(and the one on my Play Store Console) ; so my skuDetailsList
is not null or empty.
But then, here is my log output :
I/MainActivity: Billing response code == 0
Billing response code is OK
I/MainActivity: onSkuDetailsResponse ; billing result response code == 0
Billing response code OK
Loop INDEX 0 ; SkuDetails == Monthly sub (Test Billing)
skuDetailsList is not null or empty
I/MainActivity: Flow Params = null | sub_example
I/MainActivity: billingFlowResult == 0 | null
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@cb159f0
When I click on the "OK" button, interesting logs appears :
W/ProxyBillingActivity: Activity finished with resultCode 0 and billing's responseCode: 4
W/BillingHelper: Couldn't find purchase lists, trying to find single data.
W/BillingHelper: Received a bad purchase data.
Couldn't find single purchase data as well.
I looked up in BillingClient.class
and I found int ITEM_UNAVAILABLE = 4;
in the interface BillingResponseCode
Thanks
It seems the only thing to do was to wait...
It worked this morning.
Count 2 days for your inapp payments to be available on the app.