Skip to content

Commit

Permalink
Improve price per month accuracy for weekly subscriptions (#1504)
Browse files Browse the repository at this point in the history
We were calculating the price per month for weekly products as price *
4, but there are on average 4.345 weeks in a month, so this updates the
value accordingly.

---------

Co-authored-by: Toni Rico <antonio.rico.diez@revenuecat.com>
  • Loading branch information
aboedo and tonidero authored Dec 5, 2023
1 parent 3a31855 commit 27c08e2
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ data class Period(
private const val DAYS_PER_WEEK = 7.0
private const val DAYS_PER_MONTH = 30.0
private const val DAYS_PER_YEAR = 365.0
private const val WEEKS_PER_MONTH = 4.0
private const val WEEKS_PER_YEAR = 52.14
private const val MONTHS_PER_YEAR = 12.0
private const val WEEKS_PER_MONTH = DAYS_PER_YEAR / MONTHS_PER_YEAR / DAYS_PER_WEEK

fun create(iso8601: String): Period {
val pair = iso8601.toPeriod()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class PeriodTest {
companion object {
private val MAX_OFFSET = Offset.offset(0.0000001)
private val MAX_OFFSET = Offset.offset(0.0001)
}

@Test
Expand Down Expand Up @@ -61,7 +61,7 @@ class PeriodTest {
@Test
fun `valueInMonths is correct for weeks`() {
val period = Period.create("P2W")
assertThat(period.valueInMonths).isCloseTo(0.5, MAX_OFFSET)
assertThat(period.valueInMonths).isCloseTo(0.4602, MAX_OFFSET)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class PriceExtensionsPricePerMonthTest: PriceExtensionsPricePerPeriodTest() {
value = [
"$2, 1D, $60.00",
"$5, 15D, $10.00",
"$10, 1W, $40.00",
"$10, 2W, $20.00",
"$10, 1W, $43.45",
"$10, 2W, $21.73",
"$14.99, 1M, $14.99",
"$30, 2M, $15.00",
"$40, 3M, $13.33",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class PriceExtensionsPricePerWeekTest : PriceExtensionsPricePerPeriodTest() {
"$2, 14D, $1.00",
"$10, 1W, $10.00",
"$10, 2W, $5.00",
"$14.99, 1M, $3.75",
"$30, 2M, $3.75",
"$40, 3M, $3.33",
"$14.99, 1M, $3.45",
"$30, 2M, $3.45",
"$40, 3M, $3.07",
"$120, 1Y, $2.30",
"$50, 1Y, $0.96",
"$29.99, 1Y, $0.58",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ internal object TestData {
product = TestStoreProduct(
id = "com.revenuecat.weekly_product",
title = "Weekly",
price = Price(amountMicros = 1_990_000, currencyCode = "USD", formatted = "$1.99"),
price = Price(amountMicros = 1_490_000, currencyCode = "USD", formatted = "$1.49"),
description = "Weekly",
period = Period(value = 1, unit = Period.Unit.WEEK, iso8601 = "P1W"),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class VariableProcessorTest {
fun `process variables processes price_per_period`() {
expectVariablesResult("{{ price_per_period }}", "$67.99/yr", rcPackage = TestData.Packages.annual)
expectVariablesResult("{{ price_per_period }}", "$7.99/mth", rcPackage = TestData.Packages.monthly)
expectVariablesResult("{{ price_per_period }}", "$1.99/wk", rcPackage = TestData.Packages.weekly)
expectVariablesResult("{{ price_per_period }}", "$1.49/wk", rcPackage = TestData.Packages.weekly)
expectVariablesResult("{{ price_per_period }}", "$15.99/2 mths", rcPackage = TestData.Packages.bimonthly)
expectVariablesResult("{{ price_per_period }}", "$23.99/3 mths", rcPackage = TestData.Packages.quarterly)
expectVariablesResult("{{ price_per_period }}", "$39.99/6 mths", rcPackage = TestData.Packages.semester)
Expand All @@ -110,7 +110,7 @@ class VariableProcessorTest {
fun `process variables processes price_per_period localized in spanish`() {
expectVariablesResult("{{ price_per_period }}", "$67.99/a", esLocale, TestData.Packages.annual)
expectVariablesResult("{{ price_per_period }}", "$7.99/m.", esLocale, TestData.Packages.monthly)
expectVariablesResult("{{ price_per_period }}", "$1.99/sem.", esLocale, TestData.Packages.weekly)
expectVariablesResult("{{ price_per_period }}", "$1.49/sem.", esLocale, TestData.Packages.weekly)
expectVariablesResult("{{ price_per_period }}", "$15.99/2 m.", esLocale, TestData.Packages.bimonthly)
expectVariablesResult("{{ price_per_period }}", "$23.99/3 m.", esLocale, TestData.Packages.quarterly)
expectVariablesResult("{{ price_per_period }}", "$39.99/6 m.", esLocale, TestData.Packages.semester)
Expand All @@ -125,15 +125,15 @@ class VariableProcessorTest {
fun `process variables processes total_price_and_per_month`() {
expectVariablesResult("{{ total_price_and_per_month }}", "$67.99/yr ($5.67/mth)", rcPackage = TestData.Packages.annual)
expectVariablesResult("{{ total_price_and_per_month }}", "$7.99/mth", rcPackage = TestData.Packages.monthly)
expectVariablesResult("{{ total_price_and_per_month }}", "$1.99/wk ($7.96/mth)", rcPackage = TestData.Packages.weekly)
expectVariablesResult("{{ total_price_and_per_month }}", "$1.49/wk ($6.47/mth)", rcPackage = TestData.Packages.weekly)
expectVariablesResult("{{ total_price_and_per_month }}", "$1,000", rcPackage = TestData.Packages.lifetime)
}

@Test
fun `process variables processes total_price_and_per_month in spanish`() {
expectVariablesResult("{{ total_price_and_per_month }}", "$67.99/a (5,67 US$/m.)", esLocale, TestData.Packages.annual)
expectVariablesResult("{{ total_price_and_per_month }}", "$7.99/m.", esLocale, TestData.Packages.monthly)
expectVariablesResult("{{ total_price_and_per_month }}", "$1.99/sem. (7,96 US$/m.)", esLocale, TestData.Packages.weekly)
expectVariablesResult("{{ total_price_and_per_month }}", "$1.49/sem. (6,47 US$/m.)", esLocale, TestData.Packages.weekly)
expectVariablesResult("{{ total_price_and_per_month }}", "$1,000", esLocale, TestData.Packages.lifetime)
}

Expand Down

0 comments on commit 27c08e2

Please sign in to comment.