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
//! Virtual Terminal
//! ================
//! The Virtual Terminal API allows you to accept in-person payments without a POS device.
use super::PAYSTACK_BASE_URL;
use crate::{
DestinationRequest, DestinationResponse, HttpClient, PaystackAPIError, PaystackResult,
Response, TransactionSplitResponseData, VirtualTerminalRequestData,
VirtualTerminalResponseData, VirtualTerminalStatus,
};
use serde_json::json;
use std::{marker::PhantomData, sync::Arc};
#[derive(Debug, Clone)]
pub struct VirtualTerminalEndpoints<T: HttpClient + Default> {
/// Paystack API key
key: String,
/// Base URL for the virtual terminal route
base_url: String,
/// Http client for the route
http: Arc<T>,
}
impl<T: HttpClient + Default> VirtualTerminalEndpoints<T> {
/// Creates a new VirtualTerminalEndpoints instance
///
/// # Arguments
/// * `key` - The Paystack API key
/// * `http` - The HTTP client implementation to use for API requests
///
/// # Returns
/// A new VirtualTerminalEndpoints instance
pub fn new(key: Arc<String>, http: Arc<T>) -> VirtualTerminalEndpoints<T> {
let base_url = format!("{PAYSTACK_BASE_URL}/virtual_terminal");
VirtualTerminalEndpoints {
key: key.to_string(),
base_url,
http,
}
}
/// Creates a virtual terminal on your integration
///
/// # Arguments
/// * `virtual_terminal_request` - The request data to create the virtual terminal.
/// It should be created with the `VirtualTerminalRequestDataBuilder` struct.
///
/// # Returns
/// A Result containing the virtual terminal response data or an error
pub async fn create_virtual_terminal(
&self,
virtual_terminal_request: VirtualTerminalRequestData,
) -> PaystackResult<VirtualTerminalResponseData> {
let url = &self.base_url;
let body = serde_json::to_value(virtual_terminal_request)
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
let response = self
.http
.post(url, &self.key, &body)
.await
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
let parsed_response: Response<VirtualTerminalResponseData> =
serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
Ok(parsed_response)
}
/// Lists virtual terminals available on your integration
///
/// # Arguments
/// * `status` - Filter terminal by status
/// * `per_page` - Number of records per page
///
/// # Returns
/// A Result containing a vector of virtual terminal response data or an error
pub async fn list_virtual_terminals(
&self,
status: VirtualTerminalStatus,
per_page: i32,
) -> PaystackResult<Vec<VirtualTerminalResponseData>> {
let url = &self.base_url;
let status = status.to_string();
let per_page = per_page.to_string();
let query = vec![("status", status.as_str()), ("perPage", per_page.as_str())];
let response = self
.http
.get(url, &self.key, Some(&query))
.await
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
let parsed_response: Response<Vec<VirtualTerminalResponseData>> =
serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
Ok(parsed_response)
}
/// Gets details of a virtual terminal on your integration
///
/// # Arguments
/// * `code` - Code of the virtual terminal to fetch
///
/// # Returns
/// A Result containing the virtual terminal response data or an error
pub async fn fetch_virtual_terminal(
self,
code: String,
) -> PaystackResult<VirtualTerminalResponseData> {
let url = format!("{}/{}", self.base_url, code);
let response = self
.http
.get(&url, &self.key, None)
.await
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
let parsed_response: Response<VirtualTerminalResponseData> =
serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
Ok(parsed_response)
}
/// Updates a virtual terminal on your integration
///
/// # Arguments
/// * `code` - Code of the virtual terminal to update
/// * `name` - New name for the virtual terminal
///
/// # Returns
/// A Result containing the response or an error
pub async fn update_virtual_terminal(
&self,
code: String,
name: String,
) -> PaystackResult<PhantomData<String>> {
let url = format!("{}/{}", self.base_url, code);
let body = json!({
"name": name
});
let response = self
.http
.put(&url, &self.key, &body)
.await
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
let parsed_response: Response<PhantomData<String>> = serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
Ok(parsed_response)
}
/// Deactivates a virtual terminal on your integration
///
/// # Arguments
/// * `code` - Code of the virtual terminal to deactivate
///
/// # Returns
/// A Result containing the response or an error
pub async fn deactivate_virtual_terminal(
&self,
code: String,
) -> PaystackResult<PhantomData<String>> {
let url = format!("{}/{}/deactivate", self.base_url, code);
let body = json!({}); // empty body cause the route takes none
let response = self
.http
.put(&url, &self.key, &body)
.await
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
let parsed_response: Response<PhantomData<String>> = serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
Ok(parsed_response)
}
/// Adds a WhatsApp destination number to a virtual terminal
///
/// # Arguments
/// * `code` - Code of the virtual terminal
/// * `destinations` - Vector of destination requests containing notification recipients
///
/// # Returns
/// A Result containing a vector of destination responses or an error
pub async fn assign_virtual_terminal_destination(
&self,
code: String,
destinations: Vec<DestinationRequest>,
) -> PaystackResult<Vec<DestinationResponse>> {
let url = format!("{}/{}/destination/assign", self.base_url, code);
let body = json!({
"destinations": destinations
});
let response = self
.http
.post(&url, &self.key, &body)
.await
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
let parsed_response: Response<Vec<DestinationResponse>> =
serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
Ok(parsed_response)
}
/// Removes a WhatsApp destination number from a virtual terminal
///
/// # Arguments
/// * `code` - Code of the virtual terminal
/// * `targets` - Vector of destination targets to unassign
///
/// # Returns
/// A Result containing the response or an error
pub async fn unassign_virtual_terminal_destination(
&self,
code: String,
targets: Vec<String>,
) -> PaystackResult<PhantomData<String>> {
let url = format!("{}/{}/destination/unassign", self.base_url, code);
let body = json!({
"targets": targets
});
let response = self
.http
.post(&url, &self.key, &body)
.await
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
let parsed_response: Response<PhantomData<String>> = serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
Ok(parsed_response)
}
/// Adds a split payment code to a virtual terminal
///
/// # Arguments
/// * `code` - Code of the virtual terminal
/// * `split_code` - Split code to add
///
/// # Returns
/// A Result containing the transaction split response data or an error
pub async fn add_split_code_to_virtual_terminal(
&self,
code: String,
split_code: String,
) -> PaystackResult<TransactionSplitResponseData> {
let url = format!("{}/{}/split_code", self.base_url, code);
let body = json!({
"split_code": split_code
});
let response = self
.http
.put(&url, &self.key, &body)
.await
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
let parsed_response: Response<TransactionSplitResponseData> =
serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
Ok(parsed_response)
}
/// Removes a split payment code from a virtual terminal
///
/// # Arguments
/// * `code` - Code of the virtual terminal
/// * `split_code` - Split code to remove
///
/// # Returns
/// A Result containing the response or an error
pub async fn remove_split_code_from_virtual_terminal(
&self,
code: String,
split_code: String,
) -> PaystackResult<PhantomData<String>> {
let url = format!("{}/{}/split_code", self.base_url, code);
let body = json!({
"split_code": split_code
});
let response = self
.http
.delete(&url, &self.key, &body)
.await
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
let parsed_response: Response<PhantomData<String>> = serde_json::from_str(&response)
.map_err(|e| PaystackAPIError::VirtualTerminal(e.to_string()))?;
Ok(parsed_response)
}
}