-1

In my application i want use InAppBillling for buy some features !
I write payment methods into fragment but after show payment dialog and pay by user not show me any data into onActivityResult!
I write log.e into onActivityResult, but again not show.
I think not going into onActivityResult!

My fragment codes :

public class ServicesFragment extends BaseFragment implements IabHelper.OnIabSetupFinishedListener,
        IabHelper.QueryInventoryFinishedListener, IabHelper.OnIabPurchaseFinishedListener, IabHelper.OnConsumeFinishedListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_services, container, false);

...

}

    @Override
    public void onIabSetupFinished(IabResult result) {
        if (iabHelper != null) {
            iabHelper.flagEndAsync();
            //This is call for payment
            iabHelper.queryInventoryAsync(ServicesFragment.this);
        }
    }

    @Override
    public void onIabPurchaseFinished(IabResult result, final Purchase info) {
        //After payment call this
        if (result.isSuccess() && info != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    getCheckBuyFromBazaar(jwtToken, packageName, productId, purchaseToken, buyID, 0, info);
                }
            });
        }
    }

    @Override
    public void onQueryInventoryFinished(IabResult result, Inventory inv) {
        if (result.isSuccess()) {
            bazaarProgressDialog.dismiss();
            //Run bazaar dialog
            if (iabHelper != null) {
                iabHelper.flagEndAsync();
                iabHelper.launchPurchaseFlow(activity, bazaarSKU, bazaarRequestCode,
                        ServicesFragment.this, bazaarHashCode);
            }
        }
    }

    @Override
    public void onConsumeFinished(Purchase purchase, IabResult result) {
        if (result.isSuccess() && purchase != null) {
            if (purchase.getSku().equalsIgnoreCase(bazaarSKU)) {
                //When purchase consumed
                finishPage();
                loadingProgressDialog.dismiss();
            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("BazaarPaymentLog", "Result");
        if (iabHelper == null) return;

        if (requestCode == bazaarRequestCode) {
            String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
            if (resultCode == RESULT_OK) {
                try {
                    JSONObject jo = new JSONObject(purchaseData);
                    purchaseToken = jo.getString("purchaseToken");
                    productId = jo.getString("productId");
                    packageName = jo.getString("packageName");

                    Log.e("BazaarPaymentLog", "purchaseToken : " + purchaseToken);
                    if (iabHelper != null) {
                        iabHelper.handleActivityResult(requestCode, resultCode, data);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

}

Show me payment dialog and i pay it, but after not going to onActivityResult!

I write above code into activity it's ok and run onActivityResult but into fragment not going into onActivityResult!

How can i fix it?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
KokoBand
  • 229
  • 1
  • 5
  • 15

1 Answers1

1

Actually the result is passed to Activity's onActivtyResult in order to get in Fragment's method you need to pass reult fron Activity to Fragment like below

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame);
    fragment.onActivityResult(requestCode, resultCode, data);
}

and in Fragment call it same as you are calling currently

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

}

If you are using ViewPager you can get Fragment like below

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Fragment mFragment = myPagerAdapter.getItem(mViewPager.getCurrentItem());
        if (fragment instanceof ServiccesFragment){
            ((ServiccesFragment)fragment).onActivityResult(requestCode, resultCode, data);
        }

    }
Abid Khan
  • 2,451
  • 4
  • 22
  • 45