#Event: Done
Sub Class_Globals
Private mCallback As Object
Private mEventName As String
Private oBillingClient As JavaObject
Private lSubscriptionID As String = "TheSubscriptionIDHere"
End Sub
Public Sub Initialize (Callback As Object, EventName As String)
mCallback = Callback
mEventName = EventName
Dim oContext As JavaObject
oContext.InitializeContext
oBillingClient.InitializeNewInstance("<Your App PackageName Here>.<Your Class Name Here>.BillingManager", Array(oContext, Me))
End Sub
Public Sub StartCheckingSubscriptionValidity
Try
oBillingClient.RunMethod("connectIfNeeded", Null)
Return
Catch
Log(LastException)
RaiseDone
End Try
End Sub
Private Sub VerifySubscriptionOnServer (Purchase As Purchase) As ResumableSub
Try
'Verifying the subscription
Return 1
Catch
Log(LastException)
Return -1
End Try
End Sub
Private Sub BillingManager_Connected (Result As BillingResult)
Try
Dim VerifyingResult As Int
If Result.IsSuccess Then
oBillingClient.RunMethod("queryPurchases", Array("subs"))
Wait For BillingManager_PurchasesQueryCompleted (Result As BillingResult, Purchases As List)
Log("Cls Result.IsSuccess=" & Result.IsSuccess)
If Result.IsSuccess Then
Log("Cls Purchases.Size=" & Purchases.Size)
For Each Purchase As Purchase In Purchases
If Purchase.Sku = lSubscriptionID And Purchase.PurchaseState = Purchase.STATE_PURCHASED Then
Log("Cls Purchase.OrderId=" & Purchase.OrderId)
Log("Cls Purchase.OriginalJson=" & Purchase.OriginalJson)
Log("Cls Purchase.PurchaseState=" & Purchase.PurchaseState)
Log("Cls Purchase.PurchaseTime=" & Purchase.PurchaseTime)
Log("Cls Purchase.PurchaseToken=" & Purchase.PurchaseToken)
Log("Cls Purchase.Signature=" & Purchase.Signature)
Log("Cls Purchase.Sku=" & Purchase.Sku)
Log("Cls Purchase.IsAcknowledged=" & Purchase.IsAcknowledged)
Log("Cls Purchase.IsAutoRenewing=" & Purchase.IsAutoRenewing)
Wait For (VerifySubscriptionOnServer(Purchase)) Complete (VTS As Int)
VerifyingResult = VTS
Exit
End If
Next
End If
End If
If VerifyingResult = 1 Then
'Verifying OK
End If
RaiseDone
Return
Catch
Log(LastException)
RaiseDone
Return
End Try
End Sub
Private Sub RaiseDone
If SubExists(mCallback, mEventName & "_Done") Then
CallSub(mCallback, mEventName & "_Done")
End If
End Sub
#If Java
import com.android.billingclient.api.AcknowledgePurchaseParams;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.ConsumeParams;
import com.android.billingclient.api.PendingPurchasesParams;
import com.android.billingclient.api.ProductDetails;
import com.android.billingclient.api.ProductDetailsResponseListener;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.android.billingclient.api.QueryProductDetailsParams;
import com.android.billingclient.api.QueryPurchasesParams;
import com.android.billingclient.api.QueryProductDetailsResult;
import anywheresoftware.b4a.objects.BillingClientWrapper.*;
import anywheresoftware.b4a.AbsObjectWrapper;
import androidx.annotation.NonNull;
import android.app.Activity;
import java.util.ArrayList;
import java.util.List;
public static class BillingManager implements PurchasesUpdatedListener {
private BillingClient billingClient;
private Activity activity;
private B4AClass target;
/* ================= CONSTRUCTOR ================= */
public BillingManager(Activity activity, B4AClass target) {
this.activity = activity;
this.target = target;
initializeBilling();
}
/* ================= INIT ================= */
public void initializeBilling() {
PendingPurchasesParams pendingParams =
PendingPurchasesParams.newBuilder()
.enableOneTimeProducts()
.build();
billingClient = BillingClient.newBuilder(activity)
.setListener(this)
.enablePendingPurchases(pendingParams)
.build();
}
public void connectIfNeeded() {
if (billingClient.getConnectionState() != BillingClient.ConnectionState.CONNECTING) {
if (billingClient.getConnectionState() != BillingClient.ConnectionState.CONNECTED) {
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(
@NonNull BillingResult billingResult) {
target.getBA().raiseEventFromDifferentThread(target, null, 0, "billingmanager_connected", false, new Object[] {AbsObjectWrapper.ConvertToWrapper(new BillingResultWrapper(), billingResult)});
}
@Override
public void onBillingServiceDisconnected() {
}
});
} else {
BillingResult billingResult = BillingResult.newBuilder()
.setDebugMessage("Already Connected.")
.setResponseCode(BillingClient.BillingResponseCode.OK)
.build();
target.getBA().raiseEventFromDifferentThread(target, null, 0, "billingmanager_connected", false, new Object[] {AbsObjectWrapper.ConvertToWrapper(new BillingResultWrapper(), billingResult)});
}
} else {
return;
}
}
/* ================= PURCHASE RESULT ================= */
@Override
public void onPurchasesUpdated(
@NonNull BillingResult billingResult,
List<Purchase> purchases) {
anywheresoftware.b4a.objects.collections.List lPurchases = new anywheresoftware.b4a.objects.collections.List();
if (purchases != null) {
lPurchases.setObject((List)purchases);
}
//target.getBA().raiseEventFromDifferentThread(target, null, 0, "billingmanager_onpurchasesupdated", false, new Object[] {AbsObjectWrapper.ConvertToWrapper(new BillingResultWrapper(), billingResult), lPurchases});
}
/* ================= RESTORE ================= */
public void queryPurchases(String productType) {
QueryPurchasesParams params =
QueryPurchasesParams.newBuilder()
.setProductType(productType)
.build();
billingClient.queryPurchasesAsync(
params,
new com.android.billingclient.api.PurchasesResponseListener() {
@Override
public void onQueryPurchasesResponse(
@NonNull BillingResult billingResult,
@NonNull List<Purchase> purchases) {
anywheresoftware.b4a.objects.collections.List lPurchases = new anywheresoftware.b4a.objects.collections.List();
if (purchases != null) {
lPurchases.setObject((List)purchases);
}
target.getBA().raiseEventFromDifferentThread(target, null, 0, "billingmanager_purchasesquerycompleted", false, new Object[] {AbsObjectWrapper.ConvertToWrapper(new BillingResultWrapper(), billingResult), lPurchases});
}
}
);
}
public boolean isBillingReady() {
return (billingClient != null && billingClient.isReady());
}
/* ================= RELEASE ================= */
public void release() {
if (billingClient != null
&& billingClient.isReady()) {
billingClient.endConnection();
}
}
}
#End If