public interface InAppBillingService
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
Type | Property and Description |
---|---|
javafx.beans.property.ReadOnlyBooleanProperty |
ready |
Modifier and Type | Method and Description |
---|---|
javafx.concurrent.Worker<java.util.List<Product>> |
fetchProductDetails()
Retrieves the details for the current list of available in-app products.
|
javafx.concurrent.Worker<Product> |
finish(ProductOrder productOrder)
Finish a product order for a
consumable product. |
void |
initialize(java.lang.String androidPublicKey,
java.util.List<Product> registeredProducts)
Correctly initialize the In-App Billing service on the device.
|
boolean |
isReady()
Gets the value of the property ready.
|
boolean |
isSupported()
Returns true if the device supports in-app billing.
|
javafx.concurrent.Worker<ProductOrder> |
order(Product product)
Place a new order for the specified product.
|
javafx.beans.property.ReadOnlyBooleanProperty |
readyProperty() |
void |
setQueryResultListener(InAppBillingQueryResultListener listener)
Set a query listener to listen for results of asynchronous queries
of the registered in-app products.
|
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.
|
javafx.beans.property.ReadOnlyBooleanProperty readyProperty
isReady()
boolean isSupported()
void setQueryResultListener(InAppBillingQueryResultListener listener)
listener
- the query listener to setvoid setRegisteredProducts(java.util.List<Product> registeredProducts)
registeredProducts
- a list of available in-app productsvoid initialize(java.lang.String androidPublicKey, java.util.List<Product> registeredProducts)
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.
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 Androidboolean isReady()
javafx.beans.property.ReadOnlyBooleanProperty readyProperty()
isReady()
javafx.concurrent.Worker<java.util.List<Product>> fetchProductDetails()
javafx.concurrent.Worker<ProductOrder> order(Product product)
product
- the product to place an order forjavafx.concurrent.Worker<Product> finish(ProductOrder productOrder)
consumable
product. The product
order will not have been processed (and no money will have been transferred) until the order
is finished.productOrder
- the product order to finish