Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Introductory Price to InAppBillingProduct #126

Merged
merged 4 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -711,9 +713,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
32 changes: 23 additions & 9 deletions src/Plugin.InAppBilling.iOS/InAppBillingImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,18 @@ 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
});
var operatingSystemHasIntroductoryPrice = NSProcessInfo.ProcessInfo.IsOperatingSystemAtLeastVersion(new NSOperatingSystemVersion(11, 2, 0));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. Put it at the class level:

bool IsiOS112 => UIDevice.CurrentDevice.CheckSystemVersion (11,2);


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 = operatingSystemHasIntroductoryPrice && p.IntroductoryPrice != null ? p.IntroductoryPrice.LocalizedPrice() : "",
MicrosIntroductoryPrice = operatingSystemHasIntroductoryPrice && p.IntroductoryPrice != null ? (long)(p.Price.DoubleValue * 1000000d) : 0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be looking at p.IntroductoryPrice.DoubleValue?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should refactor this into a common method?

});
}

Task<IEnumerable<SKProduct>> GetProductAsync(string[] productId)
Expand Down Expand Up @@ -537,5 +540,16 @@ 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;
}
}
}