Skip to content

Commit

Permalink
Updated methods and packages, and targeted .NET 6.0
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jamesmontemagno committed May 8, 2024
1 parent 9779278 commit 000f816
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
41 changes: 28 additions & 13 deletions src/Plugin.InAppBilling/InAppBilling.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,13 @@ async Task<InAppBillingPurchase> 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 })
Expand Down Expand Up @@ -326,8 +329,8 @@ public async override Task<InAppBillingPurchase> 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)
Expand Down Expand Up @@ -359,15 +362,27 @@ async Task<InAppBillingPurchase> 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 });
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/Plugin.InAppBilling/Plugin.InAppBilling.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="MSBuild.Sdk.Extras/3.0.44">
<PropertyGroup>
<!--<TargetFrameworks>netstandard2.0;net7.0-android</TargetFrameworks>-->
<!--<TargetFrameworks>netstandard2.0;net8.0-android</TargetFrameworks>-->
<TargetFrameworks>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>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);uap10.0.19041;net6.0-windows10.0.19041;</TargetFrameworks>
<LangVersion>10.0</LangVersion>
Expand Down Expand Up @@ -98,14 +98,14 @@

<ItemGroup Condition=" $(TargetFramework.Contains('-android')) ">
<Compile Include="**\*.android.cs" />
<PackageReference Include="Xamarin.Android.Google.BillingClient" Version="6.0.1.3" />
<PackageReference Include="Xamarin.Google.Guava.ListenableFuture" Version="1.0.0.15" />
<PackageReference Include="Xamarin.Android.Google.BillingClient" Version="6.1.0.1" />
<PackageReference Include="Xamarin.Google.Guava.ListenableFuture" Version="1.0.0.17" />
</ItemGroup>

<ItemGroup Condition=" $(TargetFramework.StartsWith('MonoAndroid')) ">
<Compile Include="**\*.android.cs" />
<PackageReference Include="Xamarin.Essentials" Version="1.8.0" />
<PackageReference Include="Xamarin.Android.Google.BillingClient" Version="6.0.1.3" />
<PackageReference Include="Xamarin.Essentials" Version="1.8.1" />
<PackageReference Include="Xamarin.Android.Google.BillingClient" Version="6.1.0.1" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>

Expand Down

0 comments on commit 000f816

Please sign in to comment.