1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! Payout types for the Payrix API.
//!
//! Payouts define how and when funds are disbursed from a merchant's
//! operating balance to their bank account.
//!
//! **OpenAPI schema:** `payoutsResponse`
use serde::{Deserialize, Serialize};
use super::{bool_from_int_default_false, PayrixId, PayoutSchedule, PayoutUnit};
// =============================================================================
// PAYOUT STRUCT
// =============================================================================
/// A Payrix payout configuration.
///
/// Payouts define the schedule and parameters for disbursing funds from
/// a merchant's operating balance to their bank account.
///
/// **OpenAPI schema:** `payoutsResponse`
///
/// See API_INCONSISTENCIES.md for known deviations from this spec.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[serde(rename_all = "camelCase")]
pub struct Payout {
/// The ID of this resource.
///
/// **OpenAPI type:** string
pub id: PayrixId,
/// The date and time at which this resource was created.
///
/// Format: `YYYY-MM-DD HH:MM:SS.SSSS`
///
/// **OpenAPI type:** string (pattern: `^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{4}$`)
#[serde(default)]
pub created: Option<String>,
/// The date and time at which this resource was modified.
///
/// Format: `YYYY-MM-DD HH:MM:SS.SSSS`
///
/// **OpenAPI type:** string (pattern: `^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{4}$`)
#[serde(default)]
pub modified: Option<String>,
/// The identifier of the Login that created this resource.
///
/// **OpenAPI type:** string (ref: creator)
#[serde(default)]
pub creator: Option<PayrixId>,
/// The identifier of the Login that last modified this resource.
///
/// **OpenAPI type:** string
#[serde(default)]
pub modifier: Option<PayrixId>,
/// The Login that owns this resource.
///
/// **OpenAPI type:** string (ref: payoutsModelLogin)
#[serde(default)]
pub login: Option<PayrixId>,
/// The token of the Account that this Payout is associated with.
///
/// This account will either receive the funds or be debited for the funds
/// every time a Disbursement occurs, depending on the direction.
///
/// **OpenAPI type:** string (ref: payoutsModelAccount)
#[serde(default)]
pub account: Option<PayrixId>,
/// The identifier of the Entity that this Payout is associated with.
///
/// **OpenAPI type:** string (ref: payoutsModelEntity)
#[serde(default)]
pub entity: Option<PayrixId>,
/// The identifier of a Billing that this Payout is associated with.
///
/// Payout associated with a Billing record will be used to pay for Statements.
///
/// **OpenAPI type:** string (ref: payoutsModelBilling)
#[serde(default)]
pub billing: Option<PayrixId>,
/// The identifier of the PayoutFlow associated with this Payout.
///
/// **OpenAPI type:** string (ref: payoutsModelPayoutFlow)
#[serde(default)]
pub payout_flow: Option<PayrixId>,
/// The name of this Payout.
///
/// This field is stored as a text string and must be between 0 and 100 characters long.
///
/// **OpenAPI type:** string
#[serde(default)]
pub name: Option<String>,
/// A description of this Payout.
///
/// This field is stored as a text string and must be between 0 and 100 characters long.
///
/// **OpenAPI type:** string
#[serde(default)]
pub description: Option<String>,
/// The schedule that determines when the Payout should be triggered.
///
/// - `1` - Daily
/// - `2` - Weekly
/// - `3` - Monthly
/// - `4` - Annually
/// - `5` - Single (one-off payment)
///
/// **OpenAPI type:** integer (ref: payoutSchedule)
#[serde(default)]
pub schedule: Option<PayoutSchedule>,
/// A multiplier to adjust the schedule set in the 'schedule' field.
///
/// This field is specified as an integer and its value determines how
/// the interval is multiplied.
///
/// **OpenAPI type:** integer (int32)
#[serde(default)]
pub schedule_factor: Option<i32>,
/// The date on which payment of the Payout should start.
///
/// The date is specified as an eight digit integer in YYYYMMDD format,
/// for example, 20160120 for January 20, 2016.
///
/// **OpenAPI type:** integer (int32)
#[serde(default)]
pub start: Option<i32>,
/// The currency of the amount in this Payout.
///
/// This field is only required when um is set to ACTUAL.
/// If not set, disbursements for all currencies will be processed.
///
/// **OpenAPI type:** string (ref: Currency)
#[serde(default)]
pub currency: Option<String>,
/// The unit of measure for calculating the Payout amount.
///
/// - `1` - Percent (percentage in basis points)
/// - `2` - Actual (amount in cents)
/// - `3` - PercentNegative (negative percentage in basis points)
///
/// **OpenAPI type:** integer (ref: payoutUm)
#[serde(default)]
pub um: Option<PayoutUnit>,
/// The total amount of this Payout.
///
/// The units depend on the 'um' field:
/// - If um is 1 or 3: percentage in basis points
/// - If um is 2: amount in cents
///
/// **OpenAPI type:** integer (int64)
#[serde(default)]
pub amount: Option<i64>,
/// The threshold that ensures no disbursement is generated if it
/// doesn't reach the minimum value.
///
/// **OpenAPI type:** integer (int64)
#[serde(default)]
pub minimum: Option<i64>,
/// The maximum threshold for a disbursement.
///
/// Any amount exceeding this value will not be released and will
/// roll over to the next disbursement.
///
/// **OpenAPI type:** integer (int64)
#[serde(default)]
pub maximum: Option<i64>,
/// The minimum balance to maintain, despite any Payouts occurring.
///
/// If the Payout would reduce the balance to below this value, it is not processed.
/// This field is specified as an integer in cents.
///
/// **OpenAPI type:** integer (int32)
#[serde(default, rename = "float")]
pub float_balance: Option<i32>,
/// The secondary billing descriptor to appear on bank statements for the payout.
///
/// **OpenAPI type:** string
#[serde(default)]
pub secondary_descriptor: Option<String>,
/// Whether to skip the creation of disbursements on holidays and weekends.
///
/// - `0` - Do not skip (disbursement will be generated in Requested status
/// and process the next business day)
/// - `1` - Skip (not advised for weekly/monthly/yearly schedules as
/// disbursement will not be generated until next scheduled date)
///
/// **OpenAPI type:** integer (ref: SkipOffDays)
#[serde(default, with = "bool_from_int_default_false")]
pub skip_off_days: bool,
/// Whether sameDay payout is enabled.
///
/// - `0` - Disabled
/// - `1` - Enabled
///
/// **OpenAPI type:** integer (ref: SameDay)
#[serde(default, with = "bool_from_int_default_false")]
pub same_day: bool,
/// Whether this resource is marked as inactive.
///
/// - `0` - Active
/// - `1` - Inactive
///
/// **OpenAPI type:** integer (ref: Inactive)
#[serde(default, with = "bool_from_int_default_false")]
pub inactive: bool,
/// Whether this resource is marked as frozen.
///
/// - `0` - Not Frozen
/// - `1` - Frozen
///
/// **OpenAPI type:** integer (ref: Frozen)
#[serde(default, with = "bool_from_int_default_false")]
pub frozen: bool,
/// Disbursements associated with this payout.
///
/// **OpenAPI type:** array of disbursementsResponse
#[cfg(not(feature = "sqlx"))]
#[serde(default)]
pub disbursements: Option<Vec<serde_json::Value>>,
}
// =============================================================================
// TESTS
// =============================================================================
#[cfg(test)]
mod tests {
use super::*;
// ==================== Payout Struct Tests ====================
#[test]
fn payout_deserialize_full() {
let json = r#"{
"id": "t1_pay_12345678901234567890123",
"created": "2024-01-01 00:00:00.0000",
"modified": "2024-01-02 23:59:59.9999",
"creator": "t1_lgn_12345678901234567890123",
"modifier": "t1_lgn_12345678901234567890124",
"login": "t1_lgn_12345678901234567890125",
"account": "t1_acc_12345678901234567890123",
"entity": "t1_ent_12345678901234567890123",
"billing": "t1_bil_12345678901234567890123",
"payoutFlow": "t1_pfl_12345678901234567890123",
"name": "Daily Payout",
"description": "Daily operating payout",
"schedule": 1,
"scheduleFactor": 1,
"start": 20240101,
"currency": "USD",
"um": 2,
"amount": 50000,
"minimum": 5000,
"maximum": 100000,
"float": 10000,
"secondaryDescriptor": "MERCHANT PAYOUT",
"skipOffDays": 0,
"sameDay": 1,
"inactive": 0,
"frozen": 0
}"#;
let payout: Payout = serde_json::from_str(json).unwrap();
assert_eq!(payout.id.as_str(), "t1_pay_12345678901234567890123");
assert_eq!(payout.created, Some("2024-01-01 00:00:00.0000".to_string()));
assert_eq!(payout.modified, Some("2024-01-02 23:59:59.9999".to_string()));
assert_eq!(
payout.creator.as_ref().map(|c| c.as_str()),
Some("t1_lgn_12345678901234567890123")
);
assert_eq!(
payout.modifier.as_ref().map(|m| m.as_str()),
Some("t1_lgn_12345678901234567890124")
);
assert_eq!(
payout.login.as_ref().map(|l| l.as_str()),
Some("t1_lgn_12345678901234567890125")
);
assert_eq!(
payout.account.as_ref().map(|a| a.as_str()),
Some("t1_acc_12345678901234567890123")
);
assert_eq!(
payout.entity.as_ref().map(|e| e.as_str()),
Some("t1_ent_12345678901234567890123")
);
assert_eq!(
payout.billing.as_ref().map(|b| b.as_str()),
Some("t1_bil_12345678901234567890123")
);
assert_eq!(
payout.payout_flow.as_ref().map(|p| p.as_str()),
Some("t1_pfl_12345678901234567890123")
);
assert_eq!(payout.name, Some("Daily Payout".to_string()));
assert_eq!(payout.description, Some("Daily operating payout".to_string()));
assert_eq!(payout.schedule, Some(PayoutSchedule::Daily));
assert_eq!(payout.schedule_factor, Some(1));
assert_eq!(payout.start, Some(20240101));
assert_eq!(payout.currency, Some("USD".to_string()));
assert_eq!(payout.um, Some(PayoutUnit::Actual));
assert_eq!(payout.amount, Some(50000));
assert_eq!(payout.minimum, Some(5000));
assert_eq!(payout.maximum, Some(100000));
assert_eq!(payout.float_balance, Some(10000));
assert_eq!(
payout.secondary_descriptor,
Some("MERCHANT PAYOUT".to_string())
);
assert!(!payout.skip_off_days);
assert!(payout.same_day);
assert!(!payout.inactive);
assert!(!payout.frozen);
}
#[test]
fn payout_deserialize_minimal() {
let json = r#"{"id": "t1_pay_12345678901234567890123"}"#;
let payout: Payout = serde_json::from_str(json).unwrap();
assert_eq!(payout.id.as_str(), "t1_pay_12345678901234567890123");
assert!(payout.created.is_none());
assert!(payout.modified.is_none());
assert!(payout.creator.is_none());
assert!(payout.modifier.is_none());
assert!(payout.login.is_none());
assert!(payout.account.is_none());
assert!(payout.entity.is_none());
assert!(payout.billing.is_none());
assert!(payout.payout_flow.is_none());
assert!(payout.name.is_none());
assert!(payout.description.is_none());
assert!(payout.schedule.is_none());
assert!(payout.schedule_factor.is_none());
assert!(payout.start.is_none());
assert!(payout.currency.is_none());
assert!(payout.um.is_none());
assert!(payout.amount.is_none());
assert!(payout.minimum.is_none());
assert!(payout.maximum.is_none());
assert!(payout.float_balance.is_none());
assert!(payout.secondary_descriptor.is_none());
assert!(!payout.skip_off_days);
assert!(!payout.same_day);
assert!(!payout.inactive);
assert!(!payout.frozen);
}
#[test]
fn payout_bool_from_int() {
let json = r#"{
"id": "t1_pay_12345678901234567890123",
"skipOffDays": 1,
"sameDay": 1,
"inactive": 1,
"frozen": 1
}"#;
let payout: Payout = serde_json::from_str(json).unwrap();
assert!(payout.skip_off_days);
assert!(payout.same_day);
assert!(payout.inactive);
assert!(payout.frozen);
}
#[test]
fn payout_serialize_roundtrip() {
let json = r#"{
"id": "t1_pay_12345678901234567890123",
"entity": "t1_ent_12345678901234567890123",
"schedule": 1,
"um": 2,
"amount": 50000
}"#;
let payout: Payout = serde_json::from_str(json).unwrap();
let serialized = serde_json::to_string(&payout).unwrap();
let deserialized: Payout = serde_json::from_str(&serialized).unwrap();
assert_eq!(payout.id, deserialized.id);
assert_eq!(payout.entity, deserialized.entity);
assert_eq!(payout.schedule, deserialized.schedule);
assert_eq!(payout.um, deserialized.um);
assert_eq!(payout.amount, deserialized.amount);
}
#[test]
fn payout_float_field_serialization() {
// Verify float field uses correct JSON name "float" not "floatBalance"
let json = r#"{"id": "t1_pay_12345678901234567890123", "float": 5000}"#;
let payout: Payout = serde_json::from_str(json).unwrap();
assert_eq!(payout.float_balance, Some(5000));
let serialized = serde_json::to_string(&payout).unwrap();
assert!(serialized.contains("\"float\":5000"));
}
}