Skip to content

pranavpandey/dynamic-billing

Repository files navigation

Dynamic Billing

License Release

A library to implement Google Play in-app products and subscriptions on Android 4.1 (API 16) and above.

It uses AndroidX so, first migrate your project to AndroidX.
It is dependent on Java 8 due to the dependency on Dynamic Utils.


Contents


Installation

It can be installed by adding the following dependency to your build.gradle file:

dependencies {
    // For AndroidX enabled projects.
    implementation 'com.pranavpandey.android:dynamic-billing:1.1.0'
}

Usage

It provides an interface that can be implemented by the application or activity class to perform various operations accordingly. It also has support to create an in-app product or subscription that can be linked to a feature so that it can be enabled on-demand after a successful purchase.

For a complete reference, please read the documentation.

Initialize

DynamicBilling must be initialized once before accessing its methods.

Please make sure that you have enabled Google Play Billing for your app and have in-app products or subscriptions properly setup within the console.

// Initialize with application context.
DynamicBilling.initializeInstance(applicationContext);

Listener

DynamicBillingListener is an interface that can be implemented by the application or activity class to perform various operations and receive billing callbacks.

/**
 * An activity implementing the dynamic billing listener.
 */
public class BillingActivity extends Activity implements DynamicBillingListener {

    ...

    @Override
    public void onBillingServiceDisconnected() { }

    @Override
    public void onBillingSetupFinished(@NonNull BillingResult billingResult) { 
        // Try to query available products from the billing library.
        DynamicBilling.getInstance().queryProductDetailsAsync(
                QueryProductDetailsParams.newBuilder()
                        .setProductList(Arrays.asList(
                                QueryProductDetailsParams.Product.newBuilder()
                                        .setProductId(Feature.ID)
                                        .setProductType(Feature.TYPE)
                                        .build()
                        )).build());

        // Try to query the user purchases.
        DynamicBilling.getInstance().queryPurchasesAsync(Subscription.QUERY_PURCHASES_PARAMS);
    }

    @Override
    public void onPurchasesUpdated(@NonNull BillingResult billingResult,
            @Nullable List<Purchase> purchases) {
        // Update products state according to the user purchases.
    }

    @Override
    public void onProductDetailsResponse(@NonNull BillingResult billingResult,
            @NonNull List<ProductDetails> productDetails) { 
        // Show available products to the user.
    }

    @Override
    public void onQueryPurchasesResponse(@NonNull BillingResult billingResult,
            @NonNull List<Purchase> purchases) {
        // Update products state according to the user purchases.
    }
  
    @Override
    public void onConsumeResponse(@NonNull BillingResult billingResult,
            @NonNull String purchaseToken) { }
  
    @Override
    public void onAcknowledgePurchaseResponse(@NonNull BillingResult billingResult) {
        // Automatically acknowledge purchases if required.
        DynamicBilling.getInstance().acknowledgePurchases(billingResult, purchases);
    }
}

Sponsor

Please become a sponsor to get a detailed guide and priority support.

Dependency

It depends on the dynamic-utils to perform various internal operations. So, its functions can also be used to perform other useful operations.


Author

Pranav Pandey

GitHub Follow on Twitter Donate via PayPal


License

Copyright 2022-2023 Pranav Pandey

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.