Bug? googleplaybilling Subscription

Status
Not open for further replies.

frx

Member
Hi everyone, I need help integrating the subscriptions via Google Playbilling. Unfortunately, my code is buggy even though the skus are correct. Could someone be able to help me? I'll start by saying this is the first time I've inserted the subscriptions into the app.
subscription code:
    procesglobal
    Private billing As BillingClient
    Private MainModule As Object  ' Riferimento al modulo principale (Main), usato per callback
    Public const BILLING_KEY As String = "example"
    Dim SubscriptionEndDate As Long
    Dim IsSubscribed As Boolean
    Private SubscriptionName As String ' Salva il nome dell'abbonamento attivo
    ' Altre variabili globali
    Private SubscriptionEndDate As Long ' Salva la data di scadenza dell'abbonamento
    
    Public Const SKU_SUBSCRIPTION_1_MONTH As String = "example_1_month"
    Public Const SKU_TRIAL_7_DAYS As String = "example-7-giorni-gratuita"
    
    
    Sub ConnectToBilling
    ' Connessione al servizio di billing
    Wait For (billing.ConnectIfNeeded) Billing_Connected (Result As BillingResult)
    If Result.IsSuccess Then
        Log("Connesso con successo a Google Play Billing.")
        CheckSubscriptions ' Controlla gli abbonamenti
    Else
        Log("Errore nella connessione: " & Result.DebugMessage)
        CallSubDelayed2(MainModule, "HandleBillingError", Result.DebugMessage)
    End If
End Sub

Sub CheckSubscriptionBeforeAction
    Log("Controllo abbonamento in corso...")
    QuerySubscriptionDetails
End Sub

Sub QuerySubscriptionDetails
    Dim skuList As List
    skuList.Initialize
    skuList.Add(SKU_SUBSCRIPTION_1_MONTH)
    skuList.Add(SKU_TRIAL_7_DAYS)
    
    Log("Avvio della query per ottenere dettagli degli abbonamenti...")
    Wait For (billing.QuerySkuDetails("subs", skuList)) billing_SkuDetailsQueryCompleted (SkuDetails As List, Result As BillingResult)
End Sub

Sub CheckSubscriptions
    Wait For (billing.QueryPurchases("subs")) Billing_QueryPurchasesCompleted (Purchases As List, Result As BillingResult)
    If Result.IsSuccess Then
        For Each purchase As Purchase In Purchases
            If purchase.IsAcknowledged Then
                SetSubscriptionDetails(purchase)
                Return
            End If
        Next
    Else
        Log("Errore nella query degli abbonamenti: " & Result.DebugMessage)
    End If
    IsSubscribed = False
    CallSubDelayed(MainModule, "OnSubscriptionInactive")
End Sub


Sub PurchaseSubscription(Sku As String)
    ' Verifica connessione a Google Play Billing
    Wait For (billing.ConnectIfNeeded) Billing_Connected (Result As BillingResult)
    If Result.IsSuccess Then
        Log("Connessione a Google Play Billing riuscita.")
        
        ' Ottieni i dettagli del prodotto
        Wait For (billing.QuerySkuDetails("subs", Array As String(Sku))) billing_SkuDetailsQueryCompleted (SkuDetails As List, Result As BillingResult)
        If Result.IsSuccess And SkuDetails.Size > 0 Then
            Dim ProductDetails As SkuDetails = SkuDetails.Get(0)
            
            ' Avvia il flusso di acquisto
            Wait For (billing.LaunchBillingFlow(ProductDetails)) Billing_BillingFlowCompleted (BillingResult As BillingResult)
            If BillingResult.IsSuccess Then
                Log("Acquisto completato per SKU: " & Sku)
            Else
                Log("Errore durante l'acquisto: " & BillingResult.DebugMessage)
            End If
        Else
            Log("Impossibile ottenere i dettagli del prodotto per SKU: " & Sku)
        End If
    Else
        Log("Errore di connessione: " & Result.DebugMessage)
    End If
End Sub

Private Sub HandleSubscription(p As Purchase)
    If p.PurchaseState <> p.STATE_PURCHASED Then Return
    If billing.VerifyPurchase(p, BILLING_KEY) = False Then
        Log("Abbonamento non valido")
        Return
    End If
    If p.IsAcknowledged = False Then
        Wait For (billing.AcknowledgePurchase(p.PurchaseToken, "")) Billing_AcknowledgeCompleted (Result As BillingResult)
        If Result.IsSuccess Then
            Log("Abbonamento riconosciuto con successo.")
        Else
            Log("Errore nel riconoscimento dell'abbonamento: " & Result.DebugMessage)
        End If
    End If
    SetSubscriptionDetails(p)
End Sub






Private Sub SetSubscriptionDetails(p As Purchase)
    IsSubscribed = True
    Select Case p.Sku
        Case SKU_SUBSCRIPTION_1_MONTH
            SubscriptionEndDate = DateTime.Now + (30 * DateTime.TicksPerDay)
        Case SKU_TRIAL_7_DAYS
            SubscriptionEndDate = DateTime.Now + (7 * DateTime.TicksPerDay)
    End Select
    CallSubDelayed(MainModule, "OnSubscriptionActive")
End Sub

Sub ShowSubscriptionOptions
    Dim options As List
    options.Initialize
    options.Add("1 mese ")
    options.Add("7 Giorni Prova Gratuita")
    
    Dim result As Int = InputList(options, "Scegli un piano di abbonamento:", -1)
    If result <> DialogResponse.CANCEL Then
        Select Case result
            Case 0
                PurchaseSubscription(SKU_SUBSCRIPTION_1_MONTH)
            Case 1
                PurchaseSubscription(SKU_TRIAL_7_DAYS)
        End Select
    End If
End Sub

Sub EnablePremiumFeatures(IsEnabled As Boolean)
    If IsEnabled Then
        Log("Funzionalità premium abilitate.")
    Else
        Log("Funzionalità premium disabilitate.")
    End If
End Sub

Sub OnSubscriptionActive
    IsSubscribed = True
    Log("Abbonamento attivo")
    EnablePremiumFeatures(True)
    UpdateButtonState
End Sub

Sub OnSubscriptionInactive
    IsSubscribed = False
    Log("Abbonamento non attivo")
    EnablePremiumFeatures(False)
    UpdateButtonState
End Sub

Sub UpdateButtonState
    btnpremium.Enabled = IsSubscribed
End Sub

Sub billing_PurchasesUpdated (Result As BillingResult, Purchases As List)
    If Result.IsSuccess Then
        For Each p As Purchase In Purchases
            HandleSubscription(p)
        Next
    Else
        Log("Errore negli aggiornamenti acquisti: " & Result.DebugMessage)
    End If
End Sub








Sub IsSubscriptionValid As Boolean
    If DateTime.Now <= SubscriptionEndDate Then
        Return True
    Else
        Log("Abbonamento scaduto")
        CallSubDelayed(MainModule, "OnSubscriptionInactive")
        Return False
    End If
End Sub


Sub HandleBillingError(ErrorMessage As String)
    Log(ErrorMessage)
    Msgbox(ErrorMessage, "Errore di fatturazione")
End Sub

Sub billing_SkuDetailsQueryCompleted (SkuDetails As List, Result As BillingResult)
    If Result.IsSuccess Then
        If SkuDetails.Size > 0 Then
            For Each detail As SkuDetails In SkuDetails
                Log($"SKU trovato: ${detail.Sku}, Prezzo: ${detail.Price}, Titolo: ${detail.Title}"$)
            Next
        Else
            Log("Nessuno SKU trovato. Verifica la configurazione degli abbonamenti nella Play Console.")
        End If
    Else
        Log($"Errore durante la query degli SKU: ${Result.DebugMessage}"$)
    End If
End Sub

Sub billing_SkuQueryCompleted (SkuDetails As List, Result As BillingResult)
    If Result.IsSuccess Then
        If SkuDetails.Size > 0 Then
            For Each detail As SkuDetails In SkuDetails
                Log($"SKU trovato: ${detail.Sku}, Prezzo: ${detail.Price}, Titolo: ${detail.Title}"$)
            Next
        Else
            Log("Nessuno SKU trovato. Verifica la configurazione degli abbonamenti nella Play Console.")
        End If
    Else
        Log($"Errore durante la query degli SKU: ${Result.DebugMessage}"$)
    End If
End Sub




Private Sub btnCheckSubscription_Click
    Dim PackageName As String = "example.com" ' Sostituisci con il package name della tua app
    Dim PlayStoreUrl As String = $"https://play.google.com/store/account/subscriptions?package=${PackageName}"$
    
    Dim Intent As Intent
    Intent.Initialize(Intent.ACTION_VIEW, PlayStoreUrl)
    Intent.SetComponent("com.android.vending") ' Assicura che venga usato il Play Store
    
    Try
        StartActivity(Intent)
    Catch
        ' Fallback: Apri il browser se il Play Store non è disponibile
        Dim BrowserIntent As Intent
        BrowserIntent.Initialize(Intent.ACTION_VIEW, PlayStoreUrl)
        StartActivity(BrowserIntent)
    End Try
    
    
    
    QuerySubscriptionDetails
End Sub
 
Status
Not open for further replies.
Top