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); } }
bundle = billingService.getBuyIntent(Constants.GOOGLE_API_VERSION, contextPackageName, productId,
@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); }
if (oldSkus == null || oldSkus.isEmpty()) { buyIntentBundle = mService.getBuyIntent(3, mContext.getPackageName(), sku, itemType, extraData); } else {
@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 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()); } }
@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)); }
protected Void doInBackground(Void... params) { Log.d(LOG_TAG, "purchasePremiumTask: " + PREMIUM_SKU); Bundle buyIntentBundle; try { buyIntentBundle = billingService.getBuyIntent(3, context.getPackageName(), PREMIUM_SKU, ITEM_TYPE_INAPP, null); Log.d(LOG_TAG, "buyIntentBundle: " + buyIntentBundle); buyIntent = buyIntentBundle.getParcelable(RESPONSE_BUY_INTENT); activity.startIntentSenderForResult(buyIntent.getIntentSender(), BILLING_REQ, new Intent(), 0, 0, 0); } catch(Exception e) { Log.e(LOG_TAG, "purchasePremiumTask failed: " + e.getMessage()); } return null; } }
private void whenGetBuyIntentForIdentifierReturn(String productId, Bundle buyIntentResponse) throws android.os.RemoteException { when(nativeInAppBillingService.getBuyIntent(BILLING_API_VERSION, PACKAGE_NAME_GOOD, productId, PURCHASE_TYPE_IN_APP, DEFAULT_DEVELOPER_PAYLOAD)) .thenReturn(buyIntentResponse); }
private void whenGetBuyIntentForIdentifierThrow(String productId, RemoteException exception) throws RemoteException { when(nativeInAppBillingService.getBuyIntent(BILLING_API_VERSION, PACKAGE_NAME_GOOD, productId, PURCHASE_TYPE_IN_APP, DEFAULT_DEVELOPER_PAYLOAD)) .thenThrow(exception); }
public GetBuyIntentResponse getBuyIntent(String productId, PurchaseType purchaseType, String developerPayload) throws RemoteException { ReactiveBilling.log(null, "Get buy intent - request: %s (thread %s)", productId, Thread.currentThread().getName()); Bundle bundle = billingService.getBuyIntent(BillingService.API_VERSION, context.getPackageName(), productId, purchaseType.getIdentifier(), developerPayload); int response = bundle.getInt("RESPONSE_CODE", -1); ReactiveBilling.log(null, "Get buy intent - response code: %s", response); if (response != 0) { return new GetBuyIntentResponse(response, null); } PendingIntent buyIntent = bundle.getParcelable("BUY_INTENT"); return new GetBuyIntentResponse(response, buyIntent); } }
private PendingIntent getBuyIntent(String productId, String type) throws RemoteException { Bundle intent = billingService().getBuyIntent(BILLING_API_VERSION, installerPackageName, productId, type, DEFAULT_DEVELOPER_PAYLOAD); return fetchPendingIntentFromGetBuyIntentResponse(intent); }
@Override public boolean purchase(@NonNull Activity activity, @NonNull String sku, int requestCode, @Nullable String payload) { try { final Bundle bundle = service.getBuyIntent(3, packageName, sku, BILLING_TYPE, payload); if (responseOk(bundle)) { final PendingIntent pendingIntent = bundle.getParcelable(RESPONSE_BUY_INTENT); if (pendingIntent != null) { final IntentSender sender = pendingIntent.getIntentSender(); activity.startIntentSenderForResult(sender, requestCode, null, 0, 0, 0); return true; } } } catch (RemoteException | IntentSender.SendIntentException ignore) { } return false; }
protected PendingIntent getIntent(int num) { try { Tools.HangarLog("getIntent: " + num); if (mService == null) { Tools.HangarLog("mService is null!"); return null; } String sku = "donate_" + num; Bundle buyIntentBundle = mService.getBuyIntent(3, context.getPackageName(), sku, "inapp", null); Tools.HangarLog("buyIntentBundle: " + buyIntentBundle); return buyIntentBundle.getParcelable("BUY_INTENT"); } catch (RemoteException e) { e.printStackTrace(); } return null; } }
public void purchase(String productid) { if (connManager.getActiveNetworkInfo() != null && connManager.getActiveNetworkInfo().isConnected()) { if (mService != null) { Bundle buyIntentBundle; try { buyIntentBundle = mService.getBuyIntent(3, getPackageName(), productid, "inapp", "callback/" + productid); int status = buyIntentBundle.getInt("RESPONSE_CODE"); if (status == 0) { PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT"); try { assert pendingIntent != null; startIntentSenderForResult(pendingIntent.getIntentSender(), Lime.PAYMENT_REQUEST_CODE, new Intent(), 0, 0, 0); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); } } else { showToastMessage(getResources().getString(R.string.payment_service_failed), Toast.LENGTH_LONG); } } catch (RemoteException e) { e.printStackTrace(); } } else { showToastMessage(getResources().getString(R.string.payment_service_failed), Toast.LENGTH_LONG); } } else { showToastMessage(getResources().getString(R.string.error_network_failed), Toast.LENGTH_LONG); } }
private void startIntentSender(Bundle bundle, PurchaseFlowLauncher launcher, int requestCode, int errorCode) throws RemoteException { when(mService.getBuyIntent( mBillingContext.getApiVersion(), mBillingContext.getContext().getPackageName(), "", TYPE_IN_APP, "" )).thenReturn(bundle); try { launcher.launch(mService, mActivity, requestCode, null, "", ""); } catch (BillingException e) { assertThat(e.getErrorCode()).isEqualTo(errorCode); } finally { verify(mService).getBuyIntent( mBillingContext.getApiVersion(), mBillingContext.getContext().getPackageName(), "", TYPE_IN_APP, "" ); verifyNoMoreInteractions(mService); } }
@Test public void remoteExceptionOnLaunch() throws RemoteException { PurchaseFlowLauncher launcher = new PurchaseFlowLauncher(mBillingContext, TYPE_IN_APP); int requestCode = 1001; when(mService.getBuyIntent( mBillingContext.getApiVersion(), mBillingContext.getContext().getPackageName(), "", TYPE_IN_APP, "" )).thenThrow(RemoteException.class); try { launcher.launch(mService, mActivity, requestCode, new ArrayList<String>(), "", ""); } catch (BillingException e) { assertThat(e.getErrorCode()).isEqualTo(Constants.ERROR_REMOTE_EXCEPTION); } finally { verify(mService).getBuyIntent( mBillingContext.getApiVersion(), mBillingContext.getContext().getPackageName(), "", TYPE_IN_APP, "" ); verifyNoMoreInteractions(mService); } }
int errorCode, String errorMessage) throws RemoteException { when(mService.getBuyIntent( mBillingContext.getApiVersion(), mBillingContext.getContext().getPackageName(), assertThat(e.getMessage()).isEqualTo(errorMessage); } finally { verify(mService).getBuyIntent( mBillingContext.getApiVersion(), mBillingContext.getContext().getPackageName(),
any(IntentSender.class), anyInt(), any(Intent.class), anyInt(), anyInt(), anyInt()); when(mService.getBuyIntent( mBillingContext.getApiVersion(), mBillingContext.getContext().getPackageName(), assertThat(e.getErrorCode()).isEqualTo(Constants.ERROR_SEND_INTENT_FAILED); } finally { verify(mService).getBuyIntent( mBillingContext.getApiVersion(), mBillingContext.getContext().getPackageName(),
@Test public void bundleResponseNull() throws RemoteException { PurchaseFlowLauncher launcher = new PurchaseFlowLauncher(mBillingContext, TYPE_IN_APP); int requestCode = 1001; try { launcher.launch(mService, mActivity, requestCode, null, "", ""); } catch (BillingException e) { assertThat(e.getErrorCode()).isEqualTo(Constants.ERROR_UNEXPECTED_TYPE); assertThat(e.getMessage()).isEqualTo(Constants.ERROR_MSG_UNEXPECTED_BUNDLE_RESPONSE_NULL); } finally { verify(mService).getBuyIntent( mBillingContext.getApiVersion(), mBillingContext.getContext().getPackageName(), "", TYPE_IN_APP, "" ); verifyNoMoreInteractions(mService); } }