Skip to content

Commit

Permalink
Add in app billing 4 for android
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno committed Jul 13, 2021
1 parent 34b70f0 commit e3c333a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/Plugin.InAppBilling/Converters.android.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using Android.BillingClient.Api;

namespace Plugin.InAppBilling
Expand All @@ -14,7 +15,9 @@ public static InAppBillingPurchase ToIABPurchase(this Purchase purchase)
Id = purchase.OrderId,
IsAcknowledged = purchase.IsAcknowledged,
Payload = purchase.DeveloperPayload,
ProductId = purchase.Sku,
ProductId = purchase.Skus.FirstOrDefault(),
Quantity = purchase.Quantity,
ProductIds = purchase.Skus,
PurchaseToken = purchase.PurchaseToken,
TransactionDateUtc = DateTimeOffset.FromUnixTimeMilliseconds(purchase.PurchaseTime).DateTime,
ObfuscatedAccountId = purchase.AccountIdentifiers?.ObfuscatedAccountId,
Expand Down
4 changes: 1 addition & 3 deletions src/Plugin.InAppBilling/InAppBilling.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public override Task<bool> ConnectAsync(bool enablePendingPurchases = true)
tcsConnect?.TrySetCanceled();
tcsConnect = new TaskCompletionSource<bool>();


BillingClientBuilder = BillingClient.NewBuilder(Context);
BillingClientBuilder.SetListener(OnPurchasesUpdated);
if (enablePendingPurchases)
Expand All @@ -73,7 +72,6 @@ public override Task<bool> ConnectAsync(bool enablePendingPurchases = true)

BillingClient.StartConnection(OnSetupFinished, OnDisconnected);


return tcsConnect.Task;

void OnSetupFinished(BillingResult billingResult)
Expand Down Expand Up @@ -273,7 +271,7 @@ async Task<InAppBillingPurchase> PurchaseAsync(string productSku, string itemTyp
ParseBillingResult(result.billingResult);

//we are only buying 1 thing.
var androidPurchase = result.purchases?.FirstOrDefault(p => p.Sku == productSku);
var androidPurchase = result.purchases?.FirstOrDefault(p => p.Skus.Contains(productSku));

//for some reason the data didn't come back
if (androidPurchase == null)
Expand Down
10 changes: 6 additions & 4 deletions src/Plugin.InAppBilling/InAppBilling.apple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public async override Task<IEnumerable<InAppBillingProduct>> GetProductInfoAsync
LocalizedPrice = p.LocalizedPrice(),
MicrosPrice = (long)(p.Price.DoubleValue * 1000000d),
Name = p.LocalizedTitle,
ProductId = p.ProductIdentifier,
Description = p.LocalizedDescription,
ProductId = p.ProductIdentifier,
Description = p.LocalizedDescription,
CurrencyCode = p.PriceLocale?.CurrencyCode ?? string.Empty,
LocalizedIntroductoryPrice = HasIntroductoryPrice ? (p.IntroductoryPrice?.LocalizedPrice() ?? string.Empty) : string.Empty,
MicrosIntroductoryPrice = HasIntroductoryPrice ? (long)((p.IntroductoryPrice?.Price?.DoubleValue ?? 0) * 1000000d) : 0
Expand Down Expand Up @@ -200,7 +200,8 @@ public async override Task<InAppBillingPurchase> PurchaseAsync(string productId,
TransactionDateUtc = reference.AddSeconds(p.TransactionDate.SecondsSinceReferenceDate),
Id = p.TransactionIdentifier,
ProductId = p.Payment?.ProductIdentifier ?? string.Empty,
State = p.GetPurchaseState(),
ProductIds = new string[] { p.Payment?.ProductIdentifier ?? string.Empty },
State = p.GetPurchaseState(),
#if __IOS__ || __TVOS__
PurchaseToken = p.TransactionReceipt?.GetBase64EncodedString(NSDataBase64EncodingOptions.None) ?? string.Empty
#endif
Expand Down Expand Up @@ -541,7 +542,8 @@ public static InAppBillingPurchase ToIABPurchase(this SKPaymentTransaction trans
TransactionDateUtc = NSDateToDateTimeUtc(transaction.TransactionDate),
Id = p.TransactionIdentifier,
ProductId = p.Payment?.ProductIdentifier ?? string.Empty,
State = p.GetPurchaseState(),
ProductIds = new string[] { p.Payment?.ProductIdentifier ?? string.Empty },
State = p.GetPurchaseState(),
PurchaseToken = finalToken
};
}
Expand Down
2 changes: 2 additions & 0 deletions src/Plugin.InAppBilling/InAppBilling.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ public static IEnumerable<InAppBillingPurchase> ToInAppBillingPurchase(this stri
AutoRenewing = false // Not supported by UWP yet
};
purchase.PurchaseToken = purchase.Id;
purchase.ProductIds = new string[] { purchase.ProductId };

// Map native UWP status to PurchaseState
switch (status)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.InAppBilling/Plugin.InAppBilling.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<ItemGroup Condition=" $(TargetFramework.StartsWith('MonoAndroid')) ">
<Compile Include="**\*.android.cs" />
<PackageReference Include="Xamarin.Essentials" Version="1.6.1" />
<PackageReference Include="Xamarin.Android.Google.BillingClient" Version="3.0.0" />
<PackageReference Include="Xamarin.Android.Google.BillingClient" Version="4.0.0" />
<!--<PackageReference Include="Xamarin.AndroidX.Migration" Version="1.0.6.1" />-->
</ItemGroup>

Expand Down
11 changes: 11 additions & 0 deletions src/Plugin.InAppBilling/Shared/InAppBillingPurchase.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ public InAppBillingPurchase()
/// </summary>
public string ProductId { get; set; }


/// <summary>
/// Quanity of the purchases product
/// </summary>
public int Quantity { get; set; } = 1;

/// <summary>
/// Product Ids/Skus
/// </summary>
public IList<string> ProductIds { get; set; }

/// <summary>
/// Indicates whether the subscritpion renewes automatically. If true, the sub is active, else false the user has canceled.
/// </summary>
Expand Down

0 comments on commit e3c333a

Please sign in to comment.