GooglePlayBillingVendor implements Vendor, PurchasesUpdatedListener,
        AbstractGooglePlayBillingApi.LifecycleListener { @Override public synchronized void purchase(Activity activity, Product product, String developerPayload, PurchaseListener listener) { Preconditions.checkNotNull(activity, "Activity is null."); Preconditions.checkNotNull(product, "Product is null."); Preconditions.checkNotNull(listener, "Purchase listener is null."); throwIfUninitialized(); if (pendingProduct != null) { throw new RuntimeException("Cannot purchase product while another purchase is in progress!"); } if (developerPayload != null && developerPayload.length() > 0) { throw new RuntimeException("Developer payload is not supported in Google Play Billing!"); } this.purchaseListener = listener; this.pendingProduct = product; logSafely("Launching Google Play Billing flow for " + product.sku()); try { api.launchBillingFlow(activity, product.sku(), product.isSubscription() ? SkuType.SUBS : SkuType.INAPP); } catch (Exception e) { clearPendingPurchase(); throw e; } } 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 purchase_successfully() { GooglePlayBillingVendor vendor = successfullyInitializedVendor(); PurchaseListener listener = mock(PurchaseListener.class); mockApiPurchaseSuccess(vendor, TestData.productInappA, true); vendor.purchase(activity, TestData.productInappA, null, listener); ArgumentCaptor<Purchase> argument = ArgumentCaptor.forClass(Purchase.class); verify(listener).success(argument.capture()); assertEquals( TestData.productInappA.sku(), argument.getValue().product().sku() ); vendor.purchase(activity, TestData.productInappA, null, listener); }
@Test public void purchase_with_api_error() { GooglePlayBillingVendor vendor = successfullyInitializedVendor(); PurchaseListener listener = mock(PurchaseListener.class); mockApiPurchaseFailure(vendor, TestData.productInappA, BillingClient.BillingResponse.SERVICE_UNAVAILABLE); vendor.purchase(activity, TestData.productInappA, null, listener); ArgumentCaptor<Vendor.Error> argumentError = ArgumentCaptor.forClass(Vendor.Error.class); ArgumentCaptor<Product> argumentProduct = ArgumentCaptor.forClass(Product.class); verify(listener).failure(argumentProduct.capture(), argumentError.capture()); assertEquals( TestData.productInappA.sku(), argumentProduct.getValue().sku() ); assertEquals(VendorConstants.PURCHASE_UNAVAILABLE, argumentError.getValue().code); vendor.purchase(activity, TestData.productInappA, null, listener); }
@Test public void purchase_user_canceled() { GooglePlayBillingVendor vendor = successfullyInitializedVendor(); PurchaseListener listener = mock(PurchaseListener.class); mockApiPurchaseFailure(vendor, TestData.productInappA, BillingClient.BillingResponse.USER_CANCELED); vendor.purchase(activity, TestData.productInappA, null, listener); ArgumentCaptor<Vendor.Error> argumentError = ArgumentCaptor.forClass(Vendor.Error.class); ArgumentCaptor<Product> argumentProduct = ArgumentCaptor.forClass(Product.class); verify(listener).failure(argumentProduct.capture(), argumentError.capture()); assertEquals( TestData.productInappA.sku(), argumentProduct.getValue().sku() ); assertEquals(VendorConstants.PURCHASE_CANCELED, argumentError.getValue().code); vendor.purchase(activity, TestData.productInappA, null, listener); }
@Test public void purchase_with_signature_error() { GooglePlayBillingVendor vendor = successfullyInitializedVendor(); PurchaseListener listener = mock(PurchaseListener.class); mockApiPurchaseSuccess(vendor, TestData.productInappA, false); vendor.purchase(activity, TestData.productInappA, null, listener); ArgumentCaptor<Vendor.Error> argumentError = ArgumentCaptor.forClass(Vendor.Error.class); ArgumentCaptor<Product> argumentProduct = ArgumentCaptor.forClass(Product.class); verify(listener).failure(argumentProduct.capture(), argumentError.capture()); assertEquals( TestData.productInappA.sku(), argumentProduct.getValue().sku() ); assertEquals(VendorConstants.PURCHASE_SUCCESS_RESULT_MALFORMED, argumentError.getValue().code); vendor.purchase(activity, TestData.productInappA, null, listener); }
@Test(expected = RuntimeException.class) public void purchase_while_another_purchase_in_progress() { GooglePlayBillingVendor vendor = successfullyInitializedVendor(); PurchaseListener listener = mock(PurchaseListener.class); vendor.purchase(activity, TestData.productInappA, null, listener); vendor.purchase(activity, TestData.productInappB, null, listener); }
@Test(expected = RuntimeException.class) public void purchase_developer_payload_not_supported() { GooglePlayBillingVendor vendor = successfullyInitializedVendor(); PurchaseListener listener = mock(PurchaseListener.class); mockApiPurchaseSuccess(vendor, TestData.productInappA, true); vendor.purchase(activity, TestData.productInappA, "DEV PAYLOAD", listener); }