onProductPurchased and onActivityResult not called after a successfull purchase

Viewed 532

I have a buy button in my Android App where I am using Anjlab in-app billing library. Once the purchase is successful, I want to send a request to the server which would in turn notify me.

But, I have not been able to reach the "onProductPurchased" function at all. After research in here I found I need to get the onActivityResult called. But even that is not getting.

One of the solution to get onActivityResult was to override it, but still no go. Please find the code below and suggest me what needs to be done so that once a purchase is successful, I can make a call to the backend.

public class QuestionPrice extends Fragment implements View.OnClickListener, BillingProcessor.IBillingHandler{

private static final String PRODUCT_ID = "prod_00001";
private static final String PRODUCT_ID2 = "prod_00002";
private int paymentmade;
private String paymentdone;
ArrayList<String> arrayListOfProductIds;
private static final String LICENSE_KEY = "PUT YOUR MERCHANT KEY HERE";
private static final String MERCHANT_ID=null;
private static final String TAG = "Askquest";
private BillingProcessor bp;
private boolean readyToPurchase = false;
Button buybutton;
Button buybuttonReport;
private boolean purchased;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_question_price, container, false);
    buybutton = (Button) view.findViewById(R.id.buybutton);
    buybuttonReport = (Button) view.findViewById(R.id.buybuttonReport);
    buybutton.setOnClickListener(this);
    buybuttonReport.setOnClickListener(this);
    return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if(!BillingProcessor.isIabServiceAvailable(getActivity())) {
        Toast.makeText(getActivity(), "In-app billing service is unavailable, please upgrade Android Market/Play to version >= 3.9.16", Toast.LENGTH_SHORT).show();
    }

    bp = new BillingProcessor(getActivity(), LICENSE_KEY, MERCHANT_ID,this);
    SkuDetails skuDetails = bp.getPurchaseListingDetails("PRODUCT_ID");
    getActivity().setTitle("Products");
}


@Override
public void onClick(View v) {
    SharedPreferences msharedprefs = getActivity().getSharedPreferences("sharedprefdata", MODE_PRIVATE);
    SharedPreferences.Editor editor = msharedprefs.edit();
    switch (v.getId()) {
        case R.id.buybutton:
    if (bp.loadOwnedPurchasesFromGoogle() && bp.isPurchased(PRODUCT_ID)) {
        boolean loadOwnedPurchasesFromGoogle = bp.loadOwnedPurchasesFromGoogle();
        boolean isPurchased = bp.isPurchased(PRODUCT_ID);

        TransactionDetails premiumTransactionDetailss = bp.getPurchaseTransactionDetails(PRODUCT_ID);

        Log.i("Transaction Details ", " TT Details " + premiumTransactionDetailss);
        if (purchased) {
             Toast.makeText(getActivity(), "You have already purchased a question.", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getActivity(), "Purchase Initiated", Toast.LENGTH_SHORT).show();
            bp.purchase(getActivity(), PRODUCT_ID);
            TransactionDetails premiumTransactionDetails2 = bp.getPurchaseTransactionDetails(PRODUCT_ID);
            purchased = bp.isPurchased(PRODUCT_ID);
            Log.i("TAG", "isPurchased" + purchased);
            if (purchased) {
                Log.i("TAG", "Congratulations!!! Purchase Successfull");
                Toast.makeText(getActivity(), "Congratulations!!! Purchase Successfull", Toast.LENGTH_SHORT).show();
            }
        }
    }
            break;

        case R.id.buybuttonReport:
            TransactionDetails premiumTransactionDetails2 = bp.getPurchaseTransactionDetails(PRODUCT_ID2);
            Log.i("Transaction Details ", " TT Details "+premiumTransactionDetails2);
            if (bp.loadOwnedPurchasesFromGoogle() && bp.isPurchased(PRODUCT_ID2))
            {
                boolean loadOwnedPurchasesFromGoogle = bp.loadOwnedPurchasesFromGoogle();
                boolean  isPurchased = bp.isPurchased(PRODUCT_ID2);
                TransactionDetails premiumTransactionDetailss = bp.getPurchaseTransactionDetails(PRODUCT_ID2);
                Log.i("Transaction Details ", " TT Details "+premiumTransactionDetailss);
               Log.i("TAG", ""+msharedprefs.getInt("reportQues", -1));

                Toast.makeText(getActivity(), "You have already purchased a report.", Toast.LENGTH_SHORT).show();
            }
            else
            {
             Toast.makeText(getActivity(), "Purchase Initiated", Toast.LENGTH_SHORT).show();
             bp.purchase(getActivity(), PRODUCT_ID2);
             purchased = bp.isPurchased(PRODUCT_ID2);
             if ( purchased)
             {
                 Log.i("TxnDetails : ","TxnDetails : "+premiumTransactionDetails22.purchaseInfo.purchaseData.orderId.toString());
             }
                Log.i("TAG", "isPurchased"+purchased );
            }
            break;
    }
}

@Override
public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {

    SharedPreferences msharedprefs = getActivity().getSharedPreferences("sharedprefdata", MODE_PRIVATE);
    SharedPreferences.Editor editor = msharedprefs.edit();
    String userid = msharedprefs.getString("userid", null);
    String username = msharedprefs.getString("username", null);

    Toast.makeText(getActivity(), "On Product Purchased", Toast.LENGTH_SHORT).show();
    Log.i("TAG", "On Product Purchased" );

    purchased = bp.isPurchased(PRODUCT_ID2);
    if ( purchased) {
        Toast.makeText(getActivity(), "Congratulations!!! Purchase Successfull", Toast.LENGTH_SHORT).show();
        Log.i("TAG", "Button Clicked Product Purchased" );
        Toast.makeText(getActivity(), "Button Clicked Product Purchased", Toast.LENGTH_SHORT).show();
    }
    editor.putInt("paymentdone", 1);
    editor.commit();
    purchasemade(userid);

}

@Override
    public void onPurchaseHistoryRestored() {
    Log.i("TAG", "Button Clicked Product History Restored" );
   Toast.makeText(getActivity(), "Button Clicked Product History Restored", Toast.LENGTH_SHORT).show();
    editor.putInt("paymentdone", 0);
    editor.commit();

}

@Override
public void onBillingError(int errorCode, @Nullable Throwable error) {
    Toast.makeText(getActivity(), "Button Clicked Billing Error", Toast.LENGTH_SHORT).show();
    Log.i("TAG", "Button Clicked Billing Error" );
    editor.putInt("paymentdone", 0);
    editor.commit();
}

@Override
public void onBillingInitialized() {
    Toast.makeText(getActivity(), "Billing Initialized", Toast.LENGTH_SHORT).show();
    readyToPurchase = true;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (!bp.handleActivityResult(requestCode, resultCode, data)) {
        super.onActivityResult(requestCode, resultCode, data);
        FragmentManager fragmentManager = getFragmentManager();
        Fragment fragment = fragmentManager.findFragmentByTag("Products");
        Toast.makeText(getActivity(), "Inside On Activity Result", Toast.LENGTH_SHORT).show();
        Log.i("TAG", "Inside On Activity Result" );
        if (fragment != null)
        {
            ((QuestionPrice)fragment).onActivityResult(requestCode, resultCode, data);
        }
    }
}

@Override
public void onDestroy() {
    if (bp != null) {
        bp.release();
    }
    super.onDestroy();
}

private void purchasemade(String userid) {
    Log.i("TAG", "Inside Purchase Made" );
    Toast.makeText(getActivity(), "Inside Purchase Made", Toast.LENGTH_SHORT).show();
}
}

I tried a few steps from other similar issues posted in the SO but none of them worked.

0 Answers
Related