#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
'RestorePurchases
Dim purchaseTime As Long
Public billing As BillingClient
Public const ADS_SDK_ID As String = "laraplus2018"
Public const ADS_SDK_ID2 As String = "laraplus6mth"
Public const ADS_SDK_ID3 As String = "laraplus1"
Public const ADS_SDK_ID4 As String = "lara202312"
Public const ADS_SDK_ID5 As String = "lara202306"
Public ADS_SDK_Token As String = ""
Public sixMonthPrice, twelveMonthPrice As String = ""
Public productID, subsID, token As String = ""
Public SKU_Purchased As String = ""
Private purchList As List
Public const BILLING_KEY As String = "MIIBIjANBgk....."
Public myLog As List
End Sub
Sub Service_Create
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.
myLog.Initialize
purchList.Initialize
billing.Initialize("billing")
#if Full
RestorePurchases
#end if
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
End Sub
Sub Service_TaskRemoved
'This event will be raised when the user removes the app from the recent apps list.
End Sub
'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
Sub Service_Destroy
End Sub
public Sub RestorePurchases
Wait For (billing.ConnectIfNeeded) Billing_Connected (Result As BillingResult)
If Result.IsSuccess Then
Dim sf As Object = billing.QuerySkuDetails("subs", Array(ADS_SDK_ID4,ADS_SDK_ID5))
Wait For (sf) Billing_SkuQueryCompleted (Result As BillingResult, SkuDetailsList As List)
If Result.IsSuccess And SkuDetailsList.Size > 0 Then
For Each Sku As SkuDetails In SkuDetailsList
Dim sPrice As String
If Sku.Price = "" Then
Dim offers As List = Sku.As(JavaObject).RunMethod("getSubscriptionOfferDetails", Null)
Dim offer As JavaObject = offers.Get(0)
Dim PricingPhases As JavaObject
PricingPhases = offer.RunMethod("getPricingPhases", Null)
Dim l As List = PricingPhases.RunMethod("getPricingPhaseList", Null)
Dim PricingPhase As JavaObject
PricingPhase = l.Get(0)
sPrice = PricingPhase.RunMethod("getFormattedPrice", Null)
Else
sPrice = Sku.Price
End If
If Sku.Sku = "lara202312" Then
If sPrice.ToLowerCase = "free" Then
sPrice = "$65.99 AUD"
End If
twelveMonthPrice = sPrice
else if Sku.Sku = "lara202306" Then
If sPrice.ToLowerCase = "free" Then
sPrice = "$43.99 AUD"
End If
sixMonthPrice = sPrice
End If
Next
End If
Wait For (billing.QueryPurchases("subs")) Billing_PurchasesQueryCompleted (Result As BillingResult, Purchases As List)
If Result.IsSuccess Then
myLog.Add("(RestorePurchases) Were In")
For Each p As Purchase In Purchases
productID = p.OrderId
'Log(productID)
purchaseTime = p.PurchaseTime
SKU_Purchased = p.Sku
token = p.PurchaseToken
subsID = p.Sku
myLog.Add($"(RestorePurchases) SKU received Result - ${p.Sku}"$)
If p.Sku = "laraplus6mth" Or p.Sku = "laraplus1" Or p.Sku = "laraplus2018" Or p.Sku = "lara202312" Or p.Sku = "lara202306" Then
HandleAdsPurchase(p)
Else
Main.AppSubscriptionActive = False
End If
Next
Else
myLog.Add($"(RestorePurchases) Info NOT received from PS Result - ${Result.ResponseCodeString}"$)
End If
End If
End Sub
public 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)
If Result.IsSuccess Then
Select p.Sku
Case ADS_SDK_Token
Wait For (billing.QueryPurchases("inapp")) Billing_PurchasesQueryCompleted (Result As BillingResult, Purchases As List)
If Result.IsSuccess Then
B4XPages.MainPage.Pages.PageShop.SendEeprom(Purchases,p.Sku)
End If
Case ADS_SDK_ID, ADS_SDK_ID2, ADS_SDK_ID3,ADS_SDK_ID4,ADS_SDK_ID5
productID = p.OrderId
purchaseTime = p.PurchaseTime
SKU_Purchased = p.Sku
token = p.PurchaseToken
subsID = p.Sku
Main.AppSubscriptionActive = True
End Select
End If
Else
If p.Sku = ADS_SDK_ID Or p.Sku = ADS_SDK_ID2 Or p.Sku = ADS_SDK_ID3 Or p.Sku = ADS_SDK_ID4 Or p.Sku = ADS_SDK_ID5 Then
productID = p.OrderId
purchaseTime = p.PurchaseTime
SKU_Purchased = p.Sku
token = p.PurchaseToken
subsID = p.Sku
Main.AppSubscriptionActive = True
'Main.OKToPost = True
Else
productID = ""
Main.AppSubscriptionActive = False
'Main.OKToPost = False
End If
End If
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 abilling_PurchasesUpdatedresult of calling LaunchBillingFlow however it can be called in other cases as well.
If Result.IsSuccess Then
For Each p As Purchase In Purchases
HandleAdsPurchase(p)
Next
Else
If Result.ResponseCode = 7 Then 'Product has already been purchased
Wait For (billing.QueryPurchases("inapp")) Billing_PurchasesQueryCompleted (Result As BillingResult, Purchases As List)
For Each p As Purchase In Purchases
If Result.IsSuccess Then
B4XPages.MainPage.Pages.PageShop.SendEeprom(Purchases,p.Sku)
End If
Next
End If
End If
End Sub
public Sub ConsumeAdsProduct(Purchases As List)
For Each p As Purchase In Purchases
Select p.Sku
Case ADS_SDK_ID, ADS_SDK_ID2, ADS_SDK_ID3,ADS_SDK_ID4,ADS_SDK_ID5
Wait For (billing.Consume(p.PurchaseToken, "")) Billing_ConsumeCompleted (Result As BillingResult)
If Result.IsSuccess Then
'Log("consumed")
Main.AppSubscriptionActive = False
Else
ToastMessageShow("Consumption incomplete",True)
End If
Case ADS_SDK_Token
Wait For (billing.Consume(p.PurchaseToken, "")) Billing_ConsumeCompleted (Result As BillingResult)
If Result.IsSuccess Then
'Log("consumed")
End If
Case Else
ToastMessageShow("(ConsumeAdsProduct) Invalid SKU",True)
Main.AppSubscriptionActive = False
End Select
Next
End Sub