Advertisement
semtiko

IAB stuff

Mar 13th, 2013
1,454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.86 KB | None | 0 0
  1. public class MainActivity extends AndroidApplication {
  2.     private static final String TAG = "IAB";
  3.  
  4.     /*
  5.      * Needed only for testing.
  6.      * Set it to false when you ready for publishing your app
  7.      */
  8.     private static boolean debugMode = true;
  9.  
  10.     /*
  11.      * Any random salt (not Math.random() =) Just one word, same for all users)
  12.      */
  13.     private static final String key = "anyRandomSalt";
  14.  
  15.     /*
  16.      * Public key from market. Go to Play console, select needed app and go to "Services and API",
  17.      * and you'll find key.
  18.      */
  19.     private static final String publicKey = "longPublicKeyFromMarket";
  20.  
  21.     private static final int RC_REQUEST = 10001; // Any number
  22.     private boolean billingSupported = false; // Don't change it manually
  23.     private boolean busy = false; // Don't change it manually
  24.     private IabHelper helper;
  25.  
  26.     /*
  27.      * This is very important part.
  28.      * Here you must make SKUs names from Play Console, but one cool thing here.
  29.      * For testing without money, you can use one of 4 special names, and you should get a fake card
  30.      * prompt in the purchase screen when you try to buy this item in your app.
  31.      * You don't need to make changes in Play console and build public version of app.
  32.      * Just run it on devise in build mode. And you don't neet to make
  33.      * any changes in your AndroidManifest.xml (e.g. version) before you will ready to publish your app.
  34.      *
  35.      * Special names:
  36.      * android.test.purchased, android.test.canceled, android.test.refunded, android.test.item_unavailable
  37.      */
  38.     private static final String SKU_ADS_FREE   = "ads_free"; // non consumable. restored even after reinstall
  39.     private static final String SKU_MONEY_1000 = "money_1000"; // consumable. without restore after reinstall
  40.     private static final String SKU_MONEY_3000 = "android.test.purchased"; // consumable. without restore after reinstall
  41.  
  42.     private boolean adsfree = false;
  43.     private int money = 0;
  44.  
  45.     @Override
  46.     public void onCreate(Bundle savedInstanceState) {
  47.         super.onCreate(savedInstanceState);
  48.  
  49.         loadData();
  50.  
  51.         helper = new IabHelper(getApplicationContext(), publicKey);
  52.  
  53.         if (debugMode)
  54.             helper.enableDebugLogging(true);
  55.  
  56.         helper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
  57.             public void onIabSetupFinished(IabResult result) {
  58.                 if (result.isSuccess()) {
  59.                     billingSupported = true;
  60.                     busy = true;
  61.                     helper.queryInventoryAsync(gotInventoryListener);
  62.                     log("IAB started");
  63.                 } else {
  64.                     billingSupported = false;
  65.                     log("Problem setting up IAB: " + result);
  66.                 }
  67.             }
  68.         });
  69.  
  70.         final AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
  71.         config.useGL20     = false;
  72.         config.useWakelock = false;
  73.  
  74.         final Resolver resolver = new Resolver() {
  75.             @Override
  76.             public boolean getBusyState() {
  77.                 return busy;
  78.             }
  79.  
  80.             @Override
  81.             public boolean getAd() {
  82.                 return adsfree;
  83.             }
  84.  
  85.             @Override
  86.             public int getMoney() {
  87.                 return money;
  88.             }
  89.  
  90.             @Override
  91.             public void buyAdsFree() {
  92.                 if (!adsfree)
  93.                     buyItem(SKU_ADS_FREE);
  94.             }
  95.  
  96.             @Override
  97.             public void buyMoney1000() {
  98.                 buyItem(SKU_MONEY_1000);
  99.             }
  100.  
  101.             @Override
  102.             public void buyMoney3000() {
  103.                 buyItem(SKU_MONEY_3000);
  104.             }
  105.         };
  106.  
  107.         initialize(new Main(resolver), config);
  108.     }
  109.  
  110.     @Override
  111.     public void onDestroy() {
  112.         super.onDestroy();
  113.  
  114.         if (billingSupported()) {
  115.             helper.dispose();
  116.             helper = null;
  117.         }
  118.     }
  119.  
  120.     @Override
  121.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  122.         if (!billingSupported())
  123.             return;
  124.  
  125.         log("onActivityResult(" + requestCode + "," + resultCode + "," + data);
  126.  
  127.         if (helper.handleActivityResult(requestCode, resultCode, data)) {
  128.             log("onActivityResult handled by IABUtil");
  129.         } else {
  130.             super.onActivityResult(requestCode, resultCode, data);
  131.         }
  132.     }
  133.  
  134.     @Override
  135.     public void onResume() {
  136.         super.onResume();
  137.  
  138.         if (billingSupported() && !busy) {
  139.             busy = true;
  140.             helper.queryInventoryAsync(gotInventoryListener);
  141.         }
  142.     }
  143.  
  144.     private void buyItem(final String sku) {
  145.         if (!billingSupported()) {
  146.             log("Billing is not supported");
  147.             return;
  148.         }
  149.  
  150.         if (busy) {
  151.             log("Can't buy item. So busy yet");
  152.             return;
  153.         }
  154.  
  155.         try {
  156.             new Thread(new Runnable() {
  157.                 @Override
  158.                 public void run() {
  159.                     busy = true;
  160.                     helper.launchPurchaseFlow(MainActivity.this, sku, RC_REQUEST, purchaseFinishedListener, key);
  161.                 }
  162.             }).start();
  163.         } catch (Exception e) {
  164.             log("buyItem Exception: " + e);
  165.         }
  166.     }
  167.  
  168.     private final IabHelper.OnIabPurchaseFinishedListener purchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
  169.         public void onIabPurchaseFinished(IabResult result, final Purchase purchase) {
  170.             log("Purchase finished: " + result + ", purchase: " + purchase);
  171.  
  172.             if (purchaseIsNull(purchase)) {
  173.                 busy = false;
  174.                 return;
  175.             } else if (result.isFailure()) {
  176.                 log("Purchase failure: " + result + ", purchase: " + purchase);
  177.                 busy = false;
  178.                 return;
  179.             } else if (!verifyPayload(purchase)) {
  180.                 log("Error purchasing. Authenticity verification failed");
  181.                 busy = false;
  182.                 return;
  183.             }
  184.  
  185.             log("Purchase successful");
  186.  
  187.             if (compareSKU(purchase, SKU_ADS_FREE)) {
  188.                 adsfree = true;
  189.                 busy = false;
  190.             } else if (compareSKU(purchase, SKU_MONEY_1000) ||
  191.                    compareSKU(purchase, SKU_MONEY_3000)) {
  192.                 helper.consumeAsync(purchase, consumeFinishedListener);
  193.             }
  194.         }
  195.     };
  196.  
  197.     private final IabHelper.OnConsumeFinishedListener consumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
  198.         @Override
  199.         public void onConsumeFinished(Purchase purchase, IabResult result) {
  200.             /*
  201.              * Only for consumable items
  202.              */
  203.             if (result.isSuccess()) {
  204.                 if (compareSKU(purchase, SKU_MONEY_1000)) {
  205.                     money += 1000;
  206.                     log("Get + 1000$");
  207.                 } else if (compareSKU(purchase, SKU_MONEY_3000)) {
  208.                     money += 3000;
  209.                     log("Get + 3000$");
  210.                 }
  211.  
  212.                 saveData();
  213.             } else {
  214.                 log("Error while consuming: " + result);
  215.             }
  216.  
  217.             log("Consumption finished: " + result + ", purchase: " + purchase);
  218.             busy = false;
  219.         }
  220.     };
  221.  
  222.     private final IabHelper.QueryInventoryFinishedListener gotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
  223.         public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
  224.             if (result.isFailure()) {
  225.                 log("InventoryFinishedListener: " + result);
  226.                 busy = false;
  227.                 return;
  228.             }
  229.  
  230.             adsfree = checkPurchase(inventory, SKU_ADS_FREE); // non consumable
  231.  
  232.             if (consume(inventory, SKU_MONEY_1000)) return; // consumable
  233.             if (consume(inventory, SKU_MONEY_3000)) return; // consumable
  234.  
  235.             busy = false;
  236.         }
  237.     };
  238.  
  239.     private boolean billingSupported() {
  240.         return (helper != null && billingSupported);
  241.     }
  242.  
  243.     private boolean purchaseIsNull(Purchase purchase) {
  244.         if (purchase == null) {
  245.             log("Purchase is null");
  246.             return true;
  247.         }
  248.  
  249.         return false;
  250.     }
  251.  
  252.     private boolean checkPurchase(Inventory inventory, String name) {
  253.         Purchase purchase = inventory.getPurchase(name);
  254.         return (purchase != null && verifyPayload(purchase));
  255.     }
  256.  
  257.     private boolean consume(Inventory inventory, String name) {
  258.         if (checkPurchase(inventory, name)) {
  259.             log("Consumed: " + name);
  260.             helper.consumeAsync(inventory.getPurchase(name), consumeFinishedListener);
  261.             return true;
  262.         }
  263.  
  264.         return false;
  265.     }
  266.  
  267.     private boolean compareSKU(Purchase purchase, String name) {
  268.         return purchase.getSku().equals(name);
  269.     }
  270.  
  271.     private boolean verifyPayload(Purchase purchase) {
  272.         return purchase.getDeveloperPayload().equals(key);
  273.     }
  274.  
  275.     private void saveData() {
  276.         final SharedPreferences.Editor preferences = getPreferences(MODE_PRIVATE).edit();
  277.         preferences.putInt("money", money);
  278.         preferences.commit();
  279.     }
  280.  
  281.     private void loadData() {
  282.         final SharedPreferences preferences = getPreferences(MODE_PRIVATE);
  283.         money = preferences.getInt("money", 0);
  284.     }
  285.  
  286.     private void log(String input) {
  287.         if (debugMode)
  288.             Log.d(TAG, input);
  289.     }
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement