@capgo/native-purchases 0.0.40 → 0.0.43

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.
@@ -8,12 +8,12 @@ import com.android.billingclient.api.BillingClientStateListener;
8
8
  import com.android.billingclient.api.BillingFlowParams;
9
9
  import com.android.billingclient.api.BillingResult;
10
10
  import com.android.billingclient.api.ConsumeParams;
11
- import com.android.billingclient.api.ConsumeResponseListener;
12
11
  import com.android.billingclient.api.ProductDetails;
13
12
  import com.android.billingclient.api.ProductDetailsResponseListener;
14
13
  import com.android.billingclient.api.Purchase;
15
14
  import com.android.billingclient.api.PurchasesUpdatedListener;
16
15
  import com.android.billingclient.api.QueryProductDetailsParams;
16
+ import com.android.billingclient.api.QueryPurchasesParams;
17
17
  import com.getcapacitor.JSObject;
18
18
  import com.getcapacitor.Plugin;
19
19
  import com.getcapacitor.PluginCall;
@@ -97,6 +97,11 @@ public class NativePurchasesPlugin extends Plugin {
97
97
  }
98
98
 
99
99
  private void handlePurchase(Purchase purchase, PluginCall purchaseCall) {
100
+ Log.i(NativePurchasesPlugin.TAG, "handlePurchase" + purchase);
101
+ Log.i(
102
+ NativePurchasesPlugin.TAG,
103
+ "getPurchaseState" + purchase.getPurchaseState()
104
+ );
100
105
  if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
101
106
  // Grant entitlement to the user, then acknowledge the purchase
102
107
  // if sub then acknowledgePurchase
@@ -106,22 +111,7 @@ public class NativePurchasesPlugin extends Plugin {
106
111
  .newBuilder()
107
112
  .setPurchaseToken(purchase.getPurchaseToken())
108
113
  .build();
109
- billingClient.consumeAsync(
110
- consumeParams,
111
- new ConsumeResponseListener() {
112
- @Override
113
- public void onConsumeResponse(
114
- BillingResult billingResult,
115
- String purchaseToken
116
- ) {
117
- // Handle the result
118
- Log.i(
119
- NativePurchasesPlugin.TAG,
120
- "onConsumeResponse" + billingResult + purchaseToken
121
- );
122
- }
123
- }
124
- );
114
+ billingClient.consumeAsync(consumeParams, this::onConsumeResponse);
125
115
  } else {
126
116
  acknowledgePurchase(purchase.getPurchaseToken());
127
117
  }
@@ -357,10 +347,76 @@ public class NativePurchasesPlugin extends Plugin {
357
347
  }
358
348
  }
359
349
 
350
+ private void processUnfinishedPurchases() {
351
+ QueryPurchasesParams queryInAppPurchasesParams = QueryPurchasesParams
352
+ .newBuilder()
353
+ .setProductType(BillingClient.ProductType.INAPP)
354
+ .build();
355
+ billingClient.queryPurchasesAsync(
356
+ queryInAppPurchasesParams,
357
+ this::handlePurchases
358
+ );
359
+
360
+ QueryPurchasesParams querySubscriptionsParams = QueryPurchasesParams
361
+ .newBuilder()
362
+ .setProductType(BillingClient.ProductType.SUBS)
363
+ .build();
364
+ billingClient.queryPurchasesAsync(
365
+ querySubscriptionsParams,
366
+ this::handlePurchases
367
+ );
368
+ }
369
+
370
+ private void handlePurchases(
371
+ BillingResult billingResult,
372
+ List<Purchase> purchases
373
+ ) {
374
+ if (
375
+ billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
376
+ ) {
377
+ for (Purchase purchase : purchases) {
378
+ if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
379
+ if (purchase.isAcknowledged()) {
380
+ ConsumeParams consumeParams = ConsumeParams
381
+ .newBuilder()
382
+ .setPurchaseToken(purchase.getPurchaseToken())
383
+ .build();
384
+ billingClient.consumeAsync(consumeParams, this::onConsumeResponse);
385
+ } else {
386
+ acknowledgePurchase(purchase.getPurchaseToken());
387
+ }
388
+ }
389
+ }
390
+ }
391
+ }
392
+
393
+ private void onConsumeResponse(
394
+ BillingResult billingResult,
395
+ String purchaseToken
396
+ ) {
397
+ if (
398
+ billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
399
+ ) {
400
+ // Handle the success of the consume operation.
401
+ // For example, you can update the UI to reflect that the item has been consumed.
402
+ Log.i(
403
+ NativePurchasesPlugin.TAG,
404
+ "onConsumeResponse OK " + billingResult + purchaseToken
405
+ );
406
+ } else {
407
+ // Handle error responses.
408
+ Log.i(
409
+ NativePurchasesPlugin.TAG,
410
+ "onConsumeResponse OTHER " + billingResult + purchaseToken
411
+ );
412
+ }
413
+ }
414
+
360
415
  @PluginMethod
361
416
  public void restorePurchases(PluginCall call) {
362
417
  Log.d(NativePurchasesPlugin.TAG, "restorePurchases");
363
418
  this.initBillingClient(null);
419
+ this.processUnfinishedPurchases();
364
420
  call.resolve();
365
421
  }
366
422
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/native-purchases",
3
- "version": "0.0.40",
3
+ "version": "0.0.43",
4
4
  "description": "In-app Subscriptions Made Easy",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",