Ejemplo n.º 1
0
  // "Subscribe to infinite gas" button clicked. Explain to user, then start purchase
  // flow for subscription.
  public void onInfiniteGasButtonClicked(View arg0) {
    if (!setupDone) {
      complain("Billing Setup is not completed yet");
      return;
    }

    if (!mHelper.subscriptionsSupported()) {
      complain("Subscriptions not supported on your device yet. Sorry!");
      return;
    }

    /* TODO: for security, generate your payload here for verification. See the comments on
     *        verifyDeveloperPayload() for more info. Since this is a SAMPLE, we just use
     *        an empty string, but on a production app you should carefully generate this. */
    String payload = "";

    setWaitScreen(true);
    Log.d(TAG, "Launching purchase flow for infinite gas subscription.");
    mHelper.launchPurchaseFlow(
        this,
        SKU_INFINITE_GAS,
        IabHelper.ITEM_TYPE_SUBS,
        RC_REQUEST,
        mPurchaseFinishedListener,
        payload);
  }
Ejemplo n.º 2
0
  // User clicked the "Buy Gas" button
  public void onBuyGasButtonClicked(View arg0) {
    Log.d(TAG, "Buy gas button clicked.");

    if (mSubscribedToInfiniteGas) {
      complain("No need! You're subscribed to infinite gas. Isn't that awesome?");
      return;
    }

    if (mTank >= TANK_MAX) {
      complain("Your tank is full. Drive around a bit!");
      return;
    }

    if (!setupDone) {
      complain("Billing Setup is not completed yet");
      return;
    }
    // launch the gas purchase UI flow.
    // We will be notified of completion via mPurchaseFinishedListener
    setWaitScreen(true);
    Log.d(TAG, "Launching purchase flow for gas.");

    /* TODO: for security, generate your payload here for verification. See the comments on
     *        verifyDeveloperPayload() for more info. Since this is a SAMPLE, we just use
     *        an empty string, but on a production app you should carefully generate this. */
    String payload = "";

    mHelper.launchPurchaseFlow(this, SKU_GAS, RC_REQUEST, mPurchaseFinishedListener, payload);
  }
 public void unbindService() {
   if (_helper != null) {
     _helper.dispose();
     _helper = null;
   }
   destroyBroadcasts();
 }
Ejemplo n.º 4
0
  // We're being destroyed. It's important to dispose of the helper here!
  @Override
  public void onDestroy() {
    super.onDestroy();

    // very important:
    Log.d(TAG, "Destroying helper.");
    if (mHelper != null) mHelper.dispose();
    mHelper = null;
  }
Ejemplo n.º 5
0
  // User clicked the "Upgrade to Premium" button.
  public void onUpgradeAppButtonClicked(View arg0) {
    Log.d(TAG, "Upgrade button clicked; launching purchase flow for upgrade.");

    if (!setupDone) {
      complain("Billing Setup is not completed yet");
      return;
    }

    setWaitScreen(true);

    /* TODO: for security, generate your payload here for verification. See the comments on
     *        verifyDeveloperPayload() for more info. Since this is a SAMPLE, we just use
     *        an empty string, but on a production app you should carefully generate this. */
    String payload = "";

    mHelper.launchPurchaseFlow(this, SKU_PREMIUM, RC_REQUEST, mPurchaseFinishedListener, payload);
  }
Ejemplo n.º 6
0
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
          Log.d(TAG, "Query inventory finished.");
          if (result.isFailure()) {
            complain("Failed to query inventory: " + result);
            return;
          }

          Log.d(TAG, "Query inventory was successful.");

          /*
           * Check for items we own. Notice that for each purchase, we check
           * the developer payload to see if it's correct! See
           * verifyDeveloperPayload().
           */

          // Do we have the premium upgrade?
          Purchase premiumPurchase = inventory.getPurchase(SKU_PREMIUM);
          mIsPremium = (premiumPurchase != null && verifyDeveloperPayload(premiumPurchase));
          Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"));

          // Do we have the infinite gas plan?
          Purchase infiniteGasPurchase = inventory.getPurchase(SKU_INFINITE_GAS);
          mSubscribedToInfiniteGas =
              (infiniteGasPurchase != null && verifyDeveloperPayload(infiniteGasPurchase));
          Log.d(
              TAG,
              "User "
                  + (mSubscribedToInfiniteGas ? "HAS" : "DOES NOT HAVE")
                  + " infinite gas subscription.");
          if (mSubscribedToInfiniteGas) mTank = TANK_MAX;

          // Check for gas delivery -- if we own gas, we should fill up the tank immediately
          Purchase gasPurchase = inventory.getPurchase(SKU_GAS);
          if (gasPurchase != null && verifyDeveloperPayload(gasPurchase)) {
            Log.d(TAG, "We have gas. Consuming it.");
            mHelper.consumeAsync(inventory.getPurchase(SKU_GAS), mConsumeFinishedListener);
            return;
          }

          updateUi();
          setWaitScreen(false);
          Log.d(TAG, "Initial inventory query finished; enabling main UI.");
        }
Ejemplo n.º 7
0
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(
        TAG,
        "onActivityResult() requestCode: "
            + requestCode
            + " resultCode: "
            + resultCode
            + " data: "
            + data);

    // Pass on the activity result to the helper for handling
    if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
      // not handled, so handle it ourselves (here's where you'd
      // perform any handling of activity results not related to in-app
      // billing...
      super.onActivityResult(requestCode, resultCode, data);
    } else {
      Log.d(TAG, "onActivityResult handled by IABUtil.");
    }
  }
Ejemplo n.º 8
0
        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
          Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase);
          if (result.isFailure()) {
            complain("Error purchasing: " + result);
            setWaitScreen(false);
            return;
          }
          if (!verifyDeveloperPayload(purchase)) {
            complain("Error purchasing. Authenticity verification failed.");
            setWaitScreen(false);
            return;
          }

          Log.d(TAG, "Purchase successful.");

          if (purchase.getSku().equals(SKU_GAS)) {
            // bought 1/4 tank of gas. So consume it.
            Log.d(TAG, "Purchase is gas. Starting gas consumption.");
            mHelper.consumeAsync(purchase, mConsumeFinishedListener);
          } else if (purchase.getSku().equals(SKU_PREMIUM)) {
            // bought the premium upgrade!
            Log.d(TAG, "Purchase is premium upgrade. Congratulating user.");
            alert("Thank you for upgrading to premium!");
            mIsPremium = true;
            updateUi();
            setWaitScreen(false);
          } else if (purchase.getSku().equals(SKU_INFINITE_GAS)) {
            // bought the infinite gas subscription
            Log.d(TAG, "Infinite gas subscription purchased.");
            alert("Thank you for subscribing to infinite gas!");
            mSubscribedToInfiniteGas = true;
            mTank = TANK_MAX;
            updateUi();
            setWaitScreen(false);
          }
        }
Ejemplo n.º 9
0
        public void onConsumeFinished(Purchase purchase, IabResult result) {
          Log.d(TAG, "Consumption finished. Purchase: " + purchase + ", result: " + result);

          String sku = purchase.getSku();
          purchase.setSku(OpenIabHelper.getSku(purchase.getAppstoreName(), sku));

          if (result.isFailure()) {
            Log.e(TAG, "Error while consuming: " + result);
            UnityPlayer.UnitySendMessage(
                EVENT_MANAGER, CONSUME_PURCHASE_FAILED_CALLBACK, result.getMessage());
            return;
          }
          Log.d(TAG, "Consumption successful. Provisioning.");
          String jsonPurchase;
          try {
            jsonPurchase = purchaseToJson(purchase);
          } catch (JSONException e) {
            UnityPlayer.UnitySendMessage(
                EVENT_MANAGER, CONSUME_PURCHASE_FAILED_CALLBACK, "Couldn't serialize the purchase");
            return;
          }
          UnityPlayer.UnitySendMessage(
              EVENT_MANAGER, CONSUME_PURCHASE_SUCCEEDED_CALLBACK, jsonPurchase);
        }
Ejemplo n.º 10
0
  static {
    OpenIabHelper.mapSku(
        SKU_PREMIUM, OpenIabHelper.NAME_AMAZON, "org.onepf.trivialdrive.amazon.premium");
    OpenIabHelper.mapSku(SKU_PREMIUM, OpenIabHelper.NAME_TSTORE, "tstore_sku_premium");
    OpenIabHelper.mapSku(SKU_PREMIUM, OpenIabHelper.NAME_SAMSUNG, "100000100696/000001003746");
    OpenIabHelper.mapSku(SKU_PREMIUM, "com.yandex.store", "org.onepf.trivialdrive.premium");

    OpenIabHelper.mapSku(SKU_GAS, OpenIabHelper.NAME_AMAZON, "org.onepf.trivialdrive.amazon.gas");
    OpenIabHelper.mapSku(SKU_GAS, OpenIabHelper.NAME_TSTORE, "tstore_sku_gas");
    OpenIabHelper.mapSku(SKU_GAS, OpenIabHelper.NAME_SAMSUNG, "100000100696/000001003744");
    OpenIabHelper.mapSku(SKU_GAS, "com.yandex.store", "org.onepf.trivialdrive.gas");

    OpenIabHelper.mapSku(
        SKU_INFINITE_GAS, OpenIabHelper.NAME_AMAZON, "org.onepf.trivialdrive.amazon.infinite_gas");
    OpenIabHelper.mapSku(SKU_INFINITE_GAS, OpenIabHelper.NAME_TSTORE, "tstore_sku_infinite_gas");
    OpenIabHelper.mapSku(SKU_INFINITE_GAS, OpenIabHelper.NAME_SAMSUNG, "100000100696/000001003747");
    OpenIabHelper.mapSku(
        SKU_INFINITE_GAS, "com.yandex.store", "org.onepf.trivialdrive.infinite_gas");
  }
Ejemplo n.º 11
0
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // load game data
    loadData();

    /* base64EncodedPublicKey should be YOUR APPLICATION'S PUBLIC KEY
     * (that you got from the Google Play developer console). This is not your
     * developer public key, it's the *app-specific* public key.
     *
     * Instead of just storing the entire literal string here embedded in the
     * program,  construct the key at runtime from pieces or
     * use bit manipulation (for example, XOR with some other string) to hide
     * the actual key.  The key itself is not secret information, but we don't
     * want to make it easy for an attacker to replace the public key with one
     * of their own and then fake messages from the server.
     */
    String base64EncodedPublicKey = "CONSTRUCT_YOUR_KEY_AND_PLACE_IT_HERE";
    String YANDEX_PUBLIC_KEY = "PLACE_HERE_YANDEX_KEY";

    // Some sanity checks to see if the developer (that's you!) really followed the
    // instructions to run this sample (don't put these checks on your app!)
    if (base64EncodedPublicKey.contains("CONSTRUCT_YOUR")) {
      throw new RuntimeException(
          "Please put your app's public key in MainActivity.java. See README.");
    }
    if (getPackageName().startsWith("com.example")) {
      throw new RuntimeException("Please change the sample's package name! See README.");
    }

    // Create the helper, passing it our context and the public key to verify signatures with
    Log.d(TAG, "Creating IAB helper.");
    Map<String, String> storeKeys = new HashMap<String, String>();
    storeKeys.put(OpenIabHelper.NAME_GOOGLE, base64EncodedPublicKey);
    storeKeys.put("com.yandex.store", YANDEX_PUBLIC_KEY);

    mHelper = new OpenIabHelper(this, storeKeys);

    // enable debug logging (for a production application, you should set this to false).
    // mHelper.enableDebugLogging(true);

    // Start setup. This is asynchronous and the specified listener
    // will be called once setup completes.
    Log.d(TAG, "Starting setup.");
    mHelper.startSetup(
        new IabHelper.OnIabSetupFinishedListener() {
          public void onIabSetupFinished(IabResult result) {
            Log.d(TAG, "Setup finished.");

            if (!result.isSuccess()) {
              // Oh noes, there was a problem.
              complain("Problem setting up in-app billing: " + result);
              return;
            }

            // Hooray, IAB is fully set up. Now, let's get an inventory of stuff we own.
            Log.d(TAG, "Setup successful. Querying inventory.");
            setupDone = true;
            mHelper.queryInventoryAsync(mGotInventoryListener);
          }
        });
  }
 private void purchaseStateChanged(Intent data) {
   Log.d(TAG, "purchaseStateChanged intent: " + data);
   _helper.handleActivityResult(RC_REQUEST, Activity.RESULT_OK, data);
 }
 public boolean areSubscriptionsSupported() {
   return _helper.subscriptionsSupported();
 }
Ejemplo n.º 14
0
 public void mapSku(String sku, String storeName, String storeSku) {
   OpenIabHelper.mapSku(sku, storeName, storeSku);
 }