From 000f816514f5c6a1448e75b7025950bfac97afee Mon Sep 17 00:00:00 2001 From: James Montemagno Date: Wed, 8 May 2024 07:37:18 -0700 Subject: [PATCH] Updated methods and packages, and targeted .NET 6.0 Updated `UpgradePurchasedSubscriptionInternalAsync` and `PurchaseAsync` methods in `InAppBilling.android.cs` to handle null or empty `OfferToken`. Simplified network error check in `ConsumePurchaseAsync`. Fixed a formatting issue in `PurchaseAsync`. Commented out target Android version in `Plugin.InAppBilling.csproj` to target .NET 6.0 for multiple platforms. Updated `Xamarin.Android.Google.BillingClient`, `Xamarin.Google.Guava.ListenableFuture`, and `Xamarin.Essentials` packages in the Android and MonoAndroid target frameworks. --- .../InAppBilling.android.cs | 41 +++++++++++++------ .../Plugin.InAppBilling.csproj | 10 ++--- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/Plugin.InAppBilling/InAppBilling.android.cs b/src/Plugin.InAppBilling/InAppBilling.android.cs index 3d79c1b..f43fea5 100644 --- a/src/Plugin.InAppBilling/InAppBilling.android.cs +++ b/src/Plugin.InAppBilling/InAppBilling.android.cs @@ -274,10 +274,13 @@ async Task UpgradePurchasedSubscriptionInternalAsync(strin .SetReplaceProrationMode((int)prorationMode) .Build(); - var prodDetailsParams = BillingFlowParams.ProductDetailsParams.NewBuilder() - .SetProductDetails(skuDetails) - .SetOfferToken(skuDetails.GetSubscriptionOfferDetails()?.FirstOrDefault()?.OfferToken) - .Build(); + var t = skuDetails.GetSubscriptionOfferDetails()?.FirstOrDefault()?.OfferToken; + + + var prodDetails = BillingFlowParams.ProductDetailsParams.NewBuilder() + .SetProductDetails(skuDetails); + + var prodDetailsParams = string.IsNullOrWhiteSpace(t) ? prodDetails.Build() : prodDetails.SetOfferToken(t).Build(); var flowParams = BillingFlowParams.NewBuilder() .SetProductDetailsParamsList(new[] { prodDetailsParams }) @@ -326,8 +329,8 @@ public async override Task PurchaseAsync(string productId, { return null; } - - if(!string.IsNullOrWhiteSpace(obfuscatedProfileId) && string.IsNullOrWhiteSpace(obfuscatedAccountId)) + + if (!string.IsNullOrWhiteSpace(obfuscatedProfileId) && string.IsNullOrWhiteSpace(obfuscatedAccountId)) throw new ArgumentNullException("You must set an account id if you are setting a profile id"); switch (itemType) @@ -359,15 +362,27 @@ async Task PurchaseAsync(string productSku, string itemTyp ParseBillingResult(skuDetailsResult.Result); + var skuDetails = skuDetailsResult.ProductDetails.FirstOrDefault() ?? throw new ArgumentException($"{productSku} does not exist"); - var productDetailsParamsList = itemType == ProductType.Subs ? - BillingFlowParams.ProductDetailsParams.NewBuilder() - .SetProductDetails(skuDetails) - .SetOfferToken(subOfferToken ?? skuDetails.GetSubscriptionOfferDetails()?.FirstOrDefault()?.OfferToken ?? string.Empty) - .Build() - : BillingFlowParams.ProductDetailsParams.NewBuilder() + BillingFlowParams.ProductDetailsParams productDetailsParamsList; + + if (itemType == ProductType.Subs) + { + var t = subOfferToken ?? skuDetails.GetSubscriptionOfferDetails()?.FirstOrDefault()?.OfferToken ?? string.Empty; + + var productDetails = BillingFlowParams.ProductDetailsParams.NewBuilder() + .SetProductDetails(skuDetails); + + productDetailsParamsList = string.IsNullOrWhiteSpace(t) ? productDetails.Build() : productDetails.SetOfferToken(t).Build(); + } + else + { + productDetailsParamsList = BillingFlowParams.ProductDetailsParams.NewBuilder() .SetProductDetails(skuDetails) .Build(); + } + + var billingFlowParams = BillingFlowParams.NewBuilder() .SetProductDetailsParamsList(new[] { productDetailsParamsList }); @@ -461,7 +476,7 @@ static bool ParseBillingResult(BillingResult result, bool ignoreInvalidProducts if (result == null) throw new InAppBillingPurchaseException(PurchaseError.GeneralError); - if ((int)result.ResponseCode == Android.BillingClient.Api.BillingClient.BillingResponseCode.NetworkError) + if (result.ResponseCode == BillingResponseCode.NetworkError) throw new InAppBillingPurchaseException(PurchaseError.ServiceTimeout);//Network connection is down return result.ResponseCode switch diff --git a/src/Plugin.InAppBilling/Plugin.InAppBilling.csproj b/src/Plugin.InAppBilling/Plugin.InAppBilling.csproj index bd108ae..a89f9a9 100644 --- a/src/Plugin.InAppBilling/Plugin.InAppBilling.csproj +++ b/src/Plugin.InAppBilling/Plugin.InAppBilling.csproj @@ -1,6 +1,6 @@  - + netstandard2.0;MonoAndroid13.0;Xamarin.iOS10;Xamarin.TVOS10;Xamarin.Mac20;net6.0-android;net6.0-ios;net6.0-maccatalyst;net6.0-tvos;net6.0-macos $(TargetFrameworks);uap10.0.19041;net6.0-windows10.0.19041; 10.0 @@ -98,14 +98,14 @@ - - + + - - + +