Skip to content

Commit

Permalink
Merge pull request #208 from jamesmontemagno/feature/issue-190
Browse files Browse the repository at this point in the history
Implement ShouldAddStorePayment
  • Loading branch information
jamesmontemagno committed Feb 4, 2019
2 parents 591b57c + ccb77dc commit cf29054
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
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
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 cf29054

Please sign in to comment.