@capgo/native-purchases 8.9.13 → 8.9.15
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.
- package/android/src/main/java/ee/forgr/nativepurchases/NativePurchasesPlugin.java +467 -145
- package/ios/Sources/NativePurchasesPlugin/NativePurchasesPlugin.swift +2 -4
- package/ios/Sources/NativePurchasesPlugin/TransactionHelpers.swift +18 -0
- package/ios/Tests/NativePurchasesPluginTests/BuildNumberComparisonTests.swift +30 -0
- package/package.json +1 -1
|
@@ -14,11 +14,13 @@ import com.android.billingclient.api.BillingConfigResponseListener;
|
|
|
14
14
|
import com.android.billingclient.api.BillingFlowParams;
|
|
15
15
|
import com.android.billingclient.api.BillingResult;
|
|
16
16
|
import com.android.billingclient.api.ConsumeParams;
|
|
17
|
+
import com.android.billingclient.api.ConsumeResponseListener;
|
|
17
18
|
import com.android.billingclient.api.GetBillingConfigParams;
|
|
18
19
|
import com.android.billingclient.api.PendingPurchasesParams;
|
|
19
20
|
import com.android.billingclient.api.ProductDetails;
|
|
20
21
|
import com.android.billingclient.api.ProductDetailsResponseListener;
|
|
21
22
|
import com.android.billingclient.api.Purchase;
|
|
23
|
+
import com.android.billingclient.api.PurchasesResponseListener;
|
|
22
24
|
import com.android.billingclient.api.PurchasesUpdatedListener;
|
|
23
25
|
import com.android.billingclient.api.QueryProductDetailsParams;
|
|
24
26
|
import com.android.billingclient.api.QueryProductDetailsResult;
|
|
@@ -30,7 +32,10 @@ import com.getcapacitor.PluginMethod;
|
|
|
30
32
|
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
31
33
|
import java.util.ArrayList;
|
|
32
34
|
import java.util.Collections;
|
|
35
|
+
import java.util.IdentityHashMap;
|
|
33
36
|
import java.util.List;
|
|
37
|
+
import java.util.Map;
|
|
38
|
+
import java.util.Set;
|
|
34
39
|
import java.util.concurrent.CountDownLatch;
|
|
35
40
|
import java.util.concurrent.ExecutorService;
|
|
36
41
|
import java.util.concurrent.Executors;
|
|
@@ -38,6 +43,7 @@ import java.util.concurrent.TimeUnit;
|
|
|
38
43
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
39
44
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
40
45
|
import java.util.concurrent.atomic.AtomicReference;
|
|
46
|
+
import java.util.function.Consumer;
|
|
41
47
|
import org.json.JSONArray;
|
|
42
48
|
|
|
43
49
|
@CapacitorPlugin(name = "NativePurchases")
|
|
@@ -103,7 +109,7 @@ public class NativePurchasesPlugin extends Plugin {
|
|
|
103
109
|
return SubscriptionOfferPurchaseResult.ok(selected.getOfferToken(), selected.getBasePlanId());
|
|
104
110
|
}
|
|
105
111
|
|
|
106
|
-
private final String pluginVersion = "8.9.
|
|
112
|
+
private final String pluginVersion = "8.9.15";
|
|
107
113
|
public static final String TAG = "NativePurchases";
|
|
108
114
|
private static final int BILLING_CONNECTION_MAX_ATTEMPTS = 2;
|
|
109
115
|
private static final long BILLING_SETUP_TIMEOUT_SECONDS = 5;
|
|
@@ -114,7 +120,29 @@ public class NativePurchasesPlugin extends Plugin {
|
|
|
114
120
|
thread.setDaemon(true);
|
|
115
121
|
return thread;
|
|
116
122
|
});
|
|
117
|
-
|
|
123
|
+
BillingClient billingClient;
|
|
124
|
+
/**
|
|
125
|
+
* Number of auto acknowledge or consume operations in flight per client. Guarded by
|
|
126
|
+
* {@link #billingClientLock}. endConnection() drops queued work, so such a client is ended only
|
|
127
|
+
* by its last completion. A later call first waits for these to finish (see
|
|
128
|
+
* {@link #awaitPendingFinishes()}) so only one client is normally active.
|
|
129
|
+
*/
|
|
130
|
+
private final Map<BillingClient, Integer> pendingFinishes = new IdentityHashMap<>();
|
|
131
|
+
/**
|
|
132
|
+
* Run when a client's pending finishes are given up after the wait, so a purchase call waiting on
|
|
133
|
+
* one still settles. Guarded by {@link #billingClientLock}.
|
|
134
|
+
*/
|
|
135
|
+
private final Map<BillingClient, List<Runnable>> abandonedFinishHandlers = new IdentityHashMap<>();
|
|
136
|
+
/**
|
|
137
|
+
* Clients a later call stopped waiting on. Their counts in {@link #pendingFinishes} stay real, so
|
|
138
|
+
* they still end only when their last finish answers; the wait just ignores them. Guarded by
|
|
139
|
+
* {@link #billingClientLock}.
|
|
140
|
+
*/
|
|
141
|
+
private final Set<BillingClient> setAsideClients = Collections.newSetFromMap(new IdentityHashMap<>());
|
|
142
|
+
/** How long a new billing call waits for an in flight acknowledge or consume before it proceeds. */
|
|
143
|
+
private static final long PENDING_FINISH_WAIT_MS = 10_000;
|
|
144
|
+
/** Package-private so tests can shorten the wait. */
|
|
145
|
+
long pendingFinishWaitMs = PENDING_FINISH_WAIT_MS;
|
|
118
146
|
private volatile boolean autoFinishTransactions = true;
|
|
119
147
|
|
|
120
148
|
@PluginMethod
|
|
@@ -365,16 +393,155 @@ public class NativePurchasesPlugin extends Plugin {
|
|
|
365
393
|
@Override
|
|
366
394
|
protected void handleOnDestroy() {
|
|
367
395
|
billingExecutor.shutdownNow();
|
|
396
|
+
List<BillingClient> detached = new ArrayList<>();
|
|
397
|
+
synchronized (billingClientLock) {
|
|
398
|
+
// Clients still finishing work, including set aside ones, would otherwise never be ended.
|
|
399
|
+
for (BillingClient client : pendingFinishes.keySet()) {
|
|
400
|
+
if (client != billingClient) {
|
|
401
|
+
detached.add(client);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
pendingFinishes.clear();
|
|
405
|
+
abandonedFinishHandlers.clear();
|
|
406
|
+
setAsideClients.clear();
|
|
407
|
+
}
|
|
408
|
+
for (BillingClient client : detached) {
|
|
409
|
+
client.endConnection();
|
|
410
|
+
}
|
|
368
411
|
closeBillingClient();
|
|
369
412
|
super.handleOnDestroy();
|
|
370
413
|
}
|
|
371
414
|
|
|
372
|
-
private
|
|
415
|
+
/** Package-private so tests can stand in for a later call that replaces the client. */
|
|
416
|
+
void closeBillingClient() {
|
|
373
417
|
synchronized (billingClientLock) {
|
|
374
418
|
closeBillingClientLocked();
|
|
375
419
|
}
|
|
376
420
|
}
|
|
377
421
|
|
|
422
|
+
/** Closes {@code client} only if no later call has replaced it, so a late callback cannot end another call's connection. */
|
|
423
|
+
private void closeBillingClientIfCurrent(BillingClient client) {
|
|
424
|
+
synchronized (billingClientLock) {
|
|
425
|
+
if (billingClient == client) {
|
|
426
|
+
closeBillingClientLocked();
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Marks {@code client} as finishing a purchase so later calls replace it without ending it.
|
|
433
|
+
*
|
|
434
|
+
* @return false when the client is already closed, in which case nothing can run on it.
|
|
435
|
+
*/
|
|
436
|
+
private boolean beginFinishingPurchase(BillingClient client) {
|
|
437
|
+
return beginFinishingPurchase(client, null);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/** @param onAbandon run if this finish is given up after the wait; may be null. */
|
|
441
|
+
private boolean beginFinishingPurchase(BillingClient client, Runnable onAbandon) {
|
|
442
|
+
synchronized (billingClientLock) {
|
|
443
|
+
if (client == null || !client.isReady()) {
|
|
444
|
+
return false;
|
|
445
|
+
}
|
|
446
|
+
pendingFinishes.merge(client, 1, Integer::sum);
|
|
447
|
+
if (onAbandon != null) {
|
|
448
|
+
abandonedFinishHandlers.computeIfAbsent(client, (key) -> new ArrayList<>()).add(onAbandon);
|
|
449
|
+
}
|
|
450
|
+
return true;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Records one finished acknowledge or consume. The last one for {@code client} ends it, whether or
|
|
456
|
+
* not a later call has replaced it, and wakes calls waiting in {@link #awaitPendingFinishes()}.
|
|
457
|
+
*/
|
|
458
|
+
private void endFinishingPurchase(BillingClient client) {
|
|
459
|
+
boolean last;
|
|
460
|
+
synchronized (billingClientLock) {
|
|
461
|
+
Integer pending = pendingFinishes.get(client);
|
|
462
|
+
last = pending == null || pending <= 1;
|
|
463
|
+
if (last) {
|
|
464
|
+
pendingFinishes.remove(client);
|
|
465
|
+
abandonedFinishHandlers.remove(client);
|
|
466
|
+
setAsideClients.remove(client);
|
|
467
|
+
if (billingClient == client) {
|
|
468
|
+
billingClient = null;
|
|
469
|
+
}
|
|
470
|
+
} else {
|
|
471
|
+
pendingFinishes.put(client, pending - 1);
|
|
472
|
+
}
|
|
473
|
+
billingClientLock.notifyAll();
|
|
474
|
+
}
|
|
475
|
+
if (last) {
|
|
476
|
+
client.endConnection();
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Waits until no acknowledge or consume is in flight, so a new client is not created next to one
|
|
482
|
+
* that can still receive purchase updates. After {@link #PENDING_FINISH_WAIT_MS} it gives up and
|
|
483
|
+
* the caller detaches the finishing client instead. Safe to call while holding
|
|
484
|
+
* {@link #billingClientLock}, since wait() releases it. Package-private for tests.
|
|
485
|
+
*/
|
|
486
|
+
void awaitPendingFinishes() {
|
|
487
|
+
synchronized (billingClientLock) {
|
|
488
|
+
awaitPendingFinishesLocked();
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
private void awaitPendingFinishesLocked() {
|
|
493
|
+
long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(pendingFinishWaitMs);
|
|
494
|
+
while (hasAwaitedFinishesLocked()) {
|
|
495
|
+
long remainingMs = TimeUnit.NANOSECONDS.toMillis(deadline - System.nanoTime());
|
|
496
|
+
if (remainingMs <= 0) {
|
|
497
|
+
// Stop waiting on the unanswered finishes so they do not delay every later call too,
|
|
498
|
+
// and settle the calls waiting on them. Their clients stay open and keep their real
|
|
499
|
+
// pending counts: a slow finish is not a lost one, and endConnection() would drop it.
|
|
500
|
+
// Each client still ends when its last finish answers.
|
|
501
|
+
NativePurchasesLog.w(TAG, "Purchase finish still pending, no longer waiting on it");
|
|
502
|
+
for (BillingClient stale : new ArrayList<>(pendingFinishes.keySet())) {
|
|
503
|
+
if (!setAsideClients.add(stale)) {
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
if (billingClient == stale) {
|
|
507
|
+
billingClient = null;
|
|
508
|
+
}
|
|
509
|
+
List<Runnable> handlers = abandonedFinishHandlers.remove(stale);
|
|
510
|
+
if (handlers != null) {
|
|
511
|
+
for (Runnable handler : handlers) {
|
|
512
|
+
handler.run();
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
try {
|
|
519
|
+
billingClientLock.wait(remainingMs);
|
|
520
|
+
} catch (InterruptedException e) {
|
|
521
|
+
Thread.currentThread().interrupt();
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/** True while some client that is not set aside has a finish in flight. */
|
|
528
|
+
private boolean hasAwaitedFinishesLocked() {
|
|
529
|
+
for (BillingClient client : pendingFinishes.keySet()) {
|
|
530
|
+
if (!setAsideClients.contains(client)) {
|
|
531
|
+
return true;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return false;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/** Stands in for Play's answer when a Play call throws, so the counted finish is still undone. */
|
|
538
|
+
private static BillingResult thrownResult(RuntimeException e) {
|
|
539
|
+
return BillingResult.newBuilder()
|
|
540
|
+
.setResponseCode(BillingClient.BillingResponseCode.ERROR)
|
|
541
|
+
.setDebugMessage(String.valueOf(e.getMessage()))
|
|
542
|
+
.build();
|
|
543
|
+
}
|
|
544
|
+
|
|
378
545
|
/**
|
|
379
546
|
* Reject a purchase whose {@code offerToken} matched no current offer.
|
|
380
547
|
*
|
|
@@ -390,7 +557,11 @@ public class NativePurchasesPlugin extends Plugin {
|
|
|
390
557
|
|
|
391
558
|
private void closeBillingClientLocked() {
|
|
392
559
|
NativePurchasesLog.d(TAG, "closeBillingClient() called");
|
|
393
|
-
if (billingClient != null) {
|
|
560
|
+
if (billingClient != null && pendingFinishes.containsKey(billingClient)) {
|
|
561
|
+
// Its acknowledge or consume callback ends it; ending it here would drop that work.
|
|
562
|
+
NativePurchasesLog.d(TAG, "Detaching billing client that is still finishing a purchase");
|
|
563
|
+
billingClient = null;
|
|
564
|
+
} else if (billingClient != null) {
|
|
394
565
|
NativePurchasesLog.d(TAG, "Ending billing client connection");
|
|
395
566
|
billingClient.endConnection();
|
|
396
567
|
billingClient = null;
|
|
@@ -468,7 +639,13 @@ public class NativePurchasesPlugin extends Plugin {
|
|
|
468
639
|
}
|
|
469
640
|
}
|
|
470
641
|
|
|
471
|
-
|
|
642
|
+
/**
|
|
643
|
+
* @return true when an acknowledge or consume is in flight. Its callback then closes the billing
|
|
644
|
+
* client and resolves the call, because endConnection() cancels work still queued on the client.
|
|
645
|
+
* @param purchaseClient the client whose listener delivered the purchase; another call may have
|
|
646
|
+
* replaced {@code billingClient} by the time this runs.
|
|
647
|
+
*/
|
|
648
|
+
boolean handlePurchase(Purchase purchase, PluginCall purchaseCall, BillingClient purchaseClient) {
|
|
472
649
|
NativePurchasesLog.d(TAG, "handlePurchase() called");
|
|
473
650
|
NativePurchasesLog.d(TAG, NativePurchasesLog.purchaseDebugSummary(purchase));
|
|
474
651
|
NativePurchasesLog.d(TAG, "Purchase state: " + purchase.getPurchaseState());
|
|
@@ -487,6 +664,55 @@ public class NativePurchasesPlugin extends Plugin {
|
|
|
487
664
|
String purchaseAccountId = accountIdentifiers != null ? accountIdentifiers.getObfuscatedAccountId() : null;
|
|
488
665
|
NativePurchasesLog.d(TAG, "Purchase account identifier present: " + (purchaseAccountId != null ? "[REDACTED]" : "none"));
|
|
489
666
|
|
|
667
|
+
JSObject ret = new JSObject();
|
|
668
|
+
ret.put("transactionId", purchase.getPurchaseToken());
|
|
669
|
+
ret.put("productIdentifier", purchase.getProducts().get(0));
|
|
670
|
+
ret.put("purchaseDate", formatUtcIso8601(purchase.getPurchaseTime()));
|
|
671
|
+
ret.put("quantity", purchase.getQuantity());
|
|
672
|
+
ret.put("productType", responseProductType(purchaseCall != null ? purchaseCall.getString("productType") : null));
|
|
673
|
+
ret.put("orderId", purchase.getOrderId());
|
|
674
|
+
ret.put("purchaseToken", purchase.getPurchaseToken());
|
|
675
|
+
ret.put("isAcknowledged", purchase.isAcknowledged());
|
|
676
|
+
ret.put("purchaseState", String.valueOf(purchase.getPurchaseState()));
|
|
677
|
+
ret.put("appAccountToken", purchaseAccountId);
|
|
678
|
+
|
|
679
|
+
// Add cancellation information - ALWAYS set willCancel
|
|
680
|
+
// Note: Android doesn't provide direct cancellation information in the Purchase object
|
|
681
|
+
// This would require additional Google Play API calls to get detailed subscription status
|
|
682
|
+
ret.put("willCancel", null); // Default to null, would need API call to determine actual cancellation date
|
|
683
|
+
|
|
684
|
+
NativePurchasesLog.d(TAG, "Resolving purchase call for product: " + purchase.getProducts().get(0));
|
|
685
|
+
// Settles purchaseCall once, from Play's answer or from giving up on a lost finish.
|
|
686
|
+
AtomicBoolean settled = new AtomicBoolean(false);
|
|
687
|
+
Runnable onAbandon = () -> {
|
|
688
|
+
if (settled.compareAndSet(false, true) && purchaseCall != null) {
|
|
689
|
+
NativePurchasesLog.w(TAG, "Purchase finish was never answered; resolving with isAcknowledged false");
|
|
690
|
+
purchaseCall.resolve(ret);
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
boolean canFinish = shouldAutoAcknowledge && action != PurchaseAction.NONE;
|
|
694
|
+
if (canFinish && !beginFinishingPurchase(purchaseClient, onAbandon)) {
|
|
695
|
+
// Another call already ended this client, so an acknowledge or consume would never
|
|
696
|
+
// be answered. Resolve now; the payload shows isAcknowledged false and the app can
|
|
697
|
+
// acknowledge or consume the purchase itself.
|
|
698
|
+
NativePurchasesLog.w(TAG, "Billing client already closed, skipping auto acknowledge or consume");
|
|
699
|
+
shouldAutoAcknowledge = false;
|
|
700
|
+
}
|
|
701
|
+
// Still resolves when Play rejects the finish: the user has been charged, and a rejection
|
|
702
|
+
// would tell the app the purchase failed. isAcknowledged stays false in that case, so the
|
|
703
|
+
// app can see it and call acknowledgePurchase() or consumePurchase() itself.
|
|
704
|
+
Consumer<BillingResult> completePurchase = (finishResult) -> {
|
|
705
|
+
if (finishResult != null && finishResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
|
|
706
|
+
ret.put("isAcknowledged", true);
|
|
707
|
+
} else {
|
|
708
|
+
NativePurchasesLog.w(TAG, "Automatic purchase finish failed; isAcknowledged stays false");
|
|
709
|
+
}
|
|
710
|
+
endFinishingPurchase(purchaseClient);
|
|
711
|
+
if (settled.compareAndSet(false, true) && purchaseCall != null) {
|
|
712
|
+
purchaseCall.resolve(ret);
|
|
713
|
+
}
|
|
714
|
+
};
|
|
715
|
+
|
|
490
716
|
switch (action) {
|
|
491
717
|
case CONSUME:
|
|
492
718
|
if (!shouldAutoAcknowledge) {
|
|
@@ -495,12 +721,25 @@ public class NativePurchasesPlugin extends Plugin {
|
|
|
495
721
|
}
|
|
496
722
|
NativePurchasesLog.d(TAG, "Purchase flagged as consumable, consuming...");
|
|
497
723
|
ConsumeParams consumeParams = ConsumeParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
|
|
498
|
-
|
|
499
|
-
|
|
724
|
+
AtomicBoolean consumeAnswered = new AtomicBoolean(false);
|
|
725
|
+
try {
|
|
726
|
+
purchaseClient.consumeAsync(consumeParams, (billingResult, purchaseToken) -> {
|
|
727
|
+
if (consumeAnswered.compareAndSet(false, true)) {
|
|
728
|
+
onConsumeResponse(billingResult, purchaseToken);
|
|
729
|
+
completePurchase.accept(billingResult);
|
|
730
|
+
}
|
|
731
|
+
});
|
|
732
|
+
} catch (RuntimeException e) {
|
|
733
|
+
if (consumeAnswered.compareAndSet(false, true)) {
|
|
734
|
+
completePurchase.accept(thrownResult(e));
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
return true;
|
|
500
738
|
case ACKNOWLEDGE:
|
|
501
739
|
if (shouldAutoAcknowledge) {
|
|
502
740
|
NativePurchasesLog.d(TAG, "Purchase not acknowledged, auto-acknowledging...");
|
|
503
|
-
acknowledgePurchase(purchase.getPurchaseToken());
|
|
741
|
+
acknowledgePurchase(purchaseClient, purchase.getPurchaseToken(), completePurchase);
|
|
742
|
+
return true;
|
|
504
743
|
} else {
|
|
505
744
|
NativePurchasesLog.d(
|
|
506
745
|
TAG,
|
|
@@ -514,24 +753,6 @@ public class NativePurchasesPlugin extends Plugin {
|
|
|
514
753
|
break;
|
|
515
754
|
}
|
|
516
755
|
|
|
517
|
-
JSObject ret = new JSObject();
|
|
518
|
-
ret.put("transactionId", purchase.getPurchaseToken());
|
|
519
|
-
ret.put("productIdentifier", purchase.getProducts().get(0));
|
|
520
|
-
ret.put("purchaseDate", formatUtcIso8601(purchase.getPurchaseTime()));
|
|
521
|
-
ret.put("quantity", purchase.getQuantity());
|
|
522
|
-
ret.put("productType", responseProductType(purchaseCall != null ? purchaseCall.getString("productType") : null));
|
|
523
|
-
ret.put("orderId", purchase.getOrderId());
|
|
524
|
-
ret.put("purchaseToken", purchase.getPurchaseToken());
|
|
525
|
-
ret.put("isAcknowledged", purchase.isAcknowledged());
|
|
526
|
-
ret.put("purchaseState", String.valueOf(purchase.getPurchaseState()));
|
|
527
|
-
ret.put("appAccountToken", purchaseAccountId);
|
|
528
|
-
|
|
529
|
-
// Add cancellation information - ALWAYS set willCancel
|
|
530
|
-
// Note: Android doesn't provide direct cancellation information in the Purchase object
|
|
531
|
-
// This would require additional Google Play API calls to get detailed subscription status
|
|
532
|
-
ret.put("willCancel", null); // Default to null, would need API call to determine actual cancellation date
|
|
533
|
-
|
|
534
|
-
NativePurchasesLog.d(TAG, "Resolving purchase call for product: " + purchase.getProducts().get(0));
|
|
535
756
|
if (purchaseCall != null) {
|
|
536
757
|
purchaseCall.resolve(ret);
|
|
537
758
|
} else {
|
|
@@ -557,35 +778,63 @@ public class NativePurchasesPlugin extends Plugin {
|
|
|
557
778
|
NativePurchasesLog.d(TAG, "purchaseCall is null for failed purchase");
|
|
558
779
|
}
|
|
559
780
|
}
|
|
781
|
+
return false;
|
|
560
782
|
}
|
|
561
783
|
|
|
562
|
-
|
|
784
|
+
/**
|
|
785
|
+
* Body of each client's PurchasesUpdatedListener. {@code owner} is the client the listener belongs
|
|
786
|
+
* to. It stays open while handlePurchase has an acknowledge or consume in flight, and is closed
|
|
787
|
+
* here otherwise.
|
|
788
|
+
*/
|
|
789
|
+
void handlePurchasesUpdated(BillingResult billingResult, List<Purchase> purchases, PluginCall purchaseCall, BillingClient owner) {
|
|
790
|
+
NativePurchasesLog.d(TAG, "onPurchasesUpdated() called");
|
|
791
|
+
NativePurchasesLog.d(TAG, "Billing result: " + billingResult.getResponseCode() + " - " + billingResult.getDebugMessage());
|
|
792
|
+
NativePurchasesLog.d(TAG, "Purchases count: " + (purchases != null ? purchases.size() : 0));
|
|
793
|
+
NativePurchasesLog.i(NativePurchasesPlugin.TAG, "onPurchasesUpdated" + billingResult);
|
|
794
|
+
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && purchases != null) {
|
|
795
|
+
NativePurchasesLog.d(TAG, "Purchase update successful, processing first purchase");
|
|
796
|
+
if (handlePurchase(purchases.get(0), purchaseCall, owner)) {
|
|
797
|
+
return;
|
|
798
|
+
}
|
|
799
|
+
} else {
|
|
800
|
+
NativePurchasesLog.d(TAG, "Purchase update failed or purchases is null");
|
|
801
|
+
NativePurchasesLog.i(NativePurchasesPlugin.TAG, "onPurchasesUpdated" + billingResult);
|
|
802
|
+
if (purchaseCall != null) {
|
|
803
|
+
purchaseCall.reject("Purchase is not purchased", billingResponseCodeName(billingResult.getResponseCode()));
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
closeBillingClientIfCurrent(owner);
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
private void acknowledgePurchase(BillingClient client, String purchaseToken, Consumer<BillingResult> onComplete) {
|
|
563
810
|
NativePurchasesLog.d(TAG, "acknowledgePurchase() called");
|
|
564
811
|
AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder()
|
|
565
812
|
.setPurchaseToken(purchaseToken)
|
|
566
813
|
.build();
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
public void onAcknowledgePurchaseResponse(@NonNull BillingResult billingResult) {
|
|
572
|
-
// Handle the result of the acknowledge purchase
|
|
573
|
-
NativePurchasesLog.d(TAG, "onAcknowledgePurchaseResponse() called");
|
|
574
|
-
NativePurchasesLog.d(
|
|
575
|
-
TAG,
|
|
576
|
-
"Acknowledge result: " + billingResult.getResponseCode() + " - " + billingResult.getDebugMessage()
|
|
577
|
-
);
|
|
578
|
-
NativePurchasesLog.i(NativePurchasesPlugin.TAG, "onAcknowledgePurchaseResponse" + billingResult);
|
|
579
|
-
}
|
|
814
|
+
AtomicBoolean answered = new AtomicBoolean(false);
|
|
815
|
+
AcknowledgePurchaseResponseListener listener = (billingResult) -> {
|
|
816
|
+
if (!answered.compareAndSet(false, true)) {
|
|
817
|
+
return;
|
|
580
818
|
}
|
|
581
|
-
|
|
819
|
+
NativePurchasesLog.d(TAG, "onAcknowledgePurchaseResponse() called");
|
|
820
|
+
NativePurchasesLog.d(TAG, "Acknowledge result: " + billingResult.getResponseCode() + " - " + billingResult.getDebugMessage());
|
|
821
|
+
NativePurchasesLog.i(NativePurchasesPlugin.TAG, "onAcknowledgePurchaseResponse" + billingResult);
|
|
822
|
+
onComplete.accept(billingResult);
|
|
823
|
+
};
|
|
824
|
+
try {
|
|
825
|
+
client.acknowledgePurchase(acknowledgePurchaseParams, listener);
|
|
826
|
+
} catch (RuntimeException e) {
|
|
827
|
+
listener.onAcknowledgePurchaseResponse(thrownResult(e));
|
|
828
|
+
}
|
|
582
829
|
}
|
|
583
830
|
|
|
584
|
-
private
|
|
831
|
+
/** Package-private so tests can drive a new billing call. */
|
|
832
|
+
void initBillingClient(PluginCall purchaseCall) {
|
|
585
833
|
NativePurchasesLog.d(TAG, "initBillingClient() called");
|
|
586
834
|
NativePurchasesLog.d(TAG, "purchaseCall is null: " + (purchaseCall == null));
|
|
587
835
|
|
|
588
836
|
synchronized (billingClientLock) {
|
|
837
|
+
awaitPendingFinishesLocked();
|
|
589
838
|
closeBillingClientLocked();
|
|
590
839
|
}
|
|
591
840
|
|
|
@@ -657,38 +906,21 @@ public class NativePurchasesPlugin extends Plugin {
|
|
|
657
906
|
|
|
658
907
|
NativePurchasesLog.d(TAG, "Creating new BillingClient (attempt " + attempt + "/" + BILLING_CONNECTION_MAX_ATTEMPTS + ")");
|
|
659
908
|
final BillingClient client;
|
|
909
|
+
// The listener is built before the client exists, so it reads its owner from here.
|
|
910
|
+
final BillingClient[] listenerOwner = new BillingClient[1];
|
|
660
911
|
synchronized (billingClientLock) {
|
|
661
912
|
client = BillingClient.newBuilder(getContext())
|
|
662
913
|
.setListener(
|
|
663
914
|
new PurchasesUpdatedListener() {
|
|
664
915
|
@Override
|
|
665
916
|
public void onPurchasesUpdated(@NonNull BillingResult billingResult, List<Purchase> purchases) {
|
|
666
|
-
|
|
667
|
-
NativePurchasesLog.d(
|
|
668
|
-
TAG,
|
|
669
|
-
"Billing result: " + billingResult.getResponseCode() + " - " + billingResult.getDebugMessage()
|
|
670
|
-
);
|
|
671
|
-
NativePurchasesLog.d(TAG, "Purchases count: " + (purchases != null ? purchases.size() : 0));
|
|
672
|
-
NativePurchasesLog.i(NativePurchasesPlugin.TAG, "onPurchasesUpdated" + billingResult);
|
|
673
|
-
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && purchases != null) {
|
|
674
|
-
NativePurchasesLog.d(TAG, "Purchase update successful, processing first purchase");
|
|
675
|
-
handlePurchase(purchases.get(0), purchaseCall);
|
|
676
|
-
} else {
|
|
677
|
-
NativePurchasesLog.d(TAG, "Purchase update failed or purchases is null");
|
|
678
|
-
NativePurchasesLog.i(NativePurchasesPlugin.TAG, "onPurchasesUpdated" + billingResult);
|
|
679
|
-
if (purchaseCall != null) {
|
|
680
|
-
purchaseCall.reject(
|
|
681
|
-
"Purchase is not purchased",
|
|
682
|
-
billingResponseCodeName(billingResult.getResponseCode())
|
|
683
|
-
);
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
closeBillingClient();
|
|
917
|
+
handlePurchasesUpdated(billingResult, purchases, purchaseCall, listenerOwner[0]);
|
|
687
918
|
}
|
|
688
919
|
}
|
|
689
920
|
)
|
|
690
921
|
.enablePendingPurchases(PendingPurchasesParams.newBuilder().enableOneTimeProducts().build())
|
|
691
922
|
.build();
|
|
923
|
+
listenerOwner[0] = client;
|
|
692
924
|
billingClient = client;
|
|
693
925
|
}
|
|
694
926
|
|
|
@@ -979,47 +1211,88 @@ public class NativePurchasesPlugin extends Plugin {
|
|
|
979
1211
|
});
|
|
980
1212
|
}
|
|
981
1213
|
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
billingClient.queryPurchasesAsync(queryInAppPurchasesParams, this::handlePurchases);
|
|
1214
|
+
/** The current client, read under the lock. May be null if another call already closed it. */
|
|
1215
|
+
private BillingClient currentBillingClient() {
|
|
1216
|
+
synchronized (billingClientLock) {
|
|
1217
|
+
return billingClient;
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
989
1220
|
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
1221
|
+
/**
|
|
1222
|
+
* Queries unfinished purchases on {@code client} and acknowledges them. Each query and each
|
|
1223
|
+
* acknowledge counts as a pending finish on that client, so a later call waits for them or
|
|
1224
|
+
* detaches the client instead of ending it, and the last one ends it. Package-private for tests.
|
|
1225
|
+
*
|
|
1226
|
+
* @return false when the client is already closed and no query was sent.
|
|
1227
|
+
*/
|
|
1228
|
+
boolean processUnfinishedPurchases(BillingClient client) {
|
|
1229
|
+
NativePurchasesLog.d(TAG, "processUnfinishedPurchases() called");
|
|
1230
|
+
String[] productTypes = { BillingClient.ProductType.INAPP, BillingClient.ProductType.SUBS };
|
|
1231
|
+
// Count every query before sending any, so a fast callback for the first one cannot end the
|
|
1232
|
+
// client before the second is sent.
|
|
1233
|
+
for (int registered = 0; registered < productTypes.length; registered++) {
|
|
1234
|
+
if (!beginFinishingPurchase(client)) {
|
|
1235
|
+
NativePurchasesLog.w(TAG, "Billing client already closed, skipping unfinished purchase recovery");
|
|
1236
|
+
for (int undo = 0; undo < registered; undo++) {
|
|
1237
|
+
endFinishingPurchase(client);
|
|
1238
|
+
}
|
|
1239
|
+
return false;
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
for (String productType : productTypes) {
|
|
1243
|
+
QueryPurchasesParams params = QueryPurchasesParams.newBuilder().setProductType(productType).build();
|
|
1244
|
+
NativePurchasesLog.d(TAG, "Querying unfinished purchases of type " + productType);
|
|
1245
|
+
AtomicBoolean answered = new AtomicBoolean(false);
|
|
1246
|
+
PurchasesResponseListener listener = (billingResult, purchases) -> {
|
|
1247
|
+
if (!answered.compareAndSet(false, true)) {
|
|
1248
|
+
return;
|
|
1249
|
+
}
|
|
1250
|
+
try {
|
|
1251
|
+
handlePurchases(billingResult, purchases, client);
|
|
1252
|
+
} finally {
|
|
1253
|
+
endFinishingPurchase(client);
|
|
1254
|
+
}
|
|
1255
|
+
};
|
|
1256
|
+
try {
|
|
1257
|
+
client.queryPurchasesAsync(params, listener);
|
|
1258
|
+
} catch (RuntimeException e) {
|
|
1259
|
+
listener.onQueryPurchasesResponse(thrownResult(e), null);
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
return true;
|
|
995
1263
|
}
|
|
996
1264
|
|
|
997
|
-
private void handlePurchases(BillingResult billingResult, List<Purchase> purchases) {
|
|
1265
|
+
private void handlePurchases(BillingResult billingResult, List<Purchase> purchases, BillingClient client) {
|
|
998
1266
|
NativePurchasesLog.d(TAG, "handlePurchases() called");
|
|
999
1267
|
NativePurchasesLog.d(TAG, "Query purchases result: " + billingResult.getResponseCode() + " - " + billingResult.getDebugMessage());
|
|
1000
1268
|
NativePurchasesLog.d(TAG, "Purchases count: " + (purchases != null ? purchases.size() : 0));
|
|
1001
1269
|
|
|
1002
|
-
if (billingResult.getResponseCode()
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1270
|
+
if (billingResult.getResponseCode() != BillingClient.BillingResponseCode.OK || purchases == null) {
|
|
1271
|
+
NativePurchasesLog.d(TAG, "Query purchases failed");
|
|
1272
|
+
return;
|
|
1273
|
+
}
|
|
1274
|
+
if (!autoFinishTransactions) {
|
|
1275
|
+
NativePurchasesLog.d(TAG, "autoFinishTransactions disabled, skipping recovery acknowledgment");
|
|
1276
|
+
return;
|
|
1277
|
+
}
|
|
1278
|
+
for (Purchase purchase : purchases) {
|
|
1279
|
+
NativePurchasesLog.d(TAG, "Processing purchase for products: " + purchase.getProducts());
|
|
1280
|
+
NativePurchasesLog.d(TAG, "Purchase state: " + purchase.getPurchaseState());
|
|
1281
|
+
if (purchase.getPurchaseState() != Purchase.PurchaseState.PURCHASED) {
|
|
1282
|
+
continue;
|
|
1007
1283
|
}
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
NativePurchasesLog.d(TAG, "Purchase
|
|
1011
|
-
|
|
1012
|
-
PurchaseAction action = PurchaseActionDecider.decide(false, purchase);
|
|
1013
|
-
if (action == PurchaseAction.ACKNOWLEDGE) {
|
|
1014
|
-
NativePurchasesLog.d(TAG, "Purchase not acknowledged, acknowledging");
|
|
1015
|
-
acknowledgePurchase(purchase.getPurchaseToken());
|
|
1016
|
-
} else {
|
|
1017
|
-
NativePurchasesLog.d(TAG, "Purchase already acknowledged, skipping consume");
|
|
1018
|
-
}
|
|
1019
|
-
}
|
|
1284
|
+
PurchaseAction action = PurchaseActionDecider.decide(false, purchase);
|
|
1285
|
+
if (action != PurchaseAction.ACKNOWLEDGE) {
|
|
1286
|
+
NativePurchasesLog.d(TAG, "Purchase already acknowledged, skipping consume");
|
|
1287
|
+
continue;
|
|
1020
1288
|
}
|
|
1021
|
-
|
|
1022
|
-
|
|
1289
|
+
// Registered before the query's own finish ends, so the client cannot close in between.
|
|
1290
|
+
if (!beginFinishingPurchase(client)) {
|
|
1291
|
+
NativePurchasesLog.w(TAG, "Billing client already closed, skipping recovery acknowledgment");
|
|
1292
|
+
continue;
|
|
1293
|
+
}
|
|
1294
|
+
NativePurchasesLog.d(TAG, "Purchase not acknowledged, acknowledging");
|
|
1295
|
+
acknowledgePurchase(client, purchase.getPurchaseToken(), (result) -> endFinishingPurchase(client));
|
|
1023
1296
|
}
|
|
1024
1297
|
}
|
|
1025
1298
|
|
|
@@ -1042,11 +1315,17 @@ public class NativePurchasesPlugin extends Plugin {
|
|
|
1042
1315
|
public void restorePurchases(PluginCall call) {
|
|
1043
1316
|
NativePurchasesLog.d(TAG, "restorePurchases() called");
|
|
1044
1317
|
NativePurchasesLog.d(NativePurchasesPlugin.TAG, "restorePurchases");
|
|
1045
|
-
withBillingClient(call, () ->
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1318
|
+
withBillingClient(call, () -> restoreWithClient(call, currentBillingClient()));
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
/** Body of restorePurchases once a client exists. Package-private for tests. */
|
|
1322
|
+
void restoreWithClient(PluginCall call, BillingClient client) {
|
|
1323
|
+
if (!processUnfinishedPurchases(client)) {
|
|
1324
|
+
call.reject("Billing client closed before restore could run. Please try again.");
|
|
1325
|
+
return;
|
|
1326
|
+
}
|
|
1327
|
+
call.resolve();
|
|
1328
|
+
NativePurchasesLog.d(TAG, "restorePurchases() completed");
|
|
1050
1329
|
}
|
|
1051
1330
|
|
|
1052
1331
|
private void querySingleProductDetails(String productIdentifier, String productType, PluginCall call) {
|
|
@@ -1577,35 +1856,54 @@ public class NativePurchasesPlugin extends Plugin {
|
|
|
1577
1856
|
}
|
|
1578
1857
|
|
|
1579
1858
|
NativePurchasesLog.d(TAG, "Manually acknowledging purchase");
|
|
1580
|
-
withBillingClient(call, () ->
|
|
1581
|
-
|
|
1582
|
-
.setPurchaseToken(purchaseToken)
|
|
1583
|
-
.build();
|
|
1584
|
-
|
|
1585
|
-
billingClient.acknowledgePurchase(
|
|
1586
|
-
acknowledgePurchaseParams,
|
|
1587
|
-
new AcknowledgePurchaseResponseListener() {
|
|
1588
|
-
@Override
|
|
1589
|
-
public void onAcknowledgePurchaseResponse(@NonNull BillingResult billingResult) {
|
|
1590
|
-
NativePurchasesLog.d(TAG, "onAcknowledgePurchaseResponse() called");
|
|
1591
|
-
NativePurchasesLog.d(
|
|
1592
|
-
TAG,
|
|
1593
|
-
"Acknowledge result: " + billingResult.getResponseCode() + " - " + billingResult.getDebugMessage()
|
|
1594
|
-
);
|
|
1859
|
+
withBillingClient(call, () -> acknowledgeWithClient(call, currentBillingClient(), purchaseToken));
|
|
1860
|
+
}
|
|
1595
1861
|
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1862
|
+
/**
|
|
1863
|
+
* Manual acknowledge on {@code client}, counted as a pending finish so a later call does not
|
|
1864
|
+
* end the client under it. The call settles once: from Play's answer, or with a FINISH_TIMEOUT
|
|
1865
|
+
* rejection if a later call stops waiting on it. Package-private for tests.
|
|
1866
|
+
*/
|
|
1867
|
+
void acknowledgeWithClient(PluginCall call, BillingClient client, String purchaseToken) {
|
|
1868
|
+
AtomicBoolean settled = new AtomicBoolean(false);
|
|
1869
|
+
Runnable onAbandon = () -> {
|
|
1870
|
+
if (settled.compareAndSet(false, true)) {
|
|
1871
|
+
call.reject(
|
|
1872
|
+
"Acknowledge was not answered in time. It may still complete; check getUnfinishedTransactions before retrying.",
|
|
1873
|
+
"FINISH_TIMEOUT"
|
|
1874
|
+
);
|
|
1875
|
+
}
|
|
1876
|
+
};
|
|
1877
|
+
if (!beginFinishingPurchase(client, onAbandon)) {
|
|
1878
|
+
call.reject("Billing client closed before the acknowledge could run. Please try again.");
|
|
1879
|
+
return;
|
|
1880
|
+
}
|
|
1881
|
+
AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder()
|
|
1882
|
+
.setPurchaseToken(purchaseToken)
|
|
1883
|
+
.build();
|
|
1884
|
+
AtomicBoolean answered = new AtomicBoolean(false);
|
|
1885
|
+
AcknowledgePurchaseResponseListener listener = (billingResult) -> {
|
|
1886
|
+
if (!answered.compareAndSet(false, true)) {
|
|
1887
|
+
return;
|
|
1888
|
+
}
|
|
1889
|
+
NativePurchasesLog.d(TAG, "Acknowledge result: " + billingResult.getResponseCode() + " - " + billingResult.getDebugMessage());
|
|
1890
|
+
endFinishingPurchase(client);
|
|
1891
|
+
if (!settled.compareAndSet(false, true)) {
|
|
1892
|
+
return;
|
|
1893
|
+
}
|
|
1894
|
+
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
|
|
1895
|
+
NativePurchasesLog.d(TAG, "Purchase acknowledged successfully");
|
|
1896
|
+
call.resolve();
|
|
1897
|
+
} else {
|
|
1898
|
+
NativePurchasesLog.d(TAG, "Purchase acknowledgment failed");
|
|
1899
|
+
call.reject("Failed to acknowledge purchase: " + billingResult.getDebugMessage());
|
|
1900
|
+
}
|
|
1901
|
+
};
|
|
1902
|
+
try {
|
|
1903
|
+
client.acknowledgePurchase(acknowledgePurchaseParams, listener);
|
|
1904
|
+
} catch (RuntimeException e) {
|
|
1905
|
+
listener.onAcknowledgePurchaseResponse(thrownResult(e));
|
|
1906
|
+
}
|
|
1609
1907
|
}
|
|
1610
1908
|
|
|
1611
1909
|
@PluginMethod
|
|
@@ -1620,24 +1918,48 @@ public class NativePurchasesPlugin extends Plugin {
|
|
|
1620
1918
|
}
|
|
1621
1919
|
|
|
1622
1920
|
NativePurchasesLog.d(TAG, "Consuming purchase");
|
|
1623
|
-
withBillingClient(call, () ->
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
billingClient.consumeAsync(consumeParams, (billingResult, consumedToken) -> {
|
|
1627
|
-
NativePurchasesLog.d(TAG, "onConsumeResponse() called");
|
|
1628
|
-
NativePurchasesLog.d(TAG, "Consume result: " + billingResult.getResponseCode() + " - " + billingResult.getDebugMessage());
|
|
1921
|
+
withBillingClient(call, () -> consumeWithClient(call, currentBillingClient(), purchaseToken));
|
|
1922
|
+
}
|
|
1629
1923
|
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
}
|
|
1640
|
-
}
|
|
1924
|
+
/** Manual consume on {@code client}, with the same pending finish counting as {@link #acknowledgeWithClient}. */
|
|
1925
|
+
void consumeWithClient(PluginCall call, BillingClient client, String purchaseToken) {
|
|
1926
|
+
AtomicBoolean settled = new AtomicBoolean(false);
|
|
1927
|
+
Runnable onAbandon = () -> {
|
|
1928
|
+
if (settled.compareAndSet(false, true)) {
|
|
1929
|
+
call.reject(
|
|
1930
|
+
"Consume was not answered in time. It may still complete; check getUnfinishedTransactions before retrying.",
|
|
1931
|
+
"FINISH_TIMEOUT"
|
|
1932
|
+
);
|
|
1933
|
+
}
|
|
1934
|
+
};
|
|
1935
|
+
if (!beginFinishingPurchase(client, onAbandon)) {
|
|
1936
|
+
call.reject("Billing client closed before the consume could run. Please try again.");
|
|
1937
|
+
return;
|
|
1938
|
+
}
|
|
1939
|
+
ConsumeParams consumeParams = ConsumeParams.newBuilder().setPurchaseToken(purchaseToken).build();
|
|
1940
|
+
AtomicBoolean answered = new AtomicBoolean(false);
|
|
1941
|
+
ConsumeResponseListener listener = (billingResult, consumedToken) -> {
|
|
1942
|
+
if (!answered.compareAndSet(false, true)) {
|
|
1943
|
+
return;
|
|
1944
|
+
}
|
|
1945
|
+
NativePurchasesLog.d(TAG, "Consume result: " + billingResult.getResponseCode() + " - " + billingResult.getDebugMessage());
|
|
1946
|
+
endFinishingPurchase(client);
|
|
1947
|
+
if (!settled.compareAndSet(false, true)) {
|
|
1948
|
+
return;
|
|
1949
|
+
}
|
|
1950
|
+
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
|
|
1951
|
+
NativePurchasesLog.d(TAG, "Purchase consumed successfully");
|
|
1952
|
+
call.resolve();
|
|
1953
|
+
} else {
|
|
1954
|
+
NativePurchasesLog.d(TAG, "Purchase consumption failed");
|
|
1955
|
+
call.reject("Failed to consume purchase: " + billingResult.getDebugMessage());
|
|
1956
|
+
}
|
|
1957
|
+
};
|
|
1958
|
+
try {
|
|
1959
|
+
client.consumeAsync(consumeParams, listener);
|
|
1960
|
+
} catch (RuntimeException e) {
|
|
1961
|
+
listener.onConsumeResponse(thrownResult(e), purchaseToken);
|
|
1962
|
+
}
|
|
1641
1963
|
}
|
|
1642
1964
|
|
|
1643
1965
|
@PluginMethod
|
|
@@ -26,7 +26,7 @@ public class NativePurchasesPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
26
26
|
CAPPluginMethod(name: "finishTransaction", returnType: CAPPluginReturnPromise)
|
|
27
27
|
]
|
|
28
28
|
|
|
29
|
-
private let pluginVersion: String = "8.9.
|
|
29
|
+
private let pluginVersion: String = "8.9.15"
|
|
30
30
|
var autoFinishTransactions: Bool = true
|
|
31
31
|
private var transactionUpdatesTask: Task<Void, Never>?
|
|
32
32
|
|
|
@@ -428,10 +428,8 @@ extension NativePurchasesPlugin {
|
|
|
428
428
|
switch verificationResult {
|
|
429
429
|
case .verified(let appTransaction):
|
|
430
430
|
let originalBuildNumber = appTransaction.originalAppVersion
|
|
431
|
-
let originalInt = Int(originalBuildNumber) ?? 0
|
|
432
|
-
let targetInt = Int(targetBuildNumber) ?? 0
|
|
433
431
|
call.resolve([
|
|
434
|
-
"isOlderVersion":
|
|
432
|
+
"isOlderVersion": TransactionHelpers.isBuildNumber(originalBuildNumber, olderThan: targetBuildNumber),
|
|
435
433
|
"originalAppVersion": originalBuildNumber
|
|
436
434
|
])
|
|
437
435
|
case .unverified(_, let error):
|
|
@@ -146,6 +146,24 @@ internal class TransactionHelpers {
|
|
|
146
146
|
return dictionary
|
|
147
147
|
}
|
|
148
148
|
|
|
149
|
+
/// Compares dotted build numbers part by part, matching `compareVersions` on Android.
|
|
150
|
+
/// `CFBundleVersion` may hold up to three integers, and `Int(_:)` alone reads "1.2.3" as nil.
|
|
151
|
+
static func isBuildNumber(_ original: String, olderThan target: String) -> Bool {
|
|
152
|
+
func parts(_ value: String) -> [Int] {
|
|
153
|
+
value.split(separator: ".", omittingEmptySubsequences: false).map { Int($0.prefix { $0.isNumber }) ?? 0 }
|
|
154
|
+
}
|
|
155
|
+
let originalParts = parts(original)
|
|
156
|
+
let targetParts = parts(target)
|
|
157
|
+
for index in 0..<max(originalParts.count, targetParts.count) {
|
|
158
|
+
let originalPart = index < originalParts.count ? originalParts[index] : 0
|
|
159
|
+
let targetPart = index < targetParts.count ? targetParts[index] : 0
|
|
160
|
+
if originalPart != targetPart {
|
|
161
|
+
return originalPart < targetPart
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return false
|
|
165
|
+
}
|
|
166
|
+
|
|
149
167
|
/// Builds payloads for `Transaction.unfinished`. Unverified entries are skipped unless
|
|
150
168
|
/// `includeUnverified` is true, and then carry `verificationError` so callers can avoid trusting them.
|
|
151
169
|
static func collectUnfinishedTransactions(includeUnverified: Bool = false) async throws -> [[String: Any]] {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import XCTest
|
|
2
|
+
@testable import NativePurchasesPlugin
|
|
3
|
+
|
|
4
|
+
final class BuildNumberComparisonTests: XCTestCase {
|
|
5
|
+
func testPlainIntegersKeepNumericOrder() {
|
|
6
|
+
XCTAssertTrue(TransactionHelpers.isBuildNumber("42", olderThan: "100"))
|
|
7
|
+
XCTAssertFalse(TransactionHelpers.isBuildNumber("100", olderThan: "42"))
|
|
8
|
+
XCTAssertFalse(TransactionHelpers.isBuildNumber("42", olderThan: "42"))
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
func testDottedBuildNumbersCompareEachPart() {
|
|
12
|
+
XCTAssertTrue(TransactionHelpers.isBuildNumber("1.2.3", olderThan: "1.5.0"))
|
|
13
|
+
XCTAssertFalse(TransactionHelpers.isBuildNumber("3.0", olderThan: "2"))
|
|
14
|
+
XCTAssertFalse(TransactionHelpers.isBuildNumber("1.10", olderThan: "1.9"))
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
func testMissingPartsCountAsZero() {
|
|
18
|
+
XCTAssertFalse(TransactionHelpers.isBuildNumber("2.0.0", olderThan: "2"))
|
|
19
|
+
XCTAssertTrue(TransactionHelpers.isBuildNumber("2", olderThan: "2.0.1"))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
func testNonnumericPartsMatchAndroidParsing() {
|
|
23
|
+
// A part with no leading digits counts as zero.
|
|
24
|
+
XCTAssertFalse(TransactionHelpers.isBuildNumber("1.foo", olderThan: "1"))
|
|
25
|
+
XCTAssertTrue(TransactionHelpers.isBuildNumber("1.foo", olderThan: "1.1"))
|
|
26
|
+
// A leading numeric prefix is kept, like compareVersions on Android ("1-beta" reads as 1).
|
|
27
|
+
XCTAssertFalse(TransactionHelpers.isBuildNumber("1-beta", olderThan: "1"))
|
|
28
|
+
XCTAssertTrue(TransactionHelpers.isBuildNumber("1.2-rc1", olderThan: "1.3"))
|
|
29
|
+
}
|
|
30
|
+
}
|