| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
use crate::error::AppError; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#[derive( |
| 23 |
Clone, Copy, Debug, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize, |
| 24 |
)] |
| 25 |
#[serde(rename_all = "lowercase")] |
| 26 |
pub enum SettlementCurrency { |
| 27 |
|
| 28 |
|
| 29 |
#[default] |
| 30 |
Usd, |
| 31 |
Cad, |
| 32 |
Gbp, |
| 33 |
Aud, |
| 34 |
Nzd, |
| 35 |
Eur, |
| 36 |
} |
| 37 |
|
| 38 |
impl SettlementCurrency { |
| 39 |
|
| 40 |
pub const ALL: [SettlementCurrency; 6] = [ |
| 41 |
Self::Usd, |
| 42 |
Self::Cad, |
| 43 |
Self::Gbp, |
| 44 |
Self::Aud, |
| 45 |
Self::Nzd, |
| 46 |
Self::Eur, |
| 47 |
]; |
| 48 |
|
| 49 |
|
| 50 |
pub fn code(self) -> &'static str { |
| 51 |
match self { |
| 52 |
Self::Usd => "usd", |
| 53 |
Self::Cad => "cad", |
| 54 |
Self::Gbp => "gbp", |
| 55 |
Self::Aud => "aud", |
| 56 |
Self::Nzd => "nzd", |
| 57 |
Self::Eur => "eur", |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
pub fn code_upper(self) -> &'static str { |
| 63 |
match self { |
| 64 |
Self::Usd => "USD", |
| 65 |
Self::Cad => "CAD", |
| 66 |
Self::Gbp => "GBP", |
| 67 |
Self::Aud => "AUD", |
| 68 |
Self::Nzd => "NZD", |
| 69 |
Self::Eur => "EUR", |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
pub fn symbol(self) -> &'static str { |
| 80 |
match self { |
| 81 |
Self::Usd => "$", |
| 82 |
Self::Cad => "CA$", |
| 83 |
Self::Gbp => "\u{a3}", |
| 84 |
Self::Aud => "A$", |
| 85 |
Self::Nzd => "NZ$", |
| 86 |
Self::Eur => "\u{20ac}", |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
pub fn minimum_charge_cents(self) -> i64 { |
| 99 |
match self { |
| 100 |
Self::Gbp => 30, |
| 101 |
Self::Usd | Self::Cad | Self::Aud | Self::Nzd | Self::Eur => 50, |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
pub fn max_price_cents(self) -> i32 { |
| 112 |
match self { |
| 113 |
Self::Usd | Self::Cad | Self::Gbp | Self::Aud | Self::Nzd | Self::Eur => 1_000_000, |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
pub fn from_code(code: &str) -> Option<Self> { |
| 124 |
match code.trim().to_ascii_lowercase().as_str() { |
| 125 |
"usd" => Some(Self::Usd), |
| 126 |
"cad" => Some(Self::Cad), |
| 127 |
"gbp" => Some(Self::Gbp), |
| 128 |
"aud" => Some(Self::Aud), |
| 129 |
"nzd" => Some(Self::Nzd), |
| 130 |
"eur" => Some(Self::Eur), |
| 131 |
_ => None, |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
pub fn from_db(code: &str) -> Self { |
| 143 |
Self::from_code(code).unwrap_or_default() |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
pub fn from_stripe_account(code: &str) -> Result<Self, AppError> { |
| 152 |
Self::from_code(code).ok_or_else(|| { |
| 153 |
AppError::BadRequest(format!( |
| 154 |
"MNW cannot yet pay out in {}. Supported settlement currencies are {}.", |
| 155 |
code.to_ascii_uppercase(), |
| 156 |
Self::ALL |
| 157 |
.iter() |
| 158 |
.map(|c| c.code_upper()) |
| 159 |
.collect::<Vec<_>>() |
| 160 |
.join(", ") |
| 161 |
)) |
| 162 |
}) |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
pub fn to_stripe(self) -> stripe_types::Currency { |
| 167 |
match self { |
| 168 |
Self::Usd => stripe_types::Currency::USD, |
| 169 |
Self::Cad => stripe_types::Currency::CAD, |
| 170 |
Self::Gbp => stripe_types::Currency::GBP, |
| 171 |
Self::Aud => stripe_types::Currency::AUD, |
| 172 |
Self::Nzd => stripe_types::Currency::NZD, |
| 173 |
Self::Eur => stripe_types::Currency::EUR, |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
impl std::fmt::Display for SettlementCurrency { |
| 179 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 180 |
f.write_str(self.code_upper()) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] |
| 193 |
#[serde(rename_all = "lowercase")] |
| 194 |
pub enum ConversionChoice { |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
#[default] |
| 203 |
AtCheckout, |
| 204 |
|
| 205 |
|
| 206 |
ByBuyersBank, |
| 207 |
} |
| 208 |
|
| 209 |
impl ConversionChoice { |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
pub fn adaptive_pricing_enabled(self) -> bool { |
| 217 |
matches!(self, Self::AtCheckout) |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
pub fn from_form_value(raw: Option<&str>) -> Self { |
| 226 |
match raw { |
| 227 |
Some("bank") => Self::ByBuyersBank, |
| 228 |
_ => Self::AtCheckout, |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
pub fn as_form_value(self) -> &'static str { |
| 234 |
match self { |
| 235 |
Self::AtCheckout => "checkout", |
| 236 |
Self::ByBuyersBank => "bank", |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
pub fn from_db(raw: &str) -> Self { |
| 242 |
Self::from_form_value(Some(raw)) |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
#[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 262 |
pub struct MoneyByCurrency { |
| 263 |
totals: Vec<(SettlementCurrency, i64)>, |
| 264 |
} |
| 265 |
|
| 266 |
impl MoneyByCurrency { |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
pub fn from_rows(rows: impl IntoIterator<Item = (SettlementCurrency, i64)>) -> Self { |
| 272 |
let mut totals: Vec<(SettlementCurrency, i64)> = Vec::new(); |
| 273 |
for (currency, cents) in rows { |
| 274 |
if cents == 0 { |
| 275 |
continue; |
| 276 |
} |
| 277 |
match totals.iter_mut().find(|(c, _)| *c == currency) { |
| 278 |
Some(entry) => entry.1 += cents, |
| 279 |
None => totals.push((currency, cents)), |
| 280 |
} |
| 281 |
} |
| 282 |
totals.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.code().cmp(b.0.code()))); |
| 283 |
Self { totals } |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
pub fn is_empty(&self) -> bool { |
| 288 |
self.totals.is_empty() |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
pub fn currency_count(&self) -> usize { |
| 293 |
self.totals.len() |
| 294 |
} |
| 295 |
|
| 296 |
pub fn iter(&self) -> impl Iterator<Item = (SettlementCurrency, i64)> + '_ { |
| 297 |
self.totals.iter().copied() |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
pub fn in_currency(&self, currency: SettlementCurrency) -> i64 { |
| 306 |
self.totals |
| 307 |
.iter() |
| 308 |
.find(|(c, _)| *c == currency) |
| 309 |
.map_or(0, |(_, cents)| *cents) |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
pub fn display(&self, fallback: SettlementCurrency) -> String { |
| 322 |
if self.totals.is_empty() { |
| 323 |
return crate::formatting::format_revenue(0, fallback); |
| 324 |
} |
| 325 |
self.totals |
| 326 |
.iter() |
| 327 |
.map(|(c, cents)| crate::formatting::format_revenue(*cents, *c)) |
| 328 |
.collect::<Vec<_>>() |
| 329 |
.join(" + ") |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
impl sqlx::Type<sqlx::Postgres> for SettlementCurrency { |
| 342 |
fn type_info() -> sqlx::postgres::PgTypeInfo { |
| 343 |
<str as sqlx::Type<sqlx::Postgres>>::type_info() |
| 344 |
} |
| 345 |
|
| 346 |
fn compatible(ty: &sqlx::postgres::PgTypeInfo) -> bool { |
| 347 |
<str as sqlx::Type<sqlx::Postgres>>::compatible(ty) |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
impl<'r> sqlx::Decode<'r, sqlx::Postgres> for SettlementCurrency { |
| 352 |
fn decode( |
| 353 |
value: sqlx::postgres::PgValueRef<'r>, |
| 354 |
) -> std::result::Result<Self, sqlx::error::BoxDynError> { |
| 355 |
Ok(Self::from_db( |
| 356 |
<&str as sqlx::Decode<sqlx::Postgres>>::decode(value)?, |
| 357 |
)) |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
impl sqlx::Encode<'_, sqlx::Postgres> for SettlementCurrency { |
| 362 |
fn encode_by_ref( |
| 363 |
&self, |
| 364 |
buf: &mut sqlx::postgres::PgArgumentBuffer, |
| 365 |
) -> std::result::Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> { |
| 366 |
<&str as sqlx::Encode<sqlx::Postgres>>::encode(self.code(), buf) |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
impl sqlx::Type<sqlx::Postgres> for ConversionChoice { |
| 371 |
fn type_info() -> sqlx::postgres::PgTypeInfo { |
| 372 |
<str as sqlx::Type<sqlx::Postgres>>::type_info() |
| 373 |
} |
| 374 |
|
| 375 |
fn compatible(ty: &sqlx::postgres::PgTypeInfo) -> bool { |
| 376 |
<str as sqlx::Type<sqlx::Postgres>>::compatible(ty) |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
impl<'r> sqlx::Decode<'r, sqlx::Postgres> for ConversionChoice { |
| 381 |
fn decode( |
| 382 |
value: sqlx::postgres::PgValueRef<'r>, |
| 383 |
) -> std::result::Result<Self, sqlx::error::BoxDynError> { |
| 384 |
Ok(Self::from_db( |
| 385 |
<&str as sqlx::Decode<sqlx::Postgres>>::decode(value)?, |
| 386 |
)) |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
impl sqlx::Encode<'_, sqlx::Postgres> for ConversionChoice { |
| 391 |
fn encode_by_ref( |
| 392 |
&self, |
| 393 |
buf: &mut sqlx::postgres::PgArgumentBuffer, |
| 394 |
) -> std::result::Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> { |
| 395 |
<&str as sqlx::Encode<sqlx::Postgres>>::encode(self.as_form_value(), buf) |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
#[cfg(test)] |
| 400 |
mod tests { |
| 401 |
use super::*; |
| 402 |
|
| 403 |
#[test] |
| 404 |
fn codes_round_trip() { |
| 405 |
for c in SettlementCurrency::ALL { |
| 406 |
assert_eq!(SettlementCurrency::from_code(c.code()), Some(c)); |
| 407 |
assert_eq!(SettlementCurrency::from_code(c.code_upper()), Some(c)); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
#[test] |
| 412 |
fn from_code_is_case_and_whitespace_insensitive() { |
| 413 |
assert_eq!( |
| 414 |
SettlementCurrency::from_code(" GbP "), |
| 415 |
Some(SettlementCurrency::Gbp) |
| 416 |
); |
| 417 |
} |
| 418 |
|
| 419 |
#[test] |
| 420 |
fn unsupported_code_is_none() { |
| 421 |
|
| 422 |
assert_eq!(SettlementCurrency::from_code("jpy"), None); |
| 423 |
assert_eq!(SettlementCurrency::from_code(""), None); |
| 424 |
} |
| 425 |
|
| 426 |
#[test] |
| 427 |
fn from_db_falls_back_to_usd() { |
| 428 |
assert_eq!(SettlementCurrency::from_db("jpy"), SettlementCurrency::Usd); |
| 429 |
assert_eq!(SettlementCurrency::from_db("eur"), SettlementCurrency::Eur); |
| 430 |
} |
| 431 |
|
| 432 |
#[test] |
| 433 |
fn from_stripe_account_rejects_unsupported() { |
| 434 |
let err = SettlementCurrency::from_stripe_account("jpy").unwrap_err(); |
| 435 |
let msg = err.to_string(); |
| 436 |
assert!(msg.contains("JPY"), "should name the currency: {msg}"); |
| 437 |
assert!(msg.contains("USD"), "should list what is supported: {msg}"); |
| 438 |
} |
| 439 |
|
| 440 |
#[test] |
| 441 |
fn gbp_is_the_only_thirty_cent_minimum() { |
| 442 |
for c in SettlementCurrency::ALL { |
| 443 |
let expected = if c == SettlementCurrency::Gbp { 30 } else { 50 }; |
| 444 |
assert_eq!(c.minimum_charge_cents(), expected, "{c}"); |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
#[test] |
| 449 |
fn dollar_currencies_are_disambiguated() { |
| 450 |
|
| 451 |
let bare: Vec<_> = SettlementCurrency::ALL |
| 452 |
.iter() |
| 453 |
.filter(|c| c.symbol() == "$") |
| 454 |
.collect(); |
| 455 |
assert_eq!(bare, vec![&SettlementCurrency::Usd]); |
| 456 |
} |
| 457 |
|
| 458 |
#[test] |
| 459 |
fn symbols_are_distinct() { |
| 460 |
let mut seen = std::collections::HashSet::new(); |
| 461 |
for c in SettlementCurrency::ALL { |
| 462 |
assert!(seen.insert(c.symbol()), "duplicate symbol for {c}"); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
#[test] |
| 467 |
fn stripe_codes_agree_with_ours() { |
| 468 |
for c in SettlementCurrency::ALL { |
| 469 |
assert_eq!(c.to_stripe().to_string(), c.code(), "{c}"); |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
#[test] |
| 476 |
fn a_mangled_form_value_lands_on_the_visible_path() { |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
for raw in [None, Some(""), Some("nonsense"), Some("CHECKOUT")] { |
| 481 |
assert_eq!( |
| 482 |
ConversionChoice::from_form_value(raw), |
| 483 |
ConversionChoice::AtCheckout, |
| 484 |
"{raw:?}" |
| 485 |
); |
| 486 |
} |
| 487 |
assert_eq!( |
| 488 |
ConversionChoice::from_form_value(Some("bank")), |
| 489 |
ConversionChoice::ByBuyersBank |
| 490 |
); |
| 491 |
} |
| 492 |
|
| 493 |
#[test] |
| 494 |
fn the_choice_round_trips_through_the_form_and_the_column() { |
| 495 |
for choice in [ConversionChoice::AtCheckout, ConversionChoice::ByBuyersBank] { |
| 496 |
assert_eq!(ConversionChoice::from_db(choice.as_form_value()), choice); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
#[test] |
| 501 |
fn only_convert_at_checkout_turns_adaptive_pricing_on() { |
| 502 |
|
| 503 |
assert!(ConversionChoice::AtCheckout.adaptive_pricing_enabled()); |
| 504 |
assert!(!ConversionChoice::ByBuyersBank.adaptive_pricing_enabled()); |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
fn money(rows: &[(SettlementCurrency, i64)]) -> MoneyByCurrency { |
| 510 |
MoneyByCurrency::from_rows(rows.iter().copied()) |
| 511 |
} |
| 512 |
|
| 513 |
#[test] |
| 514 |
fn one_currency_renders_exactly_as_before() { |
| 515 |
|
| 516 |
|
| 517 |
let m = money(&[(SettlementCurrency::Usd, 123_456)]); |
| 518 |
assert_eq!(m.display(SettlementCurrency::Usd), "$1,234.56"); |
| 519 |
assert_eq!(m.currency_count(), 1); |
| 520 |
} |
| 521 |
|
| 522 |
#[test] |
| 523 |
fn two_currencies_are_listed_not_summed() { |
| 524 |
let m = money(&[ |
| 525 |
(SettlementCurrency::Usd, 12_000), |
| 526 |
(SettlementCurrency::Gbp, 90_000), |
| 527 |
]); |
| 528 |
|
| 529 |
assert_eq!(m.display(SettlementCurrency::Usd), "\u{a3}900.00 + $120.00"); |
| 530 |
assert_eq!(m.currency_count(), 2); |
| 531 |
} |
| 532 |
|
| 533 |
#[test] |
| 534 |
fn duplicate_currencies_combine() { |
| 535 |
let m = money(&[ |
| 536 |
(SettlementCurrency::Eur, 500), |
| 537 |
(SettlementCurrency::Eur, 250), |
| 538 |
]); |
| 539 |
assert_eq!(m.currency_count(), 1); |
| 540 |
assert_eq!(m.in_currency(SettlementCurrency::Eur), 750); |
| 541 |
} |
| 542 |
|
| 543 |
#[test] |
| 544 |
fn zero_rows_are_dropped_so_the_common_case_stays_single() { |
| 545 |
|
| 546 |
let m = money(&[ |
| 547 |
(SettlementCurrency::Usd, 1000), |
| 548 |
(SettlementCurrency::Gbp, 0), |
| 549 |
]); |
| 550 |
assert_eq!(m.currency_count(), 1); |
| 551 |
assert_eq!(m.display(SettlementCurrency::Usd), "$10.00"); |
| 552 |
} |
| 553 |
|
| 554 |
#[test] |
| 555 |
fn empty_renders_zero_in_the_viewers_currency() { |
| 556 |
let m = MoneyByCurrency::default(); |
| 557 |
assert!(m.is_empty()); |
| 558 |
assert_eq!(m.display(SettlementCurrency::Gbp), "\u{a3}0.00"); |
| 559 |
} |
| 560 |
|
| 561 |
#[test] |
| 562 |
fn in_currency_does_not_leak_across_currencies() { |
| 563 |
|
| 564 |
let m = money(&[(SettlementCurrency::Gbp, 5000)]); |
| 565 |
assert_eq!(m.in_currency(SettlementCurrency::Usd), 0); |
| 566 |
assert_eq!(m.in_currency(SettlementCurrency::Gbp), 5000); |
| 567 |
} |
| 568 |
|
| 569 |
#[test] |
| 570 |
fn ordering_is_stable_for_equal_amounts() { |
| 571 |
let a = money(&[ |
| 572 |
(SettlementCurrency::Usd, 100), |
| 573 |
(SettlementCurrency::Gbp, 100), |
| 574 |
]); |
| 575 |
let b = money(&[ |
| 576 |
(SettlementCurrency::Gbp, 100), |
| 577 |
(SettlementCurrency::Usd, 100), |
| 578 |
]); |
| 579 |
assert_eq!( |
| 580 |
a.display(SettlementCurrency::Usd), |
| 581 |
b.display(SettlementCurrency::Usd) |
| 582 |
); |
| 583 |
} |
| 584 |
|
| 585 |
#[test] |
| 586 |
fn default_is_usd() { |
| 587 |
assert_eq!(SettlementCurrency::default(), SettlementCurrency::Usd); |
| 588 |
} |
| 589 |
} |
| 590 |
|