2

I am working from the Dungeons example that the Android provides, and it feels like I almost got it correctly, but I can't figure out how to detect if the person pressed the back button and backed out of the purchase. Here is what I have so far:

Button buy = (Button)findViewById(R.id.buy); 
buy.setOnClickListener(new Button.OnClickListener() 
{  
   public void onClick(View v) 
   {                                           
     try
     {
        if (mBillingService.requestPurchase(issueProductId, 
                                                Consts.ITEM_TYPE_INAPP , null)) 
        {
          // Not sure what to do here. Has the person been sent to the buy screen yet or no?
          // There are these strings I need to check for but not entirely certain how:                

          if(mBillingService.checkBillingSupported(Consts.ITEM_TYPE_INAPP))
          { 
                 // OK
          } 
          else 
          {
             // If billing is not supported,  give them the item for free
                 Intent myIntent = new Intent(ExtraHelpActivity.this, PsychologyActivity.class);
                 ExtraHelpActivity.this.startActivity(myIntent);    
          }    

          try
          {   
          if (mBillingService.requestPurchase(issueProductIdPsych, 
                     Consts.ITEM_TYPE_INAPP ,  null)) 
          {
                  // HOW DO I CHECK HERE IF THE USER CANCELED OUT OF THE PURCHASE? 
                  // EVEN ON CANCEL, THEY ARE BEING TAKEN TO THE ITEM THAT THEY NEED TO PAY FOR.  


              // Send them to the screen of the article.
                  Intent myIntent = new Intent(ExtraHelpActivity.this, PsychologyActivity.class);
              ExtraHelpActivity.this.startActivity(myIntent);                         
          } 
        }
     catch ( Exception e )
     {
        // Report exception.
     }  
     });

I added some inline comments where I am confused. I am not certain how to read whether the person purchased the item, or cancelled. If someone could help me with this, that would be great.

Right now this code takes the person to the buy screen, and some people buy, but when people press cancel, they still get taken to the item which they didn't buy. :)

EDIT:

By the way, if users already bought the item and want to come back to it, the purchase request state does not change, right? So what method ends up being triggered? I tested with one purchase and I was able to buy access to one screen with an article, but now can not get to it again since the payment screen keeps telling me I already bought that item, and not taking me to the next page.

Here is how my new onStateChanged method looks like:

@Override
public void onPurchaseStateChange(PurchaseState purchaseState, String itemId,
                int quantity, long purchaseTime, String developerPayload) 
{
    if (purchaseState == PurchaseState.PURCHASED) 
    {
        mOwnedItems.add(itemId);

        if ( itemId != null && itemId.trim().equals("3") )
        {
        Intent myIntent = new Intent(ExtraHelpActivity.this, PsychologyActivity.class);
        ExtraHelpActivity.this.startActivity(myIntent);
        }
        if ( itemId != null && itemId.trim().equals("4") )
        {
           Intent myIntent = new Intent(ExtraHelpActivity.this, NumberOfBusinessesActivity.class);
    }                
   }
   else 
   if (purchaseState == PurchaseState.CANCELED) 
   {  
                // purchase canceled
   } 
   else 
   if (purchaseState == PurchaseState.REFUNDED) 
   {
                // user ask for a refund
   }
   else
   {   
                if ( itemId != null && itemId.equals("3") )
                {
                      Intent myIntent = new Intent(ExtraHelpActivity.this, PsychologyActivity.class);
                      ExtraHelpActivity.this.startActivity(myIntent);
                }
                if ( itemId != null && itemId.equals("4") )
                {
                      Intent myIntent = new Intent(ExtraHelpActivity.this, NumberOfBusinessesActivity.class);
                      ExtraHelpActivity.this.startActivity(myIntent);   
                }                   
     }           
}

and when I tested it and bought an article, it did get here to the if (purchaseState == PurchaseState.PURCHASED) but not the individual if cases inside it even though the itemId id is "3" or "4"

EDIT:

and this is how I declare the product ids at the beginning of the Activity:

String issueProductIdWebMarketing = "1";    
    String issueProductIdDonate = "2";  
    String issueProductIdPsych = "3";
    String issueProductIdNumberofBusinesses = "4";

Thanks!!

Genadinik
  • 18,153
  • 63
  • 185
  • 284

2 Answers2

3

According to the billing integration guide:

when the requested transaction changes state (for example, the purchase is successfully charged to a credit card or the user cancels the purchase), the Google Play application sends an IN_APP_NOTIFY broadcast intent. This message contains a notification ID, which you can use to retrieve the transaction details for the REQUEST_PURCHASE request.

And the intent com.android.vending.billing.PURCHASE_STATE_CHANGED contains purchaseState

The purchase state of the order. Possible values are 0 (purchased), 1 (canceled), 2 (refunded), or 3 (expired, for subscription purchases only).

source (table 4)


EDIT: In the dungeons sample, there is an inner class extending PurchaseObserver (see Dungeons activity) that does the job, see especially (onPurchaseStateChange)


EDIT2: some pseudo-code

in the activity (Dungeons class in the sdk sample):

Button buy = (Button)findViewById(R.id.buy); 
buy.setEnabled(false);
buy.setOnClickListener(new Button.OnClickListener() {  
    public void onClick(View v) 
    {
        // show purchase dialog and call mBillingService.requestPurchase() on dialog validation
    }
});

// request to see if supported
if (!mBillingService.checkBillingSupported()) {
    // not supported
}

in the observer (DungeonsPurchaseObserver in the sample):

public void onBillingSupported(boolean supported, String type) {
    if (type == null || type.equals(Consts.ITEM_TYPE_INAPP)) {
        if (supported) {
           // billing supported: enable buy button
        } else {
           // billing not supported: disable buy button
        }
    }
}

public void onPurchaseStateChange(PurchaseState purchaseState, String itemId,
    int quantity, long purchaseTime, String developerPayload) {
    if (purchaseState == PurchaseState.PURCHASED) {
        // item purchased
    } else if (purchaseState == PurchaseState.CANCELED) {
        // purchase canceled
    } else if (purchaseState == PurchaseState.REFUND) {
        // user ask for a refund
    }
}
  • yes I have been staring at that class and method for a while, but not sure exactly how to edit it. I am really hoping for someone to share a code snippet of how they did it. In my case I need the code to either take the person to the next page or not. Also - in the button click handler - is that not where I should make the intent to go to the purchased page? – Genadinik Aug 25 '12 at 13:48
  • I added some pseudo code with some comments. This is nicely documented and the sample code provide everything needed (I mean tools) to do it, so I don't see where is your difficulty here can you tell us more (where are you stuck, what are you not understanding, ...)? –  Aug 25 '12 at 14:09
  • I see, and depending on the itemId, I would go to the next screen depending on the item they bought. But just one question, how can I go to that screen? Usually I do something like this: Intent myIntent = new Intent(ExtraHelpActivity.this, PsychologyActivity.class); ExtraHelpActivity.this.startActivity(myIntent); ....but now I can't do that since the observer is not an activity..right? What would be the correct way to go to an intent from the observer? Thanks!! – Genadinik Aug 26 '12 at 00:37
  • there is also this code in the onStateChange method: mHandler.post(new Runnable() { public void run() { onPurchaseStateChange( purchaseState, itemId, quantity, purchaseTime, developerPayload); } }); ....should I just comment it out? Or put it in the very last else if the purchaseState doesn't match anything else? – Genadinik Aug 26 '12 at 01:17
  • @Genadinik, there is a misunderstanding here, you need to have something similar to `DungeonsPurchaseObserver` in your activity. Being an inner class you have access to your activity. You don't have to do anything in `Observer` class –  Aug 26 '12 at 11:33
  • yes I realized that you were talking about the inner class. Now it makes good sense. I will do a live update of the app tomorrow, and if it works, I will accept your answer. Thank you for being so responsive. – Genadinik Aug 26 '12 at 12:45
  • I tried a few things today, and I am a bit stuck on how to get people who already bought access to a content page to get access to it again. I made an edit at the bottom of the original question - if you could help me understand how to go about it, that would be great :) – Genadinik Aug 26 '12 at 21:40
  • also, do you think in this case it makes sense to change purchaseState == PurchaseState.PURCHASED to an equals call? – Genadinik Aug 26 '12 at 22:34
  • `PurchaseState` is an enum so `==` is ok –  Aug 27 '12 at 05:14
  • I would save purchased item somewhere (pref, db), check if the item is already purchased when the user is back (from the saved value) and act accordingly (i.e. show extra activity or buy dialog). –  Aug 27 '12 at 05:23
  • I ran this in test mode, and it worked very well, but when I run it on the live site with real product ids, the code I pasted at the bottom of my question doesn't work. I also added to it the way I declare my real productIds – Genadinik Aug 27 '12 at 15:37
  • thank you, I just awarded you the bounty. I am still working on some things that aren't right, but you really helped get me much further than where I was. – Genadinik Aug 28 '12 at 14:39
  • btw I just put another question here http://stackoverflow.com/questions/12162730/android-testing-billing-works-but-same-code-doesnt-work-on-live-app ...if you feel like taking a look, that would be awesome :) – Genadinik Aug 28 '12 at 16:41
1

The problem is that the call mBillingService.requestPurchase is not synchronous. So you can not check right after if the purchase was successful or not.

I recommend you use the library AndroidBillingLibrary. With it you can easily get all the Google Play's events. For that you can use onRequestPurchaseResponse() and onPurchaseStateChanged() from the helpers AbstractBillingActivity or AbstractBillingFragment.

Brais Gabin
  • 5,827
  • 6
  • 57
  • 92