iOS Question Code to check active subscription

toro1950

Active Member
Licensed User
Hi, I'm having trouble testing my app subscription through RevenueCat. I'm not sure which code is the correct one for these two.
If Main.HasPremium Then or If Success Then
If I test with Main.HasPremium it tells me that the subscription is not active, if I test with Success it tells me that it is active

B4X:
PurchaseHelper.Initialize(Me,"PurchaseHelper",m_API_KEY)
    PurchaseHelper.ProductIndentififer = Array As String("Mesi12") 'The Product identifyer
        Wait For (PurchaseHelper.CheckPurchases) complete (Success As Boolean)
    If Main.HasPremium Then
    If Success Then
I have a doubt about the code, because in the example
I downloaded, the same code is used to display the existing subscription.

B4X:
B4XPages.SetTitle(Me,"RevenueCat Example")
    
    PurchaseHelper.Initialize(Me,"PurchaseHelper","YourRevenueCatApiKey")
    PurchaseHelper.ProductIndentififer = Array As String("all_access_1_month_lognote","all_access_1_year_lognote") 'The Product identifyer
    
    Wait For (PurchaseHelper.CheckPurchases) complete (Success As Boolean) 'Must be called at app start, as we check here whether the user has the Premium version
    Log("Do I have a premium version? " & Success)
    
    Wait For (PurchaseHelper.GetProductsInformation(PurchaseHelper.ProductIndentififer)) complete (lstPurchases As List)
    
    For Each ProductInfo As ProductInformation In lstPurchases
        'Log(ProductInfo.Tag)
        'Log(ProductInfo.Description)   
        CustomListView1.AddTextItem(ProductInfo.Title & " - " & ProductInfo.LocalizedPrice,ProductInfo.ProductIdentifier)   
    Next
So I'm asking for clarification on the correct code to verify that the subscription is active.
I'm hoping someone can help me, I made another post about RevenueCat a while ago with no response,
I don't think anyone uses RevenueCat or is there another way to do it?
 

Alexander Stolte

Expert
Licensed User
Longtime User
The confusion: Success is just the return value of CheckPurchases, which always returns True (it only indicates the function completed, not the subscription status).

The correct way: Use Main.HasPremium to check the actual subscription status.
B4X:
Wait For (PurchaseHelper.CheckPurchases) complete (Success As Boolean)
If Main.HasPremium Then
    Log("User has active subscription")
Else
    Log("No active subscription")
End If

Why it might show "not active" during testing:
  1. Sandbox vs Production: Make sure you're using the correct RevenueCat API key (check if it's the public SDK key, not the secret key)
  2. Product Identifier mismatch: Your product identifier "Mesi12" must exactly match what's configured in RevenueCat dashboard
  3. User ID: RevenueCat tracks subscriptions per user. If you're testing with different user IDs, the subscription won't carry over
  4. Cache issue: The helper caches the expiration date in KeyChain. For testing, you can try clearing the cache:
B4X:
p.KeyChainPut("RevenueCat_SubscriptionExpiresDate","")

5. Check the logs: Add this after CheckPurchases to debug:
B4X:
Log("HasPremium: " & Main.HasPremium)
Log("Expiry: " & p.KeyChainGet("RevenueCat_SubscriptionExpiresDate"))
 
Upvote 0

toro1950

Active Member
Licensed User
Alexander, thank you for your reply, but something still doesn't make sense to me. I use Main.HasPremium and responds that it is not active,
the variable Mod3.VAB is false, I call the sub from the sub B4XPage Appear

B4X:
private Sub contpagato
    PurchaseHelper.Initialize(Me,"PurchaseHelper",m_API_KEY)
    PurchaseHelper.ProductIndentififer = Array As String("Mesi12") 'The Product identifyer
    
    Wait For (PurchaseHelper.CheckPurchases) complete (Success As Boolean) 
    
    If Main.HasPremium=True Then
        Mod3.VAB=True
        Return
    Else
       Mod3.VAB=False
      B4XPages.ShowPage("page2")
    (where do I make the purchase)
      End If
      
End Sub
that's what I call the purchase sub in Page 2 where I have a panel showing the features and type of subscription

B4X:
PurchaseHelper.Initialize(Me,"PurchaseHelper",m_API_KEY)
    PurchaseHelper.ProductIndentififer = Array As String("Mesi12") 'The Product identifyer
    Wait For (PurchaseHelper.RequestPayment("Mesi12")) Complete (Success As Boolean)
        
    If Success Then
But as a response it tells me that I already have a subscription, see attached image
 

Attachments

  • image1.png
    image1.png
    411.8 KB · Views: 20
Upvote 0

toro1950

Active Member
Licensed User
Hi Alexander, this morning I tried to make the purchase again, initially it seems to have been successful
as per the attached image, then in the end I get the message (Purchase Failed, Purchase was cancelled)
but if I try the purchase again the message I attached yesterday appears, expiring today 14th
 

Attachments

  • immagine2.png
    immagine2.png
    259.3 KB · Views: 13
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Please test the b4xlib. in the attachment. I have added a few logs that should explain why this happens so often for you. Unfortunately, logs are only visible in debug mode in b4i. What time zone do you live in, as this may also be a problem in sandbox mode, which I need to check.
 

Attachments

  • RevenueCat.b4xlib
    6.7 KB · Views: 9
Upvote 0

toro1950

Active Member
Licensed User
Hi, I tested the new library but the result doesn't change, the subscription always appears inactive.
As soon as I make the purchase it allows me to proceed but in the end it says it has been cancelled.
However, it actually activates it even if it doesn't show up, because when I re-purchase it says the
subscription is already present. I live in Italy, 50 km from Rome.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Hi, I tested the new library but the result doesn't change, the subscription always appears inactive.
As soon as I make the purchase it allows me to proceed but in the end it says it has been cancelled.
However, it actually activates it even if it doesn't show up, because when I re-purchase it says the
subscription is already present. I live in Italy, 50 km from Rome.
I haven't changed anything except introducing logs to better understand what's happening in the background. Have you tested it in debug mode and can you show me the logs?

I use the library in three of my apps and have never had any problems so far, except that sometimes the current subscription cannot be loaded in the sandbox after purchase. Either it's a configuration error, or you're experiencing the rare bug every time.
 
Upvote 0

toro1950

Active Member
Licensed User
Alexander, I don't know how to test the app in debug mode, the logs only appear if there is a problem, attached is the image of my IDE
I apologize for my ignorance
 

Attachments

  • immgine3.png
    immgine3.png
    13.5 KB · Views: 8
Upvote 0
Top