Android Question In App Billing (Querying for Purchased Items)

tucano2000

Active Member
Licensed User
Longtime User
Is there an equivalent method to the code below into B4A using InAppBilling3 library to retrieve information about purchases made by the user from your app ?

B4X:
Bundle ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);

int response = ownedItems.getInt("RESPONSE_CODE");
if (response == 0) {
   ArrayList<String> ownedSkus =
      ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
   ArrayList<String>  purchaseDataList =
      ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
   ArrayList<String>  signatureList =
      ownedItems.getStringArrayList("INAPP_DATA_SIGNATURE");
   String continuationToken =
      ownedItems.getString("INAPP_CONTINUATION_TOKEN");
  
   for (int i = 0; i < purchaseDataList.size(); ++i) {
      String purchaseData = purchaseDataList.get(i);
      String signature = signatureList.get(i);
      String sku = ownedSkus.get(i);
 
      // do something with this purchase information
      // e.g. display the updated list of products owned by user
   }

   // if continuationToken != null, call getPurchases again
   // and pass in the token to retrieve more items
}
 

tucano2000

Active Member
Licensed User
Longtime User
I used Manager.GetOwnedProducts, but this only returns Products (Purchases) if I buy something in the same session that I have opened the application.

See what I did in my code.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("teste")
  If FirstTime Then
      manager.Initialize("manager", key)
      manager.DebugLogging = True

  End If
End Sub

Sub Manager_BillingSupported (Supported As Boolean, Message As String)
  Log(Supported & ", " & Message)
  Log("Subscriptions supported: " & manager.SubscriptionsSupported)
  If Supported Then
          manager.GetOwnedProducts
  End If
End Sub

Sub manager_OwnedProducts (Success As Boolean, purchases As Map)
    Log("manager_OwnedProducts Success = " & Success)
    Log("purchases size = " & purchases.Size)
    If Success  Then
        For Each k As String In purchases.Keys
             Log(k)
        Next
    End If
End Sub

The above code returns zero . It is as if I had not bought anything or signed anything. What am I doing wrong ?

I need to check if a subscription has expired every time you open the application.

In the documentation of google says:

To query for active subscriptions, use the getPurchases method, again with the product type parameter set to "subs".

Bundle activeSubs = mService.getPurchases(3,"com.example.myapp",
"subs", continueToken);

The call returns a Bundle with all the active subscriptions owned by the user. Once a subscription expires without renewal, it will no longer appear in the returned Bundle.
 
Last edited:
Upvote 0

tucano2000

Active Member
Licensed User
Longtime User
Ok Erel, then how I can check to check if the subscription has expired using InAppBilling3 ? Is possible ? There is another method ?
 
Last edited:
Upvote 0

ivan.tellez

Active Member
Licensed User
Longtime User
GetOwnedProducts is for managed purchases, a subscription will not be listed calling GetOwnedProducts.
 
Upvote 0

tucano2000

Active Member
Licensed User
Longtime User
Finally I'm getting use InAppBilling3 library to retrieve information of purchased products or subscriptions. Now how do I get the Server Response Codes when the user tries to buy a product and no success ?

_PurchaseCompleted event only returns state success true or false. I need the other server response as well:

https://developer.android.com/google/play/billing/billing_reference.html#billing-codes

BILLING_RESPONSE_RESULT_OK 0 Success
BILLING_RESPONSE_RESULT_USER_CANCELED 1 User pressed back or canceled the dialog
BILLING_RESPONSE_RESULT_SERVICE_UNAVAILABLE 2 Network connection is down
BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE 3 Billing API version is not supported for the type requested
BILLING_RESPONSE_RESULT_ITEM_UNAVAILABLE 4 Requested product is not available for purchase
5 BILLING_RESPONSE_RESULT_DEVELOPER_ERROR Invalid arguments provided to the API. This error can que Also Indicate the application was not signed or enquiry.c Correctly set-up for In-app Billing in Google Play, or does not have the Necessary permissions in its manifest
BILLING_RESPONSE_RESULT_ERROR 6 Fatal error during the API action
BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED 7 Failure to purchase since item is already owned
BILLING_RESPONSE_RESULT_ITEM_NOT_OWNED 8 Failure to consume since item is not owned
 
Last edited:
Upvote 0
Top