@capgo/native-purchases 8.6.2 → 8.6.4

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.2";
47
+ private final String pluginVersion = "8.6.4";
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();
@@ -569,15 +607,7 @@ public class NativePurchasesPlugin extends Plugin {
569
607
  );
570
608
  QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder().setProductList(productList).build();
571
609
  Log.d(TAG, "Initializing billing client for purchase");
572
- try {
573
- this.initBillingClient(call);
574
- } catch (RuntimeException e) {
575
- Log.e(TAG, "Failed to initialize billing client: " + e.getMessage());
576
- closeBillingClient();
577
- // Call already rejected in initBillingClient
578
- return;
579
- }
580
- try {
610
+ withBillingClient(call, () -> {
581
611
  Log.d(TAG, "Querying product details for purchase");
582
612
  billingClient.queryProductDetailsAsync(
583
613
  params,
@@ -645,11 +675,7 @@ public class NativePurchasesPlugin extends Plugin {
645
675
  }
646
676
  }
647
677
  );
648
- } catch (Exception e) {
649
- Log.d(TAG, "Exception during purchase: " + e.getMessage());
650
- closeBillingClient();
651
- call.reject(e.getMessage());
652
- }
678
+ });
653
679
  }
654
680
 
655
681
  private void processUnfinishedPurchases() {
@@ -713,17 +739,11 @@ public class NativePurchasesPlugin extends Plugin {
713
739
  public void restorePurchases(PluginCall call) {
714
740
  Log.d(TAG, "restorePurchases() called");
715
741
  Log.d(NativePurchasesPlugin.TAG, "restorePurchases");
716
- try {
717
- this.initBillingClient(call);
718
- } catch (RuntimeException e) {
719
- Log.e(TAG, "Failed to initialize billing client: " + e.getMessage());
720
- closeBillingClient();
721
- // Call already rejected in initBillingClient
722
- return;
723
- }
724
- this.processUnfinishedPurchases();
725
- call.resolve();
726
- Log.d(TAG, "restorePurchases() completed");
742
+ withBillingClient(call, () -> {
743
+ this.processUnfinishedPurchases();
744
+ call.resolve();
745
+ Log.d(TAG, "restorePurchases() completed");
746
+ });
727
747
  }
728
748
 
729
749
  private void querySingleProductDetails(String productIdentifier, String productType, PluginCall call) {
@@ -741,15 +761,7 @@ public class NativePurchasesPlugin extends Plugin {
741
761
 
742
762
  QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder().setProductList(productList).build();
743
763
  Log.d(TAG, "Initializing billing client for single product query");
744
- try {
745
- this.initBillingClient(call);
746
- } catch (RuntimeException e) {
747
- Log.e(TAG, "Failed to initialize billing client: " + e.getMessage());
748
- closeBillingClient();
749
- // Call already rejected in initBillingClient
750
- return;
751
- }
752
- try {
764
+ withBillingClient(call, () -> {
753
765
  Log.d(TAG, "Querying product details");
754
766
  billingClient.queryProductDetailsAsync(
755
767
  params,
@@ -855,11 +867,7 @@ public class NativePurchasesPlugin extends Plugin {
855
867
  }
856
868
  }
857
869
  );
858
- } catch (Exception e) {
859
- Log.d(TAG, "Exception during single product query: " + e.getMessage());
860
- closeBillingClient();
861
- call.reject(e.getMessage());
862
- }
870
+ });
863
871
  }
864
872
 
865
873
  private void queryProductDetails(List<String> productIdentifiers, String productType, PluginCall call) {
@@ -881,15 +889,7 @@ public class NativePurchasesPlugin extends Plugin {
881
889
  Log.d(TAG, "Total products in query list: " + productList.size());
882
890
  QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder().setProductList(productList).build();
883
891
  Log.d(TAG, "Initializing billing client for product query");
884
- try {
885
- this.initBillingClient(call);
886
- } catch (RuntimeException e) {
887
- Log.e(TAG, "Failed to initialize billing client: " + e.getMessage());
888
- closeBillingClient();
889
- // Call already rejected in initBillingClient
890
- return;
891
- }
892
- try {
892
+ withBillingClient(call, () -> {
893
893
  Log.d(TAG, "Querying product details");
894
894
  billingClient.queryProductDetailsAsync(
895
895
  params,
@@ -1006,11 +1006,7 @@ public class NativePurchasesPlugin extends Plugin {
1006
1006
  }
1007
1007
  }
1008
1008
  );
1009
- } catch (Exception e) {
1010
- Log.d(TAG, "Exception during product query: " + e.getMessage());
1011
- closeBillingClient();
1012
- call.reject(e.getMessage());
1013
- }
1009
+ });
1014
1010
  }
1015
1011
 
1016
1012
  @PluginMethod
@@ -1077,32 +1073,23 @@ public class NativePurchasesPlugin extends Plugin {
1077
1073
  return;
1078
1074
  }
1079
1075
 
1080
- try {
1081
- this.initBillingClient(call);
1082
- } catch (RuntimeException e) {
1083
- Log.e(TAG, "Failed to initialize billing client: " + e.getMessage());
1084
- closeBillingClient();
1085
- // Call already rejected in initBillingClient
1086
- return;
1087
- }
1088
-
1089
- JSONArray allPurchases = new JSONArray();
1090
- AtomicInteger pendingQueries = new AtomicInteger((queryInApp ? 1 : 0) + (querySubs ? 1 : 0));
1091
- AtomicBoolean finished = new AtomicBoolean(false);
1092
-
1093
- Runnable maybeFinish = () -> {
1094
- int remaining = pendingQueries.decrementAndGet();
1095
- Log.d(TAG, "Pending purchase queries remaining: " + remaining);
1096
- if (remaining <= 0 && finished.compareAndSet(false, true)) {
1097
- JSObject result = new JSObject();
1098
- result.put("purchases", allPurchases);
1099
- Log.d(TAG, "Returning " + allPurchases.length() + " purchases");
1100
- closeBillingClient();
1101
- call.resolve(result);
1102
- }
1103
- };
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
+ };
1104
1092
 
1105
- try {
1106
1093
  if (queryInApp) {
1107
1094
  Log.d(TAG, "Querying in-app purchases");
1108
1095
  QueryPurchasesParams queryInAppParams = QueryPurchasesParams.newBuilder()
@@ -1208,12 +1195,7 @@ public class NativePurchasesPlugin extends Plugin {
1208
1195
  }
1209
1196
  });
1210
1197
  }
1211
- } catch (Exception e) {
1212
- Log.d(TAG, "Exception during getPurchases: " + e.getMessage());
1213
- finished.set(true);
1214
- closeBillingClient();
1215
- call.reject(e.getMessage());
1216
- }
1198
+ });
1217
1199
  }
1218
1200
 
1219
1201
  @PluginMethod
@@ -1255,16 +1237,7 @@ public class NativePurchasesPlugin extends Plugin {
1255
1237
  }
1256
1238
 
1257
1239
  Log.d(TAG, "Manually acknowledging purchase with token: " + purchaseToken);
1258
- try {
1259
- this.initBillingClient(call);
1260
- } catch (RuntimeException e) {
1261
- Log.e(TAG, "Failed to initialize billing client: " + e.getMessage());
1262
- closeBillingClient();
1263
- // Call already rejected in initBillingClient
1264
- return;
1265
- }
1266
-
1267
- try {
1240
+ withBillingClient(call, () -> {
1268
1241
  AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder()
1269
1242
  .setPurchaseToken(purchaseToken)
1270
1243
  .build();
@@ -1289,11 +1262,7 @@ public class NativePurchasesPlugin extends Plugin {
1289
1262
  }
1290
1263
  }
1291
1264
  );
1292
- } catch (Exception e) {
1293
- Log.d(TAG, "Exception during acknowledgePurchase: " + e.getMessage());
1294
- closeBillingClient();
1295
- call.reject(e.getMessage());
1296
- }
1265
+ });
1297
1266
  }
1298
1267
 
1299
1268
  @PluginMethod
@@ -1308,15 +1277,7 @@ public class NativePurchasesPlugin extends Plugin {
1308
1277
  }
1309
1278
 
1310
1279
  Log.d(TAG, "Consuming purchase with token: " + purchaseToken);
1311
- try {
1312
- this.initBillingClient(call);
1313
- } catch (RuntimeException e) {
1314
- Log.e(TAG, "Failed to initialize billing client: " + e.getMessage());
1315
- closeBillingClient();
1316
- return;
1317
- }
1318
-
1319
- try {
1280
+ withBillingClient(call, () -> {
1320
1281
  ConsumeParams consumeParams = ConsumeParams.newBuilder().setPurchaseToken(purchaseToken).build();
1321
1282
 
1322
1283
  billingClient.consumeAsync(consumeParams, (billingResult, consumedToken) -> {
@@ -1333,11 +1294,7 @@ public class NativePurchasesPlugin extends Plugin {
1333
1294
  call.reject("Failed to consume purchase: " + billingResult.getDebugMessage());
1334
1295
  }
1335
1296
  });
1336
- } catch (Exception e) {
1337
- Log.d(TAG, "Exception during consumePurchase: " + e.getMessage());
1338
- closeBillingClient();
1339
- call.reject(e.getMessage());
1340
- }
1297
+ });
1341
1298
  }
1342
1299
 
1343
1300
  @PluginMethod
@@ -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.2"
26
+ private let pluginVersion: String = "8.6.4"
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.2",
3
+ "version": "8.6.4",
4
4
  "description": "In-app Subscriptions Made Easy",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -51,7 +51,8 @@
51
51
  "prepublishOnly": "bun run build",
52
52
  "check:wiring": "node scripts/check-capacitor-plugin-wiring.mjs",
53
53
  "example:install": "cd example-app && bun install --frozen-lockfile",
54
- "example:build": "bun run build && cd example-app && bun install --frozen-lockfile && bun run build"
54
+ "example:build": "bun run build && cd example-app && bun install --frozen-lockfile && bun run build",
55
+ "changelog:ai": "node scripts/generate-ai-changelog.mjs"
55
56
  },
56
57
  "devDependencies": {
57
58
  "@capacitor/android": "^8.0.0",