Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid billing - how to set up a buy buttons and correctly read whether the user canceled or bought?
    primarykey
    data
    text
    <p>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:</p> <pre><code>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. } }); </code></pre> <p>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.</p> <p>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. :) </p> <p>EDIT:</p> <p>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.</p> <p>Here is how my new onStateChanged method looks like:</p> <pre><code>@Override public void onPurchaseStateChange(PurchaseState purchaseState, String itemId, int quantity, long purchaseTime, String developerPayload) { if (purchaseState == PurchaseState.PURCHASED) { mOwnedItems.add(itemId); if ( itemId != null &amp;&amp; itemId.trim().equals("3") ) { Intent myIntent = new Intent(ExtraHelpActivity.this, PsychologyActivity.class); ExtraHelpActivity.this.startActivity(myIntent); } if ( itemId != null &amp;&amp; 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 &amp;&amp; itemId.equals("3") ) { Intent myIntent = new Intent(ExtraHelpActivity.this, PsychologyActivity.class); ExtraHelpActivity.this.startActivity(myIntent); } if ( itemId != null &amp;&amp; itemId.equals("4") ) { Intent myIntent = new Intent(ExtraHelpActivity.this, NumberOfBusinessesActivity.class); ExtraHelpActivity.this.startActivity(myIntent); } } } </code></pre> <p>and when I tested it and bought an article, it did get here to the <code>if (purchaseState == PurchaseState.PURCHASED)</code> but not the individual if cases inside it even though the itemId id is "3" or "4" </p> <p>EDIT:</p> <p>and this is how I declare the product ids at the beginning of the Activity:</p> <pre><code>String issueProductIdWebMarketing = "1"; String issueProductIdDonate = "2"; String issueProductIdPsych = "3"; String issueProductIdNumberofBusinesses = "4"; </code></pre> <p>Thanks!!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload