0

i am fairly new to android development, and am having major troubles understanding how to implement google in app billing. I have read the official google documentation, read through quite a few tutorials, etc. The one that i have found that is the most simple to me is the following. Even then i am struggling to get it working. http://redappz.com/micro-transactions-tutorial-iap-for-android/

What i have done is, imported all helper classes from trivial drive. Have a google dev account with a signed apk.

The logic i want for my application is when a purchase is successful in Activity A. Activity B displays a button.

public class WorkoutPlan1 extends AppCompatActivity {

private Button WorkoutPlan1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_workoutplan1);
        WorkoutPlan1 = (Button) findViewById(R.id.Workout1MoreInfo);

Base code with out any google billing stuff. Basic button just to run the google billing off.

     public class WorkoutPlan1 extends AppCompatActivity {

private Button WorkoutPlan1;
//*************************************************************************************************************************
// Debug tag, for logging
static final String TAG = "test";
// SKUs for our products: the premium upgrade (non-consumable)
static final String SKU_PREMIUM = "android.test.purchased";
// Does the user have the premium upgrade?
boolean mIsPremium = false;
// (arbitrary) request code for the purchase flow
static final int RC_REQUEST = 1;
// The helper object
IabHelper mHelper;
//*************************************************************************************************************************

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    //*************************************************************************************************************************
    String base64EncodedPublicKey = "KEY GOES HERE FROM MY GOOGLE DEV";
    //It is recommended to add more security than just pasting it in your source code;
    mHelper = new IabHelper(this, base64EncodedPublicKey);
    Log.d(TAG, "Starting setup.");
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            Log.d(TAG, "Setup finished.");

            if (!result.isSuccess()) {
    // Oh noes, there was a problem.
                Log.d(TAG, "Problem setting up In-app Billing: " + result);
            }
    // Hooray, IAB is fully set up!
           // mHelper.queryInventoryAsync(mGotInventoryListener);
        }
    });
        //*************************************************************************************************************************
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_workoutplan1);
        WorkoutPlan1 = (Button) findViewById(R.id.Workout1MoreInfo);
}
     //*************************************************************************************************************************
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
        Log.d(TAG, "Query inventory finished.");
        if (result.isFailure()) {
            Log.d(TAG, "Failed to query inventory: " + result);
            return;
        }
        else {
            Log.d(TAG, "Query inventory was successful.");
            // does the user have the premium upgrade?
            mIsPremium = inventory.hasPurchase(SKU_PREMIUM);

            // update UI accordingly
            WorkoutPlan1.setVisibility(View.VISIBLE);
            Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));
        }
        Log.d(TAG, "Initial inventory query finished; enabling main UI.");
    }
};

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
        if (result.isFailure()) {
            Log.d(TAG, "Error purchasing: " + result);
            return;
        }
        else if (purchase.getSku().equals(SKU_PREMIUM)) {
            // give user access to premium content and update the UI
            WorkoutPlan1.setVisibility(View.VISIBLE);
        }
    }
};

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

    Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
    // Pass on the activity result to the helper for handling
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
        super.onActivityResult(requestCode, resultCode, data);
    } else {
        Log.d(TAG, "onActivityResult handled by IABUtil.");
    }
}

Im aware i dont have an onclick listener on the button. But i dont even know how i would invoke the payment / purchase flow.

Any help AT ALL to point me in the right direction would be so appreciated, as at this point i just feel lost.

Thank you everyone

uwotm8
  • 123
  • 3
  • 10

1 Answers1

0

You would use the IabHelper object to invoke the purchase flow. For instance:

try { mHelper.launchPurchaseFlow(this, SKU_BUNDLE, RC_REQUEST, mPurchaseFinishedListener, payload); } catch (IabAsyncInProgressException e) { complain("Error launching purchase flow. Another async operation in progress."); }

... So I would include this code snippet inside an onClickListener for your button. And then you would handle the purchases' response through the OnIabPurchaseFinishedListener.

P.S. I'm answering this with my mobile device, so excuse the poor formatting

DaveNOTDavid
  • 1,753
  • 5
  • 19
  • 37