iOS Question Check if user has a active subscription of my product - iStore

Xandoca

Active Member
Licensed User
Longtime User
Hi there,

Is there a method or code using B4i to check if user has a active subscription of my product?
Using B4a with google in app purchases you can check using:
B4X:
Wait For (billing.QueryPurchases("subs")) Billing_PurchasesQueryCompleted (Result As BillingResult, Purchases As List)
If Result.IsSuccess Then
    If Purchases.Size > 0 Then
        For Each p As Purchase In Purchases
            If p.Sku = "myProduct" Then
                If p.PurchaseState = p.STATE_PURCHASED Then
                    Return True
                End If
            End If
        Next
    end if
end if
return false

I've implemented in B4a without using a backend, all inside my app. is this possible in B4i?

Thank you
 

Xandoca

Active Member
Licensed User
Longtime User
Thank you Erel.
My point is not paying for a server to do that and also implement the a complex logic to handle the subscription status. For what I saw, it's a complex logic both on server or device.
I've been researching all day ... Firebase (user authentication/app user id) and RevenueCat can be a good solution. There's a free plan on both services that works for me.
I've saw a lot of services like RevenueCat... using REST API.
I will share the results of this strategy here when I complete the tests.
Regards
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Thank you Erel.
My point is not paying for a server to do that and also implement the a complex logic to handle the subscription status. For what I saw, it's a complex logic both on server or device.
I've been researching all day ... Firebase (user authentication/app user id) and RevenueCat can be a good solution. There's a free plan on both services that works for me.
I've saw a lot of services like RevenueCat... using REST API.
I will share the results of this strategy here when I complete the tests.
Regards
You can just purchase a simple vps for usd 2.5 / month and I can share my php script that checks the receipt status.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Hi Tufanv, thank you.
I Will try that.
Can you share the php script?
check last msg:
 
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
Thank you Erel.
My point is not paying for a server to do that and also implement the a complex logic to handle the subscription status. For what I saw, it's a complex logic both on server or device.
I've been researching all day ... Firebase (user authentication/app user id) and RevenueCat can be a good solution. There's a free plan on both services that works for me.
I've saw a lot of services like RevenueCat... using REST API.
I will share the results of this strategy here when I complete the tests.
Regards
Hi,

Just sharing... I got RevenueCat working for App Store and also Play Store. It took sometime to configure but it's worth it because I've learned a lot about REST API and how to implement it using B4X.
Aside the configuration you have to do at RevenueCat, you only need two functions (two endpoints) to work with RevenueCat:
1) Check User Plan Status (user will be created if user doesn't exist at RevenueCat:
B4X:
Public Sub PlanStatus
    Dim J As HttpJob
    J.Initialize("CreateUserORGetUserInfo", Me)
    J.Download("https://api.revenuecat.com/v1/subscribers/" & SharedCode.MeuPerfil.UIDFirebase)
    'SharedCode.MeuPerfil.UIDFirebase is the user id that will be created at revenuecat to identify the user.
    
    J.GetRequest.SetHeader("Accept","application/json")
    #if b4i
    J.GetRequest.SetHeader("X-Platform","ios")
    #end if
    #if b4a
    J.GetRequest.SetHeader("X-Platform","android")
    #End If
    J.GetRequest.SetHeader("Content-Type","application/json")
    J.GetRequest.SetHeader("Authorization","Bearer " & Main.ptrc )
    'Main.ptrc    revenue cat key
    
    
    Wait For (J) JobDone(j As HttpJob)
    
    If J.Success Then
        Log(J.GetString)

        Dim js As JSONParser
        js.Initialize(J.GetString)
        Dim Map1 As Map = js.NextObject
        
        Dim subscriber As Map = Map1.Get("subscriber")
        
        If subscriber.Size>0 Then
            
            Dim subscriptions As Map = subscriber.Get("subscriptions")
            If subscriptions.Size>0 Then

                For Each Key As String In subscriptions.Keys
                    'check if it's one of your products
                    If pm.ADS_SDK_ID.IndexOf(Key) > -1 Then 
                        
                        Dim p As Map = subscriptions.Get(Key)
                        'validating expire date using p.Get("expires_date")
                        ....
                    End If               
                Next               
                End If           
            Else
                'no subscriptions
            
            End If
        Else
            'no subscriptions

        End If
        
        j.Release
        
    Else
        'handle error
        
        j.Release
    End If

End Sub
2) Register a purchase at RevenueCat after user buy it:
B4X:
Public Sub RegisterPurchaseRevenueCat(Sku As String, PurchaseToken As String, price As String)

#if b4a
    Dim J As HttpJob
    J.Initialize("RegisterPurchaseRevenueCat", Me)
    Dim req As String = $"{
     "product_id": "${Sku}",
     "currency": "BRL",
     "is_restore": "false",
     "app_user_id": "${SharedCode.MeuPerfil.UIDFirebase}",
     "fetch_token": "${PurchaseToken}"
}"$
#End If
#if b4i
    Dim J As HttpJob
    J.Initialize("RegisterPurchaseRevenueCat", Me)
    Dim req As String = $"{
     "product_id": "${Sku}",
     "price": ${price},
     "currency": "BRL",
     "is_restore": "false",
     "app_user_id": "${SharedCode.MeuPerfil.UIDFirebase}",
     "fetch_token": "${PurchaseToken}"
}"$
        
#End If
    J.PostString("https://api.revenuecat.com/v1/receipts", req)
    J.GetRequest.SetHeader("Authorization","Bearer " & Main.ptrc)
    J.GetRequest.SetHeader("Accept","application/json")
    J.GetRequest.SetContentType("application/json")
    #if b4a
    J.GetRequest.SetHeader("X-Platform","android")
    #End If
    #if b4i
    J.GetRequest.SetHeader("X-Platform","ios")
    #End If
    
    Wait For (J) JobDone(j As HttpJob)
    If J.Success Then
        Log(J.GetString)
        'check if it is one of your products.
        For i = 0 To ADS_SDK_ID.Size - 1
            Dim pr As String = ADS_SDK_ID.get(i)
            If J.GetString.Contains(pr) Then
                'it's one of your prodcuts
            End If
        Next
        
    Else
        'handle errors
    End If
    
    j.Release
End Sub

B4i - App Store
I used the following code to got the receipt and encode it to send to RevenueCat (PurchaseToken):
B4X:
Dim no As NativeObject = Product
Dim receipt As NativeObject = no.GetField("transactionReceipt")
If receipt.IsInitialized Then
    Dim b() As Byte = receipt.NSDataToArray(receipt)
    Dim stringu As StringUtils
    Dim receiptstring As String = stringu.EncodeBase64(b)
end if

B4a - Play Store
Token to be sent to RevenueCat is the PurchaseToken from Purchase object that you got from billing_PurchasesUpdated event.

Regards.
 
Upvote 0
Top