PurchaseManager { @NonNull public Completable consumePurchase(@NonNull final ConsumablePurchase consumablePurchase) { Logger.info(PurchaseManager.this, "Consuming the purchase of {}", consumablePurchase.getInAppPurchase()); return rxInAppBillingServiceConnection.bindToInAppBillingService() .flatMapCompletable(inAppBillingService -> { try { final int responseCode = inAppBillingService.consumePurchase(API_VERSION, context.getPackageName(), consumablePurchase.getPurchaseToken()); if (BILLING_RESPONSE_CODE_OK == responseCode) { Logger.info(PurchaseManager.this, "Successfully consumed the purchase of {}", consumablePurchase.getInAppPurchase()); return Completable.complete(); } else { Logger.warn(PurchaseManager.this, "Received an unexpected response code, {}, for the consumption of this product.", responseCode); return Completable.error(new Exception("Received an unexpected response code for the consumption of this product.")); } } catch (RemoteException e) { Logger.error(PurchaseManager.this, "Failed to consume this purchase", e); return Completable.error(e); } }); } @Inject  PurchaseManager(@NonNull Context context,
                           @NonNull RemoteSubscriptionManager remoteSubscriptionManager,
                           @NonNull PurchaseWallet purchaseWallet,
                           @NonNull Analytics analytics); @VisibleForTesting  PurchaseManager(@NonNull Context context,
                    @NonNull RemoteSubscriptionManager remoteSubscriptionManager,
                    @NonNull PurchaseWallet purchaseWallet,
                    @NonNull Analytics analytics,
                    @NonNull Scheduler subscribeOnScheduler,
                    @NonNull Scheduler observeOnScheduler); void addEventListener(@NonNull PurchaseEventsListener listener); void removeEventListener(@NonNull PurchaseEventsListener listener); @SuppressLint("CheckResult") @SuppressWarnings("ResultOfMethodCallIgnored") void initialize(@NonNull Application application); void onActivityResumed(@NonNull Activity activity); void onActivityPaused(); void initiatePurchase(@NonNull final InAppPurchase inAppPurchase, @NonNull final PurchaseSource purchaseSource); @VisibleForTesting void sendMockPurchaseRequest(@NonNull InAppPurchase inAppPurchase); boolean onActivityResult(int requestCode, int resultCode, @Nullable Intent data); @NonNull Observable<Set<ManagedProduct>> getAllOwnedPurchases(); @NonNull Observable<Set<AvailablePurchase>> getAllAvailablePurchases(); @NonNull Observable<Set<InAppPurchase>> getAllAvailablePurchaseSkus(); @NonNull Completable consumePurchase(@NonNull final ConsumablePurchase consumablePurchase); static final int REQUEST_CODE; }
@Test public void consumePurchaseThrowsRemoteException() throws Exception { when(consumablePurchase.getPurchaseToken()).thenReturn(PURCHASE_TOKEN); when(inAppBillingService.consumePurchase(3, packageName, PURCHASE_TOKEN)).thenThrow(new RemoteException()); purchaseManager.consumePurchase(consumablePurchase).test() .assertNoValues() .assertNotComplete() .assertError(RemoteException.class); }
@Test public void consumePurchaseFails() throws Exception { when(consumablePurchase.getPurchaseToken()).thenReturn(PURCHASE_TOKEN); when(inAppBillingService.consumePurchase(3, packageName, PURCHASE_TOKEN)).thenReturn(RESULT_ERROR); purchaseManager.consumePurchase(consumablePurchase).test() .assertNoValues() .assertNotComplete() .assertError(Exception.class); }
@Test public void consumePurchaseSucceeds() throws Exception { when(consumablePurchase.getPurchaseToken()).thenReturn(PURCHASE_TOKEN); when(inAppBillingService.consumePurchase(3, packageName, PURCHASE_TOKEN)).thenReturn(RESULT_OK); purchaseManager.consumePurchase(consumablePurchase).test() .assertNoValues() .assertComplete() .assertNoErrors(); }