@capgo/native-purchases 0.0.31 → 0.0.34

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.
@@ -27,11 +27,17 @@ import java.util.List;
27
27
  import java.util.concurrent.CountDownLatch;
28
28
  import org.json.JSONArray;
29
29
  import org.json.JSONException;
30
+ import java.util.concurrent.TimeoutException;
31
+ import java.util.concurrent.Phaser;
32
+ import java.util.concurrent.Semaphore;
33
+ import java.util.concurrent.TimeUnit;
30
34
 
31
35
  @CapacitorPlugin(name = "NativePurchases")
32
36
  public class NativePurchasesPlugin extends Plugin {
33
37
 
34
38
  public final String PLUGIN_VERSION = "0.0.25";
39
+ public static final String TAG = "NativePurchases";
40
+ private static final Phaser semaphoreReady = new Phaser(1);
35
41
  private BillingClient billingClient;
36
42
 
37
43
  private void isBillingSupported(PluginCall call) {
@@ -40,6 +46,59 @@ public class NativePurchasesPlugin extends Plugin {
40
46
  call.resolve();
41
47
  }
42
48
 
49
+ @Override
50
+ public void load() {
51
+ super.load();
52
+ Log.i(NativePurchasesPlugin.TAG, "load");
53
+ semaphoreDown();
54
+ }
55
+
56
+ private void semaphoreWait(Number waitTime) {
57
+ Log.i(NativePurchasesPlugin.TAG, "semaphoreWait " + waitTime);
58
+ try {
59
+ // Log.i(CapacitorUpdater.TAG, "semaphoreReady count " + CapacitorUpdaterPlugin.this.semaphoreReady.getCount());
60
+ NativePurchasesPlugin.this.semaphoreReady.awaitAdvanceInterruptibly(
61
+ NativePurchasesPlugin.this.semaphoreReady.getPhase(),
62
+ waitTime.longValue(),
63
+ TimeUnit.SECONDS
64
+ );
65
+ // Log.i(CapacitorUpdater.TAG, "semaphoreReady await " + res);
66
+ Log.i(
67
+ NativePurchasesPlugin.TAG,
68
+ "semaphoreReady count " +
69
+ NativePurchasesPlugin.this.semaphoreReady.getPhase()
70
+ );
71
+ } catch (InterruptedException e) {
72
+ Log.i(NativePurchasesPlugin.TAG, "semaphoreWait InterruptedException");
73
+ e.printStackTrace();
74
+ } catch (TimeoutException e) {
75
+ throw new RuntimeException(e);
76
+ }
77
+ }
78
+
79
+ private void semaphoreUp() {
80
+ Log.i(NativePurchasesPlugin.TAG, "semaphoreUp");
81
+ NativePurchasesPlugin.this.semaphoreReady.register();
82
+ }
83
+
84
+ private void semaphoreDown() {
85
+ Log.i(NativePurchasesPlugin.TAG, "semaphoreDown");
86
+ Log.i(
87
+ NativePurchasesPlugin.TAG,
88
+ "semaphoreDown count " +
89
+ NativePurchasesPlugin.this.semaphoreReady.getPhase()
90
+ );
91
+ NativePurchasesPlugin.this.semaphoreReady.arriveAndDeregister();
92
+ }
93
+
94
+ private void closeBillingClient() {
95
+ if (billingClient != null) {
96
+ billingClient.endConnection();
97
+ billingClient = null;
98
+ semaphoreDown();
99
+ }
100
+ }
101
+
43
102
  private void handlePurchase(Purchase purchase, PluginCall purchaseCall) {
44
103
  if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
45
104
  // Grant entitlement to the user, then acknowledge the purchase
@@ -78,7 +137,7 @@ public class NativePurchasesPlugin extends Plugin {
78
137
  public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
79
138
  // Handle the result of the acknowledge purchase
80
139
  Log.i(
81
- "NativePurchases",
140
+ NativePurchasesPlugin.TAG,
82
141
  "onAcknowledgePurchaseResponse" + billingResult
83
142
  );
84
143
  }
@@ -87,10 +146,9 @@ public class NativePurchasesPlugin extends Plugin {
87
146
  }
88
147
 
89
148
  private void initBillingClient(PluginCall purchaseCall) {
90
- if (billingClient != null) {
91
- billingClient.endConnection();
92
- billingClient = null;
93
- }
149
+ semaphoreWait(10);
150
+ closeBillingClient();
151
+ semaphoreUp();
94
152
  CountDownLatch semaphoreReady = new CountDownLatch(1);
95
153
  billingClient =
96
154
  BillingClient
@@ -102,7 +160,7 @@ public class NativePurchasesPlugin extends Plugin {
102
160
  BillingResult billingResult,
103
161
  List<Purchase> purchases
104
162
  ) {
105
- Log.i("NativePurchases", "onPurchasesUpdated" + billingResult);
163
+ Log.i(NativePurchasesPlugin.TAG, "onPurchasesUpdated" + billingResult);
106
164
  if (
107
165
  billingResult.getResponseCode() ==
108
166
  BillingClient.BillingResponseCode.OK &&
@@ -114,13 +172,12 @@ public class NativePurchasesPlugin extends Plugin {
114
172
  handlePurchase(purchases.get(0), purchaseCall);
115
173
  } else {
116
174
  // Handle any other error codes.
117
- Log.i("NativePurchases", "onPurchasesUpdated" + billingResult);
175
+ Log.i(NativePurchasesPlugin.TAG, "onPurchasesUpdated" + billingResult);
118
176
  if (purchaseCall != null) {
119
177
  purchaseCall.reject("Purchase is not purchased");
120
178
  }
121
179
  }
122
- billingClient.endConnection();
123
- billingClient = null;
180
+ closeBillingClient();
124
181
  return;
125
182
  }
126
183
  }
@@ -218,8 +275,7 @@ public class NativePurchasesPlugin extends Plugin {
218
275
  List<ProductDetails> productDetailsList
219
276
  ) {
220
277
  if (productDetailsList.size() == 0) {
221
- billingClient.endConnection();
222
- billingClient = null;
278
+ closeBillingClient();
223
279
  call.reject("Product not found");
224
280
  return;
225
281
  }
@@ -259,22 +315,21 @@ public class NativePurchasesPlugin extends Plugin {
259
315
  billingFlowParams
260
316
  );
261
317
  Log.i(
262
- "NativePurchases",
318
+ NativePurchasesPlugin.TAG,
263
319
  "onProductDetailsResponse2" + billingResult2
264
320
  );
265
321
  }
266
322
  }
267
323
  );
268
324
  } catch (Exception e) {
269
- billingClient.endConnection();
270
- billingClient = null;
325
+ closeBillingClient();
271
326
  call.reject(e.getMessage());
272
327
  }
273
328
  }
274
329
 
275
330
  @PluginMethod
276
331
  public void restorePurchases(PluginCall call) {
277
- Log.d("NativePurchases", "restorePurchases");
332
+ Log.d(NativePurchasesPlugin.TAG, "restorePurchases");
278
333
  this.initBillingClient(null);
279
334
  call.resolve();
280
335
  }
@@ -305,7 +360,7 @@ public class NativePurchasesPlugin extends Plugin {
305
360
  }
306
361
  }
307
362
 
308
- Log.d("NativePurchases", "getProducts: " + productIdentifiers);
363
+ Log.d(NativePurchasesPlugin.TAG, "getProducts: " + productIdentifiers);
309
364
  List<QueryProductDetailsParams.Product> productList = new ArrayList<>();
310
365
  for (String productIdentifier : productIdentifiers) {
311
366
  productList.add(
@@ -336,13 +391,12 @@ public class NativePurchasesPlugin extends Plugin {
336
391
  List<ProductDetails> productDetailsList
337
392
  ) {
338
393
  if (productDetailsList.size() == 0) {
339
- billingClient.endConnection();
340
- billingClient = null;
394
+ closeBillingClient();
341
395
  call.reject("Product not found");
342
396
  return;
343
397
  }
344
398
  Log.i(
345
- "NativePurchases",
399
+ NativePurchasesPlugin.TAG,
346
400
  "onProductDetailsResponse" + billingResult + productDetailsList
347
401
  );
348
402
  // Process the result
@@ -386,7 +440,7 @@ public class NativePurchasesPlugin extends Plugin {
386
440
  for (ProductDetails.SubscriptionOfferDetails offerDetails : productDetails.getSubscriptionOfferDetails()) {
387
441
  if (
388
442
  offerDetails
389
- .getOfferId()
443
+ .getBasePlanId()
390
444
  .equals(subscriptionOfferIdentifier)
391
445
  ) {
392
446
  selectedOfferDetails = offerDetails;
@@ -428,15 +482,13 @@ public class NativePurchasesPlugin extends Plugin {
428
482
  products.put(product);
429
483
  }
430
484
  ret.put("products", products);
431
- billingClient.endConnection();
432
- billingClient = null;
485
+ closeBillingClient();
433
486
  call.resolve(ret);
434
487
  }
435
488
  }
436
489
  );
437
490
  } catch (Exception e) {
438
- billingClient.endConnection();
439
- billingClient = null;
491
+ closeBillingClient();
440
492
  call.reject(e.getMessage());
441
493
  }
442
494
  }
@@ -82,6 +82,10 @@ public class NativePurchasesPlugin: CAPPlugin {
82
82
  Task {
83
83
  do {
84
84
  try await AppStore.sync()
85
+ // make finish() calls for all transactions
86
+ // for transaction in AppStore.transactions {
87
+ // await transaction.finish()
88
+ // }
85
89
  call.resolve()
86
90
  } catch {
87
91
  call.reject(error.localizedDescription)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/native-purchases",
3
- "version": "0.0.31",
3
+ "version": "0.0.34",
4
4
  "description": "In-app Subscriptions Made Easy",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",