Skip to content

Commit

Permalink
Merge pull request #385 from jamesmontemagno/gh-384
Browse files Browse the repository at this point in the history
Add callback on Android for on purchases updated
  • Loading branch information
jamesmontemagno authored Jul 13, 2021
2 parents a0d0f3c + 992cf0a commit 34b70f0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
27 changes: 20 additions & 7 deletions src/Plugin.InAppBilling/InAppBilling.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ namespace Plugin.InAppBilling
[Preserve(AllMembers = true)]
public class InAppBillingImplementation : BaseInAppBilling
{
/// <summary>
/// Gets the context, aka the currently activity.
/// This is set from the MainApplication.cs file that was laid down by the plugin
/// </summary>
/// <value>The context.</value>
Activity Activity =>
/// <summary>
/// Gets or sets a callback for out of band purchases to complete.
/// </summary>
public static Action<BillingResult, List<InAppBillingPurchase>> OnAndroidPurchasesUpdated { get; set; } = null;

/// <summary>
/// Gets the context, aka the currently activity.
/// This is set from the MainApplication.cs file that was laid down by the plugin
/// </summary>
/// <value>The context.</value>
Activity Activity =>
Xamarin.Essentials.Platform.CurrentActivity ?? throw new NullReferenceException("Current Activity is null, ensure that the MainActivity.cs file is configuring Xamarin.Essentials in your source code so the In App Billing can use it.");

Context Context => Android.App.Application.Context;
Expand All @@ -40,7 +45,10 @@ public InAppBillingImplementation()

BillingClient BillingClient { get; set; }
BillingClient.Builder BillingClientBuilder { get; set; }
bool IsConnected { get; set; }
/// <summary>
/// Determines if it is connected to the backend actively (Android).
/// </summary>
public override bool IsConnected { get; set; }
TaskCompletionSource<(BillingResult billingResult, IList<Purchase> purchases)> tcsPurchase;
TaskCompletionSource<bool> tcsConnect;
/// <summary>
Expand Down Expand Up @@ -84,6 +92,11 @@ void OnDisconnected()
public void OnPurchasesUpdated(BillingResult billingResult, IList<Android.BillingClient.Api.Purchase> purchases)
{
tcsPurchase?.TrySetResult((billingResult, purchases));

if (OnAndroidPurchasesUpdated == null)
return;

OnAndroidPurchasesUpdated?.Invoke(billingResult, purchases?.Select(p => p.ToIABPurchase())?.ToList());
}

/// <summary>
Expand Down
12 changes: 8 additions & 4 deletions src/Plugin.InAppBilling/InAppBilling.apple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ static bool HasIntroductoryPrice
}
}
#endif
/// <summary>
/// Determines if it is connected to the backend actively (Android).
/// </summary>
public override bool IsConnected { get; set; } = true;

/// <summary>
/// Gets or sets a callback for out of band purchases to complete.
/// </summary>
public static Action<InAppBillingPurchase> OnPurchaseComplete { get; set; } = null;
/// <summary>
/// Gets or sets a callback for out of band purchases to complete.
/// </summary>
public static Action<InAppBillingPurchase> OnPurchaseComplete { get; set; } = null;

public static Func<SKPaymentQueue, SKPayment, SKProduct, bool> OnShouldAddStorePayment { get; set; } = null;

Expand Down
5 changes: 5 additions & 0 deletions src/Plugin.InAppBilling/InAppBilling.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public class InAppBillingImplementation : BaseInAppBilling
public InAppBillingImplementation()
{
}
/// <summary>
/// Determines if it is connected to the backend actively (Android).
/// </summary>
public override bool IsConnected { get; set; } = true;

/// <summary>
/// Gets or sets if in testing mode. Only for UWP
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Plugin.InAppBilling/Shared/BaseInAppBilling.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Plugin.InAppBilling

public abstract class BaseInAppBilling : IInAppBilling, IDisposable
{
public abstract bool IsConnected { get; set; }

/// <summary>
/// Gets or sets if in testing mode
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Plugin.InAppBilling/Shared/IInAppBilling.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ namespace Plugin.InAppBilling
[Preserve(AllMembers = true)]
public interface IInAppBilling : IDisposable
{
/// <summary>
/// Determines if it is connected to the backend actively (Android).
/// </summary>
public bool IsConnected { get; set; }
/// <summary>
/// Gets or sets if in testing mode
/// </summary>
Expand Down

0 comments on commit 34b70f0

Please sign in to comment.