Skip to content

Commit

Permalink
Fixes #147 update purchaseasync
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno committed May 2, 2018
1 parent 98942ce commit 95d169c
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/Plugin.InAppBilling.iOS/InAppBillingImplementation.cs
Expand Up @@ -59,16 +59,17 @@ public async override Task<IEnumerable<InAppBillingProduct>> GetProductInfoAsync
{
var products = await GetProductAsync(productIds);

return products.Select(p => new InAppBillingProduct {
LocalizedPrice = p.LocalizedPrice(),
MicrosPrice = (long)(p.Price.DoubleValue * 1000000d),
Name = p.LocalizedTitle,
ProductId = p.ProductIdentifier,
Description = p.LocalizedDescription,
CurrencyCode = p.PriceLocale?.CurrencyCode ?? string.Empty,
LocalizedIntroductoryPrice = IsiOS112 ? (p.IntroductoryPrice?.LocalizedPrice() ?? string.Empty) : string.Empty,
MicrosIntroductoryPrice = IsiOS112 ? (long)((p.IntroductoryPrice?.Price?.DoubleValue ?? 0) * 1000000d) : 0
});
return products.Select(p => new InAppBillingProduct
{
LocalizedPrice = p.LocalizedPrice(),
MicrosPrice = (long)(p.Price.DoubleValue * 1000000d),
Name = p.LocalizedTitle,
ProductId = p.ProductIdentifier,
Description = p.LocalizedDescription,
CurrencyCode = p.PriceLocale?.CurrencyCode ?? string.Empty,
LocalizedIntroductoryPrice = IsiOS112 ? (p.IntroductoryPrice?.LocalizedPrice() ?? string.Empty) : string.Empty,
MicrosIntroductoryPrice = IsiOS112 ? (long)((p.IntroductoryPrice?.Price?.DoubleValue ?? 0) * 1000000d) : 0
});
}

Task<IEnumerable<SKProduct>> GetProductAsync(string[] productId)
Expand Down Expand Up @@ -197,7 +198,8 @@ public async override Task<InAppBillingPurchase> PurchaseAsync(string productId,
TransactionDateUtc = reference.AddSeconds(p.TransactionDate.SecondsSinceReferenceDate),
Id = p.TransactionIdentifier,
ProductId = p.Payment?.ProductIdentifier ?? string.Empty,
State = p.GetPurchaseState()
State = p.GetPurchaseState(),
PurchaseToken = p.TransactionReceipt?.GetBase64EncodedString(NSDataBase64EncodingOptions.None) ?? string.Empty
};

if (verifyPurchase == null)
Expand Down Expand Up @@ -480,7 +482,7 @@ public static InAppBillingPurchase ToIABPurchase(this SKPaymentTransaction trans
ProductId = p.Payment?.ProductIdentifier ?? string.Empty,
State = p.GetPurchaseState(),
PurchaseToken = p.TransactionReceipt?.GetBase64EncodedString(NSDataBase64EncodingOptions.None) ?? string.Empty
};
};
}

static DateTime NSDateToDateTimeUtc(NSDate date)
Expand Down Expand Up @@ -520,7 +522,7 @@ public static PurchaseState GetPurchaseState(this SKPaymentTransaction transacti
[Preserve(AllMembers = true)]
static class SKProductExtension
{


/// <remarks>
/// Use Apple's sample code for formatting a SKProduct price
Expand Down Expand Up @@ -548,19 +550,20 @@ public static string LocalizedPrice(this SKProduct product)
return formattedString;
}

public static string LocalizedPrice(this SKProductDiscount product)
public static string LocalizedPrice(this SKProductDiscount product)
{
if (product?.PriceLocale == null)
return string.Empty;

var formatter = new NSNumberFormatter() {
FormatterBehavior = NSNumberFormatterBehavior.Version_10_4,
NumberStyle = NSNumberFormatterStyle.Currency,
Locale = product.PriceLocale
};
var formattedString = formatter.StringFromNumber(product.Price);
Console.WriteLine(" ** formatter.StringFromNumber(" + product.Price + ") = " + formattedString + " for locale " + product.PriceLocale.LocaleIdentifier);
return formattedString;
}
var formatter = new NSNumberFormatter()
{
FormatterBehavior = NSNumberFormatterBehavior.Version_10_4,
NumberStyle = NSNumberFormatterStyle.Currency,
Locale = product.PriceLocale
};
var formattedString = formatter.StringFromNumber(product.Price);
Console.WriteLine(" ** formatter.StringFromNumber(" + product.Price + ") = " + formattedString + " for locale " + product.PriceLocale.LocaleIdentifier);
return formattedString;
}
}
}

0 comments on commit 95d169c

Please sign in to comment.