private void makePurchase(boolean isSubscription) { try { Bundle buyIntentBundle = inAppBillingService.getBuyIntent( 3, getActivity().getPackageName(), TEST_SKU, isSubscription ? "subs" : "inapp", "this is a test"); int response = getResponseCodeFromBundle(buyIntentBundle); if (response != BILLING_RESPONSE_RESULT_OK) { Log.e(TAG, "Unable to buy item, Error response: " + response); return; } PendingIntent pendingIntent = buyIntentBundle.getParcelable(BUY_INTENT); getActivity().startIntentSenderForResult( pendingIntent.getIntentSender(), IN_APP_PURCHASE_RESULT, new Intent(), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0)); } catch (IntentSender.SendIntentException e) { Log.e(TAG, "In app purchase send intent exception.", e); } catch (RemoteException e) { Log.e(TAG, "In app purchase remote exception.", e); } }
Utility.logd(TAG, "In app billing service connected"); try { Bundle ownedItems = inAppBillingService.getPurchases( IN_APP_PURCHASE_VERSION, context.getPackageName(),
Bundle skuDetails = billingService.getSkuDetails(Constants.GOOGLE_API_VERSION, contextPackageName, purchaseType,
bundle = billingService.getBuyIntentToReplaceSkus(Constants.GOOGLE_API_SUBSCRIPTION_CHANGE_VERSION, contextPackageName, oldProductIds, bundle = billingService.getBuyIntentExtraParams(Constants.GOOGLE_API_VR_SUPPORTED_VERSION, contextPackageName, productId, bundle = billingService.getBuyIntent(Constants.GOOGLE_API_VERSION, contextPackageName, productId, bundle = billingService.getBuyIntentExtraParams(Constants.GOOGLE_API_VR_SUPPORTED_VERSION, contextPackageName, productId,
@Override public void start(@Nonnull IInAppBillingService service, @Nonnull String packageName) throws RemoteException { final int response = mExtraParams != null ? service.isBillingSupportedExtraParams(mApiVersion, packageName, mProduct, mExtraParams) : service.isBillingSupported(mApiVersion, packageName, mProduct); if (!handleError(response)) { onSuccess(new Object()); } }
@Override void start(@Nonnull IInAppBillingService service, @Nonnull String packageName) throws RemoteException, RequestException { final String payload = mPayload == null ? "" : mPayload; final Bundle bundle = mExtraParams != null ? service.getBuyIntentExtraParams(mApiVersion, packageName, mSku, mProduct, payload, mExtraParams) : service.getBuyIntent(mApiVersion, packageName, mSku, mProduct, payload); if (handleError(bundle)) { return; } final PendingIntent pendingIntent = bundle.getParcelable("BUY_INTENT"); Check.isNotNull(pendingIntent); onSuccess(pendingIntent); }
@Test public void testShouldError() throws Exception { final Request request = newRequest(); final RequestListener l = mock(RequestListener.class); request.setListener(l); final Bundle bundle = newBundle(BILLING_UNAVAILABLE); final IInAppBillingService service = mock(IInAppBillingService.class); when(service.isBillingSupported(anyInt(), anyString(), anyString())).thenReturn(BILLING_UNAVAILABLE); when(service.consumePurchase(anyInt(), anyString(), anyString())).thenReturn(BILLING_UNAVAILABLE); when(service.getPurchases(anyInt(), anyString(), anyString(), anyString())).thenReturn(bundle); when(service.getPurchaseHistory(anyInt(), anyString(), anyString(), anyString(), any(Bundle.class))).thenReturn(bundle); when(service.getSkuDetails(anyInt(), anyString(), anyString(), any(Bundle.class))).thenReturn(bundle); when(service.getBuyIntent(anyInt(), anyString(), anyString(), anyString(), anyString())).thenReturn(bundle); request.start(service, "testse"); verify(l).onError(eq(BILLING_UNAVAILABLE), any(Exception.class)); verify(l, never()).onSuccess(any()); }
private void consumePurchase(String packageName, String token) { try { int consumeResponse = inAppBillingService.consumePurchase( IN_APP_PURCHASE_VERSION, packageName, token); if (consumeResponse == 0) { Log.d(TAG, "Successfully consumed package: " + packageName); } else { Log.d(TAG, "Faileds to consume package: " + packageName + " " + consumeResponse); } } catch (RemoteException e) { Log.e(TAG, "Consuming purchase remote exception.", e); } }
public boolean isSubscriptionUpdateSupported() { // Avoid calling the service again if this value is true if (isSubsUpdateSupported) { return true; } try { int response = billingService.isBillingSupported(Constants.GOOGLE_API_SUBSCRIPTION_CHANGE_VERSION, contextPackageName, Constants.PRODUCT_TYPE_SUBSCRIPTION); isSubsUpdateSupported = response == Constants.BILLING_RESPONSE_RESULT_OK; } catch (RemoteException e) { e.printStackTrace(); } return isSubsUpdateSupported; }
@Test public void shouldCancelTestPurchaseWhenCancellingTestPurchases() throws Exception { activityBindAndConnect(); whenGetPurchasesRequestReturn(purchasesResponseOneTransactionFullEditionSandboxOrder()); v3InAppbillingService.cancelTestPurchases(); verify(nativeInAppBillingService).getPurchases(BILLING_API_VERSION, PACKAGE_NAME_GOOD, PURCHASE_TYPE_IN_APP, null); verify(nativeInAppBillingService).consumePurchase(eq(BILLING_API_VERSION), eq(PACKAGE_NAME_GOOD), anyString()); }
if (oldSkus == null || oldSkus.isEmpty()) { buyIntentBundle = mService.getBuyIntent(3, mContext.getPackageName(), sku, itemType, extraData); } else { return; buyIntentBundle = mService.getBuyIntentToReplaceSkus(5, mContext.getPackageName(), oldSkus, sku, itemType, extraData);
@Test public void testShouldLoadPurchasesWhenProductsBecameSupported() throws Exception { when(mService.isBillingSupported(eq(3), anyString(), eq(IN_APP))).thenReturn(BILLING_UNAVAILABLE); when(mService.isBillingSupported(eq(3), anyString(), eq(SUBSCRIPTION))).thenReturn(BILLING_UNAVAILABLE); when(mService.getPurchases(anyInt(), anyString(), anyString(), isNull(String.class))).thenReturn(newPurchasesBundle(0, false)); mCheckout.start(); final AwaitingCallback c1 = new AwaitingCallback(); mCheckout.loadInventory(Inventory.Request.create().loadAllPurchases(), c1); c1.waitWhileLoading(); assertFalse(c1.mProducts.get(IN_APP).supported); assertFalse(c1.mProducts.get(SUBSCRIPTION).supported); assertTrue(c1.mProducts.get(IN_APP).getPurchases().isEmpty()); when(mService.isBillingSupported(eq(3), anyString(), eq(IN_APP))).thenReturn(OK); when(mService.isBillingSupported(eq(3), anyString(), eq(SUBSCRIPTION))).thenReturn(OK); final AwaitingCallback c2 = new AwaitingCallback(); mCheckout.loadInventory(Inventory.Request.create().loadAllPurchases(), c2); c2.waitWhileLoading(); assertTrue(c2.mProducts.get(IN_APP).supported); assertTrue(c2.mProducts.get(SUBSCRIPTION).supported); assertTrue(c2.mProducts.get(IN_APP).getPurchases().size() == 1); }
private void prepareMultiPurchasesService(@Nonnull Billing billing) throws RemoteException, JSONException { final IInAppBillingService service = mock(IInAppBillingService.class); when(service.getPurchases(anyInt(), anyString(), anyString(), isNull(String.class))).thenReturn(newPurchasesBundle(0, true)); when(service.getPurchases(anyInt(), anyString(), anyString(), eq("1"))).thenReturn(newPurchasesBundle(1, true)); when(service.getPurchases(anyInt(), anyString(), anyString(), eq("2"))).thenReturn(newPurchasesBundle(2, true)); when(service.getPurchases(anyInt(), anyString(), anyString(), eq("3"))).thenReturn(newPurchasesBundle(3, true)); when(service.getPurchases(anyInt(), anyString(), anyString(), eq("4"))).thenReturn(newPurchasesBundle(4, false)); when(service.getPurchaseHistory(anyInt(), anyString(), anyString(), isNull(String.class), any(Bundle.class))).thenReturn(newPurchasesBundle(0, true, true)); when(service.getPurchaseHistory(anyInt(), anyString(), anyString(), eq("1"), any(Bundle.class))).thenReturn(newPurchasesBundle(1, true, true)); when(service.getPurchaseHistory(anyInt(), anyString(), anyString(), eq("2"), any(Bundle.class))).thenReturn(newPurchasesBundle(2, true, true)); when(service.getPurchaseHistory(anyInt(), anyString(), anyString(), eq("3"), any(Bundle.class))).thenReturn(newPurchasesBundle(3, true, true)); when(service.getPurchaseHistory(anyInt(), anyString(), anyString(), eq("4"), any(Bundle.class))).thenReturn(newPurchasesBundle(4, false, true)); Tests.setService(billing, service); }
@Override void start(@Nonnull IInAppBillingService service, @Nonnull String packageName) throws RemoteException, RequestException { final Bundle bundle = service.getBuyIntentToReplaceSkus(mApiVersion, packageName, mOldSkus, mNewSku, mProduct, mPayload == null ? "" : mPayload); if (handleError(bundle)) { return; } final PendingIntent pendingIntent = bundle.getParcelable("BUY_INTENT"); Check.isNotNull(pendingIntent); onSuccess(pendingIntent); }
@Test public void testShouldSuccess() throws Exception { final Request r = newRequest(); final RequestListener l = mock(RequestListener.class); r.setListener(l); final IInAppBillingService service = mock(IInAppBillingService.class); when(service.isBillingSupported(anyInt(), anyString(), anyString())).thenReturn(OK); when(service.consumePurchase(anyInt(), anyString(), anyString())).thenReturn(OK); final Bundle purchases = new Bundle(); purchases.putStringArrayList("INAPP_PURCHASE_DATA_LIST", new ArrayList<String>()); when(service.getPurchases(anyInt(), anyString(), anyString(), anyString())).thenReturn(purchases); when(service.getPurchaseHistory(anyInt(), anyString(), anyString(), anyString(), any(Bundle.class))).thenReturn(purchases); final Bundle skuDetails = new Bundle(); skuDetails.putStringArrayList("DETAILS_LIST", new ArrayList<String>()); when(service.getSkuDetails(anyInt(), anyString(), anyString(), any(Bundle.class))).thenReturn(skuDetails); final Bundle buyIntent = new Bundle(); buyIntent.putParcelable("BUY_INTENT", PendingIntent.getActivity(RuntimeEnvironment.application, 100, new Intent(), 0)); when(service.getBuyIntent(anyInt(), anyString(), anyString(), anyString(), anyString())).thenReturn(buyIntent); r.start(service, ""); verify(l).onSuccess(anyObject()); verify(l, never()).onError(anyInt(), any(Exception.class)); }
if (transaction != null && !TextUtils.isEmpty(transaction.purchaseToken)) int response = billingService.consumePurchase(Constants.GOOGLE_API_VERSION, contextPackageName, transaction.purchaseToken);
public boolean isOneTimePurchaseSupported() { if(!isInitialized()) { Log.e(LOG_TAG, "Make sure BillingProcessor was initialized before calling isOneTimePurchaseSupported()"); return false; } if (isOneTimePurchasesSupported) { return true; } try { int response = billingService.isBillingSupported(Constants.GOOGLE_API_VERSION, contextPackageName, Constants.PRODUCT_TYPE_MANAGED); isOneTimePurchasesSupported = response == Constants.BILLING_RESPONSE_RESULT_OK; } catch (RemoteException e) { e.printStackTrace(); } return isOneTimePurchasesSupported; }
private Bundle getBuyIntent(IInAppBillingService service, List<String> oldItemIds, String itemId, String developerPayload) throws BillingException { try { // Purchase an item if (oldItemIds == null || oldItemIds.isEmpty()) { return service.getBuyIntent( mApiVersion, mPackageName, itemId, mItemType, developerPayload); } // Upgrade/downgrade of subscriptions must be done on api version 5 // See https://developer.android.com/google/play/billing/billing_reference.html#upgrade-getBuyIntentToReplaceSkus return service.getBuyIntentToReplaceSkus( BillingApi.VERSION_5.getValue(), mPackageName, oldItemIds, itemId, mItemType, developerPayload); } catch (RemoteException e) { throw new BillingException(Constants.ERROR_REMOTE_EXCEPTION, e.getMessage()); } }
oldSkus.add("test"); when(mService.getBuyIntentToReplaceSkus( BillingApi.VERSION_5.getValue(), mBillingContext.getContext().getPackageName(), assertThat(e.getMessage()).isEqualTo(Constants.ERROR_MSG_PENDING_INTENT); } finally { verify(mService).getBuyIntentToReplaceSkus( BillingApi.VERSION_5.getValue(), mBillingContext.getContext().getPackageName(),
Bundle bundle = billingService.getPurchases(Constants.GOOGLE_API_VERSION, contextPackageName, type, null); if (bundle.getInt(Constants.RESPONSE_CODE) == Constants.BILLING_RESPONSE_RESULT_OK)