@@ -443,15 +443,11 @@ private void fetchPurchasedProducts() {
443443 if (billingResult .getResponseCode () == OK ) {
444444 if (inAppPurchases .isEmpty ()) {
445445 Log ("Query IN-APP Purchases: the list is empty" );
446-
447- //in this case the inAppPurchases list returned by the API is empty
448- List <PurchaseInfo > emptyPurchases = new ArrayList <>();
449- findUiHandler ().post (() -> billingEventListener .onPurchasedProductsFetched (SkuType .INAPP , emptyPurchases , true ));
450446 } else {
451447 Log ("Query IN-APP Purchases: data found and progress" );
452-
453- processPurchases (SkuType .INAPP , inAppPurchases , true );
454448 }
449+
450+ processPurchases (SkuType .INAPP , inAppPurchases , true );
455451 } else {
456452 Log ("Query IN-APP Purchases: failed" );
457453 }
@@ -463,15 +459,11 @@ private void fetchPurchasedProducts() {
463459 if (billingResult .getResponseCode () == OK ) {
464460 if (subscriptionPurchases .isEmpty ()) {
465461 Log ("Query SUBS Purchases: the list is empty" );
466-
467- //in this case the subscriptionPurchases list returned by the API is empty
468- List <PurchaseInfo > emptyPurchases = new ArrayList <>();
469- findUiHandler ().post (() -> billingEventListener .onPurchasedProductsFetched (SkuType .SUBS , emptyPurchases , true ));
470462 } else {
471463 Log ("Query SUBS Purchases: data found and progress" );
472-
473- processPurchases (SkuType .SUBS , subscriptionPurchases , true );
474464 }
465+
466+ processPurchases (SkuType .SUBS , subscriptionPurchases , true );
475467 } else {
476468 Log ("Query SUBS Purchases: failed" );
477469 }
@@ -508,51 +500,48 @@ public SupportState isSubscriptionSupported() {
508500 * Checks purchases signature for more security
509501 */
510502 private void processPurchases (SkuType skuType , List <Purchase > allPurchases , boolean purchasedProductsFetched ) {
511- if (! allPurchases . isEmpty ()) {
503+ List < PurchaseInfo > signatureValidPurchases = new ArrayList <>();
512504
513- List <PurchaseInfo > signatureValidPurchases = new ArrayList <>();
505+ //create a list with signature valid purchases
506+ List <Purchase > validPurchases = allPurchases .stream ().filter (this ::isPurchaseSignatureValid ).collect (Collectors .toList ());
507+ for (Purchase purchase : validPurchases ) {
514508
515- //create a list with signature valid purchases
516- List <Purchase > validPurchases = allPurchases .stream ().filter (this ::isPurchaseSignatureValid ).collect (Collectors .toList ());
517- for (Purchase purchase : validPurchases ) {
509+ //query all SKUs as a list
510+ List <String > purchasesSkus = purchase .getSkus ();
518511
519- //query all SKUs as a list
520- List <String > purchasesSkus = purchase .getSkus ();
512+ //loop through all SKUs and progress for each SKU individually
513+ for (int i = 0 ; i < purchasesSkus .size (); i ++) {
514+ String purchaseSku = purchasesSkus .get (i );
521515
522- //loop through all SKUs and progress for each SKU individually
523- for ( int i = 0 ; i < purchasesSkus . size (); i ++ ) {
524- String purchaseSku = purchasesSkus .get (i );
516+ Optional < SkuInfo > skuInfo = fetchedSkuInfoList . stream (). filter ( it -> it . getSku (). equals ( purchaseSku )). findFirst ();
517+ if ( skuInfo . isPresent () ) {
518+ SkuDetails skuDetails = skuInfo .get (). getSkuDetails ( );
525519
526- Optional <SkuInfo > skuInfo = fetchedSkuInfoList .stream ().filter (it -> it .getSku ().equals (purchaseSku )).findFirst ();
527- if (skuInfo .isPresent ()) {
528- SkuDetails skuDetails = skuInfo .get ().getSkuDetails ();
520+ PurchaseInfo purchaseInfo = new PurchaseInfo (generateSkuInfo (skuDetails ), purchase );
521+ signatureValidPurchases .add (purchaseInfo );
529522
530- PurchaseInfo purchaseInfo = new PurchaseInfo (generateSkuInfo (skuDetails ), purchase );
531- signatureValidPurchases .add (purchaseInfo );
532-
533- }
534523 }
535524 }
525+ }
536526
537- if (purchasedProductsFetched ) {
538- fetchedPurchasedProducts = true ;
539- findUiHandler ().post (() -> billingEventListener .onPurchasedProductsFetched (skuType , signatureValidPurchases , false ));
540- } else {
541- findUiHandler ().post (() -> billingEventListener .onProductsPurchased (signatureValidPurchases ));
542- }
527+ if (purchasedProductsFetched ) {
528+ fetchedPurchasedProducts = true ;
529+ findUiHandler ().post (() -> billingEventListener .onPurchasedProductsFetched (skuType , signatureValidPurchases ));
530+ } else {
531+ findUiHandler ().post (() -> billingEventListener .onProductsPurchased (signatureValidPurchases ));
532+ }
543533
544- purchasedProductsList .addAll (signatureValidPurchases );
534+ purchasedProductsList .addAll (signatureValidPurchases );
545535
546- for (PurchaseInfo purchaseInfo : signatureValidPurchases ) {
547- if (shouldAutoConsume ) {
548- consumePurchase (purchaseInfo );
549- }
536+ for (PurchaseInfo purchaseInfo : signatureValidPurchases ) {
537+ if (shouldAutoConsume ) {
538+ consumePurchase (purchaseInfo );
539+ }
550540
551- if (shouldAutoAcknowledge ) {
552- boolean isSkuConsumable = purchaseInfo .getSkuProductType () == SkuProductType .CONSUMABLE ;
553- if (!isSkuConsumable ) {
554- acknowledgePurchase (purchaseInfo );
555- }
541+ if (shouldAutoAcknowledge ) {
542+ boolean isSkuConsumable = purchaseInfo .getSkuProductType () == SkuProductType .CONSUMABLE ;
543+ if (!isSkuConsumable ) {
544+ acknowledgePurchase (purchaseInfo );
556545 }
557546 }
558547 }
@@ -730,7 +719,7 @@ private void Log(String debugMessage) {
730719 */
731720 public void release () {
732721 if (billingClient != null && billingClient .isReady ()) {
733- Log ("BillingClient instance release: ending connection..." );
722+ Log ("BillingConnector instance release: ending connection..." );
734723 billingClient .endConnection ();
735724 }
736725 }
0 commit comments