@capgo/native-purchases 8.6.1 → 8.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -34,6 +34,8 @@ import java.util.ArrayList;
34
34
  import java.util.Collections;
35
35
  import java.util.List;
36
36
  import java.util.concurrent.CountDownLatch;
37
+ import java.util.concurrent.ExecutorService;
38
+ import java.util.concurrent.Executors;
37
39
  import java.util.concurrent.TimeUnit;
38
40
  import java.util.concurrent.atomic.AtomicBoolean;
39
41
  import java.util.concurrent.atomic.AtomicInteger;
@@ -42,88 +44,94 @@ import org.json.JSONArray;
42
44
  @CapacitorPlugin(name = "NativePurchases")
43
45
  public class NativePurchasesPlugin extends Plugin {
44
46
 
45
- private final String pluginVersion = "8.6.1";
47
+ private final String pluginVersion = "8.6.3";
46
48
  public static final String TAG = "NativePurchases";
47
- private static final int BILLING_CONNECTION_MAX_ATTEMPTS = 3;
48
- private static final long BILLING_SETUP_TIMEOUT_SECONDS = 10;
49
- private static final long[] BILLING_CONNECTION_BACKOFF_MS = { 0, 1000, 2000 };
49
+ private static final int BILLING_CONNECTION_MAX_ATTEMPTS = 2;
50
+ private static final long BILLING_SETUP_TIMEOUT_SECONDS = 5;
51
+ private static final long[] BILLING_CONNECTION_BACKOFF_MS = { 0, 1000 };
50
52
  private final Object billingClientLock = new Object();
53
+ private final ExecutorService billingExecutor = Executors.newSingleThreadExecutor((r) -> {
54
+ Thread thread = new Thread(r, "NativePurchases-Billing");
55
+ thread.setDaemon(true);
56
+ return thread;
57
+ });
51
58
  private BillingClient billingClient;
52
59
 
53
60
  @PluginMethod
54
61
  public void isBillingSupported(PluginCall call) {
55
62
  Log.d(TAG, "isBillingSupported() called");
56
- try {
57
- // Try to initialize billing client to check if billing is actually available
58
- // Pass null so initBillingClient doesn't reject the call - we'll handle the result ourselves
59
- this.initBillingClient(null);
60
- // If initialization succeeded, billing is supported
61
- JSObject ret = new JSObject();
62
- ret.put("isBillingSupported", true);
63
- Log.d(TAG, "isBillingSupported() returning true - billing client initialized successfully");
64
- closeBillingClient();
65
- call.resolve(ret);
66
- } catch (RuntimeException e) {
67
- Log.e(TAG, "isBillingSupported() - billing client initialization failed: " + e.getMessage());
68
- closeBillingClient();
69
- // Return false instead of rejecting - this is a check method
70
- JSObject ret = new JSObject();
71
- ret.put("isBillingSupported", false);
72
- Log.d(TAG, "isBillingSupported() returning false - billing not available");
73
- call.resolve(ret);
74
- } catch (Exception e) {
75
- Log.e(TAG, "isBillingSupported() - unexpected error: " + e.getMessage());
76
- closeBillingClient();
77
- JSObject ret = new JSObject();
78
- ret.put("isBillingSupported", false);
79
- Log.d(TAG, "isBillingSupported() returning false - unexpected error");
80
- call.resolve(ret);
81
- }
63
+ billingExecutor.execute(() -> {
64
+ try {
65
+ // Pass null so initBillingClient doesn't reject the call - we'll handle the result ourselves
66
+ this.initBillingClient(null);
67
+ JSObject ret = new JSObject();
68
+ ret.put("isBillingSupported", true);
69
+ Log.d(TAG, "isBillingSupported() returning true - billing client initialized successfully");
70
+ closeBillingClient();
71
+ call.resolve(ret);
72
+ } catch (RuntimeException e) {
73
+ Log.e(TAG, "isBillingSupported() - billing client initialization failed: " + e.getMessage());
74
+ closeBillingClient();
75
+ JSObject ret = new JSObject();
76
+ ret.put("isBillingSupported", false);
77
+ Log.d(TAG, "isBillingSupported() returning false - billing not available");
78
+ call.resolve(ret);
79
+ } catch (Exception e) {
80
+ Log.e(TAG, "isBillingSupported() - unexpected error: " + e.getMessage());
81
+ closeBillingClient();
82
+ JSObject ret = new JSObject();
83
+ ret.put("isBillingSupported", false);
84
+ Log.d(TAG, "isBillingSupported() returning false - unexpected error");
85
+ call.resolve(ret);
86
+ }
87
+ });
82
88
  }
83
89
 
84
90
  @PluginMethod
85
91
  public void getStorefront(PluginCall call) {
86
92
  Log.d(TAG, "getStorefront() called");
87
- try {
88
- // Pass null so initBillingClient doesn't reject the call - getStorefront
89
- // always resolves and reports an empty countryCode when the storefront
90
- // can't be determined, mirroring isBillingSupported() and the iOS side.
91
- this.initBillingClient(null);
92
- } catch (RuntimeException e) {
93
- Log.e(TAG, "getStorefront() - billing client init failed: " + e.getMessage());
94
- closeBillingClient();
95
- call.resolve(emptyStorefront());
96
- return;
97
- }
98
- try {
99
- billingClient.getBillingConfigAsync(
100
- GetBillingConfigParams.newBuilder().build(),
101
- new BillingConfigResponseListener() {
102
- @Override
103
- public void onBillingConfigResponse(@NonNull BillingResult billingResult, BillingConfig billingConfig) {
104
- JSObject ret = new JSObject();
105
- if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && billingConfig != null) {
106
- String countryCode = billingConfig.getCountryCode();
107
- Log.d(TAG, "getBillingConfig success, countryCode: " + countryCode);
108
- // JSObject.put(name, null) removes the key, so coalesce to "".
109
- ret.put("countryCode", countryCode != null ? countryCode : "");
110
- } else {
111
- Log.e(
112
- TAG,
113
- "getBillingConfig unavailable: " + billingResult.getResponseCode() + " - " + billingResult.getDebugMessage()
114
- );
115
- ret.put("countryCode", "");
93
+ billingExecutor.execute(() -> {
94
+ try {
95
+ // Pass null so initBillingClient doesn't reject the call - getStorefront
96
+ // always resolves and reports an empty countryCode when the storefront
97
+ // can't be determined, mirroring isBillingSupported() and the iOS side.
98
+ this.initBillingClient(null);
99
+ billingClient.getBillingConfigAsync(
100
+ GetBillingConfigParams.newBuilder().build(),
101
+ new BillingConfigResponseListener() {
102
+ @Override
103
+ public void onBillingConfigResponse(@NonNull BillingResult billingResult, BillingConfig billingConfig) {
104
+ JSObject ret = new JSObject();
105
+ if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && billingConfig != null) {
106
+ String countryCode = billingConfig.getCountryCode();
107
+ Log.d(TAG, "getBillingConfig success, countryCode: " + countryCode);
108
+ // JSObject.put(name, null) removes the key, so coalesce to "".
109
+ ret.put("countryCode", countryCode != null ? countryCode : "");
110
+ } else {
111
+ Log.e(
112
+ TAG,
113
+ "getBillingConfig unavailable: " +
114
+ billingResult.getResponseCode() +
115
+ " - " +
116
+ billingResult.getDebugMessage()
117
+ );
118
+ ret.put("countryCode", "");
119
+ }
120
+ closeBillingClient();
121
+ call.resolve(ret);
116
122
  }
117
- closeBillingClient();
118
- call.resolve(ret);
119
123
  }
120
- }
121
- );
122
- } catch (Exception e) {
123
- Log.e(TAG, "getBillingConfigAsync threw: " + e.getMessage());
124
- closeBillingClient();
125
- call.resolve(emptyStorefront());
126
- }
124
+ );
125
+ } catch (RuntimeException e) {
126
+ Log.e(TAG, "getStorefront() - billing client init failed: " + e.getMessage());
127
+ closeBillingClient();
128
+ call.resolve(emptyStorefront());
129
+ } catch (Exception e) {
130
+ Log.e(TAG, "getBillingConfigAsync threw: " + e.getMessage());
131
+ closeBillingClient();
132
+ call.resolve(emptyStorefront());
133
+ }
134
+ });
127
135
  }
128
136
 
129
137
  private JSObject emptyStorefront() {
@@ -140,6 +148,36 @@ public class NativePurchasesPlugin extends Plugin {
140
148
  Log.d(TAG, "Plugin load() completed");
141
149
  }
142
150
 
151
+ @FunctionalInterface
152
+ private interface BillingTask {
153
+ void run() throws Exception;
154
+ }
155
+
156
+ private void withBillingClient(PluginCall call, BillingTask task) {
157
+ billingExecutor.execute(() -> {
158
+ try {
159
+ initBillingClient(call);
160
+ task.run();
161
+ } catch (RuntimeException e) {
162
+ Log.e(TAG, "Failed to initialize billing client: " + e.getMessage());
163
+ closeBillingClient();
164
+ } catch (Exception e) {
165
+ Log.e(TAG, "Billing task failed: " + e.getMessage());
166
+ closeBillingClient();
167
+ if (call != null) {
168
+ call.reject(e.getMessage());
169
+ }
170
+ }
171
+ });
172
+ }
173
+
174
+ @Override
175
+ protected void handleOnDestroy() {
176
+ billingExecutor.shutdownNow();
177
+ closeBillingClient();
178
+ super.handleOnDestroy();
179
+ }
180
+
143
181
  private void closeBillingClient() {
144
182
  synchronized (billingClientLock) {
145
183
  closeBillingClientLocked();
@@ -427,14 +465,15 @@ public class NativePurchasesPlugin extends Plugin {
427
465
  @Override
428
466
  public void onBillingServiceDisconnected() {
429
467
  Log.d(TAG, "onBillingServiceDisconnected() called");
468
+ if (!setupSettled.compareAndSet(false, true)) {
469
+ Log.d(TAG, "Billing service disconnected after setup settled");
470
+ return;
471
+ }
430
472
  synchronized (billingClientLock) {
431
473
  if (billingClient == client) {
432
474
  billingClient = null;
433
475
  }
434
476
  }
435
- if (!setupSettled.compareAndSet(false, true)) {
436
- return;
437
- }
438
477
  setupError[0] = BillingResult.newBuilder()
439
478
  .setResponseCode(BillingClient.BillingResponseCode.SERVICE_DISCONNECTED)
440
479
  .setDebugMessage("Billing service disconnected during setup")
@@ -467,6 +506,13 @@ public class NativePurchasesPlugin extends Plugin {
467
506
  throw new RuntimeException(getBillingSetupErrorMessage(setupError[0]));
468
507
  }
469
508
 
509
+ synchronized (billingClientLock) {
510
+ if (billingClient != client || !client.isReady()) {
511
+ closeBillingClientLocked();
512
+ throw new RuntimeException("Billing service disconnected. Please try again.");
513
+ }
514
+ }
515
+
470
516
  Log.d(TAG, "Billing client setup completed successfully");
471
517
  } catch (InterruptedException e) {
472
518
  Thread.currentThread().interrupt();
@@ -561,15 +607,7 @@ public class NativePurchasesPlugin extends Plugin {
561
607
  );
562
608
  QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder().setProductList(productList).build();
563
609
  Log.d(TAG, "Initializing billing client for purchase");
564
- try {
565
- this.initBillingClient(call);
566
- } catch (RuntimeException e) {
567
- Log.e(TAG, "Failed to initialize billing client: " + e.getMessage());
568
- closeBillingClient();
569
- // Call already rejected in initBillingClient
570
- return;
571
- }
572
- try {
610
+ withBillingClient(call, () -> {
573
611
  Log.d(TAG, "Querying product details for purchase");
574
612
  billingClient.queryProductDetailsAsync(
575
613
  params,
@@ -637,11 +675,7 @@ public class NativePurchasesPlugin extends Plugin {
637
675
  }
638
676
  }
639
677
  );
640
- } catch (Exception e) {
641
- Log.d(TAG, "Exception during purchase: " + e.getMessage());
642
- closeBillingClient();
643
- call.reject(e.getMessage());
644
- }
678
+ });
645
679
  }
646
680
 
647
681
  private void processUnfinishedPurchases() {
@@ -705,17 +739,11 @@ public class NativePurchasesPlugin extends Plugin {
705
739
  public void restorePurchases(PluginCall call) {
706
740
  Log.d(TAG, "restorePurchases() called");
707
741
  Log.d(NativePurchasesPlugin.TAG, "restorePurchases");
708
- try {
709
- this.initBillingClient(call);
710
- } catch (RuntimeException e) {
711
- Log.e(TAG, "Failed to initialize billing client: " + e.getMessage());
712
- closeBillingClient();
713
- // Call already rejected in initBillingClient
714
- return;
715
- }
716
- this.processUnfinishedPurchases();
717
- call.resolve();
718
- Log.d(TAG, "restorePurchases() completed");
742
+ withBillingClient(call, () -> {
743
+ this.processUnfinishedPurchases();
744
+ call.resolve();
745
+ Log.d(TAG, "restorePurchases() completed");
746
+ });
719
747
  }
720
748
 
721
749
  private void querySingleProductDetails(String productIdentifier, String productType, PluginCall call) {
@@ -733,15 +761,7 @@ public class NativePurchasesPlugin extends Plugin {
733
761
 
734
762
  QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder().setProductList(productList).build();
735
763
  Log.d(TAG, "Initializing billing client for single product query");
736
- try {
737
- this.initBillingClient(call);
738
- } catch (RuntimeException e) {
739
- Log.e(TAG, "Failed to initialize billing client: " + e.getMessage());
740
- closeBillingClient();
741
- // Call already rejected in initBillingClient
742
- return;
743
- }
744
- try {
764
+ withBillingClient(call, () -> {
745
765
  Log.d(TAG, "Querying product details");
746
766
  billingClient.queryProductDetailsAsync(
747
767
  params,
@@ -847,11 +867,7 @@ public class NativePurchasesPlugin extends Plugin {
847
867
  }
848
868
  }
849
869
  );
850
- } catch (Exception e) {
851
- Log.d(TAG, "Exception during single product query: " + e.getMessage());
852
- closeBillingClient();
853
- call.reject(e.getMessage());
854
- }
870
+ });
855
871
  }
856
872
 
857
873
  private void queryProductDetails(List<String> productIdentifiers, String productType, PluginCall call) {
@@ -873,15 +889,7 @@ public class NativePurchasesPlugin extends Plugin {
873
889
  Log.d(TAG, "Total products in query list: " + productList.size());
874
890
  QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder().setProductList(productList).build();
875
891
  Log.d(TAG, "Initializing billing client for product query");
876
- try {
877
- this.initBillingClient(call);
878
- } catch (RuntimeException e) {
879
- Log.e(TAG, "Failed to initialize billing client: " + e.getMessage());
880
- closeBillingClient();
881
- // Call already rejected in initBillingClient
882
- return;
883
- }
884
- try {
892
+ withBillingClient(call, () -> {
885
893
  Log.d(TAG, "Querying product details");
886
894
  billingClient.queryProductDetailsAsync(
887
895
  params,
@@ -998,11 +1006,7 @@ public class NativePurchasesPlugin extends Plugin {
998
1006
  }
999
1007
  }
1000
1008
  );
1001
- } catch (Exception e) {
1002
- Log.d(TAG, "Exception during product query: " + e.getMessage());
1003
- closeBillingClient();
1004
- call.reject(e.getMessage());
1005
- }
1009
+ });
1006
1010
  }
1007
1011
 
1008
1012
  @PluginMethod
@@ -1069,32 +1073,23 @@ public class NativePurchasesPlugin extends Plugin {
1069
1073
  return;
1070
1074
  }
1071
1075
 
1072
- try {
1073
- this.initBillingClient(call);
1074
- } catch (RuntimeException e) {
1075
- Log.e(TAG, "Failed to initialize billing client: " + e.getMessage());
1076
- closeBillingClient();
1077
- // Call already rejected in initBillingClient
1078
- return;
1079
- }
1080
-
1081
- JSONArray allPurchases = new JSONArray();
1082
- AtomicInteger pendingQueries = new AtomicInteger((queryInApp ? 1 : 0) + (querySubs ? 1 : 0));
1083
- AtomicBoolean finished = new AtomicBoolean(false);
1084
-
1085
- Runnable maybeFinish = () -> {
1086
- int remaining = pendingQueries.decrementAndGet();
1087
- Log.d(TAG, "Pending purchase queries remaining: " + remaining);
1088
- if (remaining <= 0 && finished.compareAndSet(false, true)) {
1089
- JSObject result = new JSObject();
1090
- result.put("purchases", allPurchases);
1091
- Log.d(TAG, "Returning " + allPurchases.length() + " purchases");
1092
- closeBillingClient();
1093
- call.resolve(result);
1094
- }
1095
- };
1076
+ withBillingClient(call, () -> {
1077
+ JSONArray allPurchases = new JSONArray();
1078
+ AtomicInteger pendingQueries = new AtomicInteger((queryInApp ? 1 : 0) + (querySubs ? 1 : 0));
1079
+ AtomicBoolean finished = new AtomicBoolean(false);
1080
+
1081
+ Runnable maybeFinish = () -> {
1082
+ int remaining = pendingQueries.decrementAndGet();
1083
+ Log.d(TAG, "Pending purchase queries remaining: " + remaining);
1084
+ if (remaining <= 0 && finished.compareAndSet(false, true)) {
1085
+ JSObject result = new JSObject();
1086
+ result.put("purchases", allPurchases);
1087
+ Log.d(TAG, "Returning " + allPurchases.length() + " purchases");
1088
+ closeBillingClient();
1089
+ call.resolve(result);
1090
+ }
1091
+ };
1096
1092
 
1097
- try {
1098
1093
  if (queryInApp) {
1099
1094
  Log.d(TAG, "Querying in-app purchases");
1100
1095
  QueryPurchasesParams queryInAppParams = QueryPurchasesParams.newBuilder()
@@ -1200,12 +1195,7 @@ public class NativePurchasesPlugin extends Plugin {
1200
1195
  }
1201
1196
  });
1202
1197
  }
1203
- } catch (Exception e) {
1204
- Log.d(TAG, "Exception during getPurchases: " + e.getMessage());
1205
- finished.set(true);
1206
- closeBillingClient();
1207
- call.reject(e.getMessage());
1208
- }
1198
+ });
1209
1199
  }
1210
1200
 
1211
1201
  @PluginMethod
@@ -1247,16 +1237,7 @@ public class NativePurchasesPlugin extends Plugin {
1247
1237
  }
1248
1238
 
1249
1239
  Log.d(TAG, "Manually acknowledging purchase with token: " + purchaseToken);
1250
- try {
1251
- this.initBillingClient(call);
1252
- } catch (RuntimeException e) {
1253
- Log.e(TAG, "Failed to initialize billing client: " + e.getMessage());
1254
- closeBillingClient();
1255
- // Call already rejected in initBillingClient
1256
- return;
1257
- }
1258
-
1259
- try {
1240
+ withBillingClient(call, () -> {
1260
1241
  AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder()
1261
1242
  .setPurchaseToken(purchaseToken)
1262
1243
  .build();
@@ -1281,11 +1262,7 @@ public class NativePurchasesPlugin extends Plugin {
1281
1262
  }
1282
1263
  }
1283
1264
  );
1284
- } catch (Exception e) {
1285
- Log.d(TAG, "Exception during acknowledgePurchase: " + e.getMessage());
1286
- closeBillingClient();
1287
- call.reject(e.getMessage());
1288
- }
1265
+ });
1289
1266
  }
1290
1267
 
1291
1268
  @PluginMethod
@@ -1300,15 +1277,7 @@ public class NativePurchasesPlugin extends Plugin {
1300
1277
  }
1301
1278
 
1302
1279
  Log.d(TAG, "Consuming purchase with token: " + purchaseToken);
1303
- try {
1304
- this.initBillingClient(call);
1305
- } catch (RuntimeException e) {
1306
- Log.e(TAG, "Failed to initialize billing client: " + e.getMessage());
1307
- closeBillingClient();
1308
- return;
1309
- }
1310
-
1311
- try {
1280
+ withBillingClient(call, () -> {
1312
1281
  ConsumeParams consumeParams = ConsumeParams.newBuilder().setPurchaseToken(purchaseToken).build();
1313
1282
 
1314
1283
  billingClient.consumeAsync(consumeParams, (billingResult, consumedToken) -> {
@@ -1325,11 +1294,7 @@ public class NativePurchasesPlugin extends Plugin {
1325
1294
  call.reject("Failed to consume purchase: " + billingResult.getDebugMessage());
1326
1295
  }
1327
1296
  });
1328
- } catch (Exception e) {
1329
- Log.d(TAG, "Exception during consumePurchase: " + e.getMessage());
1330
- closeBillingClient();
1331
- call.reject(e.getMessage());
1332
- }
1297
+ });
1333
1298
  }
1334
1299
 
1335
1300
  @PluginMethod
@@ -8,7 +8,7 @@ enum PurchaseAction {
8
8
  NONE
9
9
  }
10
10
 
11
- final class PurchaseActionDecider {
11
+ public final class PurchaseActionDecider {
12
12
 
13
13
  private PurchaseActionDecider() {}
14
14
 
@@ -23,7 +23,7 @@ public class NativePurchasesPlugin: CAPPlugin, CAPBridgedPlugin {
23
23
  CAPPluginMethod(name: "getStorefront", returnType: CAPPluginReturnPromise)
24
24
  ]
25
25
 
26
- private let pluginVersion: String = "8.6.1"
26
+ private let pluginVersion: String = "8.6.3"
27
27
  private var transactionUpdatesTask: Task<Void, Never>?
28
28
 
29
29
  @objc func getPluginVersion(_ call: CAPPluginCall) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/native-purchases",
3
- "version": "8.6.1",
3
+ "version": "8.6.3",
4
4
  "description": "In-app Subscriptions Made Easy",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",