Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private IabHelper mHelper;
- private IabBroadcastReceiver mBroadcastReceiver;
- private IabHelper.QueryInventoryFinishedListener mGotInventoryListener;
- boolean mIsPremium = false;
- static final String SKU_PREMIUM = "premium";
- static final int RC_REQUEST = 10001;
- private IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener;
- String base64EncodedPublicKey = "TU_CLAVE_APP"
- mHelper = new IabHelper(this, base64EncodedPublicKey);
- mHelper.enableDebugLogging(true); //quitarlo si es modo release
- Log.d(TAG, "Starting setup.");
- mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
- @Override
- public void onIabSetupFinished(IabResult result) {
- Log.d(TAG, "Setup finished.");
- if (!result.isSuccess()) {
- Log.e(TAG, "In-app Billing setup failed: " + result);
- } else {
- Log.d(TAG, "In-app Billing is set up OK");
- }
- if (mHelper == null) return;
- mBroadcastReceiver = new IabBroadcastReceiver((IabBroadcastReceiver.IabBroadcastListener) MainActivity.this);
- IntentFilter broadcastFilter = new IntentFilter(IabBroadcastReceiver.ACTION);
- registerReceiver(mBroadcastReceiver, broadcastFilter);
- // IAB is fully set up. Now, let's get an inventory of stuff we own.
- Log.d(TAG, "Setup successful. Querying inventory.");
- try {
- mHelper.queryInventoryAsync(mGotInventoryListener);
- } catch (IabHelper.IabAsyncInProgressException e) {
- complain("Error querying inventory. Another async operation in progress.");
- }
- }
- });
- mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
- public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
- Log.d(TAG, "Query inventory finished.");
- // Have we been disposed of in the meantime? If so, quit.
- if (mHelper == null) return;
- // Is it a failure?
- if (result.isFailure()) {
- complain("Failed to query inventory: " + result);
- return;
- }
- Log.d(TAG, "QUERY inventory was successful.");
- // Do we have the premium upgrade?
- Purchase premiumPurchase = inventory.getPurchase(SKU_PREMIUM);
- mIsPremium = (premiumPurchase != null && verifyDeveloperPayload(premiumPurchase));
- Log.d("PREMIUM =","User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));
- alert("User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));
- Log.d(TAG, "Initial inventory query finished; enabling main UI.");
- }
- };
- String payload = "mypurchasetoken";
- try {
- mHelper.launchPurchaseFlow(MainActivity.this, SKU_PREMIUM, RC_REQUEST,
- mPurchaseFinishedListener, payload);
- } catch (IabHelper.IabAsyncInProgressException e) {
- e.printStackTrace();
- }
- mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
- public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
- Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);
- // if we were disposed of in the 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.w(TAG, "Purchase successful.");
- if (purchase.getSku().equals(SKU_PREMIUM)) {
- // bought the premium upgrade!
- Log.w(TAG, "Purchase is premium upgrade. Congratulating user.");
- alert("Thank you for upgrading to premium!");
- mIsPremium = true;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement