Dynamic In-App Purchases

2020-06-25 04:06发布

问题:

This question (Android In-App Billing Dynamic Product List) was asked over 3 years ago. Are dynamic in-app purchase items still unavailable to Android?

The reason I’m looking to implement this sort of functionality is because my app features a way for certain users to create their own in-app purchases for others to buy.

It seems like an alternative solution would be to use consumable in-app currency but I’d much rather have a purchase option directly through the Play Store (especially since I’ve seen methods for hacking in-game currency everywhere).

Regardless of the option, I do want to use server-side verification. Might there be a way to add in-app purchases through a server?

回答1:

I'm not sure if this will suffice, but you can use the Google Play API and insert new in app billing products that way. All in app billing products for Android still need to be specified in the Google Play Developer Console, but at least with the Play API, you can do it programatically.



回答2:

I managed to implement this using a Google Play Api and uploading the products to the playstore dynamically/programmatically.

First I used PHP since I am using Laravel for my backend REST API. It is up to you to choose you own. I will installed the google/apiclient library using composer using composer require google/apiclient

https://developers.google.com/android-publisher/api-ref/inappproducts

https://developers.google.com/android-publisher/api-ref/inappproducts/insert

A sample product could be uploaded as such:

Step1: You will need to authenticate to the service. By using a service json file or Auth oath if you prefer. In my case I used the service json file and enabled the Google Play API in my GCP console.

Step2: You need an sku(unique) refer to the docs and see the required format

Step3: You need to set a Price(micros) you can use https://github.com/Torann/laravel-currency to convert from one currency to another which I used in this case to obtain my currency based on the default currency of the Store(In app products) NB: The currencies must match else you will get an error.

Step4: You need to set Listings which are basically the title/descriptions under that your app needs for translations purposes


$client = new Google_Client();
$client->setAuthConfig(storage_path('service_account.json'));

// You have to add this scope so it knows it is in app billing store
$client->addScope('https://www.googleapis.com/auth/androidpublisher');
$service = new Google_Service_AndroidPublisher($client);

$body = new \Google_Service_AndroidPublisher_InAppProduct();
$body->setSku("unique_sku_here");
$body->setPackageName('android_app_package_name'); // e.g com.example.in_app_test

//1.00 USD = 1,000,000 Microns.

// Create a new price object 
$price = new \Google_Service_AndroidPublisher_Price();
$price->setCurrency('USD');

//Using **torann/currency** library to convert from one currency to another

$new_price = intval(currency("1000", $from = "EUR", $to = "USD", false));
$price->priceMicros = $new_price * 1000000;
$body->setDefaultPrice($price);

// Setting the default language

$body->defaultLanguage = "en_US";
$body->setListings([
                    'en_US' => [
                        'title' => 'Product Title',
                        'description' => "Product Description here"
                    ]
                ]);    

$purchase = $service->inappproducts->insert(
                     'android_app_package_name',
                     $body,
                    [
                      'autoConvertMissingPrices' => true,
                    ]);

$purchase->setStatus(true); // Always set this to mark the product active on the GPlay console.

With this you will have a return of a successful product upload.

Cheers. Feel free to comment should you have any blockers, I would be happy to clarify.