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
//! Batch types for the Payrix API.
//!
//! Batches represent groups of transactions that are settled together,
//! typically at the end of a business day.
//!
//! **OpenAPI schema:** `batchesResponse`
use payrix_macros::PayrixEntity;
use serde::{Deserialize, Serialize};
use super::{bool_from_int_default_false, BatchStatus, PayrixId};
// =============================================================================
// BATCH ENUMS
// =============================================================================
/// Payment platform/processor values.
///
/// **OpenAPI schema:** `platformModel`
///
/// Valid values:
/// - `APPLE` - Apple Payment Processor
/// - `ELAVON` - Elavon processor
/// - `FIRSTDATA` - FirstData processor
/// - `GOOGLE` - Google Payment Processor
/// - `VANTIV` - WorldPay (Vantiv/Litle) eComm (VAP) processor
/// - `VCORE` - WorldPay (Vantiv) Core processor
/// - `TDBANKCA` - External funding with TD Bank Canada via Operating Account
/// - `WELLSACH` - Wells Fargo ACH processor
/// - `WELLSFARGO` - Wells Fargo Merchant Services processor
/// - `WFSINGLE` - WFSINGLE processor
/// - `WORLDPAY` - WORLDPAY processor
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum Platform {
/// Apple Payment Processor.
#[default]
Apple,
/// Elavon processor.
Elavon,
/// FirstData processor.
#[serde(rename = "FIRSTDATA")]
FirstData,
/// Google Payment Processor.
Google,
/// WorldPay (Vantiv/Litle) eComm (VAP) processor.
Vantiv,
/// WorldPay (Vantiv) Core processor.
#[serde(rename = "VCORE")]
VCore,
/// External funding with TD Bank Canada via Operating Account.
#[serde(rename = "TDBANKCA")]
TdBankCa,
/// Wells Fargo ACH processor.
#[serde(rename = "WELLSACH")]
WellsAch,
/// Wells Fargo Merchant Services processor.
#[serde(rename = "WELLSFARGO")]
WellsFargo,
/// WFSINGLE processor.
#[serde(rename = "WFSINGLE")]
WfSingle,
/// WORLDPAY processor.
#[serde(rename = "WORLDPAY")]
WorldPay,
}
// =============================================================================
// BATCH STRUCT
// =============================================================================
/// A Payrix batch.
///
/// Batches group transactions for settlement.
///
/// **OpenAPI schema:** `batchesResponse`
///
/// See API_INCONSISTENCIES.md for known deviations from this spec.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, PayrixEntity)]
#[payrix(create = CreateBatch, update = UpdateBatch)]
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
#[serde(rename_all = "camelCase")]
pub struct Batch {
/// The ID of this resource.
///
/// **OpenAPI type:** string
#[payrix(readonly)]
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}$`)
#[payrix(readonly)]
#[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}$`)
#[payrix(readonly)]
#[serde(default)]
pub modified: Option<String>,
/// The identifier of the Login that created this resource.
///
/// **OpenAPI type:** string (ref: creator)
#[payrix(readonly)]
#[serde(default)]
pub creator: Option<PayrixId>,
/// The identifier of the Login that last modified this resource.
///
/// **OpenAPI type:** string
#[payrix(readonly)]
#[serde(default)]
pub modifier: Option<PayrixId>,
/// The identifier of the Merchant that is associated with this Batch.
///
/// **OpenAPI type:** string (ref: batchesModelMerchant)
#[payrix(create_only)]
#[serde(default)]
pub merchant: Option<PayrixId>,
/// The date the batch was first opened.
///
/// Format: date (YYYY-MM-DD)
///
/// **OpenAPI type:** string (date)
#[serde(default)]
pub date: Option<String>,
/// The date the batch was sent to the processor for processing.
///
/// Format: date (YYYY-MM-DD)
///
/// **OpenAPI type:** string (date)
#[serde(default)]
pub processing_date: Option<String>,
/// Internal ID set for processing.
///
/// **OpenAPI type:** string
#[serde(default)]
pub processing_id: Option<String>,
/// The platform used to process this resource.
///
/// **OpenAPI type:** string (ref: platformModel)
#[serde(default)]
pub platform: Option<Platform>,
/// The current status of this Batch.
///
/// - `open` - This Batch can accept more transactions.
/// - `closed` - This Batch is closed to new transactions and ready to be sent to processor.
///
/// **OpenAPI type:** string (ref: batchStatus)
#[serde(default)]
pub status: Option<BatchStatus>,
/// The reference code of the batch.
///
/// This field is automatically generated and stored as a text string
/// (0-50 characters).
///
/// **OpenAPI type:** string
#[serde(default, rename = "ref")]
pub reference: Option<String>,
/// The merchant's reference code of the batch.
///
/// This field is stored as a text string (0-50 characters).
///
/// **OpenAPI type:** string
#[serde(default)]
pub client_ref: Option<String>,
/// The default batches close time.
///
/// 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 close_time: Option<String>,
/// Whether this resource is marked as inactive.
///
/// - `0` - Active
/// - `1` - Inactive
///
/// **OpenAPI type:** integer (ref: Inactive)
#[payrix(mutable)]
#[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)
#[payrix(mutable)]
#[serde(default, with = "bool_from_int_default_false")]
pub frozen: bool,
// =========================================================================
// NESTED RELATIONS (expandable via API)
// =========================================================================
/// Batch references associated with this batch.
///
/// **OpenAPI type:** array
#[cfg(not(feature = "sqlx"))]
#[serde(default)]
pub batch_refs: Option<Vec<serde_json::Value>>,
/// Transactions associated with this batch.
///
/// **OpenAPI type:** array of txnsResponse
#[cfg(not(feature = "sqlx"))]
#[serde(default)]
pub txns: Option<Vec<serde_json::Value>>,
}
// =============================================================================
// TESTS
// =============================================================================
#[cfg(test)]
mod tests {
use super::*;
// ==================== Platform Tests ====================
#[test]
fn platform_serialize_all_variants() {
assert_eq!(serde_json::to_string(&Platform::Apple).unwrap(), "\"APPLE\"");
assert_eq!(serde_json::to_string(&Platform::Elavon).unwrap(), "\"ELAVON\"");
assert_eq!(serde_json::to_string(&Platform::FirstData).unwrap(), "\"FIRSTDATA\"");
assert_eq!(serde_json::to_string(&Platform::Google).unwrap(), "\"GOOGLE\"");
assert_eq!(serde_json::to_string(&Platform::Vantiv).unwrap(), "\"VANTIV\"");
assert_eq!(serde_json::to_string(&Platform::VCore).unwrap(), "\"VCORE\"");
assert_eq!(serde_json::to_string(&Platform::TdBankCa).unwrap(), "\"TDBANKCA\"");
assert_eq!(serde_json::to_string(&Platform::WellsAch).unwrap(), "\"WELLSACH\"");
assert_eq!(serde_json::to_string(&Platform::WellsFargo).unwrap(), "\"WELLSFARGO\"");
assert_eq!(serde_json::to_string(&Platform::WfSingle).unwrap(), "\"WFSINGLE\"");
assert_eq!(serde_json::to_string(&Platform::WorldPay).unwrap(), "\"WORLDPAY\"");
}
#[test]
fn platform_deserialize_all_variants() {
assert_eq!(serde_json::from_str::<Platform>("\"APPLE\"").unwrap(), Platform::Apple);
assert_eq!(serde_json::from_str::<Platform>("\"ELAVON\"").unwrap(), Platform::Elavon);
assert_eq!(serde_json::from_str::<Platform>("\"FIRSTDATA\"").unwrap(), Platform::FirstData);
assert_eq!(serde_json::from_str::<Platform>("\"GOOGLE\"").unwrap(), Platform::Google);
assert_eq!(serde_json::from_str::<Platform>("\"VANTIV\"").unwrap(), Platform::Vantiv);
assert_eq!(serde_json::from_str::<Platform>("\"VCORE\"").unwrap(), Platform::VCore);
assert_eq!(serde_json::from_str::<Platform>("\"TDBANKCA\"").unwrap(), Platform::TdBankCa);
assert_eq!(serde_json::from_str::<Platform>("\"WELLSACH\"").unwrap(), Platform::WellsAch);
assert_eq!(serde_json::from_str::<Platform>("\"WELLSFARGO\"").unwrap(), Platform::WellsFargo);
assert_eq!(serde_json::from_str::<Platform>("\"WFSINGLE\"").unwrap(), Platform::WfSingle);
assert_eq!(serde_json::from_str::<Platform>("\"WORLDPAY\"").unwrap(), Platform::WorldPay);
}
#[test]
fn platform_default() {
assert_eq!(Platform::default(), Platform::Apple);
}
// ==================== Batch Struct Tests ====================
#[test]
fn batch_deserialize_full() {
let json = r#"{
"id": "t1_bat_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",
"merchant": "t1_mer_12345678901234567890123",
"date": "2024-01-01",
"processingDate": "2024-01-02",
"processingId": "proc_123456",
"platform": "VANTIV",
"status": "open",
"ref": "REF-12345",
"clientRef": "CLIENT-REF-001",
"closeTime": "2024-01-01 23:59:59",
"inactive": 0,
"frozen": 1
}"#;
let batch: Batch = serde_json::from_str(json).unwrap();
assert_eq!(batch.id.as_str(), "t1_bat_12345678901234567890123");
assert_eq!(batch.creator.as_ref().map(|c| c.as_str()), Some("t1_lgn_12345678901234567890123"));
assert_eq!(batch.modifier.as_ref().map(|m| m.as_str()), Some("t1_lgn_12345678901234567890124"));
assert_eq!(batch.merchant.as_ref().map(|m| m.as_str()), Some("t1_mer_12345678901234567890123"));
assert_eq!(batch.date, Some("2024-01-01".to_string()));
assert_eq!(batch.processing_date, Some("2024-01-02".to_string()));
assert_eq!(batch.processing_id, Some("proc_123456".to_string()));
assert_eq!(batch.platform, Some(Platform::Vantiv));
assert_eq!(batch.status, Some(BatchStatus::Open));
assert_eq!(batch.reference, Some("REF-12345".to_string()));
assert_eq!(batch.client_ref, Some("CLIENT-REF-001".to_string()));
assert_eq!(batch.close_time, Some("2024-01-01 23:59:59".to_string()));
assert!(!batch.inactive);
assert!(batch.frozen);
}
#[test]
fn batch_deserialize_minimal() {
let json = r#"{"id": "t1_bat_12345678901234567890123"}"#;
let batch: Batch = serde_json::from_str(json).unwrap();
assert_eq!(batch.id.as_str(), "t1_bat_12345678901234567890123");
assert!(batch.creator.is_none());
assert!(batch.merchant.is_none());
assert!(batch.date.is_none());
assert!(batch.platform.is_none());
assert!(batch.status.is_none());
assert!(!batch.inactive);
assert!(!batch.frozen);
}
#[test]
fn batch_bool_from_int_zero_is_false() {
let json = r#"{"id": "t1_bat_12345678901234567890123", "inactive": 0, "frozen": 0}"#;
let batch: Batch = serde_json::from_str(json).unwrap();
assert!(!batch.inactive);
assert!(!batch.frozen);
}
#[test]
fn batch_bool_from_int_one_is_true() {
let json = r#"{"id": "t1_bat_12345678901234567890123", "inactive": 1, "frozen": 1}"#;
let batch: Batch = serde_json::from_str(json).unwrap();
assert!(batch.inactive);
assert!(batch.frozen);
}
#[test]
fn batch_bool_from_int_missing_defaults_false() {
let json = r#"{"id": "t1_bat_12345678901234567890123"}"#;
let batch: Batch = serde_json::from_str(json).unwrap();
assert!(!batch.inactive);
assert!(!batch.frozen);
}
#[test]
fn batch_status_open() {
let json = r#"{"id": "t1_bat_12345678901234567890123", "status": "open"}"#;
let batch: Batch = serde_json::from_str(json).unwrap();
assert_eq!(batch.status, Some(BatchStatus::Open));
}
#[test]
fn batch_status_closed() {
let json = r#"{"id": "t1_bat_12345678901234567890123", "status": "closed"}"#;
let batch: Batch = serde_json::from_str(json).unwrap();
assert_eq!(batch.status, Some(BatchStatus::Closed));
}
#[test]
fn batch_platform_variants() {
let test_cases = [
("VANTIV", Platform::Vantiv),
("VCORE", Platform::VCore),
("WORLDPAY", Platform::WorldPay),
("WELLSFARGO", Platform::WellsFargo),
];
for (platform_str, expected_platform) in test_cases {
let json = format!(r#"{{"id": "t1_bat_12345678901234567890123", "platform": "{}"}}"#, platform_str);
let batch: Batch = serde_json::from_str(&json).unwrap();
assert_eq!(batch.platform, Some(expected_platform));
}
}
}