android - In app billing consumable item -


i trying implement google in-app billing selling consumable items (coins). tested non-consumable item , worked fine. can't make consumable. every time test it, can buy once ! here code :

public class mainactivity extends appcompatactivity { iabhelper mhelper;

boolean verifydeveloperpayload(purchase p) {     string payload = p.getdeveloperpayload();  /*  * todo: verify developer payload of purchase correct.  * same 1 sent when initiating purchase.  *  * warning: locally generating random string when starting purchase ,  * verifying here might seem approach, fail in  * case user purchases item on 1 device , uses app on  * different device, because on other device not have access  * random string generated.  *  * developer payload has these characteristics:  *  * 1. if 2 different users purchase item, payload different between them,  *    1 user's purchase can't replayed user.  *  * 2. payload must such can verify when app wasn't  *    1 initiated purchase flow (so items purchased user on  *    1 device work on other devices owned user).  *  * using own server store , verify developer payloads across app  * installations recommended.  */      return true; } @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      string base64encodedpublickey = "miibijanbgkqhkig9w0baqefaaocaq8amiibcgkcaqealjqqtbdm6zp0585ar0ykzayqish29+akzpdu4fguo3wlovm9uposnmmbmo8odzqbcvzdlkufocohg/52qoqk9crvihdhjm+o1gk9+hjsdvkzo0pww5+1sjscq7cw0ntxiddqvsyt0wwc2zkn8fpxyz1n9pghh21jxbvidycnh1gyk+mclt6jwcvxkl8bygc0ss7k9f+7khy+b/gg8zsl2xhccqlid/8cejqh7yvmpciwa8lhvhb7rgz/nug/v2ydhmuy6f8ifh6+yguu2xrhdu0v8wz24ykw2kw4svzbm5zmc/dxcgx+hiwvl+/yafqhj0ygqww4altukv6pydac1qidaqab";      // compute public key , store in base64encodedpublickey     mhelper = new iabhelper(this, base64encodedpublickey);     mhelper.enabledebuglogging(true);      mhelper.startsetup(new iabhelper.oniabsetupfinishedlistener() {         public void oniabsetupfinished(iabresult result) {             if (!result.issuccess()) {                 // oh no, there problem.                 log.d("tag", "problem setting in-app billing: " + result);                 log.i("tag", "error");             }             // hooray, iab set up!             //check owned items & consum em             checkowneditems();             //make test purchase                 makepurchase();         }     });  }  private void makepurchase() {     try {         mhelper.launchpurchaseflow(this, "next", 10001,                 mpurchasefinishedlistener, "bgoa+v7g/yqdxvkrqq+jtfn4uqzbpiqjo4pf9rzj");     } catch (iabhelper.iabasyncinprogressexception e) {         e.printstacktrace();         showtoast("oh no error purchase" + string.valueof(e));     } }  private void checkowneditems() {     try {         mhelper.queryinventoryasync(mgotinventorylistener);     } catch (iabhelper.iabasyncinprogressexception e) {         showtoast("oh no error in check()");         //complain("error querying inventory. async operation in progress.");     } }   // listener that's called when finish querying items , subscriptions own iabhelper.queryinventoryfinishedlistener mgotinventorylistener = new iabhelper.queryinventoryfinishedlistener() {     public void onqueryinventoryfinished(iabresult result, inventory inventory) {         purchase item = inventory.getpurchase("next");         if (item != null && verifydeveloperpayload(item)) {             //log.d("tag", "we have gas. consuming it.");             try {                 mhelper.consumeasync(inventory.getpurchase("next"), mconsumefinishedlistener);             } catch (iabhelper.iabasyncinprogressexception e) {                 // complain("error consuming gas. async operation in progress.");                 showtoast("oh no error when consuming");             }             return;         }     }  };   // called when consumption complete iabhelper.onconsumefinishedlistener mconsumefinishedlistener = new iabhelper.onconsumefinishedlistener() {     public void onconsumefinished(purchase purchase, iabresult result) {         log.d("tag", "consumption finished. purchase: " + purchase + ", result: " + result);          // if disposed of in meantime, quit.         if (mhelper == null) return;          // know "gas" sku because it's 1 consume,         // don't check sku consumed. if have more 1         // sku, should check...         if (result.issuccess()) {             // consumed, apply effects of item in our             // game world's logic, in our case means filling gas tank bit             //log.d(tag, "consumption successful. provisioning.");             //mtank = mtank == tank_max ? tank_max : mtank + 1;             // savedata();             // alert("you filled 1/4 tank. tank " + string.valueof(mtank) + "/4 full!");         }         else {             //complain("error while consuming: " + result);         }         log.d("tag", "end consumption flow.");     } };   // callback when purchase finished iabhelper.oniabpurchasefinishedlistener mpurchasefinishedlistener = new iabhelper.oniabpurchasefinishedlistener() {     public void oniabpurchasefinished(iabresult result, purchase purchase) {         log.d("tag", "purchase finished: " + result + ", purchase: " + purchase);          // if disposed of in meantime, quit.         if (mhelper == null) return;          if (result.isfailure()) {             // complain("error purchasing: " + result);             return;         }         if (!verifydeveloperpayload(purchase)) {             // complain("error purchasing. authenticity verification failed.");             return;         }          log.d("tag", "purchase successful.");          if (purchase.getsku().equals("next")) {              log.d("tag", "purchase gas. starting gas consumption.");             try {                 mhelper.consumeasync(purchase, mconsumefinishedlistener);             } catch (iabhelper.iabasyncinprogressexception e) {                 //complain("error consuming gas. async operation in progress.");                 showtoast("oh no error when consuming");                 return;             }         }     } };  @override public void ondestroy() {     super.ondestroy();     if (mhelper != null) try {         mhelper.dispose();     } catch (iabhelper.iabasyncinprogressexception e) {         e.printstacktrace();     }     mhelper = null; } private void showtoast(string message) {     toast.maketext(this, message, toast.length_long).show(); } 

} }

sorry english , thanks.

i having trouble using native in-app billing system, found this library

hope helps.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -