Skip to content

Commit

Permalink
Merge pull request #126 from PhilippeCreytens/master
Browse files Browse the repository at this point in the history
Add Introductory Price to InAppBillingProduct
  • Loading branch information
jamesmontemagno authored Mar 22, 2018
2 parents d72b28b + fe31234 commit 7a08bbe
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
19 changes: 19 additions & 0 deletions src/Plugin.InAppBilling.Abstractions/InAppBillingProduct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ public class InAppBillingProduct
/// </summary>
public Int64 MicrosPrice { get; set; }

/// <summary>
/// Gets or sets the localized introductory price.
/// </summary>
/// <value>The localized introductory price.</value>
public string LocalizedIntroductoryPrice { get; set; }

/// <summary>
/// Introductory price of the product in micor-units
/// </summary>
/// <value>The introductory price.</value>
public Int64 MicrosIntroductoryPrice { get; set; }

/// <summary>
/// Gets a value indicating whether this <see cref="T:Plugin.InAppBilling.Abstractions.InAppBillingProduct"/>
/// has introductory price. This is an optional value in the answer from the server, requires a boolean to check if this exists
/// </summary>
/// <value><c>true</c> if has introductory price; otherwise, <c>false</c>.</value>
public bool HasIntroductoryPrice => !string.IsNullOrEmpty(LocalizedIntroductoryPrice);

}

}
22 changes: 18 additions & 4 deletions src/Plugin.InAppBilling.Android/InAppBillingImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ public async override Task<IEnumerable<InAppBillingProduct>> GetProductInfoAsync
CurrencyCode = product.CurrencyCode,
LocalizedPrice = product.Price,
ProductId = product.ProductId,
MicrosPrice = product.MicrosPrice
MicrosPrice = product.MicrosPrice,
LocalizedIntroductoryPrice = product.IntroductoryPrice,
MicrosIntroductoryPrice = product.IntroductoryPriceAmountMicros
});
}

Expand Down Expand Up @@ -717,9 +719,21 @@ public Product()
[JsonProperty(PropertyName = "price_amount_micros")]
public Int64 MicrosPrice { get; set; }

public override string ToString()
{
return string.Format("[Product: Title={0}, Price={1}, Type={2}, Description={3}, ProductId={4}]", Title, Price, Type, Description, ProductId);
[JsonProperty(PropertyName = "introductoryPrice")]
public string IntroductoryPrice { get; set; }

// 0 is default if this property is not set
[JsonProperty(PropertyName = "introductoryPriceAmountMicros")]
public Int64 IntroductoryPriceAmountMicros { get; set; }

[JsonProperty(PropertyName = "introductoryPricePeriod")]
public string IntroductoryPricePeriod { get; set; }

[JsonProperty(PropertyName = "introductoryPriceCycles")]
public int IntroductoryPriceCycles { get; set; }

public override string ToString() {
return string.Format("[Product: Title={0}, Price={1}, Type={2}, Description={3}, ProductId={4}, CurrencyCode={5}, MicrosPrice={6}, IntroductoryPrice={7}, IntroductoryPriceAmountMicros={8}, IntroductoryPricePeriod={9}, IntroductoryPriceCycles={10}]", Title, Price, Type, Description, ProductId, CurrencyCode, MicrosPrice, IntroductoryPrice, IntroductoryPriceAmountMicros, IntroductoryPricePeriod, IntroductoryPriceCycles);
}
}

Expand Down
38 changes: 29 additions & 9 deletions src/Plugin.InAppBilling.iOS/InAppBillingImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using UIKit;

namespace Plugin.InAppBilling
{
Expand Down Expand Up @@ -56,15 +57,16 @@ 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
});
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 = p.IntroductoryPrice.CanBeUsed() ? p.IntroductoryPrice.LocalizedPrice() : "",
MicrosIntroductoryPrice = p.IntroductoryPrice.CanBeUsed() ? (long)(p.IntroductoryPrice.Price.DoubleValue * 1000000d) : 0
});
}

Task<IEnumerable<SKProduct>> GetProductAsync(string[] productId)
Expand Down Expand Up @@ -515,6 +517,8 @@ public static PurchaseState GetPurchaseState(this SKPaymentTransaction transacti
[Preserve(AllMembers = true)]
static class SKProductExtension
{
static bool IsiOS112 => UIDevice.CurrentDevice.CheckSystemVersion(11, 2);

/// <remarks>
/// Use Apple's sample code for formatting a SKProduct price
/// https://developer.apple.com/library/ios/#DOCUMENTATION/StoreKit/Reference/SKProduct_Reference/Reference/Reference.html#//apple_ref/occ/instp/SKProduct/priceLocale
Expand All @@ -537,5 +541,21 @@ public static string LocalizedPrice(this SKProduct product)
Console.WriteLine(" ** formatter.StringFromNumber(" + product.Price + ") = " + formattedString + " for locale " + product.PriceLocale.LocaleIdentifier);
return formattedString;
}

public static string LocalizedPrice(this SKProductDiscount product) {
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;
}

public static bool CanBeUsed(this SKProductDiscount product)
{
return IsiOS112 && product != null;
}
}
}

0 comments on commit 7a08bbe

Please sign in to comment.