Interface InAppBillingService

  • All Known Implementing Classes:
    IOSInAppBillingService

    public interface InAppBillingService
    With the in-app billing service you can query information about in-app products and purchases that were done and place new orders for those products.

    Your application must be properly configured with the list of in-app products when you want to use this service. The list of available in-app products must be passed in first by initializing the service. Each registered product must have the identifier and type that matches the configuration of your in-app product on iOS and Android. For your convenience, you should use the same identifier for each product on both iOS and Android.

    Example

     InAppBillingService service = Services.get(InAppBillingService.class)
              .orElseThrow(() -> new RuntimeException("Could not load In-App Billing service"));
    
          // initialize and register available products
          service.initialize(androidBase64LicenseKey, Arrays.asList(
                  new Product("com.sample.appone.product1_identifier", Product.Type.CONSUMABLE),
                  new Product("com.sample.appone.product2_identifier", Product.Type.NON_CONSUMABLE)
          ));
    
          // construct UI based on available in-app products
          Worker<List<Product>> productDetails = service.fetchProductDetails();
          productDetails.stateProperty().addListener((obs, ov, nv) -> {
              for (Product product : productDetails.getValue()) {
                  Label price = new Label(product.getDetails().getCurrency() + product.getDetails().getPrice());
                  Label title = new Label(product.getDetails().getTitle());
                  Button buy = new Button("buy");
                  buy.setOnAction(e -> order(product));
                  HBox productControls = new HBox(10.0, price, title, buy);
                  productList.getChildren().add(productControls);
              }
          });
    
          // place an order
          private void order(Product product) {
              Worker<ProductOrder> order = service.order(product1);
              order.stateProperty().addListener((obs, ov, nv) -> {
                  if (nv == Worker.State.FAILED) {
                      order.getException().printStackTrace();
                  } else if (nv == Worker.State.SUCCEEDED) {
                      Product boughtProduct = order.getValue().getProduct();
                      if (boughtProduct.getType() == Product.Type.CONSUMABLE) {
                          Worker<Product> finishOrder = service.finish(order.getValue());
                          finishOrder.stateProperty().addListener((obs2, ov2, nv2) -> {
                              if (nv2 == Worker.State.FAILED) {
                                  finishOrder.getException().printStackTrace();
                              } else if (nv2 == Worker.State.SUCCEEDED) {
                                  Product finishedProduct = finishOrder.getValue();
                                  new Toast("You bought: " + finishedProduct.getTitle()).show();
                              }
                          });
                      } else {
                          new Toast("You bought: " + boughtProduct.getTitle()).show();
                      }
                  }
              }
          });
      );}

    Android Configuration

    The permission com.android.vending.BILLING needs to be added.

     <manifest ...>
        <uses-permission android:name="com.android.vending.BILLING"/>
        ...
      </manifest>

    iOS Configuration: none

    Since:
    3.4.0
    • Method Detail

      • isSupported

        boolean isSupported()
        Returns true if the device supports in-app billing.
        Returns:
        True if the device supports in-app billing.
      • setQueryResultListener

        void setQueryResultListener​(InAppBillingQueryResultListener listener)
        Set a query listener to listen for results of asynchronous queries of the registered in-app products.
        Parameters:
        listener - the query listener to set
      • setRegisteredProducts

        void setRegisteredProducts​(java.util.List<Product> registeredProducts)
        Updates the list of available products that are configured in the in-app product sections of the application configuration for iOS and Android.
        Parameters:
        registeredProducts - a list of available in-app products
      • initialize

        void initialize​(java.lang.String androidPublicKey,
                        java.util.List<Product> registeredProducts)
        Correctly initialize the In-App Billing service on the device. This makes sure that everything is in place before you can start interacting with the methods that deal with the actual in-app products.

        When initialization completed successfully, a query will be triggered to fetch the initial purchase details for each registered in-app product. You should set a query listener to act upon the results of that query.

        Parameters:
        androidPublicKey - the license key of your Android application which can be found in the Google Play developer console.
        registeredProducts - a list of available in-app products that are configured in your application configuration for iOS and Android
      • isReady

        boolean isReady()
      • readyProperty

        javafx.beans.property.ReadOnlyBooleanProperty readyProperty()
      • fetchProductDetails

        javafx.concurrent.Worker<java.util.List<Product>> fetchProductDetails()
        Retrieves the details for the current list of available in-app products. In case a product from the list of registered products could not be queried, they will not be listed in the returned list. This method must be called to get the details of each registered product, like the title, description and localised price and currency. You can use these details to build up your UI.
        Returns:
        A list of products for which the details could be fetched, wrapped in a Worker.
      • order

        javafx.concurrent.Worker<ProductOrder> order​(Product product)
        Place a new order for the specified product. The returned Worker can be used to determine if the order was successful.
        Parameters:
        product - the product to place an order for
        Returns:
        A ProductOrder containing details about the processed order, wrapped in a Worker.
      • finish

        javafx.concurrent.Worker<Product> finish​(ProductOrder productOrder)
        Finish a product order for a consumable product. The product order will not have been processed (and no money will have been transferred) until the order is finished.
        Parameters:
        productOrder - the product order to finish
        Returns:
        The product that was ordered with in the provided ProductOrder with updated details, wrapped in a Worker.