Beispiel #1
0
  @Override
  public void queryPurchases(Preferences pref) {
    this.pref = pref;
    try {
      Bundle ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
      int response = ownedItems.getInt("RESPONSE_CODE");
      if (response == 0) {
        ArrayList<String> ownedSkus = ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");

        for (int i = 0; i < ownedSkus.size(); i++) {
          String name = null;

          if (ownedSkus.get(i).equals("ryan")) name = "Ryan";
          else if (ownedSkus.get(i).equals("ash")) name = "Ash";
          else if (ownedSkus.get(i).equals("rob")) name = "Rob";
          else if (ownedSkus.get(i).equals("battle_cat")) name = "BattleCat";
          else if (ownedSkus.get(i).equals("xorp")) name = "Xorp";
          else if (ownedSkus.get(i).equals("rootsworth")) name = "Rootsworth";
          else if (ownedSkus.get(i).equals("snap")) name = "Snap";
          else if (ownedSkus.get(i).equals("metatron")) name = "Metatron";
          else if (ownedSkus.get(i).equals("abaddon")) name = "Abaddon";

          pref.putBoolean(name, true);
          pref.flush();
        }
      }
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }
  int queryPurchases(Inventory inv, String itemType) throws JSONException, RemoteException {
    // Query purchases
    logDebug("Querying owned items, item type: " + itemType);
    logDebug("Package name: " + mContext.getPackageName());
    boolean verificationFailed = false;
    String continueToken = null;

    do {
      logDebug("Calling getPurchases with continuation token: " + continueToken);
      Bundle ownedItems =
          mService.getPurchases(3, mContext.getPackageName(), itemType, continueToken);

      int response = getResponseCodeFromBundle(ownedItems);
      logDebug("Owned items response: " + String.valueOf(response));
      if (response != BILLING_RESPONSE_RESULT_OK) {
        logDebug("getPurchases() failed: " + getResponseDesc(response));
        return response;
      }
      if (!ownedItems.containsKey(RESPONSE_INAPP_ITEM_LIST)
          || !ownedItems.containsKey(RESPONSE_INAPP_PURCHASE_DATA_LIST)
          || !ownedItems.containsKey(RESPONSE_INAPP_SIGNATURE_LIST)) {
        logError("Bundle returned from getPurchases() doesn't contain required fields.");
        return IABHELPER_BAD_RESPONSE;
      }

      ArrayList<String> ownedSkus = ownedItems.getStringArrayList(RESPONSE_INAPP_ITEM_LIST);
      ArrayList<String> purchaseDataList =
          ownedItems.getStringArrayList(RESPONSE_INAPP_PURCHASE_DATA_LIST);
      ArrayList<String> signatureList =
          ownedItems.getStringArrayList(RESPONSE_INAPP_SIGNATURE_LIST);

      for (int i = 0; i < purchaseDataList.size(); ++i) {
        String purchaseData = purchaseDataList.get(i);
        String signature = signatureList.get(i);
        String sku = ownedSkus.get(i);
        if (Security.verifyPurchase(mSignatureBase64, purchaseData, signature)) {
          logDebug("Sku is owned: " + sku);
          Purchase purchase = new Purchase(itemType, purchaseData, signature);

          if (TextUtils.isEmpty(purchase.getToken())) {
            logWarn("BUG: empty/null token!");
            logDebug("Purchase data: " + purchaseData);
          }

          // Record ownership and token
          inv.addPurchase(purchase);
        } else {
          logWarn("Purchase signature verification **FAILED**. Not adding item.");
          logDebug("   Purchase data: " + purchaseData);
          logDebug("   Signature: " + signature);
          verificationFailed = true;
        }
      }

      continueToken = ownedItems.getString(INAPP_CONTINUATION_TOKEN);
      logDebug("Continuation token: " + continueToken);
    } while (!TextUtils.isEmpty(continueToken));

    return verificationFailed ? IABHELPER_VERIFICATION_FAILED : BILLING_RESPONSE_RESULT_OK;
  }
Beispiel #3
0
  @Click(R.id.get_purchases_button)
  public void getPurchases(View v) {
    try {
      Bundle ownedItems;
      ownedItems = billingService.getPurchases(apiLevel, getPackageName(), "inapp", null);

      if (ownedItems.getInt("RESPONSE_CODE") != 0) {
        logView.append("getPurchases Fail\n");
        return;
      }

      ArrayList<String> purchaseDataList =
          ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
      if (purchaseDataList.size() == 0) {
        logView.append("getPurchases empty\n");
      }

      for (String purchaseData : purchaseDataList) {
        logView.append("getPurchases: " + purchaseData + "\n");
      }
    } catch (RemoteException e) {
      String msg = "getPurchases: " + e.getMessage() + "\n";
      logView.append(msg);

      Log.e(TAG, msg);
      e.printStackTrace();
    }
  }
 static void insertPurchases(
     @Nonnull Billing billing, @Nonnull String product, @Nonnull List<Purchase> purchases)
     throws RemoteException {
   final Bundle bundle = newBundle(OK);
   final ArrayList<String> list = new ArrayList<String>();
   for (Purchase purchase : purchases) {
     list.add(purchase.toJson());
   }
   bundle.putStringArrayList(Purchases.BUNDLE_DATA_LIST, list);
   final IInAppBillingService service = ((TestServiceConnector) billing.getConnector()).service;
   when(service.getPurchases(anyInt(), anyString(), eq(product), isNull(String.class)))
       .thenReturn(bundle);
 }
  @NonNull
  @Override
  public ReceiptResponse getProductPurchases(
      @NonNull @ProductType String type, String continueToken) throws BillingException {
    try {
      Bundle data = delegate.getPurchases(API_VERSION, packageName, type, continueToken);
      checkBundle(data);
      ArrayList<String> skus = data.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
      ArrayList<String> purchases = data.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
      ArrayList<String> signatures = data.getStringArrayList("INAPP_DATA_SIGNATURE_LIST");
      String nextToken = data.getString("INAPP_CONTINUATION_TOKEN");

      if (skus == null
          || purchases == null
          || signatures == null
          || skus.size() != purchases.size()
          || purchases.size() != signatures.size()) {
        throw new BillingException("unexpected data in response");
      }

      List<Receipt> receipts = new ArrayList<>(skus.size());

      for (int i = 0; i < skus.size(); i++) {
        String json = purchases.get(i);
        String signature = signatures.get(i);
        Purchase purchase = gson.fromJson(json, Purchase.class);
        Receipt receipt =
            new Receipt.Builder()
                .purchase(purchase)
                .signature(signature)
                .originalJson(json)
                .build();
        receipts.add(receipt);
      }

      return ReceiptResponse.Factory.make(receipts, nextToken);
    } catch (RemoteException e) {
      throw new BillingException(e);
    }
  }
Beispiel #6
0
  @Click(R.id.consume_purchase)
  public void consumePurchase(View v) {
    try {
      Bundle ownedItems = billingService.getPurchases(apiLevel, getPackageName(), "inapp", null);

      ArrayList<String> purchaseDataList =
          ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
      for (String purchaseData : purchaseDataList) {
        JSONObject obj = new JSONObject(purchaseData);
        String purchaseToken = obj.optString("token", obj.optString("purchaseToken"));

        billingService.consumePurchase(apiLevel, getPackageName(), purchaseToken);
        logView.append("消費しました\n");
      }
    } catch (Exception e) {
      String msg = "getPurchases: " + e.getMessage() + "\n";
      logView.append(msg);

      Log.e(TAG, msg);
      e.printStackTrace();
    }
  }
Beispiel #7
0
 public Bundle getPurchases(String continueToken) throws RemoteException {
   return billingService.getPurchases(
       3, context.getPackageName(), ITEM_TYPE_INAPP, continueToken);
 }