module PayPal.Adaptive.Core.Money where import Import data Money = USD { _usdCents :: Int } deriving (Eq, Show) -- NOTE: Once we add more currencies this instance will violate the -- Monoid laws. instance Monoid Money where mempty = USD 0 mappend (USD c1) (USD c2) = USD $ c1 + c2 m2Currency :: Money -> Text m2Currency (USD _) = "USD" m2PayPal :: Money -> String m2PayPal (USD c) = let s = show (abs c) a = case length s of 0 -> "0.00" 1 -> "0.0" <> s 2 -> "0." <> s _ -> tailInsert 2 '.' s in if c < 0 then '-':a else a where tailInsert :: Int -> a -> [a] -> [a] tailInsert i x xs = let (ys, zs) = splitAt i (reverse xs) in reverse (ys <> pure x <> zs)