Skip to content

Commit

Permalink
Fixes #29 for out of band subs
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno committed Jan 10, 2018
1 parent db2f6ea commit 6d30c26
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Plugin.InAppBilling.iOS/InAppBillingImplementation.cs
Expand Up @@ -15,12 +15,17 @@ namespace Plugin.InAppBilling
[Preserve(AllMembers = true)]
public class InAppBillingImplementation : BaseInAppBilling
{
/// <summary>
/// Gets or sets a callback for out of band purchases to complete.
/// </summary>
public static Action<InAppBillingPurchase> OnPurchaseComplete { get; set; } = null;

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

Expand Down Expand Up @@ -378,6 +383,12 @@ class PaymentObserver : SKPaymentTransactionObserver
public event Action<SKPaymentTransaction[]> TransactionsRestored;

List<SKPaymentTransaction> restoredTransactions = new List<SKPaymentTransaction>();
private readonly Action<InAppBillingPurchase> onPurchaseSuccess;

public PaymentObserver(Action<InAppBillingPurchase> onPurchaseSuccess = null)
{
this.onPurchaseSuccess = onPurchaseSuccess;
}

public override void UpdatedTransactions(SKPaymentQueue queue, SKPaymentTransaction[] transactions)
{
Expand All @@ -401,6 +412,10 @@ public override void UpdatedTransactions(SKPaymentQueue queue, SKPaymentTransact
case SKPaymentTransactionState.Restored:
case SKPaymentTransactionState.Purchased:
TransactionCompleted?.Invoke(transaction, true);

if (TransactionCompleted != null)
onPurchaseSuccess?.Invoke(transaction.ToIABPurchase());

SKPaymentQueue.DefaultQueue.FinishTransaction(transaction);
break;
case SKPaymentTransactionState.Failed:
Expand Down

0 comments on commit 6d30c26

Please sign in to comment.