GooglePlayBillingVendor implements Vendor, PurchasesUpdatedListener,
        AbstractGooglePlayBillingApi.LifecycleListener { @Override public synchronized void consume(@NonNull final Context context, @NonNull final Purchase purchase, @NonNull final ConsumeListener listener) { Preconditions.checkNotNull(context, "Purchase is null"); Preconditions.checkNotNull(listener, "Consume listener is null"); throwIfUninitialized(); final Product product = purchase.product(); if (product.isSubscription()) { throw new IllegalStateException("Cannot consume a subscription"); } if (tokensToBeConsumed.contains(purchase.token())) { logSafely("Token was already scheduled to be consumed - skipping..."); listener.failure(purchase, new Error(VendorConstants.CONSUME_UNAVAILABLE, -1)); return; } logSafely("Consuming " + product.sku()); tokensToBeConsumed.add(purchase.token()); api.consumePurchase(purchase.token(), new ConsumeResponseListener() { @Override public void onConsumeResponse(int responseCode, String purchaseToken) { if (responseCode == BillingResponse.OK) { logSafely("Successfully consumed " + purchase.product().sku() + "!"); listener.success(purchase); } else { logSafely("Error consuming " + purchase.product().sku() + " with code "+responseCode); tokensToBeConsumed.remove(purchaseToken); listener.failure(purchase, getConsumeError(responseCode)); } } }); } GooglePlayBillingVendor();  GooglePlayBillingVendor(String publicKey64);  GooglePlayBillingVendor(AbstractGooglePlayBillingApi api);  GooglePlayBillingVendor(AbstractGooglePlayBillingApi api, @Nullable String publicKey64); @Override String id(); @Override synchronized void initialize(Context context, InitializationListener listener); @Override synchronized void initialized(boolean success); @Override void disconnected(); @Override void dispose(Context context); @Override synchronized void purchase(Activity activity, Product product, String developerPayload, PurchaseListener listener); @Override void onPurchasesUpdated(@BillingResponse int responseCode,
                                   @Nullable List<com.android.billingclient.api.Purchase> purchases); @Override synchronized void consume(@NonNull final Context context, @NonNull final Purchase purchase, @NonNull final ConsumeListener listener); @Override void getInventory(@NonNull Context context, @Nullable Collection<String> itemSkus, @Nullable Collection<String> subSkus,
                             @NonNull InventoryListener listener); @Override void getProductDetails(@NonNull Context context, @NonNull final String sku, final boolean isSubscription,
                                  @NonNull final ProductDetailsListener listener); @Override void setLogger(Logger logger); @Override boolean available(); @Override boolean canPurchase(Product product); @Override boolean onActivityResult(int requestCode, int resultCode, Intent data); @Override Product getProductFrom(JSONObject json); @Override Purchase getPurchaseFrom(JSONObject json);  }
@Test public void consume() throws JSONException { GooglePlayBillingVendor vendor = successfullyInitializedVendor(); ConsumeListener listener = mock(ConsumeListener.class); Purchase purchase = GooglePlayBillingPurchase.create(TestData.productInappA, new TestPurchase(TestData.productInappA)); mockConsume(vendor, BillingClient.BillingResponse.OK); vendor.consume(context, purchase, listener); verify(listener).success(purchase); }
@Test() public void cannot_consume_twice() throws JSONException { GooglePlayBillingVendor vendor = successfullyInitializedVendor(); ConsumeListener listener = mock(ConsumeListener.class); Purchase purchase = GooglePlayBillingPurchase.create(TestData.productInappA, new TestPurchase(TestData.productInappA)); mockConsume(vendor, BillingClient.BillingResponse.OK); vendor.consume(context, purchase, listener); verify(listener).success(purchase); vendor.consume(context, purchase, listener); ArgumentCaptor<Vendor.Error> argumentError = ArgumentCaptor.forClass(Vendor.Error.class); verify(listener).failure(eq(purchase), argumentError.capture()); assertEquals(VendorConstants.CONSUME_UNAVAILABLE, argumentError.getValue().code); }
@Test public void consume_error() throws JSONException { GooglePlayBillingVendor vendor = successfullyInitializedVendor(); ConsumeListener listener = mock(ConsumeListener.class); Purchase purchase = GooglePlayBillingPurchase.create(TestData.productInappA, new TestPurchase(TestData.productInappA)); mockConsume(vendor, BillingClient.BillingResponse.ITEM_NOT_OWNED); vendor.consume(context, purchase, listener); ArgumentCaptor<Vendor.Error> argumentError = ArgumentCaptor.forClass(Vendor.Error.class); verify(listener).failure(eq(purchase), argumentError.capture()); assertEquals(VendorConstants.CONSUME_NOT_OWNED, argumentError.getValue().code); }