Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. private IabHelper mHelper;
  2. private IabBroadcastReceiver mBroadcastReceiver;
  3. private IabHelper.QueryInventoryFinishedListener mGotInventoryListener;
  4. boolean mIsPremium = false;
  5. static final String SKU_PREMIUM = "premium";
  6. static final int RC_REQUEST = 10001;
  7. private IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener;
  8.  
  9. String base64EncodedPublicKey = "TU_CLAVE_APP"
  10. mHelper = new IabHelper(this, base64EncodedPublicKey);
  11.  
  12. mHelper.enableDebugLogging(true); //quitarlo si es modo release
  13.  
  14. Log.d(TAG, "Starting setup.");
  15. mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
  16. @Override
  17. public void onIabSetupFinished(IabResult result) {
  18. Log.d(TAG, "Setup finished.");
  19.  
  20. if (!result.isSuccess()) {
  21. Log.e(TAG, "In-app Billing setup failed: " + result);
  22. } else {
  23. Log.d(TAG, "In-app Billing is set up OK");
  24. }
  25.  
  26. if (mHelper == null) return;
  27.  
  28. mBroadcastReceiver = new IabBroadcastReceiver((IabBroadcastReceiver.IabBroadcastListener) MainActivity.this);
  29. IntentFilter broadcastFilter = new IntentFilter(IabBroadcastReceiver.ACTION);
  30. registerReceiver(mBroadcastReceiver, broadcastFilter);
  31.  
  32. // IAB is fully set up. Now, let's get an inventory of stuff we own.
  33. Log.d(TAG, "Setup successful. Querying inventory.");
  34. try {
  35. mHelper.queryInventoryAsync(mGotInventoryListener);
  36. } catch (IabHelper.IabAsyncInProgressException e) {
  37. complain("Error querying inventory. Another async operation in progress.");
  38. }
  39. }
  40. });
  41.  
  42. mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
  43. public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
  44. Log.d(TAG, "Query inventory finished.");
  45.  
  46. // Have we been disposed of in the meantime? If so, quit.
  47. if (mHelper == null) return;
  48.  
  49. // Is it a failure?
  50. if (result.isFailure()) {
  51. complain("Failed to query inventory: " + result);
  52. return;
  53. }
  54.  
  55. Log.d(TAG, "QUERY inventory was successful.");
  56.  
  57.  
  58. // Do we have the premium upgrade?
  59. Purchase premiumPurchase = inventory.getPurchase(SKU_PREMIUM);
  60. mIsPremium = (premiumPurchase != null && verifyDeveloperPayload(premiumPurchase));
  61. Log.d("PREMIUM =","User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));
  62. alert("User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));
  63.  
  64. Log.d(TAG, "Initial inventory query finished; enabling main UI.");
  65. }
  66. };
  67.  
  68. String payload = "mypurchasetoken";
  69.  
  70. try {
  71. mHelper.launchPurchaseFlow(MainActivity.this, SKU_PREMIUM, RC_REQUEST,
  72. mPurchaseFinishedListener, payload);
  73. } catch (IabHelper.IabAsyncInProgressException e) {
  74. e.printStackTrace();
  75. }
  76.  
  77. mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
  78. public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
  79. Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);
  80.  
  81. // if we were disposed of in the meantime, quit.
  82. if (mHelper == null) return;
  83.  
  84. if (result.isFailure()) {
  85. complain("Error purchasing: " + result);
  86. return;
  87. }
  88. if (!verifyDeveloperPayload(purchase)) {
  89. complain("Error purchasing. Authenticity verification failed.");
  90. return;
  91. }
  92.  
  93. Log.w(TAG, "Purchase successful.");
  94.  
  95. if (purchase.getSku().equals(SKU_PREMIUM)) {
  96. // bought the premium upgrade!
  97. Log.w(TAG, "Purchase is premium upgrade. Congratulating user.");
  98. alert("Thank you for upgrading to premium!");
  99. mIsPremium = true;
  100. }
  101. }
  102. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement