Godot Google Play Billing

repository·master·Indexed 42 days ago

https://github.com/godot-sdk-integrations/godot-google-play-billing

A Godot Android plugin that integrates the Google Play Billing library to implement In-App Purchases (IAP) and subscriptions. It provides the BillingClient class for managing the purchase lifecycle, including querying product details, initiating purchase flows, and handling purchase acknowledgement and consumption. Compatible with Godot 4.2+ (master branch) and older versions (1.x branch).

Tokens
11.9K
Snippets
35
Records
49
Agent score
52%

What's inside Godot Google Play Billing

  1. Overview of GodotGooglePlayBilling

    master

    The GodotGooglePlayBilling plugin enables in-app purchases for Android games developed with the Godot Engine. It integrates the Google Play Billing library to allow developers to sell digital content directly within their games.

    Key Features

    • One-time product purchases: Sell non-consumable items like premium feature unlocks or ad removal.
    • Subscription support: Manage recurring billing for services or content.
    • Consumables: Sell items that can be used multiple times, such as in-game currency (coins), lives, or power-ups.
    • Signal-based API: Uses Godot's signal system for a straightforward, asynchronous workflow.

    Requirements

    • Platform: Android devices distributed via Google Play.
    • Configuration: Requires a properly configured app in the Google Play Console.
  2. Process and validate purchase states

    master

    When receiving purchase data from query_purchases_response or on_purchase_updated, you must check the purchase_state to ensure the transaction is complete.

    Important: If the state is PENDING, do not award the product. Only process the purchase when the state is PURCHASED.

    # PurchaseState enum values
    enum PurchaseState {
    	UNSPECIFIED,
    	PURCHASED,
    	PENDING,
    }
    
    # Usage logic
    func _process_purchase(purchase):
    	if purchase.purchase_state == BillingClient.PurchaseState.PURCHASED:
    		# Award the item here
    	elif purchase.purchase_state == BillingClient.PurchaseState.PENDING:
    		# Wait for completion
    		pass
  3. Handle connection lifecycle with BillingClient signals

    master
    The BillingClient uses signals to communicate connection status. You should wait for the connected signal before attempting any billing operations (like querying products or making purchases). If the connection fails, listen to connect_error. The disconnected signal is emitted when the connection to Google Play is lost (e.g., due to network issues).
  4. Configure Google Play Console for In-App Products

    master

    Before the billing plugin can query products, they must be defined and activated in the Google Play Console.

    Requirements:

    • Product ID Match: The Product ID created in the Play Console must exactly match the string ID used in your Godot project code.
    • Activation: All products must be set to an Active status in the Play Console.

    Example Product IDs:

    • coins_100
    • remove_ads
    • premium_upgrade
    coins_100
    remove_ads
    premium_upgrade
  5. Configure Android Export for Billing

    master

    To ensure in-app purchases work correctly, your Android export settings must match your Google Play Console configuration.

    1. Go to Project > Export in the main menu.
    2. Add an Android export preset if one does not exist.
    3. Verify the following settings:
      • gradle/use_gradle_build must be checked.
      • The Package name must match your application in the Google Play Console.
      • Internet permission must be enabled.
      • The project must be signed with a release keystore.
  6. Install the GodotGooglePlayBilling plugin

    master

    To install the plugin in your Godot project:

    1. Download the latest release from the Releases page.
    2. Unzip the download and copy the plugin contents into your project's addons folder so the path looks like: your_project/addons/GodotGooglePlayBilling/.
    3. In the Godot Editor, navigate to Project > Project Settings > Plugins and enable GodotGooglePlayBilling.

    Version Compatibility Note:

    • Use the master branch for Godot 4.2 and above.
    • Use the 1.x branch and 1.x releases for older Godot versions.
    your_project/addons/GodotGooglePlayBilling/
  7. Prepare your application for Google Play Billing

    master

    To ensure compatibility with the billing plugin, follow these initial setup steps in the Google Play Console:

    1. Create Application: Create your app in the console. The package name must be identical to your Godot Android export package name.
    2. Upload Test Build: Google Play Billing requires a build to be uploaded to a testing track (the Internal testing track is recommended) to function correctly.
  8. Implement a typical billing flow

    master

    To successfully process a purchase, follow this sequence of operations:

    1. Initialize: Create an instance of BillingClient.
    2. Connect: Connect to the billing service by calling start_connection() and waiting for the connected signal.
    3. Query: Retrieve product information (prices, descriptions, etc.) using query_product_details().
    4. Purchase: Initiate the purchase flow using purchase().
    5. Handle Result: Listen for the on_purchase_updated signal to confirm the transaction status.
    6. Finalize: Acknowledge or consume the purchase to prevent it from being refunded or lost.

    Note: All billing operations must be performed through a single instance of BillingClient.

    var billing_client := BillingClient.new()
    
    func _ready():
        # 1. Connect to signals
        billing_client.connected.connect(_on_connected)
        # 2. Start connection
        billing_client.start_connection()
    
    func _on_connected():
        # 3. Query products once connected
        var productIds = ["coins_100"]
        billing_client.query_product_details(productIds, BillingClient.ProductType.INAPP)
  9. Enable Remote Debug Testing for Billing

    master

    You can test purchase flows while running the app locally (via remote debugging) without downloading a new build from the Play Store every time. For this to work, the following conditions must be met:

    1. Package Name Match: Your local app's package name must exactly match the package name configured in the Google Play Console.
    2. License Testing: The device's primary Google Play Store account must be added to the License Testing list in the Play Console.
    3. Initial Upload: At least one build containing the billing plugin must have been previously uploaded to the Play Console to activate billing for that specific package name.
    4. Product Status: All In-App Products or Subscriptions must be set to Active status in the Play Console.