paystack/models/
terminal_models.rs

1//! Terminal
2//! ==========
3//! This file contains the models and options for the Terminal endpoint of the Paystack
4
5use derive_builder::Builder;
6use serde::{Deserialize, Serialize};
7use std::fmt;
8
9/// The request body to send an event from your application to the Paystack Terminal
10#[derive(Debug, Clone, Builder, Serialize, Deserialize)]
11pub struct EventRequest {
12    #[serde(rename = "type")]
13    pub event_type: EventType,
14    pub action: TerminalAction,
15    pub data: EventRequestData,
16}
17
18/// The paramters needed to perform the specified action.
19///
20/// For the invoice type, you need to pass the invoice id and offline reference: {id: invoice_id, reference: offline_reference}.
21///
22/// For the transaction type, you can pass the transaction id: {id: transaction_id}, reference field can be `None`
23#[derive(Debug, Serialize, Deserialize, Clone, Builder)]
24pub struct EventRequestData {
25    pub id: String,
26    #[builder(setter(strip_option), default)]
27    pub reference: Option<String>,
28}
29
30/// The type of event to push.
31/// Paystack currently support `invoice` and `transaction`
32#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
33pub enum EventType {
34    Invoice,
35    Transaction,
36}
37
38/// The action the Terminal needs to perform.
39///
40/// - For the `Invoice` type, the action can either be `Process` or `View`.
41///
42/// - For the `Transaction` type, the action can either be `Process` or `Print`.
43#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
44pub enum TerminalAction {
45    Process,
46    View,
47    Print,
48}
49
50impl fmt::Display for TerminalAction {
51    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
52        let action = match self {
53            TerminalAction::Process => "process",
54            TerminalAction::Print => "print",
55            TerminalAction::View => "view",
56        };
57        write!(f, "{}", action)
58    }
59}
60
61impl fmt::Display for EventType {
62    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
63        let event = match self {
64            EventType::Invoice => "invoice",
65            EventType::Transaction => "transaction",
66        };
67        write!(f, "{}", event)
68    }
69}
70
71/// Update request for terminal
72#[derive(Debug, Serialize, Deserialize, Builder, Default)]
73pub struct UpdateTerminalRequest {
74    /// Name of the terminal
75    #[builder(setter(strip_option), default)]
76    pub address: Option<String>,
77    /// The address of the terminal
78    #[builder(setter(strip_option), default)]
79    pub name: Option<String>,
80}
81
82/// Response data for the send event route in the terminal endpoint.
83#[derive(Serialize, Deserialize, Debug, Clone, Default)]
84pub struct SendEventResponseData {
85    pub id: String,
86}
87
88/// Response data for the fetch event status route in the terminal endpoint.
89#[derive(Serialize, Deserialize, Debug, Clone, Default)]
90pub struct FetchEventStatusResponseData {
91    pub delivered: bool,
92}
93
94/// Response data for fetch terminal status route in the terminal endpoint.
95#[derive(Serialize, Deserialize, Debug, Clone, Default)]
96pub struct FetchTerminalStatusResponseData {
97    pub online: bool,
98    pub available: bool,
99}
100
101/// Response data for terminal
102#[derive(Serialize, Deserialize, Debug, Clone, Default)]
103pub struct TerminalData {
104    pub id: u64,
105    pub serial_number: String,
106    pub device_make: Option<String>,
107    pub terminal_id: String,
108    pub integration: u64,
109    pub domain: String,
110    pub name: String,
111    pub address: Option<String>,
112    pub status: String,
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118
119    #[test]
120    fn create_event_request() {
121        let even_request_data = EventRequestDataBuilder::default()
122            .id("some-id".to_string())
123            .reference("some-ref".to_string())
124            .build()
125            .expect("failed to build event request data");
126
127        let event_request = EventRequestBuilder::default()
128            .event_type(EventType::Invoice)
129            .action(TerminalAction::Process)
130            .data(even_request_data)
131            .build()
132            .expect("failed to build event request");
133
134        assert_eq!(&event_request.event_type, &EventType::Invoice);
135        assert_eq!(&event_request.action, &TerminalAction::Process);
136        assert_eq!(&event_request.data.id, "some-id");
137        assert_eq!(&event_request.data.reference, &Some("some-ref".to_string()))
138    }
139
140    #[test]
141    fn create_update_terminal_request() {
142        let update_request = UpdateTerminalRequestBuilder::default()
143            .address("some-address".to_string())
144            .name("some-name".to_string())
145            .build()
146            .expect("failed to build update terminal request");
147
148        assert_eq!(update_request.address, Some("some-address".to_string()));
149        assert_eq!(update_request.name, Some("some-name".to_string()));
150    }
151}