Android Question In App Purchase question

ilan

Expert
Licensed User
Longtime User
hi

i am having a look at the inapp purchase example from erel here:

and if i open the example i have this 2 events:

B4X:
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 = ADS_SDK_ID Then HandleAdsPurchase(p)
            Next
        End If
    End If
End Sub

B4X:
Private Sub HandleAdsPurchase (p As Purchase)
    If p.PurchaseState <> p.STATE_PURCHASED Then Return
    'Verify the purchase signature.
    'This cannot be done with the test id.
    If p.Sku.StartsWith("android.test") = False And billing.VerifyPurchase(p, BILLING_KEY) = False Then
        Log("Invalid purchase")
        Return
    End If
    If p.IsAcknowledged = False Then
        'we either acknowledge the product or consume it.
        Wait For (billing.AcknowledgePurchase(p.PurchaseToken, "")) Billing_AcknowledgeCompleted (Result As BillingResult)
        Log("Acknowledged: " & Result.IsSuccess)
    End If
    AdsRemoved = True
    Log("AdsRemoved")
    UpdateAdsState
End Sub

i used this example for years but now something is not clear to me.
you call the event: HandleAdsPurchase(p) if p.Sku = ADS_SDK_ID

then in handle purchase you say:

B4X:
 If p.Sku.StartsWith("android.test") = False And billing.VerifyPurchase(p, BILLING_KEY) = False Then
        Log("Invalid purchase")
        Return
    End If
but should not it be:


B4X:
 If p.Sku.StartsWith("android.test") = True And billing.VerifyPurchase(p, BILLING_KEY) = False Then
        Log("Invalid purchase")
        Return
    End If

???

only if the name of the inapp product is the one you are checking has been purchased false then invalid purchase but if you ask if the name is false then it will already be false because you called the HandleAdsPurchase(p) after the name mached the inapp purchase here:

B4X:
  If p.Sku = ADS_SDK_ID Then HandleAdsPurchase(p)

looks wrong to me, or am i missing something??
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is a bit confusing but the code is correct. The comment explains it:
B4X:
'Verify the purchase signature.
'This cannot be done with the test id.
If p.Sku.StartsWith("android.test") = False And billing.VerifyPurchase(p, BILLING_KEY) = False Then

You cannot verify android.test purchases, so only non-test ads are verified.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
It is a bit confusing but the code is correct. The comment explains it:
B4X:
'Verify the purchase signature.
'This cannot be done with the test id.
If p.Sku.StartsWith("android.test") = False And billing.VerifyPurchase(p, BILLING_KEY) = False Then

You cannot verify android.test purchases, so only non-test ads are verified.
thanx erel but now i am more confused.

in your example you set a const

B4X:
Public const ADS_SDK_ID As String = "android.test.purchased"

and i understood that this is the in app ID so i changed it to my in app ID
and my code looks like this:

B4X:
'Verify the purchase signature.
'This cannot be done with the test id.
If p.Sku.StartsWith("removeads") = False And billing.VerifyPurchase(p, BILLING_KEY) = False Then

do i need to leave this code with "android.test" ?
 
Upvote 0
Top