Android Tutorial In-App consumable purchases

Hi All, this is an little tutorial to implement "App-In consumable purchases"


B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim manager As BillingManager3
    Private key As String = "MIIBIjANBgkqXXXX......"
    Dim Productid1, Productid2 As String
    Dim Producttype As String
    Dim DeveloperPayload As String
    DeveloperPayload="Example In-App Consumable" ' ---> Name of my App "
    Producttype="inapp" ' ---> this is an input
    Productid1 = "price_300" ' ---> this is an input I it is the product id i defined in the store
    Productid2 = "price_500" ' ---> this is an input I it is the product id i defined in the store
End Sub
 
Sub Globals
 
 
    Private Button3 As Button
    Private Label2 As Label
    Private Button1 As Button
    Private Label1 As Label
End Sub
 
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime = True Then
        manager.Initialize("manager", key)
    End If
    manager.DebugLogging = True
    Activity.LoadLayout("Main")
 
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(Success)
If Success Then
        Log(purchases)
        For Each p As Purchase In purchases.Values
        Log(p.Productid & ", Purchased? " & (p.PurchaseState = p.STATE_PURCHASED))
        Next
End If
End Sub
 
 
Sub button1_click
    manager.RequestPayment(Productid1, Producttype, DeveloperPayload)
End Sub
 
Sub Button3_Click
    manager.RequestPayment(Productid2, Producttype, DeveloperPayload)
End Sub
 
Sub Manager_PurchaseCompleted(Success As Boolean, Product As Purchase)
If Success Then
   manager.ConsumeProduct(Product)
   If Product.Productid = "price_300" Then
        Msgbox("Hai acquistato price_300","Messaggio")
         Label1.Text = "Acquisto Prima Opzione"
         Label2.Text = "Hai 60 Secondi"
   Else If Product.Productid = "price_500" Then
        Msgbox("Hai acquistato price_500","Messaggio")
        Label1.Text = "Acquisto Seconda Opzione"
        Label2.Text = "Hai 100 Secondi"
    End If
End If
End Sub
 

Cableguy

Expert
Licensed User
Longtime User
I see a code... But no tuturial of any sort... A small intro would make this thread a lot more useful, don't you think?
 

MarcoRome

Expert
Licensed User
Longtime User
Right Cableguy :)

1) Go "Developer Console". "Create New Application" you have this result ( Figure 1 ) and click top "Service API":

image-1-31AC_5465C2F7.png


2) Open window with your Key RSA. ***Copy Key***

image-2-EE28_5465C325.png



3) Click top "Product In-App" and click top "Add New Product"

image-3-4B9E_5465C356.png


4) Click top "Product Management" and insert "ID Product" example: price_test3 ( dont forget this: An ID should consist of lowercase letters (az), numbers (0-9), underscore (_) and dot (.). It must also begin with lowercase letters or numbers. For example, IDs "un_id", "1_2_3"or"una.spada" are valid, while the ID "UN_ID", "_1_2_3"or".una.spada" are not. ).
Repeat same operation to add new product, example price_test4.
In this example we have included for price_test3 --> 0,50€ and for price_test4 --> 0,60€.


image-4-7499_5465C37F.png



ok is done :) so you have:

A) Key: "MIIBIjANBgkqXXXX......"( see step 2 )
B) Product: price_test3 and price_test4 ( see step 4 ).

Now insert this in your code:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim manager As BillingManager3
    Private key As String = "MIIBIjANBgkqXXXX......" '**** SEE STEP 2
    Dim Productid1, Productid2 As String
    Dim Producttype As String
    Dim DeveloperPayload As String
    DeveloperPayload="Example In-App Consumable" ' ---> Name of my App "
    Producttype="inapp" ' ---> this is an input
    Productid1 = "price_test3" ' ---> this is an input I it is the product id i defined in the store **** SEE STEP 4
    Productid2 = "price_test4" ' ---> this is an input I it is the product id i defined in the store **** SEE STEP 4
End Sub
Sub Globals
    Private Button3 As Button
    Private Label2 As Label
    Private Button1 As Button
    Private Label1 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime = True Then
        manager.Initialize("manager", key)
    End If
    manager.DebugLogging = True
    Activity.LoadLayout("Main")
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(Success)
If Success Then
        Log(purchases)
        For Each p As Purchase In purchases.Values
        Log(p.Productid & ", Purchased? " & (p.PurchaseState = p.STATE_PURCHASED))
        Next
End If
End Sub
Sub button1_click
    manager.RequestPayment(Productid1, Producttype, DeveloperPayload)
End Sub
Sub Button3_Click
    manager.RequestPayment(Productid2, Producttype, DeveloperPayload)
End Sub
Sub Manager_PurchaseCompleted(Success As Boolean, Product As Purchase)
If Success Then
   manager.ConsumeProduct(Product) '******************THIS IS IMPORTANT FOR App-In consumable purchases
   If Product.Productid = "price_test3" Then
        Msgbox("Hai acquistato price_test3","Messaggio")
         Label1.Text = "Acquisto Prima Opzione"
         Label2.Text = "Hai 60 Secondi"
   Else If Product.Productid = "price_test4" Then
        Msgbox("Hai acquistato price_test4","Messaggio")
        Label1.Text = "Acquisto Seconda Opzione"
        Label2.Text = "Hai 100 Secondi"
    End If
End If
End Sub

So you have in your app two button ( button_1 & button3 ).
In button_1 we insert:
B4X:
manager.RequestPayment(Productid1, Producttype, DeveloperPayload)

in button_3 we insert:
B4X:
manager.RequestPayment(Productid2, Producttype, DeveloperPayload)

so when user click about button_1 buy "price_test3" when click button_2 but "price_test4".When you insert in Manager_PurchaseCompleted this line:
B4X:
manager.ConsumeProduct(Product)

you allow your user to purchase the same product more than once in consumption.

Good work
I hope that so is more useful
Bye
Marco
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi, I need my beta testers not to be billed for the add-ins.
Any way to do it?
Thanks
If you add their email addresses to your tester list (go into your Developer Console, click on "Settings", "Account Details", then scroll down to "GMail accounts with testing access") then they can do IAP transactions without being charged. It seems that they don't have to be GMail accounts either. I use accounts that aren't GMail & they work fine. The account does have to be the same account the tester uses to install the app on their device though.
 
Top