Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AndroidApplication {
- private static final String TAG = "IAB";
- /*
- * Needed only for testing.
- * Set it to false when you ready for publishing your app
- */
- private static boolean debugMode = true;
- /*
- * Any random salt (not Math.random() =) Just one word, same for all users)
- */
- private static final String key = "anyRandomSalt";
- /*
- * Public key from market. Go to Play console, select needed app and go to "Services and API",
- * and you'll find key.
- */
- private static final String publicKey = "longPublicKeyFromMarket";
- private static final int RC_REQUEST = 10001; // Any number
- private boolean billingSupported = false; // Don't change it manually
- private boolean busy = false; // Don't change it manually
- private IabHelper helper;
- /*
- * This is very important part.
- * Here you must make SKUs names from Play Console, but one cool thing here.
- * For testing without money, you can use one of 4 special names, and you should get a fake card
- * prompt in the purchase screen when you try to buy this item in your app.
- * You don't need to make changes in Play console and build public version of app.
- * Just run it on devise in build mode. And you don't neet to make
- * any changes in your AndroidManifest.xml (e.g. version) before you will ready to publish your app.
- *
- * Special names:
- * android.test.purchased, android.test.canceled, android.test.refunded, android.test.item_unavailable
- */
- private static final String SKU_ADS_FREE = "ads_free"; // non consumable. restored even after reinstall
- private static final String SKU_MONEY_1000 = "money_1000"; // consumable. without restore after reinstall
- private static final String SKU_MONEY_3000 = "android.test.purchased"; // consumable. without restore after reinstall
- private boolean adsfree = false;
- private int money = 0;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- loadData();
- helper = new IabHelper(getApplicationContext(), publicKey);
- if (debugMode)
- helper.enableDebugLogging(true);
- helper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
- public void onIabSetupFinished(IabResult result) {
- if (result.isSuccess()) {
- billingSupported = true;
- busy = true;
- helper.queryInventoryAsync(gotInventoryListener);
- log("IAB started");
- } else {
- billingSupported = false;
- log("Problem setting up IAB: " + result);
- }
- }
- });
- final AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
- config.useGL20 = false;
- config.useWakelock = false;
- final Resolver resolver = new Resolver() {
- @Override
- public boolean getBusyState() {
- return busy;
- }
- @Override
- public boolean getAd() {
- return adsfree;
- }
- @Override
- public int getMoney() {
- return money;
- }
- @Override
- public void buyAdsFree() {
- if (!adsfree)
- buyItem(SKU_ADS_FREE);
- }
- @Override
- public void buyMoney1000() {
- buyItem(SKU_MONEY_1000);
- }
- @Override
- public void buyMoney3000() {
- buyItem(SKU_MONEY_3000);
- }
- };
- initialize(new Main(resolver), config);
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- if (billingSupported()) {
- helper.dispose();
- helper = null;
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (!billingSupported())
- return;
- log("onActivityResult(" + requestCode + "," + resultCode + "," + data);
- if (helper.handleActivityResult(requestCode, resultCode, data)) {
- log("onActivityResult handled by IABUtil");
- } else {
- super.onActivityResult(requestCode, resultCode, data);
- }
- }
- @Override
- public void onResume() {
- super.onResume();
- if (billingSupported() && !busy) {
- busy = true;
- helper.queryInventoryAsync(gotInventoryListener);
- }
- }
- private void buyItem(final String sku) {
- if (!billingSupported()) {
- log("Billing is not supported");
- return;
- }
- if (busy) {
- log("Can't buy item. So busy yet");
- return;
- }
- try {
- new Thread(new Runnable() {
- @Override
- public void run() {
- busy = true;
- helper.launchPurchaseFlow(MainActivity.this, sku, RC_REQUEST, purchaseFinishedListener, key);
- }
- }).start();
- } catch (Exception e) {
- log("buyItem Exception: " + e);
- }
- }
- private final IabHelper.OnIabPurchaseFinishedListener purchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
- public void onIabPurchaseFinished(IabResult result, final Purchase purchase) {
- log("Purchase finished: " + result + ", purchase: " + purchase);
- if (purchaseIsNull(purchase)) {
- busy = false;
- return;
- } else if (result.isFailure()) {
- log("Purchase failure: " + result + ", purchase: " + purchase);
- busy = false;
- return;
- } else if (!verifyPayload(purchase)) {
- log("Error purchasing. Authenticity verification failed");
- busy = false;
- return;
- }
- log("Purchase successful");
- if (compareSKU(purchase, SKU_ADS_FREE)) {
- adsfree = true;
- busy = false;
- } else if (compareSKU(purchase, SKU_MONEY_1000) ||
- compareSKU(purchase, SKU_MONEY_3000)) {
- helper.consumeAsync(purchase, consumeFinishedListener);
- }
- }
- };
- private final IabHelper.OnConsumeFinishedListener consumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
- @Override
- public void onConsumeFinished(Purchase purchase, IabResult result) {
- /*
- * Only for consumable items
- */
- if (result.isSuccess()) {
- if (compareSKU(purchase, SKU_MONEY_1000)) {
- money += 1000;
- log("Get + 1000$");
- } else if (compareSKU(purchase, SKU_MONEY_3000)) {
- money += 3000;
- log("Get + 3000$");
- }
- saveData();
- } else {
- log("Error while consuming: " + result);
- }
- log("Consumption finished: " + result + ", purchase: " + purchase);
- busy = false;
- }
- };
- private final IabHelper.QueryInventoryFinishedListener gotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
- public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
- if (result.isFailure()) {
- log("InventoryFinishedListener: " + result);
- busy = false;
- return;
- }
- adsfree = checkPurchase(inventory, SKU_ADS_FREE); // non consumable
- if (consume(inventory, SKU_MONEY_1000)) return; // consumable
- if (consume(inventory, SKU_MONEY_3000)) return; // consumable
- busy = false;
- }
- };
- private boolean billingSupported() {
- return (helper != null && billingSupported);
- }
- private boolean purchaseIsNull(Purchase purchase) {
- if (purchase == null) {
- log("Purchase is null");
- return true;
- }
- return false;
- }
- private boolean checkPurchase(Inventory inventory, String name) {
- Purchase purchase = inventory.getPurchase(name);
- return (purchase != null && verifyPayload(purchase));
- }
- private boolean consume(Inventory inventory, String name) {
- if (checkPurchase(inventory, name)) {
- log("Consumed: " + name);
- helper.consumeAsync(inventory.getPurchase(name), consumeFinishedListener);
- return true;
- }
- return false;
- }
- private boolean compareSKU(Purchase purchase, String name) {
- return purchase.getSku().equals(name);
- }
- private boolean verifyPayload(Purchase purchase) {
- return purchase.getDeveloperPayload().equals(key);
- }
- private void saveData() {
- final SharedPreferences.Editor preferences = getPreferences(MODE_PRIVATE).edit();
- preferences.putInt("money", money);
- preferences.commit();
- }
- private void loadData() {
- final SharedPreferences preferences = getPreferences(MODE_PRIVATE);
- money = preferences.getInt("money", 0);
- }
- private void log(String input) {
- if (debugMode)
- Log.d(TAG, input);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement