The way I see it is that the onProductPurchased
method should invoke when a purchase was successful, however that is not the case for me.
It doesn't actually fire unless I complete a purchase and then click the "Purchase" button again. That's when it says Already purchased
and I've looked around for a good 3 hours trying to find a solution, yet I can't, I found a SO post explaining a solution to someone who had a similar problem so I tried implementing both the 1st and the 2nd solution but it didn't work, it had the same behaviour.
Am I missing some parts of the implementation? I looked at the sample app and it looks fairly similar.
package com.the.name.ui;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.anjlab.android.iab.v3.BillingProcessor;
import com.anjlab.android.iab.v3.TransactionDetails;
import com.the.name.R;
public class PremiumFragment extends Fragment implements BillingProcessor.IBillingHandler {
private String LICENSE_KEY = "removedforthesakeofthepost";
BillingProcessor bp;
private Button btnPurchase;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
bp = new BillingProcessor(getContext(), LICENSE_KEY, PremiumFragment.this);
bp.initialize();
View rootView = inflater.inflate(R.layout.fragment_premium, container, false);
btnPurchase = rootView.findViewById(R.id.purchasebtn);
btnPurchase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bp.purchase(getActivity(), "android.test.purchased", null /*or developer payload*/, null);
}
});
return rootView;
}
@Override
public void onProductPurchased(String productId, TransactionDetails details) {
/*
* Called when requested PRODUCT ID was successfully purchased
*/
Toast.makeText(getContext(), "Already purchased", Toast.LENGTH_SHORT).show();
}
@Override
public void onPurchaseHistoryRestored() {
}
@Override
public void onBillingError(int errorCode, Throwable error) {
}
@Override
public void onBillingInitialized() {
}
@Override
public void onDestroy() {
if (bp != null) {
bp.release();
Toast.makeText(getActivity(), "onDestroy", Toast.LENGTH_SHORT).show();
}
super.onDestroy();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!bp.handleActivityResult(requestCode, resultCode, data))
super.onActivityResult(requestCode, resultCode, data);
}
}