@Override public void onServiceConnected(ComponentName name, IBinder service) { billingService = IInAppBillingService.Stub.asInterface(service); String packageName = context.getPackageName(); try { // check for in-app billing v3 support int response = billingService.isBillingSupported(3, packageName, ITEM_TYPE_INAPP); if (response != BILLING_RESPONSE_RESULT_OK) { if (listener != null) { listener.onSetupFinished( new Result(response, "Error checking for billing v3 support.")); } return; } } catch (RemoteException e) { if (listener != null) { listener.onSetupFinished( new Result( HELPER_REMOTE_EXCEPTION, "RemoteException while setting up in-app billing.")); } e.printStackTrace(); return; } if (listener != null) { listener.onSetupFinished(new Result(BILLING_RESPONSE_RESULT_OK, "Setup successful.")); } }
@Override public void onServiceConnected(ComponentName name, IBinder service) { billingService = IInAppBillingService.Stub.asInterface(service); new AsyncTask<String, String, Bundle>() { private Bundle ownedItems = null; private Bundle ownedSubs = null; @Override protected Bundle doInBackground(String... params) { Bundle skuDetails = null; try { ArrayList<String> skuList = new ArrayList<String>(); skuList.add(productId); // Add subscription codes skuList.add(LibrelioApplication.getYearlySubsCode(getContext())); skuList.add(LibrelioApplication.getMonthlySubsCode(getContext())); Bundle querySkus = new Bundle(); querySkus.putStringArrayList("ITEM_ID_LIST", skuList); // Retrieve relevant in app items skuDetails = billingService.getSkuDetails(3, getPackageName(), "inapp", querySkus); ArrayList<String> details = skuDetails.getStringArrayList("DETAILS_LIST"); // Retrieve relevant subscriptions skuDetails = billingService.getSkuDetails(3, getPackageName(), "subs", querySkus); ArrayList<String> subsDetails = skuDetails.getStringArrayList("DETAILS_LIST"); // Combine in app and subscriptions details.addAll(subsDetails); skuDetails.putStringArrayList("DETAILS_LIST", details); // Retrieve owned in app items ownedItems = billingService.getPurchases(3, getPackageName(), "inapp", null); // Retrieve owned AND current subscriptions ownedSubs = billingService.getPurchases(3, getPackageName(), "subs", null); } catch (RemoteException e) { Log.d(TAG, "InAppBillingService failed", e); return null; } return skuDetails; } @Override protected void onPostExecute(Bundle skuDetails) { // If item was purchase then download begin without open billing activity int getPurchaseResponse = ownedItems.getInt("RESPONSE_CODE"); if (TEST_MODE) { getPurchaseResponse = -1; } if (getPurchaseResponse == 0) { ArrayList<String> ownedSkus = ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST"); for (String s : ownedSkus) { Log.d(TAG, productId + " already purchased? " + s); } if (ownedSkus.contains(productId)) { prepareDownloadWithOwnedItem(ownedItems, productId); return; } ownedSkus = ownedSubs.getStringArrayList("INAPP_PURCHASE_ITEM_LIST"); for (String s : ownedSkus) { Log.d(TAG, productId + " already purchased? " + s); } if (ownedSkus.contains(LibrelioApplication.getYearlySubsCode(getContext()))) { prepareDownloadWithOwnedItem( ownedSubs, LibrelioApplication.getYearlySubsCode(getContext())); return; } if (ownedSkus.contains(LibrelioApplication.getMonthlySubsCode(getContext()))) { prepareDownloadWithOwnedItem( ownedSubs, LibrelioApplication.getMonthlySubsCode(getContext())); return; } } int response = skuDetails.getInt("RESPONSE_CODE"); if (response == 0) { Log.d(TAG, "response code was success"); ArrayList<String> details = skuDetails.getStringArrayList("DETAILS_LIST"); for (String detail : details) { Log.d(TAG, "response = " + detail); JSONObject object = null; String sku = ""; String price = ""; String title = ""; try { object = new JSONObject(detail); sku = object.getString("productId"); price = object.getString("price"); title = object.getString("title"); } catch (JSONException e) { Log.e(TAG, "getSKU details failed", e); } if (sku.equals(productId)) { productPrice = price; productTitle = title; } else if (sku.equals(LibrelioApplication.getYearlySubsCode(getContext()))) { yearlySubPrice = price; yearlySubTitle = title; } else if (sku.equals(LibrelioApplication.getMonthlySubsCode(getContext()))) { monthlySubPrice = price; monthlySubTitle = title; } } } if (!checkForValidSubscription(BillingActivity.this, fileName)) { setupDialogView(); } super.onPostExecute(skuDetails); } protected void prepareDownloadWithOwnedItem(Bundle ownedBundle, String subsoritemID) { ArrayList<String> ownedSkus = ownedBundle.getStringArrayList("INAPP_PURCHASE_ITEM_LIST"); int idx = ownedSkus.indexOf(subsoritemID); ArrayList<String> purchaseDataList = ownedBundle.getStringArrayList("INAPP_PURCHASE_DATA_LIST"); ArrayList<String> signatureList = ownedBundle.getStringArrayList("INAPP_DATA_SIGNATURE_LIST"); Log.d(TAG, "[getPurchases] purchaseDataList: " + purchaseDataList); Log.d(TAG, "[getPurchases] signatureList: " + signatureList); if (purchaseDataList != null) { ownedItemPurshaseData = purchaseDataList.get(idx); } if (signatureList != null) { ownedItemSignature = signatureList.get(idx); } onDownloadAction(ownedItemPurshaseData, ownedItemSignature); return; } }.execute(); }
@Override public void onServiceConnected(ComponentName name, IBinder service) { mService = IInAppBillingService.Stub.asInterface(service); }