From ccb77dcd1bb789c0006e7c78e47022e7515d3960 Mon Sep 17 00:00:00 2001 From: James Montemagno Date: Wed, 23 Jan 2019 19:40:56 -0800 Subject: [PATCH] Implement ShouldAddStorePayment --- .../InAppBillingTests.iOS/AppDelegate.cs | 19 +++++++++++++++++-- .../InAppBillingImplementation.cs | 13 +++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/InAppBillingTests/InAppBillingTests.iOS/AppDelegate.cs b/src/InAppBillingTests/InAppBillingTests.iOS/AppDelegate.cs index 155e2e9..52e811c 100644 --- a/src/InAppBillingTests/InAppBillingTests.iOS/AppDelegate.cs +++ b/src/InAppBillingTests/InAppBillingTests.iOS/AppDelegate.cs @@ -3,6 +3,7 @@ using System.Linq; using Foundation; +using StoreKit; using UIKit; namespace InAppBillingTests.iOS @@ -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; + } + + } + + } diff --git a/src/Plugin.InAppBilling.iOS/InAppBillingImplementation.cs b/src/Plugin.InAppBilling.iOS/InAppBillingImplementation.cs index 7a89906..3a5c858 100644 --- a/src/Plugin.InAppBilling.iOS/InAppBillingImplementation.cs +++ b/src/Plugin.InAppBilling.iOS/InAppBillingImplementation.cs @@ -23,12 +23,14 @@ public class InAppBillingImplementation : BaseInAppBilling /// public static Action OnPurchaseComplete { get; set; } = null; + public static Func OnShouldAddStorePayment { get; set; } = null; + /// /// Default constructor for In App Billing on iOS /// public InAppBillingImplementation() { - paymentObserver = new PaymentObserver(OnPurchaseComplete); + paymentObserver = new PaymentObserver(OnPurchaseComplete, OnShouldAddStorePayment); SKPaymentQueue.DefaultQueue.AddTransactionObserver(paymentObserver); } @@ -421,10 +423,17 @@ class PaymentObserver : SKPaymentTransactionObserver List restoredTransactions = new List(); private readonly Action onPurchaseSuccess; + private readonly Func onShouldAddStorePayment; - public PaymentObserver(Action onPurchaseSuccess = null) + public PaymentObserver(Action onPurchaseSuccess, Func 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)