2

I want to connect my Javafx product to Googe Play store. To do that, I included the gluon plugin in-app-billing. But, for using this service, I only found this example from http://docs.gluonhq.com/charm/javadoc/4.4.1/com/gluonhq/charm/down/plugins/InAppBillingService.html

This example provide some information to do that but they do not completely explain the billing and purchase mechanisms with Google Play, "in the context gluon".
Could I get a Java code example that provides the mechanisms to bill and purchase?

That would cover these questions:
- how to initiate a connection with Google Play with keys, ....
- how to purchase a product
- how to know if the product was already bought, before?
- ....

Thanks

Note that this example provide some idea about what to do, but unfortunatley it is not accurate enough:
- productList is not defined. It is related to productDetails by which way?
- replace "finishedProduct.getTitle()" BY "finishedProduct.getDetails.getTitle()"
- idem with boughtProduct
- product1 in "Worker order = service.order(product1);" is undefined. Replace "product1" by "product" seems to be fine?
- the private method "order" is not syntaxically correct
- and the string androidBase64LicenseKey is not defined (but that is normal :) )

Pascal DUTOIT
  • 121
  • 1
  • 13

1 Answers1

1

InAppBillingSample is a full working sample that shows how the in-app billing Down service can be used to add purchases to your app has just been released.

In-App Billing service

The Charm Down service is be added to the app, along with the Settings one.

Down services

The main parts of this service are:

Retrieve products

When the app starts, you can retrieve the exiting products:

Worker<List<Product>> productDetails = service.fetchProductDetails();

and on succeed, process them accordingly:

createProductGrid(productDetails.getValue());

In this case, the products will show up at the bottom of the view, with a button to purchase them if possible:

Buying a product

If the user decides to buy a product, the order is processed:

Worker<ProductOrder> order = service.order(product);

When the order is successfully processed, we call finish to complete the order:

Worker<Product> finish = service.finish(order.getValue());

and on succeed you get the purchased product:

Product finishedProduct = finish.getValue();
...
player.boughtHealthPotion();

Sample Setup

The setup for Android and iOS requires that you enter the Google Play Developer console or the Apple App Store Connect and register some products for in-app purchase.

In this sample, these products are:

com.gluonhq.inappbilling.health_potion (consumable product)
com.gluonhq.inappbilling.wooden_shield (non consumable product)

in-app products

Providing some details, like price, name or description:

details

For Android, you also need from the Google Play developer account the base64-encoded RSA public key, that has to be copied to PlayerService:

private static final String BASE_64_ANDROID_PUBLIC_KEY = "MIIBIjANBgkqhkiG*******DAQAB";

Note that to test the apk/ipa you need to do release/distribution, so the app has to be signed properly.

Desktop

On desktop the in-app billing service won't work but you can test the app so far.

The sample has a player that can fight and on every fight loses some health (starting from 10).

When you run out of health, you can buy a health potion, or you can also use a wooden shield to reduce the health lost on each fight. This works only on mobile of course.

And you can see the inventory you have:

Mobile

When you run the app on Android or iOS, you can buy at any time the consumable products, and only once the non consumable ones.

When testing there is no charge involved when you do the purchased:

On Android:

or on iOS:

Once you have some items purchased, you can consume then from the inventory:

José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • José: with your accurate explanation, I have included without problem this billing API in my code. Thanks a lot. Could you explain now how did you test your code questionning the Google Play server? For my part, by using your API, I can't discuss with the Google Play server. In other words, "PlayerService.getInstance().getService().ifPresent(service ->" works BUT then the related service is never ready. How can we debug a code with a distant server that we cannot get any information about the problem? – Pascal DUTOIT Aug 15 '18 at 05:08
  • By referring to the InAppBillingSample project and its BasicView file, the service is correctly instantiate and initialized. But then, the service is never ready: PlayerService.getInstance().getService().ifPresent(service -> { if (service.isReady()) { updateProductDetails(service); } else { service.readyProperty().addListener(...() { @Override public void changed(ObservableValue ...) { if (newValue) { updateProductDetails(service) – Pascal DUTOIT Aug 17 '18 at 12:14
  • The public_key and the product are well defined before – Pascal DUTOIT Aug 17 '18 at 12:15
  • José: I don't see, in your example, the case in which the Google Play server does not respond. What happens, in this case? As my application is Freemium, the solution would be to go in a degraded mode (for instance, the demo). But, to do that, I should use an information (about the "no server response") coming from your solution. Thanks in advance – Pascal DUTOIT Aug 30 '18 at 12:56
  • In BasicView.java file,line 177, the test "product.equals(InAppProduct.WOODEN_SHIELD.getProduct()" does not work due to the fact that the data structures are not the same. For solving that, the test must be applied on the product IDs as follows: if (product.getID().equals(InAppProduct.WOODEN_SHIELD.getProduct().getID()) &&.....{ buy.setDisable(true); } This modification should be applied in any case of comparison on the product reference (ex: line 90 of PlayserService.java) - Regards – Pascal DUTOIT Sep 05 '18 at 09:16
  • The `equals` works precisely on the Product's id, as you can see [here](https://bitbucket.org/gluon-oss/charm-down/src/tip/plugins/plugin-in-app-billing/src/main/java/com/gluonhq/charm/down/plugins/inappbilling/Product.java#lines-89). – José Pereda Sep 10 '18 at 08:38