paystack/models/
terminal.rs

1//! Terminal Models
2//! ====================
3//! This file contains the models and enums for working with the terminal endpoint.
4
5use derive_builder::Builder;
6use serde::{Deserialize, Serialize};
7use std::fmt;
8
9/// This struct is used to create an event body for sending an event to the paystack Terminal using the Paystack API.
10/// This struct should be created using the `SendEventBodyBuilder`
11/// The Builder derivation allows for the automatic implementation of the builder
12#[derive(Serialize, Builder, Debug, Default, Clone)]
13pub struct SendEventBody {
14    /// The type of event to push. Currently support `invoice` and `transaction`
15    event_type: EventType,
16    /// The action the Terminal needs to perform.
17    action: ActionType,
18    /// The parameters needed to perform the specified action.
19    data: EventData,
20}
21
22/// The data about the event to send to the API.
23/// For the `invoice` type, you need to pass the invoice id and offline reference: `{id: invoice_id, reference: offline_reference}`.
24/// For the `transaction type, you can pass the transaction id: `{id: transaction_id}`
25#[derive(Deserialize, Serialize, Clone, Debug, Default)]
26pub struct EventData {
27    /// Transaction id
28    pub id: String,
29    /// Transaction offline reference. Only required for `invoice` type.
30    pub reference: Option<String>,
31}
32
33/// The terminal update information to send to the API.
34///
35/// This is initialized with the UpdateTerminalBodyBuilder to keep the crate coherent.
36#[derive(Serialize, Default, Debug, Builder, Clone)]
37pub struct UpdateTerminalBody {
38    /// New name for the terminal
39    #[builder(default = "None")]
40    pub name: Option<String>,
41    /// New address for the terminal
42    #[builder(default = "None")]
43    pub address: Option<String>,
44}
45
46/// This struct represents the response of sending an event to the terminal.
47#[derive(Deserialize, Debug, Clone)]
48pub struct SendEventResponse {
49    /// This lets you know if your response was successful or not.
50    pub status: bool,
51    /// This is a summary of the response and its status.
52    pub message: String,
53    /// This contains the results of your request.
54    pub data: SendEventResponseData,
55}
56
57/// This struct represents the data of the event response.
58#[derive(Deserialize, Debug, Clone)]
59pub struct SendEventResponseData {
60    /// Id of the sent event.
61    pub id: String,
62}
63
64/// This struct represents the response for checking the status of an event sent to the Terminal
65#[derive(Deserialize, Debug, Clone)]
66pub struct FetchEventStatusResponse {
67    /// This lets you know if your response was successful or not.
68    pub status: bool,
69    /// This is a summary of the response and its status.
70    pub message: String,
71    /// This contains the results of your request.
72    pub data: FetchEventStatusResponseData,
73}
74
75/// This struct represents the data of the event status
76#[derive(Deserialize, Debug, Clone)]
77pub struct FetchEventStatusResponseData {
78    /// If the event has been delivered or not.
79    pub delivered: bool,
80}
81
82/// This struct represents the response for checking the status of an event sent to the Terminal
83#[derive(Deserialize, Debug, Clone)]
84pub struct FetchTerminalStatusResponse {
85    /// This lets you know if your response was successful or not.
86    pub status: bool,
87    /// This is a summary of the response and its status.
88    pub message: String,
89    /// This contains the results of your request.
90    pub data: FetchTerminalStatusResponseData,
91}
92
93/// This struct represents the data of the event status
94#[derive(Deserialize, Debug, Clone)]
95pub struct FetchTerminalStatusResponseData {
96    /// If terminal is online or not.
97    pub online: bool,
98    /// If the terminal is available or not.
99    pub available: bool,
100}
101
102/// This struct represents the response for fetch terminal.
103#[derive(Deserialize, Debug, Clone)]
104pub struct FetchTerminalResponse {
105    /// This lets you know if your response was successful or not.
106    pub status: bool,
107    /// This is a summary of the response and its status.
108    pub message: String,
109    /// This contains the results of your request.
110    pub data: FetchTerminalResponseData,
111}
112
113/// Response Data for Fetch Terminal Route
114#[derive(Deserialize, Debug, Clone)]
115pub struct FetchTerminalResponseData {
116    /// Response Id
117    pub id: i32,
118    /// Terminal serial number
119    pub serial_number: String,
120    /// Terminal make
121    pub device_make: Option<String>,
122    /// Terminal Id
123    pub terminal_id: String,
124    /// Id of integration associated with terminal
125    pub integration: i32,
126    /// Domain of integration
127    pub domain: String,
128    /// Name of terminal
129    pub name: String,
130    /// Address associated with terminal
131    pub address: Option<String>,
132    /// Terminal status
133    pub status: String,
134}
135
136/// Response of updating the terminal data
137#[derive(Deserialize, Clone)]
138pub struct TerminalResponseWithNoData {
139    /// This lets you know if your response was successful or not.
140    pub status: bool,
141    /// This is a summary of the response and its status.
142    pub message: String,
143}
144
145/// Represents the different action the Terminal needs to perform.
146///
147/// # Variants
148///
149/// - `Process`: Process action.
150/// - `View`: View action.
151/// - `Print`: Print action.
152///
153/// # Examples
154///
155/// ```
156/// use paystack::ActionType;
157///
158/// let process_action = ActionType::Process;
159/// let print_action = ActionType::Print;
160/// let view_action = ActionType::View;
161///
162/// println!("{:?}", process_action); // Prints: process
163/// ```
164#[derive(Serialize, Debug, Clone, Default)]
165pub enum ActionType {
166    /// Process the event. Valid for all event types.
167    #[default]
168    Process,
169    /// View the event. Valid for only Invoice event type.
170    View,
171    /// Print the event. Valid for only Transaction event type.
172    Print,
173}
174
175impl fmt::Display for ActionType {
176    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
177        let action = match self {
178            ActionType::Process => "process",
179            ActionType::View => "view",
180            ActionType::Print => "print",
181        };
182        write!(f, "{}", action)
183    }
184}
185
186/// Represents the different terminal event types supported by the Paystack API.
187///
188/// The `EventType` enum defines the possible even types that can be sent to Paystack Terminal.
189/// The paystack API currently supports `Invoice` and `Transaction`. This list will be periodically updated as the API evolves. Feel free to open a PR if you catch the change before us.
190///
191/// # Variants
192///
193/// - `Invoice`: Invoice event.
194/// - `Transaction`: Transaction event.
195///
196/// # Examples
197///
198/// ```
199/// use paystack::EventType;
200///
201/// let invoice_event = EventType::Invoice;
202/// let terminal_event = EventType::Transaction;
203///
204/// println!("{:?}", invoice_event); // Prints: invoice
205/// ```
206/// The example demonstrates the usage of the `EventType` enum from the Paystack crate, creating instances of each variant and printing a debug representation.
207///
208#[derive(Debug, Serialize, Clone, Default)]
209#[non_exhaustive]
210pub enum EventType {
211    /// Invoice event
212    #[default]
213    Invoice,
214    /// Transaction event
215    Transaction,
216}
217
218impl fmt::Display for EventType {
219    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
220        let terminal_type = match self {
221            EventType::Invoice => "invoice",
222            EventType::Transaction => "transaction",
223        };
224        write!(f, "{}", terminal_type)
225    }
226}