use BigDecimal for USD amounts

This commit is contained in:
Thibault Duplessis 2016-07-14 12:21:53 +02:00
parent 791a2fce59
commit 11c6339888
2 changed files with 12 additions and 6 deletions

View file

@ -21,12 +21,12 @@ object Checkout {
val form = Form(mapping(
"token" -> nonEmptyText,
"email" -> optional(email),
"amount" -> number(min = 100),
"amount" -> number(min = 100, max = 100 * 100000),
"freq" -> nonEmptyText
)(Checkout.apply)(Checkout.unapply))
}
case class Switch(usd: Int) {
case class Switch(usd: BigDecimal) {
def cents = Usd(usd).cents
}
@ -34,6 +34,8 @@ case class Switch(usd: Int) {
object Switch {
val form = Form(mapping(
"usd" -> number(min = 1)
"usd" -> bigDecimal(precision = 10, scale = 2)
.verifying(_ >= 1)
.verifying(_ <= 100000)
)(Switch.apply)(Switch.unapply))
}

View file

@ -6,14 +6,18 @@ case class CustomerId(value: String) extends AnyVal
case class ChargeId(value: String) extends AnyVal
case class Source(value: String) extends AnyVal
case class Usd(value: Int) extends AnyVal with Ordered[Usd] {
case class Usd(value: BigDecimal) extends AnyVal with Ordered[Usd] {
def compare(other: Usd) = value compare other.value
def cents = Cents(value * 100)
def cents = Cents((value * 100).toInt)
override def toString = s"$$$value"
}
object Usd {
def apply(value: Double): Usd = Usd(BigDecimal(value))
def apply(value: Int): Usd = Usd(BigDecimal(value))
}
case class Cents(value: Int) extends AnyVal with Ordered[Cents] {
def compare(other: Cents) = value compare other.value
def usd = Usd(value / 100)
def usd = Usd(value / 100d)
override def toString = usd.toString
}