payrix 0.3.0

Rust client for the Payrix payment processing API
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! Disbursement types for the Payrix API.
//!
//! Disbursements represent actual fund transfers to an entity's bank account.
//!
//! **OpenAPI schema:** `disbursementsResponse`

use serde::{Deserialize, Serialize};

use super::{DisbursementStatus, PayrixId};

// =============================================================================
// DISBURSEMENT ENUMS
// =============================================================================

/// Funding status for a disbursement.
///
/// Indicates if entries were processed for this Disbursement.
///
/// **OpenAPI schema:** `FundingStatus`
///
/// Valid values:
/// - `pending` - Pending entry creation and processing
/// - `processed` - Entry created and processed
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum FundingStatus {
    /// Pending entry creation and processing.
    #[default]
    Pending,

    /// Entry created and processed.
    Processed,
}

/// Disbursement entries status.
///
/// The current status of disbursementEntries creation.
///
/// **OpenAPI schema:** `DisbursementEntriesStatus`
///
/// Valid values:
/// - `pending` - Pending entry creation and processing
/// - `processing` - Entry is still processing
/// - `processed` - Entry created and processed
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DisbursementEntriesStatus {
    /// Pending entry creation and processing.
    #[default]
    Pending,

    /// Entry is still processing.
    Processing,

    /// Entry created and processed.
    Processed,
}

// =============================================================================
// DISBURSEMENT STRUCT
// =============================================================================

/// A Payrix disbursement.
///
/// Disbursements represent actual fund transfers to an entity's bank account.
///
/// **OpenAPI schema:** `disbursementsResponse`
///
/// 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 Disbursement {
    /// 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 identifier of the Entity that owns this Disbursement.
    ///
    /// **OpenAPI type:** string (ref: disbursementsModelEntity)
    #[serde(default)]
    pub entity: Option<PayrixId>,

    /// The token of the accounts resource used for this Disbursement.
    ///
    /// **OpenAPI type:** string (ref: disbursementsModelAccount)
    #[serde(default)]
    pub account: Option<PayrixId>,

    /// A reference to the actual account or card data used for this disbursement.
    ///
    /// If someone changes the details in their bank account within Payrix Pro,
    /// the account token will point to a new account but the payment will
    /// always point to the data used for this disbursement.
    ///
    /// **OpenAPI type:** string
    #[serde(default)]
    pub payment: Option<PayrixId>,

    /// The identifier of the Payout that represents the schedule for this Disbursement.
    ///
    /// **OpenAPI type:** string (ref: disbursementsModelPayout)
    #[serde(default)]
    pub payout: Option<PayrixId>,

    /// The settlement record for this disbursement.
    ///
    /// **OpenAPI type:** string (ref: disbursementsModelSettlement)
    #[serde(default)]
    pub settlement: Option<PayrixId>,

    /// The identifier of the Statement being paid by this Disbursement.
    ///
    /// **OpenAPI type:** string (ref: disbursementsModelStatement)
    #[serde(default)]
    pub statement: Option<PayrixId>,

    /// A description of this Disbursement.
    ///
    /// **OpenAPI type:** string
    #[serde(default)]
    pub description: Option<String>,

    /// A secondary descriptor for the ACH transaction sent to the receiving bank.
    ///
    /// **OpenAPI type:** string
    #[serde(default)]
    pub secondary_descriptor: Option<String>,

    /// The total amount of this Disbursement in cents.
    ///
    /// **OpenAPI type:** integer (int64)
    #[serde(default)]
    pub amount: Option<i64>,

    /// The amount that has been returned within the disbursement, in cents.
    ///
    /// **OpenAPI type:** integer (int64)
    #[serde(default)]
    pub returned_amount: Option<i64>,

    /// The current status of this Disbursement.
    ///
    /// - `1` - Requested: The request for this Disbursement has been received.
    /// - `2` - Processing: This Disbursement is being processed to be paid out.
    /// - `3` - Processed: This Disbursement has been paid by ACH to the bank account.
    /// - `4` - Failed: A problem occurred and the payment processor has failed.
    /// - `5` - Denied: The disbursement was denied.
    /// - `6` - Returned: This Disbursement has been returned.
    ///
    /// **OpenAPI type:** integer (ref: disbursementStatus)
    #[serde(default)]
    pub status: Option<DisbursementStatus>,

    /// Indicates if entries were processed for this Disbursement.
    ///
    /// - `pending` - Pending entry creation and processing.
    /// - `processed` - Entry created and processed.
    ///
    /// **OpenAPI type:** string (ref: FundingStatus)
    #[serde(default)]
    pub funding_status: Option<FundingStatus>,

    /// A timestamp indicating when the Disbursement was processed.
    ///
    /// Format: `YYYY-MM-DD HH:MM:SS`
    ///
    /// **OpenAPI type:** string (pattern: `^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}`)
    #[serde(default)]
    pub processed: Option<String>,

    /// The current status of disbursementEntries creation.
    ///
    /// - `pending` - Pending entry creation and processing.
    /// - `processing` - Entry is still processing.
    /// - `processed` - Entry created and processed.
    ///
    /// **OpenAPI type:** string (ref: DisbursementEntriesStatus)
    #[serde(default)]
    pub disbursement_entries_status: Option<DisbursementEntriesStatus>,

    /// The last negative Entry processed included in the disbursement.
    ///
    /// **OpenAPI type:** string (ref: disbursementsModelLastNegativeEntry)
    ///
    /// NOTE: API returns "0" or "1" (boolean) when no entry exists, not a proper ID.
    #[serde(default)]
    pub last_negative_entry: Option<String>,

    /// The last negative PendingEntry processed included in the disbursement.
    ///
    /// **OpenAPI type:** string (ref: disbursementsModelLastNegativePendingEntry)
    ///
    /// NOTE: API returns "0" or "1" (boolean) when no entry exists, not a proper ID.
    #[serde(default)]
    pub last_negative_pending_entry: Option<String>,

    /// The last positive ReserveEntry processed included in the disbursement.
    ///
    /// **OpenAPI type:** string (ref: disbursementsModelLastPositiveReserveEntry)
    ///
    /// NOTE: API returns "0" or "1" (boolean) when no entry exists, not a proper ID.
    #[serde(default)]
    pub last_positive_reserve_entry: Option<String>,

    /// The last positive Entry processed included in the disbursement.
    ///
    /// **OpenAPI type:** string (ref: disbursementsModelLastPositiveEntry)
    ///
    /// NOTE: API returns "0" or "1" (boolean) when no entry exists, not a proper ID.
    #[serde(default)]
    pub last_positive_entry: Option<String>,

    /// The last positive PendingEntry processed included in the disbursement.
    ///
    /// **OpenAPI type:** string (ref: disbursementsModelLastPositivePendingEntry)
    ///
    /// NOTE: API returns "0" or "1" (boolean) when no entry exists, not a proper ID.
    #[serde(default)]
    pub last_positive_pending_entry: Option<String>,

    /// The last negative ReserveEntry processed included in the disbursement.
    ///
    /// **OpenAPI type:** string (ref: disbursementsModelLastNegativeReserveEntry)
    ///
    /// NOTE: API returns "0" or "1" (boolean) when no entry exists, not a proper ID.
    #[serde(default)]
    pub last_negative_reserve_entry: Option<String>,

    /// The currency of this Disbursement.
    ///
    /// Example: `"USD"`
    ///
    /// **OpenAPI type:** string (ref: Currency)
    #[serde(default)]
    pub currency: Option<String>,

    /// The expiration date of the related debit account.
    ///
    /// **OpenAPI type:** string
    #[serde(default)]
    pub expiration: Option<String>,

    /// Whether sameDay funding is enabled or disabled for this disbursement.
    ///
    /// - `0` - Disabled
    /// - `1` - Enabled
    ///
    /// **OpenAPI type:** integer (ref: disbursementsSameDay)
    #[serde(default)]
    pub same_day: Option<i32>,

    // =========================================================================
    // NESTED RELATIONS (expandable via API)
    // =========================================================================

    /// Adjustments associated with this disbursement.
    ///
    /// This field is populated when expanding `adjustments` in API requests.
    ///
    /// **OpenAPI type:** array of adjustmentsResponse
    #[cfg(not(feature = "sqlx"))]
    #[serde(default)]
    pub adjustments: Option<Vec<serde_json::Value>>,

    /// Disbursement entries associated with this disbursement.
    ///
    /// This field is populated when expanding `disbursementEntries` in API requests.
    ///
    /// **OpenAPI type:** array of disbursementEntriesResponse
    #[cfg(not(feature = "sqlx"))]
    #[serde(default)]
    pub disbursement_entries: Option<Vec<serde_json::Value>>,

    /// Disbursement results associated with this disbursement.
    ///
    /// This field is populated when expanding `disbursementResults` in API requests.
    ///
    /// **OpenAPI type:** array of disbursementResultsResponse
    #[cfg(not(feature = "sqlx"))]
    #[serde(default)]
    pub disbursement_results: Option<Vec<serde_json::Value>>,

    /// Entity returns associated with this disbursement.
    ///
    /// This field is populated when expanding `entityReturns` in API requests.
    ///
    /// **OpenAPI type:** array of entityReturnsResponse
    #[cfg(not(feature = "sqlx"))]
    #[serde(default)]
    pub entity_returns: Option<Vec<serde_json::Value>>,

    /// Entries associated with this disbursement.
    ///
    /// This field is populated when expanding `entries` in API requests.
    ///
    /// **OpenAPI type:** array of entriesResponse
    #[cfg(not(feature = "sqlx"))]
    #[serde(default)]
    pub entries: Option<Vec<serde_json::Value>>,

    /// Entry origins associated with this disbursement.
    ///
    /// This field is populated when expanding `entryOrigins` in API requests.
    ///
    /// **OpenAPI type:** array of entryOriginsResponse
    #[cfg(not(feature = "sqlx"))]
    #[serde(default)]
    pub entry_origins: Option<Vec<serde_json::Value>>,

    /// Funding references associated with this disbursement.
    ///
    /// This field is populated when expanding `funding` in API requests.
    ///
    /// **OpenAPI type:** array of entityRefsResponse
    #[cfg(not(feature = "sqlx"))]
    #[serde(default)]
    pub funding: Option<Vec<serde_json::Value>>,

    /// Pending entries associated with this disbursement.
    ///
    /// This field is populated when expanding `pendingEntries` in API requests.
    ///
    /// **OpenAPI type:** array of pendingEntriesResponse
    #[cfg(not(feature = "sqlx"))]
    #[serde(default)]
    pub pending_entries: Option<Vec<serde_json::Value>>,
}

// =============================================================================
// TESTS
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    // ==================== FundingStatus Tests ====================

    #[test]
    fn funding_status_serialize_all_variants() {
        assert_eq!(serde_json::to_string(&FundingStatus::Pending).unwrap(), "\"pending\"");
        assert_eq!(serde_json::to_string(&FundingStatus::Processed).unwrap(), "\"processed\"");
    }

    #[test]
    fn funding_status_deserialize_all_variants() {
        assert_eq!(serde_json::from_str::<FundingStatus>("\"pending\"").unwrap(), FundingStatus::Pending);
        assert_eq!(serde_json::from_str::<FundingStatus>("\"processed\"").unwrap(), FundingStatus::Processed);
    }

    #[test]
    fn funding_status_default() {
        assert_eq!(FundingStatus::default(), FundingStatus::Pending);
    }

    // ==================== DisbursementEntriesStatus Tests ====================

    #[test]
    fn disbursement_entries_status_serialize_all_variants() {
        assert_eq!(serde_json::to_string(&DisbursementEntriesStatus::Pending).unwrap(), "\"pending\"");
        assert_eq!(serde_json::to_string(&DisbursementEntriesStatus::Processing).unwrap(), "\"processing\"");
        assert_eq!(serde_json::to_string(&DisbursementEntriesStatus::Processed).unwrap(), "\"processed\"");
    }

    #[test]
    fn disbursement_entries_status_deserialize_all_variants() {
        assert_eq!(
            serde_json::from_str::<DisbursementEntriesStatus>("\"pending\"").unwrap(),
            DisbursementEntriesStatus::Pending
        );
        assert_eq!(
            serde_json::from_str::<DisbursementEntriesStatus>("\"processing\"").unwrap(),
            DisbursementEntriesStatus::Processing
        );
        assert_eq!(
            serde_json::from_str::<DisbursementEntriesStatus>("\"processed\"").unwrap(),
            DisbursementEntriesStatus::Processed
        );
    }

    #[test]
    fn disbursement_entries_status_default() {
        assert_eq!(DisbursementEntriesStatus::default(), DisbursementEntriesStatus::Pending);
    }

    // ==================== Disbursement Struct Tests ====================

    #[test]
    fn disbursement_deserialize_full() {
        let json = r#"{
            "id": "t1_dis_12345678901234567890123",
            "created": "2024-01-01 00:00:00.0000",
            "modified": "2024-01-16 12:00:00.0000",
            "creator": "t1_lgn_12345678901234567890123",
            "modifier": "t1_lgn_12345678901234567890124",
            "entity": "t1_ent_12345678901234567890123",
            "account": "t1_acc_12345678901234567890123",
            "payment": "t1_pay_12345678901234567890123",
            "payout": "t1_pyt_12345678901234567890123",
            "settlement": "t1_stl_12345678901234567890123",
            "statement": "t1_sta_12345678901234567890123",
            "description": "Weekly payout disbursement",
            "secondaryDescriptor": "PAYRIX PAYOUT",
            "amount": 100000,
            "returnedAmount": 0,
            "status": 3,
            "fundingStatus": "processed",
            "processed": "2024-01-16 10:30:00",
            "disbursementEntriesStatus": "processed",
            "lastNegativeEntry": "t1_ent_neg12345678901234567890",
            "lastPositiveEntry": "t1_ent_pos12345678901234567890",
            "currency": "USD",
            "expiration": "2025-12",
            "sameDay": 0
        }"#;

        let disbursement: Disbursement = serde_json::from_str(json).unwrap();
        assert_eq!(disbursement.id.as_str(), "t1_dis_12345678901234567890123");
        assert_eq!(disbursement.creator.as_ref().map(|c| c.as_str()), Some("t1_lgn_12345678901234567890123"));
        assert_eq!(disbursement.modifier.as_ref().map(|m| m.as_str()), Some("t1_lgn_12345678901234567890124"));
        assert_eq!(disbursement.entity.as_ref().map(|e| e.as_str()), Some("t1_ent_12345678901234567890123"));
        assert_eq!(disbursement.account.as_ref().map(|a| a.as_str()), Some("t1_acc_12345678901234567890123"));
        assert_eq!(disbursement.payment.as_ref().map(|p| p.as_str()), Some("t1_pay_12345678901234567890123"));
        assert_eq!(disbursement.payout.as_ref().map(|p| p.as_str()), Some("t1_pyt_12345678901234567890123"));
        assert_eq!(disbursement.settlement.as_ref().map(|s| s.as_str()), Some("t1_stl_12345678901234567890123"));
        assert_eq!(disbursement.statement.as_ref().map(|s| s.as_str()), Some("t1_sta_12345678901234567890123"));
        assert_eq!(disbursement.description, Some("Weekly payout disbursement".to_string()));
        assert_eq!(disbursement.secondary_descriptor, Some("PAYRIX PAYOUT".to_string()));
        assert_eq!(disbursement.amount, Some(100000));
        assert_eq!(disbursement.returned_amount, Some(0));
        assert_eq!(disbursement.status, Some(DisbursementStatus::Processed));
        assert_eq!(disbursement.funding_status, Some(FundingStatus::Processed));
        assert_eq!(disbursement.processed, Some("2024-01-16 10:30:00".to_string()));
        assert_eq!(disbursement.disbursement_entries_status, Some(DisbursementEntriesStatus::Processed));
        assert_eq!(disbursement.currency, Some("USD".to_string()));
        assert_eq!(disbursement.expiration, Some("2025-12".to_string()));
        assert_eq!(disbursement.same_day, Some(0));
    }

    #[test]
    fn disbursement_deserialize_minimal() {
        let json = r#"{"id": "t1_dis_12345678901234567890123"}"#;

        let disbursement: Disbursement = serde_json::from_str(json).unwrap();
        assert_eq!(disbursement.id.as_str(), "t1_dis_12345678901234567890123");
        assert!(disbursement.creator.is_none());
        assert!(disbursement.entity.is_none());
        assert!(disbursement.status.is_none());
        assert!(disbursement.amount.is_none());
    }

    #[test]
    fn disbursement_status_values() {
        // Test all disbursement status values from OpenAPI
        let json_requested = r#"{"id": "t1_dis_12345678901234567890123", "status": 1}"#;
        let json_processing = r#"{"id": "t1_dis_12345678901234567890123", "status": 2}"#;
        let json_processed = r#"{"id": "t1_dis_12345678901234567890123", "status": 3}"#;
        let json_failed = r#"{"id": "t1_dis_12345678901234567890123", "status": 4}"#;
        let json_denied = r#"{"id": "t1_dis_12345678901234567890123", "status": 5}"#;
        let json_returned = r#"{"id": "t1_dis_12345678901234567890123", "status": 6}"#;

        assert_eq!(
            serde_json::from_str::<Disbursement>(json_requested).unwrap().status,
            Some(DisbursementStatus::Requested)
        );
        assert_eq!(
            serde_json::from_str::<Disbursement>(json_processing).unwrap().status,
            Some(DisbursementStatus::Processing)
        );
        assert_eq!(
            serde_json::from_str::<Disbursement>(json_processed).unwrap().status,
            Some(DisbursementStatus::Processed)
        );
        assert_eq!(
            serde_json::from_str::<Disbursement>(json_failed).unwrap().status,
            Some(DisbursementStatus::Failed)
        );
        assert_eq!(
            serde_json::from_str::<Disbursement>(json_denied).unwrap().status,
            Some(DisbursementStatus::Denied)
        );
        assert_eq!(
            serde_json::from_str::<Disbursement>(json_returned).unwrap().status,
            Some(DisbursementStatus::Returned)
        );
    }

    #[test]
    fn disbursement_same_day_flag() {
        let json_disabled = r#"{"id": "t1_dis_12345678901234567890123", "sameDay": 0}"#;
        let json_enabled = r#"{"id": "t1_dis_12345678901234567890123", "sameDay": 1}"#;

        assert_eq!(serde_json::from_str::<Disbursement>(json_disabled).unwrap().same_day, Some(0));
        assert_eq!(serde_json::from_str::<Disbursement>(json_enabled).unwrap().same_day, Some(1));
    }
}