Myket In-App Purchase Guide for B4A (Basic4Android)
For the complete B4A library and runnable sample project, see the official sample repository on GitHub.
The Myket In-App Billing SDK for B4A is the official library for integrating Myket In-App Billing (IAB) into Android applications targeting B4A v14.
This SDK wraps Myket's native billing client (ir.myket.billingclient.IabHelper v2.2), exposing its methods and event callbacks in B4A.
Directory Structure
./B4A/
├── MyketBillingLibrary/ # Compiled B4A library files (copy to Additional Libraries)
│ ├── MyketBilling.jar # B4A Java wrapper binary with embedded manifest macro
│ ├── MyketBilling.xml # B4A IDE autocomplete & type descriptor
│ └── MyketBilling-2.2.aar # Underlying Myket Billing SDK AAR (v2.2)
│
├── MyketBillingSample/ # Complete B4A sample application (TrivialDrive)
│ ├── MyketBillingSample.b4a
│ ├── MyketBillingSample.b4a.meta
│ ├── Starter.bas
│ ├── Files/ # Game graphics and assets
│ └── Objects/ # Compiled APK & build artifacts
│
└── README.md
Prerequisites
- A registered developer account on the Myket Developer Portal.
- An application registered in your Myket developer panel with a reserved package name.
- Your app's RSA Public Key copied from the Myket developer console.
- In-app products (SKUs) defined in your application console on Myket.
- B4A v14 and Android build tools installed.
- A test Android device with the Myket app installed.
Getting Started
1. Install the Library in B4A
Copy all three files from ./B4A/MyketBillingLibrary/ to your B4A Additional Libraries folder:
MyketBilling.jarMyketBilling.xmlMyketBilling-2.2.aar
In the B4A IDE, refresh the Libraries Manager tab and check MyketBilling (or myketbilling).
2. Configure the Android Manifest
Add the manifest macro in the B4A Manifest Editor (Project -> Manifest Editor):
CreateResourceFromFile(Macro, MyketBilling.MyketBillingReceiver)
The macro automatically configures:
- Required permissions (
INTERNET,ir.mservices.market.BILLING) - Android 11+ package visibility queries for Myket and Chrome
- Required activities (
ProxyBillingActivity,PaymentCallbackActivity,CustomTabLauncherActivity) - Metadata (
market_id,market_bind_address,sdk_version) - Broadcast receiver (
IABReceiver)
3. Required Project Dependencies
Add the following #AdditionalJar directives to your main module's #Region Project Attributes:
#AdditionalJar: androidx.core:core
#AdditionalJar: androidx.browser:browser
#AdditionalJar: org.jetbrains.kotlin-kotlin-stdlib
#AdditionalJar: org.jetbrains.kotlinx-kotlinx-coroutines-core-jvm
#AdditionalJar: org.jetbrains.kotlinx-kotlinx-coroutines-android
#AdditionalJar: okhttp-4.9.0
#AdditionalJar: okio-2.8.0
#AdditionalJar: gson-2.8.5
(All of these JARs / AARs are included by default in B4A v14 / Android SDK).
Code Examples & API Guide
1. Declaration and Initialization
Sub Globals
Private mHelper As MyketIabHelper ' or MyketBilling
End Sub
Sub Activity_Create(FirstTime As Boolean)
' Initialize with EventName prefix and your Base64 Public Key from Myket Console
mHelper.Initialize("IabHelper", "YOUR_BASE64_PUBLIC_KEY")
mHelper.EnableDebugLogging(True)
mHelper.StartSetup
End Sub
2. Setup and Inventory Query Events
' Raised when connection to billing service finishes
Private Sub IabHelper_SetupFinished (Result As MyketIabResult)
If Result.Success Then
Log("Connected to Myket billing service successfully.")
' Query active purchases and product details
Dim skuList As List
skuList.Initialize
skuList.Add("gas")
skuList.Add("premium")
mHelper.QueryInventoryAsync3(True, skuList)
Else
Log("Billing setup failed: " & Result.Message)
End If
End Sub
' Raised when inventory query completes
Private Sub IabHelper_QueryInventoryFinished (Result As MyketIabResult, Inventory As MyketInventory)
If Result.Success Then
' 1. Check and consume unconsumed consumable purchases (crash recovery)
If Inventory.HasPurchase("gas") Then
Dim gasPurchase As MyketPurchase = Inventory.GetPurchase("gas")
mHelper.ConsumeAsync(gasPurchase)
End If
' 2. Verify non-consumable ownership
If Inventory.HasPurchase("premium") Then
Log("User owns premium upgrade.")
' Unlock premium feature
End If
Else
Log("Failed to query inventory: " & Result.Message)
End If
End Sub
3. Purchasing and Consuming Products
' Launch purchase flow
Sub BuyGas_Click
mHelper.LaunchPurchaseFlow("gas", "payload_tracking_123")
End Sub
' Raised when purchase flow finishes
Private Sub IabHelper_PurchaseFinished (Result As MyketIabResult, Purchase As MyketPurchase)
If Result.Success Then
Log("Purchase successful: " & Purchase.Sku)
' If consumable (e.g. gas, coins):
If Purchase.Sku = "gas" Then
mHelper.ConsumeAsync(Purchase)
Else
' Non-consumable feature unlock
UnlockFeature
End If
Else
Log("Purchase failed: " & Result.Message)
End If
End Sub
' Raised when consumption finishes
Private Sub IabHelper_ConsumeFinished (Purchase As MyketPurchase, Result As MyketIabResult)
If Result.Success Then
Log("Product consumed successfully: " & Purchase.Sku)
' Credit coins or refill gas
Else
Log("Failed to consume product: " & Result.Message)
End If
End Sub
The developerPayload string must not exceed 800 characters.
Never consume non-consumable products. Consuming an item permanently removes it from the user's active inventory on Myket.
4. Lifecycle Management
Release the service connection in Activity_Pause when the user closes the activity:
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed And mHelper.IsInitialized Then
mHelper.Dispose
End If
End Sub
Methods Reference (MyketIabHelper)
| Method | Description |
|---|---|
Initialize(EventName, Base64PublicKey) | Initializes the helper with context and RSA public key |
EnableDebugLogging(Enable) | Enables/disables Logcat debug logs |
EnableDebugLogging2(Enable, Tag) | Enables/disables debug logs with custom tag |
StartSetup() | Starts connection to Myket billing service asynchronously |
StartSetup2(EventName) | Starts connection with a custom callback event prefix |
LaunchPurchaseFlow(Sku, DeveloperPayload) | Initiates in-app purchase flow with payload |
LaunchPurchaseFlow2(Sku) | Initiates in-app purchase flow without payload |
LaunchPurchaseFlowCustom(EventName, Sku, ItemType, Payload) | Initiates flow with custom event and item type |
QueryInventoryAsync() | Queries owned items and SKU details asynchronously |
QueryInventoryAsync2(QuerySkuDetails) | Queries owned items, specifying whether to fetch details |
QueryInventoryAsync3(QuerySkuDetails, SkuList) | Queries owned items + specific SKUs from a B4A List |
QueryInventoryAsync4(QuerySkuDetails, SkuIds) | Queries owned items + specific SKUs from a String[] |
ConsumeAsync(Purchase) | Consumes a purchased item asynchronously |
ConsumeAsync2(EventName, Purchase) | Consumes a purchased item with custom callback event |
Dispose() | Disposes helper and releases service connections |
GetResponseDesc(Code) | Returns human-readable string for numeric response code |
Events Reference
| Event Signature | Raised When |
|---|---|
Sub EventName_SetupFinished (Result As MyketIabResult) | Setup completes successfully or with error |
Sub EventName_QueryInventoryFinished (Result As MyketIabResult, Inventory As MyketInventory) | Inventory query completes |
Sub EventName_PurchaseFinished (Result As MyketIabResult, Purchase As MyketPurchase) | Purchase flow finishes or is cancelled |
Sub EventName_ConsumeFinished (Purchase As MyketPurchase, Result As MyketIabResult) | Item consumption finishes |
Sample Application (TrivialDrive)
The subscription flow in the sample does not mean Myket currently supports subscriptions. Myket does not currently support subscription products; do not use the subscription example for a real product.
The sample project in ./B4A/MyketBillingSample/ implements the TrivialDrive scenario matching MainActivity.java from the native Android sample:
- Consumable Gas (
"gas"): Refills the car's tank by 1/4 after each consumption. - Non-Consumable Premium Upgrade (
"premium"): Permanently upgrades the car to the red model. - Lifecycle Management: Safely disposes the billing helper in
Activity_PausewhenUserClosed = True.