Example #1
0
 List<String> getAllOwnedSkus(String itemType) {
   List<String> result = new ArrayList<String>();
   for (Purchase p : purchaseMap.values()) {
     if (p.getItemType().equals(itemType)) result.add(p.getSku());
   }
   return result;
 }
Example #2
0
  /**
   * Handles an activity result that's part of the purchase flow in in-app billing. If you are
   * calling {@link #launchPurchaseFlow}, then you must call this method from your Activity's {@link
   * android.app.Activity@onActivityResult} method. This method MUST be called from the UI thread of
   * the Activity.
   *
   * @param requestCode The requestCode as you received it.
   * @param resultCode The resultCode as you received it.
   * @param data The data (Intent) as you received it.
   * @return Returns true if the result was related to a purchase flow and was handled; false if the
   *     result was not related to a purchase, in which case you should handle it normally.
   */
  public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
    Result result;
    if (requestCode != this.requestCode) return false;

    checkSetupDone("handleActivityResult");

    asyncFlag.end();

    if (data == null) {
      result = new Result(HELPER_BAD_RESPONSE, "Null data in IAB result");
      if (purchaseListener != null) {
        purchaseListener.onPurchaseFinished(result, null);
      }
      return true;
    }

    int responseCode = getResponseCodeFromIntent(data);
    String purchaseData = data.getStringExtra(RESPONSE_INAPP_PURCHASE_DATA);
    String dataSignature = data.getStringExtra(RESPONSE_INAPP_SIGNATURE);

    if (resultCode == Activity.RESULT_OK && responseCode == BILLING_RESPONSE_RESULT_OK) {

      if (purchaseData == null || dataSignature == null) {
        result =
            new Result(HELPER_UNKNOWN_ERROR, "IAB returned null purchaseData or dataSignature");
        if (purchaseListener != null) {
          purchaseListener.onPurchaseFinished(result, null);
        }
        return true;
      }

      Purchase purchase;
      try {
        purchase = new Purchase(purchaseData, dataSignature);
        String sku = purchase.getSku();

        if (!security.verifyPurchase(purchaseData, dataSignature)) {
          result =
              new Result(
                  HELPER_VERIFICATION_FAILED, "Signature verification failed for sku " + sku);
          if (purchaseListener != null) {
            purchaseListener.onPurchaseFinished(result, purchase);
          }
          return true;
        }
      } catch (JSONException e) {
        e.printStackTrace();
        result = new Result(HELPER_BAD_RESPONSE, "Failed to parse purchase data.");
        if (purchaseListener != null) {
          purchaseListener.onPurchaseFinished(result, null);
        }
        return true;
      }

      if (purchaseListener != null) {
        purchaseListener.onPurchaseFinished(
            new Result(BILLING_RESPONSE_RESULT_OK, "Success"), purchase);
      }
    } else if (resultCode == Activity.RESULT_OK) {
      if (purchaseListener != null) {
        result = new Result(responseCode, "Problem purchashing item.");
        purchaseListener.onPurchaseFinished(result, null);
      }
    } else if (resultCode == Activity.RESULT_CANCELED) {
      result = new Result(HELPER_USER_CANCELLED, "User canceled.");
      if (purchaseListener != null) {
        purchaseListener.onPurchaseFinished(result, null);
      }
    } else {
      result = new Result(HELPER_UNKNOWN_PURCHASE_RESPONSE, "Unknown purchase response.");
      if (purchaseListener != null) purchaseListener.onPurchaseFinished(result, null);
    }
    return true;
  }
Example #3
0
 void addPurchase(Purchase p) {
   purchaseMap.put(p.getSku(), p);
 }