use serde::{Deserialize, Serialize};
use serde_json::Value;
pub type TransactionType = String;
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct RoutableConnectorChoice {
pub connector: String,
#[serde(default)]
pub merchant_connector_id: Option<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct RoutingDictionaryRecord {
pub id: String,
pub profile_id: String,
pub name: String,
pub kind: String,
#[serde(default)]
pub description: Option<String>,
#[serde(default)]
pub created_at: Option<i64>,
#[serde(default)]
pub modified_at: Option<i64>,
#[serde(default)]
pub algorithm_for: Option<TransactionType>,
#[serde(default)]
pub decision_engine_routing_id: Option<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct MerchantRoutingAlgorithm {
pub id: String,
pub profile_id: String,
pub name: String,
pub algorithm: Value,
#[serde(default)]
pub description: Option<String>,
#[serde(default)]
pub created_at: Option<i64>,
#[serde(default)]
pub modified_at: Option<i64>,
#[serde(default)]
pub algorithm_for: Option<TransactionType>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct RoutingDictionary {
pub merchant_id: String,
#[serde(default)]
pub active_id: Option<String>,
#[serde(default = "Vec::new")]
pub records: Vec<RoutingDictionaryRecord>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct ActiveRoutingAlgorithm {
#[serde(default)]
pub algorithm: Option<MerchantRoutingAlgorithm>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
#[non_exhaustive]
pub enum ActiveRoutingConfig {
Envelope(Box<ActiveRoutingAlgorithm>),
Records(Vec<RoutingDictionaryRecord>),
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct ProfileDefaultRoutingConfig {
pub profile_id: String,
#[serde(default = "Vec::new")]
pub connectors: Vec<RoutableConnectorChoice>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
#[non_exhaustive]
pub enum DefaultProfileRouting {
PerProfile(Vec<ProfileDefaultRoutingConfig>),
Connectors(Vec<RoutableConnectorChoice>),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RoutingConnectorCap {
pub merchant_connector_id: String,
pub limit: i64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub used: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reserved: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub unit: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub window: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub currency: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub limit_max: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub current_limit: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub current_limit_source: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub current_limit_drawn_at: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub set_current_limit: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub distribution: Option<ConnectorCapDistribution>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub current_limit_drawn_from: Option<ConnectorCapDrawnFrom>,
#[serde(flatten, default)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ConnectorCapDistribution {
pub kind: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mean: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub std_dev: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub mode: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bands: Option<Vec<ConnectorCapBand>>,
#[serde(flatten, default)]
pub extra: serde_json::Map<String, Value>,
}
impl ConnectorCapDistribution {
#[must_use]
pub fn uniform() -> Self {
Self {
kind: "uniform".to_string(),
..Default::default()
}
}
#[must_use]
pub fn normal(mean: i64, std_dev: i64) -> Self {
Self {
kind: "normal".to_string(),
mean: Some(mean),
std_dev: Some(std_dev),
..Default::default()
}
}
#[must_use]
pub fn triangular(mode: i64) -> Self {
Self {
kind: "triangular".to_string(),
mode: Some(mode),
..Default::default()
}
}
#[must_use]
pub fn weighted(bands: Vec<ConnectorCapBand>) -> Self {
Self {
kind: "weighted".to_string(),
bands: Some(bands),
..Default::default()
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ConnectorCapBand {
pub min: i64,
pub max: i64,
pub weight: i32,
}
impl ConnectorCapBand {
#[must_use]
pub fn new(min: i64, max: i64, weight: i32) -> Self {
Self { min, max, weight }
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ConnectorCapDrawnFrom {
pub limit: i64,
pub limit_max: i64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub distribution: Option<ConnectorCapDistribution>,
#[serde(flatten, default)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RoutingConnectorPaymentRange {
pub merchant_connector_id: String,
pub currency: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub min_amount: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_amount: Option<i64>,
#[serde(flatten, default)]
pub extra: serde_json::Map<String, Value>,
}
impl RoutingConnectorPaymentRange {
#[must_use]
pub fn new(merchant_connector_id: impl Into<String>, currency: impl Into<String>) -> Self {
Self {
merchant_connector_id: merchant_connector_id.into(),
currency: currency.into(),
min_amount: None,
max_amount: None,
extra: serde_json::Map::new(),
}
}
}
impl RoutingConnectorCap {
#[must_use]
pub fn new(merchant_connector_id: impl Into<String>, limit: i64) -> Self {
Self {
merchant_connector_id: merchant_connector_id.into(),
limit,
used: None,
reserved: None,
unit: None,
window: None,
currency: None,
limit_max: None,
current_limit: None,
current_limit_source: None,
current_limit_drawn_at: None,
set_current_limit: None,
distribution: None,
current_limit_drawn_from: None,
extra: serde_json::Map::new(),
}
}
#[must_use]
pub fn amount(
merchant_connector_id: impl Into<String>,
limit: i64,
currency: impl Into<String>,
window: impl Into<String>,
) -> Self {
let mut cap = Self::new(merchant_connector_id, limit);
cap.unit = Some("amount".to_string());
cap.currency = Some(currency.into());
cap.window = Some(window.into());
cap
}
#[must_use]
pub fn window(mut self, window: impl Into<String>) -> Self {
self.window = Some(window.into());
self
}
#[must_use]
pub fn limit_max(mut self, limit_max: i64) -> Self {
self.limit_max = Some(limit_max);
self
}
#[must_use]
pub fn distribution(mut self, distribution: ConnectorCapDistribution) -> Self {
self.distribution = Some(distribution);
self
}
#[must_use]
pub fn set_current_limit(mut self, limit: i64) -> Self {
self.set_current_limit = Some(limit);
self
}
#[must_use]
pub fn has_capacity(&self) -> Option<bool> {
let used = self.used?;
let limit = self.current_limit.unwrap_or(self.limit);
Some(used + self.reserved.unwrap_or(0) < limit)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RoutingConnectorCaps {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub profile_id: Option<String>,
#[serde(default = "Vec::new")]
pub caps: Vec<RoutingConnectorCap>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub active_caps: Option<Vec<RoutingConnectorCap>>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub scheduled_account_ids: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub features: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub payment_ranges: Option<Vec<RoutingConnectorPaymentRange>>,
#[serde(flatten, default)]
pub extra: serde_json::Map<String, Value>,
}
impl RoutingConnectorCaps {
#[must_use]
pub fn new(caps: Vec<RoutingConnectorCap>) -> Self {
Self {
profile_id: None,
caps,
active_caps: None,
scheduled_account_ids: Vec::new(),
features: Vec::new(),
payment_ranges: None,
extra: serde_json::Map::new(),
}
}
#[must_use]
pub fn payment_ranges(mut self, ranges: Vec<RoutingConnectorPaymentRange>) -> Self {
self.payment_ranges = Some(ranges);
self
}
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct RoutingConfigVersion {
pub version: i32,
pub name: String,
#[serde(default)]
pub description: String,
pub kind: String,
pub algorithm: Value,
pub valid_from: i64,
#[serde(default)]
pub valid_until: Option<i64>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
impl RoutingConfigVersion {
#[must_use]
pub fn is_live(&self) -> bool {
self.valid_until.is_none()
}
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct RoutingConfigHistoryResponse {
pub id: String,
pub profile_id: String,
#[serde(default = "Vec::new")]
pub versions: Vec<RoutingConfigVersion>,
pub total_count: i64,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum MethodSurcharge {
#[serde(rename = "fixed")]
Fixed {
amount: i64,
},
#[serde(rename = "rate")]
Rate {
percent: f64,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PerMethodSurchargeItem {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub payment_method: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub payment_method_type: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub card_network: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub connector: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub merchant_connector_id: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub conditions: Vec<SurchargeCondition>,
pub surcharge: MethodSurcharge,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tax_on_surcharge_percent: Option<f64>,
}
impl PerMethodSurchargeItem {
pub fn new(payment_method: impl Into<String>, surcharge: MethodSurcharge) -> Self {
Self {
payment_method: Some(payment_method.into()),
payment_method_type: None,
card_network: None,
connector: None,
merchant_connector_id: None,
conditions: Vec::new(),
surcharge,
tax_on_surcharge_percent: None,
}
}
pub fn for_connector(connector: impl Into<String>, surcharge: MethodSurcharge) -> Self {
Self {
payment_method: None,
payment_method_type: None,
card_network: None,
connector: Some(connector.into()),
merchant_connector_id: None,
conditions: Vec::new(),
surcharge,
tax_on_surcharge_percent: None,
}
}
#[must_use]
pub fn connector(mut self, connector: impl Into<String>) -> Self {
self.connector = Some(connector.into());
self
}
#[must_use]
pub fn merchant_connector_id(mut self, merchant_connector_id: impl Into<String>) -> Self {
self.merchant_connector_id = Some(merchant_connector_id.into());
self
}
#[must_use]
pub fn condition(mut self, condition: SurchargeCondition) -> Self {
self.conditions.push(condition);
self
}
#[must_use]
pub fn conditions(mut self, conditions: Vec<SurchargeCondition>) -> Self {
self.conditions = conditions;
self
}
#[must_use]
pub fn payment_method_type(mut self, payment_method_type: impl Into<String>) -> Self {
self.payment_method_type = Some(payment_method_type.into());
self
}
#[must_use]
pub fn card_network(mut self, card_network: impl Into<String>) -> Self {
self.card_network = Some(card_network.into());
self
}
#[must_use]
pub fn tax_on_surcharge_percent(mut self, percent: f64) -> Self {
self.tax_on_surcharge_percent = Some(percent);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "source", rename_all = "snake_case")]
#[non_exhaustive]
pub enum SurchargeCondition {
Metadata {
key: String,
operator: String,
value: String,
},
Currency {
operator: String,
value: String,
},
Amount {
operator: String,
value: String,
},
}
impl SurchargeCondition {
pub fn metadata(
key: impl Into<String>,
operator: impl Into<String>,
value: impl Into<String>,
) -> Self {
Self::Metadata {
key: key.into(),
operator: operator.into(),
value: value.into(),
}
}
pub fn currency(operator: impl Into<String>, value: impl Into<String>) -> Self {
Self::Currency {
operator: operator.into(),
value: value.into(),
}
}
pub fn amount(operator: impl Into<String>, value: impl Into<String>) -> Self {
Self::Amount {
operator: operator.into(),
value: value.into(),
}
}
}
pub type SurchargeDirection = String;
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct RoutingVolumeCounters {
pub profile_id: String,
#[serde(default)]
pub routing_algorithm_id: Option<String>,
#[serde(default)]
pub read_at: Option<String>,
#[serde(default = "Vec::new")]
pub counters: Vec<RoutingVolumeCounter>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct RoutingVolumeCounter {
pub window: String,
pub scope: String,
#[serde(default)]
pub payment_method: Option<String>,
#[serde(default)]
pub window_start: Option<String>,
#[serde(default)]
pub window_end: Option<String>,
#[serde(default)]
pub amount_usd: Option<f64>,
#[serde(default)]
pub threshold_currency: Option<String>,
#[serde(default)]
pub amount_in_threshold_currency: Option<f64>,
#[serde(default = "Vec::new")]
pub rules: Vec<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct RoutingConfigDeleteResponse {
pub id: String,
#[serde(default)]
pub profile_id: Option<String>,
#[serde(default)]
pub deleted: Option<bool>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct SurchargeRuleResponse {
pub name: String,
pub version: i64,
#[serde(default)]
pub profile_id: Option<String>,
#[serde(default)]
pub surcharges: Option<Vec<PerMethodSurchargeItem>>,
#[serde(default)]
pub default_surcharge: Option<MethodSurcharge>,
#[serde(default)]
pub show_surcharge_breakup_screen: Option<bool>,
#[serde(default)]
pub valid_from: Option<String>,
#[serde(default)]
pub valid_until: Option<String>,
#[serde(default = "Vec::new")]
pub warnings: Vec<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RoutingConnectorCapSchedule {
#[serde(default)]
pub revision: Option<String>,
#[serde(default = "Vec::new")]
pub entries: Vec<RoutingConnectorCapScheduleEntry>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub timezone: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub observed_at: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub next_change_at: Option<String>,
#[serde(flatten, default)]
pub extra: serde_json::Map<String, Value>,
}
impl RoutingConnectorCapSchedule {
#[must_use]
pub fn new(revision: Option<String>, entries: Vec<RoutingConnectorCapScheduleEntry>) -> Self {
Self {
revision,
entries,
timezone: None,
observed_at: None,
next_change_at: None,
extra: serde_json::Map::new(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RoutingConnectorCapScheduleEntry {
pub id: String,
pub starts_at: String,
pub ends_at: String,
pub cap: RoutingConnectorCap,
#[serde(flatten, default)]
pub extra: serde_json::Map<String, Value>,
}
impl RoutingConnectorCapScheduleEntry {
#[must_use]
pub fn new(
id: impl Into<String>,
starts_at: impl Into<String>,
ends_at: impl Into<String>,
cap: RoutingConnectorCap,
) -> Self {
Self {
id: id.into(),
starts_at: starts_at.into(),
ends_at: ends_at.into(),
cap,
extra: serde_json::Map::new(),
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct RoutingConnectorCapReset {
pub merchant_connector_id: String,
pub unit: String,
pub window: String,
#[serde(default)]
pub currency: Option<String>,
#[serde(default)]
pub cleared: Option<i64>,
#[serde(default)]
pub reserved: Option<i64>,
pub reset_at: String,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct ConnectorCapOverride {
pub merchant_connector_id: String,
pub level: String,
pub scope_id: String,
#[serde(default)]
pub scope_name: Option<String>,
pub mode: String,
#[serde(default = "Vec::new")]
pub caps: Vec<RoutingConnectorCap>,
pub modified_at: String,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct ConnectorCapOverrideList {
#[serde(default = "Vec::new")]
pub overrides: Vec<ConnectorCapOverride>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct ConnectorCapOverrides {
pub merchant_connector_id: String,
pub account_scope: String,
#[serde(default = "Vec::new")]
pub overrides: Vec<ConnectorCapOverride>,
#[serde(default = "Vec::new")]
pub shops: Vec<ConnectorCapShopLimit>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct ConnectorCapShopLimit {
pub profile_id: String,
#[serde(default)]
pub profile_name: Option<String>,
pub source: String,
#[serde(default)]
pub source_id: Option<String>,
#[serde(default)]
pub mode: Option<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct CheckoutThemeProgramResponse {
pub id: String,
pub name: String,
#[serde(default)]
pub profile_id: Option<String>,
#[serde(default)]
pub algorithm: Value,
pub version: i32,
pub is_active: bool,
pub valid_from: String,
#[serde(default)]
pub valid_until: Option<String>,
#[serde(default = "Vec::new")]
pub warnings: Vec<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct CheckoutThemeConversionResponse {
pub segment: String,
pub filters: CheckoutThemeConversionFilters,
#[serde(default = "Vec::new")]
pub cells: Vec<CheckoutThemeConversionCell>,
#[serde(default = "Vec::new")]
pub comparisons: Vec<CheckoutThemeConversionComparison>,
pub denominator: String,
#[serde(default = "Vec::new")]
pub caveats: Vec<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct CheckoutThemeConversionFilters {
#[serde(default)]
pub device_class: Option<String>,
#[serde(default)]
pub country: Option<String>,
#[serde(default)]
pub selection: Option<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct CheckoutThemeConversionCell {
#[serde(default)]
pub variant: Option<String>,
#[serde(default)]
pub appearance: Option<String>,
#[serde(default)]
pub segment: Option<String>,
#[serde(default)]
pub test_mode: Option<bool>,
pub rendered: i64,
pub paid: i64,
#[serde(default)]
pub rate: Option<f64>,
#[serde(default)]
pub interval_low: Option<f64>,
#[serde(default)]
pub interval_high: Option<f64>,
pub verdict: String,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct CheckoutThemeConversionComparison {
pub variant: String,
#[serde(default)]
pub segment: Option<String>,
#[serde(default)]
pub test_mode: Option<bool>,
#[serde(default)]
pub variant_rate: Option<f64>,
#[serde(default)]
pub baseline_rate: Option<f64>,
pub separates: bool,
#[serde(default)]
pub higher: Option<String>,
#[serde(flatten)]
pub extra: serde_json::Map<String, Value>,
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn dictionary_decodes_records_and_a_null_active_id() {
let dict: RoutingDictionary = serde_json::from_str(
r#"{
"merchant_id": "merch_1",
"active_id": null,
"records": [{
"id": "rou_1",
"profile_id": "pro_1",
"name": "EU Priority",
"kind": "priority",
"description": "EU traffic",
"created_at": 1767225600000,
"modified_at": 1767312000000,
"algorithm_for": "payment",
"brand_new_field": "keep me"
}]
}"#,
)
.unwrap();
assert_eq!(dict.active_id, None);
let record = &dict.records[0];
assert_eq!(record.kind, "priority");
assert_eq!(record.created_at, Some(1_767_225_600_000));
assert_eq!(record.algorithm_for.as_deref(), Some("payment"));
assert_eq!(record.extra["brand_new_field"], "keep me");
}
#[test]
fn merchant_routing_algorithm_keeps_the_algorithm_as_raw_json() {
let algo: MerchantRoutingAlgorithm = serde_json::from_str(
r#"{
"id": "rou_1",
"profile_id": "pro_1",
"name": "EU Priority",
"description": "",
"algorithm": {"type": "priority", "data": [{"connector": "stripe"}]},
"created_at": 1767225600000,
"modified_at": 1767225600000,
"algorithm_for": "payment"
}"#,
)
.unwrap();
assert_eq!(algo.algorithm["type"], "priority");
assert_eq!(algo.algorithm["data"][0]["connector"], "stripe");
}
#[test]
fn active_routing_config_distinguishes_the_envelope_from_a_record_list() {
let envelope: ActiveRoutingConfig = serde_json::from_str(r#"{"algorithm": null}"#).unwrap();
match envelope {
ActiveRoutingConfig::Envelope(e) => assert!(e.algorithm.is_none()),
other => panic!("expected the envelope arm, got {other:?}"),
}
let records: ActiveRoutingConfig = serde_json::from_str(
r#"[{"id":"rou_1","profile_id":"pro_1","name":"n","kind":"single"}]"#,
)
.unwrap();
match records {
ActiveRoutingConfig::Records(r) => assert_eq!(r[0].id, "rou_1"),
other => panic!("expected the records arm, got {other:?}"),
}
}
#[test]
fn default_profile_routing_prefers_the_per_profile_arm() {
let per_profile: DefaultProfileRouting = serde_json::from_str(
r#"[{"profile_id":"pro_1","connectors":[{"connector":"stripe"}]}]"#,
)
.unwrap();
match per_profile {
DefaultProfileRouting::PerProfile(p) => {
assert_eq!(p[0].profile_id, "pro_1");
assert_eq!(p[0].connectors[0].connector, "stripe");
}
other => panic!("expected the per-profile arm, got {other:?}"),
}
let connectors: DefaultProfileRouting =
serde_json::from_str(r#"[{"connector":"stripe","merchant_connector_id":"mca_1"}]"#)
.unwrap();
match connectors {
DefaultProfileRouting::Connectors(c) => {
assert_eq!(c[0].merchant_connector_id.as_deref(), Some("mca_1"));
}
other => panic!("expected the connectors arm, got {other:?}"),
}
let empty: DefaultProfileRouting = serde_json::from_str("[]").unwrap();
match empty {
DefaultProfileRouting::PerProfile(p) => assert!(p.is_empty()),
other => panic!("an empty array must take the per-profile arm, got {other:?}"),
}
}
#[test]
fn surcharge_rule_response_round_trips_the_surcharge_union() {
let rule: SurchargeRuleResponse = serde_json::from_str(
r#"{
"name": "EU surcharges",
"profile_id": null,
"version": 3,
"valid_from": "2026-01-01T00:00:00Z",
"valid_until": null,
"surcharges": [
{"payment_method": "crypto", "surcharge": {"rate": {"percent": 1.0}}},
{
"payment_method": "card",
"card_network": "Visa",
"surcharge": {"fixed": {"amount": 35}},
"tax_on_surcharge_percent": 20.0
}
],
"default_surcharge": {"fixed": {"amount": 10}},
"warnings": ["card network scoping is ignored on stripe"]
}"#,
)
.unwrap();
assert_eq!(rule.version, 3);
assert_eq!(rule.profile_id, None);
let surcharges = rule.surcharges.expect("surcharges decode");
assert_eq!(
surcharges[0].surcharge,
MethodSurcharge::Rate { percent: 1.0 }
);
assert_eq!(
surcharges[1].surcharge,
MethodSurcharge::Fixed { amount: 35 }
);
assert_eq!(surcharges[1].card_network.as_deref(), Some("Visa"));
assert_eq!(surcharges[1].tax_on_surcharge_percent, Some(20.0));
assert_eq!(
rule.default_surcharge,
Some(MethodSurcharge::Fixed { amount: 10 })
);
assert_eq!(rule.warnings.len(), 1);
}
#[test]
fn surcharge_rule_response_defaults_warnings_to_empty() {
let rule: SurchargeRuleResponse =
serde_json::from_str(r#"{"name":"n","version":1}"#).unwrap();
assert!(rule.warnings.is_empty());
assert_eq!(rule.surcharges, None);
assert_eq!(rule.valid_from, None);
}
#[test]
fn method_surcharge_serializes_with_lowercase_wire_tags() {
assert_eq!(
serde_json::to_value(MethodSurcharge::Fixed { amount: 35 }).unwrap(),
json!({"fixed": {"amount": 35}})
);
assert_eq!(
serde_json::to_value(MethodSurcharge::Rate { percent: 1.5 }).unwrap(),
json!({"rate": {"percent": 1.5}})
);
}
#[test]
fn per_method_item_omits_the_scopes_it_was_not_given() {
let item = PerMethodSurchargeItem::new("crypto", MethodSurcharge::Rate { percent: 1.0 });
assert_eq!(
serde_json::to_value(&item).unwrap(),
json!({"payment_method": "crypto", "surcharge": {"rate": {"percent": 1.0}}})
);
let scoped = PerMethodSurchargeItem::new("card", MethodSurcharge::Fixed { amount: 35 })
.card_network("Visa")
.payment_method_type("credit")
.tax_on_surcharge_percent(20.0);
let value = serde_json::to_value(&scoped).unwrap();
assert_eq!(value["card_network"], "Visa");
assert_eq!(value["payment_method_type"], "credit");
assert_eq!(value["tax_on_surcharge_percent"], 20.0);
}
}