Private Sub B4XPage_Created (Root1 As B4XView)
...
RestorePurchases
...
End Sub
Private Sub RestorePurchases
    Wait For (billing.ConnectIfNeeded) Billing_Connected (Result As BillingResult)
    If Result.IsSuccess Then
        Wait For (billing.QueryPurchases("inapp")) Billing_PurchasesQueryCompleted (Result As BillingResult, Purchases As List)
        Log("Query completed: " & Result.IsSuccess)
        If Result.IsSuccess Then
            For Each p As Purchase In Purchases
                If p.Sku.StartsWith("bse.echocalc.donate") Then HandleAdsPurchase(p)
            Next
        End If
    End If
End Sub
Public Sub PurchaseDonation (sProductID As String)
    ' called from a menu click on a page
    'make sure that the store service is connected
    Wait For (billing.ConnectIfNeeded) Billing_Connected (Result As BillingResult)
    If Result.IsSuccess Then
        'get the sku details
        Dim sf As Object = billing.QuerySkuDetails("inapp", Array(sProductID))
        Wait For (sf) Billing_SkuQueryCompleted (Result As BillingResult, SkuDetails As List)
        If Result.IsSuccess Then
            If SkuDetails.Size >= 1 Then
                Result = billing.LaunchBillingFlow(SkuDetails.Get(0))
                If Result.IsSuccess Then Return
            End If
        End If
    End If
    ToastMessageShow("Error starting billing process", True)
End Sub
Sub billing_PurchasesUpdated (Result As BillingResult, Purchases As List)
    'This event will be raised when the status of one or more of the purchases has changed.
    'It will usually happen as a result of calling LaunchBillingFlow however it can be called in other cases as well.
    If Result.IsSuccess Then
        For Each p As Purchase In Purchases
            If p.Sku.StartsWith("bse.echocalc.donate") Then
                HandleAdsPurchase(p)
            Else
                Log("Unexpected product: " & p.Sku)
            End If
        Next
    End If
End Sub
Private Sub HandleAdsPurchase (p As Purchase)
    Dim dDonated As Double
    Dim sValue As String
    If p.PurchaseState <> p.STATE_PURCHASED Then Return
    'Verify the purchase signature.
    'This cannot be done with the test id.
    If (p.Sku.StartsWith("bse.echocalc.donate") = False) And (billing.VerifyPurchase(p, BILLING_KEY) = False) Then
        Log("Invalid purchase")
        Return
    End If
    'we either acknowledge the product or consume it.
    'If p.IsAcknowledged = False Then
    'Wait For (billing.AcknowledgePurchase(p.PurchaseToken, "")) Billing_AcknowledgeCompleted (Result As BillingResult)
    'Log("Acknowledged: " & Result.IsSuccess)
    'End If
    'SKU = bse.echocalc.donate2 or bse.echocalc.donate5 or bse.echocalc.donate10
    sValue = p.Sku.SubString (19)
    If IsNumber (sValue) Then
        wait For (billing.Consume(p.PurchaseToken, "")) Billing_ConsumeCompleted (Result As BillingResult)
        Log("Consumed: " & Result.IsSuccess)
        dDonated = Assets.GetSetting ("Donated")
        dDonated = dDonated + sValue
        Assets.SetSetting ("Donated", dDonated)
        Log("Donation confirmed")
        Patient.UpdateDonationStatus
    Else
        Log("Error with purchase: " & p.Sku)
    End If
End Sub