Skip to content

Commit

Permalink
Add some additional null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno committed Aug 1, 2017
1 parent 58e6b65 commit e586db4
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Plugin.InAppBilling.iOS/InAppBillingImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public async override Task<IEnumerable<InAppBillingPurchase>> GetPurchasesAsync(
{
var purchases = await RestoreAsync();

if (purchases == null)
return;

var converted = purchases
.Where(p => p != null)
Expand Down Expand Up @@ -181,6 +183,9 @@ Task<SKPaymentTransaction> PurchaseAsync(string productId)
Action<SKPaymentTransaction, bool> handler = null;
handler = new Action<SKPaymentTransaction, bool>((tran, success) =>
{
if (tran?.Payment == null)
return;

// Only handle results from this request
if (productId != tran.Payment.ProductIdentifier)
return;
Expand Down Expand Up @@ -349,6 +354,9 @@ public override void UpdatedTransactions(SKPaymentQueue queue, SKPaymentTransact

foreach (var transaction in transactions)
{
if (transaction?.TransactionState == null)
break;

Debug.WriteLine($"Updated Transaction | {transaction.ToStatusString()}");

switch (transaction.TransactionState)
Expand All @@ -370,6 +378,9 @@ public override void UpdatedTransactions(SKPaymentQueue queue, SKPaymentTransact

public override void RestoreCompletedTransactionsFinished(SKPaymentQueue queue)
{
if (restoredTransactions == null)
return;

// This is called after all restored transactions have hit UpdatedTransactions
// at this point we are done with the restore request so let's fire up the event
var allTransactions = restoredTransactions.ToArray();
Expand All @@ -395,7 +406,7 @@ public override void RestoreCompletedTransactionsFailedWithError(SKPaymentQueue
static class SKTransactionExtensions
{
public static string ToStatusString(this SKPaymentTransaction transaction) =>
transaction.ToIABPurchase()?.ToString() ?? string.Empty;
transaction?.ToIABPurchase()?.ToString() ?? string.Empty;


public static InAppBillingPurchase ToIABPurchase(this SKPaymentTransaction transaction)
Expand All @@ -405,6 +416,7 @@ public static InAppBillingPurchase ToIABPurchase(this SKPaymentTransaction trans
if (p == null)
return null;


return new InAppBillingPurchase
{
TransactionDateUtc = NSDateToDateTimeUtc(p.TransactionDate),
Expand All @@ -424,6 +436,9 @@ static DateTime NSDateToDateTimeUtc(NSDate date)
public static PurchaseState GetPurchaseState(this SKPaymentTransaction transaction)
{

if (transaction?.TransactionState == null)
return PurchaseState.Unknown;

switch (transaction.TransactionState)
{
case SKPaymentTransactionState.Restored:
Expand Down

0 comments on commit e586db4

Please sign in to comment.