Skip to content

Commit

Permalink
Implement ShouldAddStorePayment
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno committed Jan 24, 2019
1 parent 19eb5a9 commit ccb77dc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
19 changes: 17 additions & 2 deletions src/InAppBillingTests/InAppBillingTests.iOS/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;

using Foundation;
using StoreKit;
using UIKit;

namespace InAppBillingTests.iOS
Expand All @@ -25,7 +26,21 @@ public override bool FinishedLaunching(UIApplication app, NSDictionary options)
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());

return base.FinishedLaunching(app, options);
//initialize current one.
Plugin.InAppBilling.InAppBillingImplementation.OnShouldAddStorePayment = OnShouldAddStorePayment;
var current = Plugin.InAppBilling.CrossInAppBilling.Current;

return base.FinishedLaunching(app, options);
}
}

bool OnShouldAddStorePayment(SKPaymentQueue queue, SKPayment payment, SKProduct product)
{
// true in app purchase is initiated, false cancels it.
// you can check if it was already purchased.
return true;
}

}


}
13 changes: 11 additions & 2 deletions src/Plugin.InAppBilling.iOS/InAppBillingImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ public class InAppBillingImplementation : BaseInAppBilling
/// </summary>
public static Action<InAppBillingPurchase> OnPurchaseComplete { get; set; } = null;

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

/// <summary>
/// Default constructor for In App Billing on iOS
/// </summary>
public InAppBillingImplementation()
{
paymentObserver = new PaymentObserver(OnPurchaseComplete);
paymentObserver = new PaymentObserver(OnPurchaseComplete, OnShouldAddStorePayment);
SKPaymentQueue.DefaultQueue.AddTransactionObserver(paymentObserver);
}

Expand Down Expand Up @@ -421,10 +423,17 @@ class PaymentObserver : SKPaymentTransactionObserver

List<SKPaymentTransaction> restoredTransactions = new List<SKPaymentTransaction>();
private readonly Action<InAppBillingPurchase> onPurchaseSuccess;
private readonly Func<SKPaymentQueue, SKPayment, SKProduct, bool> onShouldAddStorePayment;

public PaymentObserver(Action<InAppBillingPurchase> onPurchaseSuccess = null)
public PaymentObserver(Action<InAppBillingPurchase> onPurchaseSuccess, Func<SKPaymentQueue, SKPayment, SKProduct, bool> onShouldAddStorePayment)
{
this.onPurchaseSuccess = onPurchaseSuccess;
this.onShouldAddStorePayment = onShouldAddStorePayment;
}

public override bool ShouldAddStorePayment(SKPaymentQueue queue, SKPayment payment, SKProduct product)
{
return onShouldAddStorePayment?.Invoke(queue, payment, product) ?? false;
}

public override void UpdatedTransactions(SKPaymentQueue queue, SKPaymentTransaction[] transactions)
Expand Down

0 comments on commit ccb77dc

Please sign in to comment.