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
use reqwest::Client as ReqwestClient;
use reqwest::header::{ACCEPT, CONTENT_TYPE, HeaderMap, HeaderValue};
use serde::{Serialize, de::DeserializeOwned};
use std::sync::Arc;
use crate::config::Config;
use crate::error::Error;
use crate::models::{
CreateDepositParams, CreateWithdrawalParams, DepositResponse, IpnType, NetworkType,
TransactionStatusResponse, TransactionType, VerifyIpnParams, VerifyIpnResponse,
WalletBalanceResponse, WithdrawalResponse,
};
/// SagaPay API client
#[derive(Debug, Clone)]
pub struct Client {
config: Arc<Config>,
http_client: ReqwestClient,
}
impl Client {
/// Create a new SagaPay client
pub fn new(config: Config) -> Self {
let http_client = ReqwestClient::builder()
.timeout(std::time::Duration::from_secs(30))
.build()
.expect("Failed to create HTTP client");
Self {
config: Arc::new(config),
http_client,
}
}
/// Create a deposit address
///
/// # Example
///
/// ```no_run
/// use sagapay::{AddressType, Client, Config, CreateDepositParams, NetworkType};
///
/// #[tokio::main]
/// async fn main() -> Result<(), sagapay::Error> {
/// let client = Client::new(Config {
/// api_key: "your-api-key".to_string(),
/// api_secret: "your-api-secret".to_string(),
/// ..Config::default()
/// });
///
/// let deposit = client.create_deposit(CreateDepositParams {
/// network_type: NetworkType::BEP20,
/// contract_address: "0".to_string(),
/// amount: "1.5".to_string(),
/// ipn_url: "https://example.com/webhook".to_string(),
/// udf: Some("order-123".to_string()),
/// address_type: Some(AddressType::Temporary),
/// transfer_balance: None,
/// }).await?;
///
/// println!("Deposit address: {}", deposit.address);
/// Ok(())
/// }
/// ```
pub async fn create_deposit(
&self,
params: CreateDepositParams,
) -> Result<DepositResponse, Error> {
self.post("/create-deposit", ¶ms).await
}
/// Create a withdrawal
///
/// # Example
///
/// ```no_run
/// use sagapay::{Client, Config, CreateWithdrawalParams, NetworkType};
///
/// #[tokio::main]
/// async fn main() -> Result<(), sagapay::Error> {
/// let client = Client::new(Config {
/// api_key: "your-api-key".to_string(),
/// api_secret: "your-api-secret".to_string(),
/// ..Config::default()
/// });
///
/// let withdrawal = client.create_withdrawal(CreateWithdrawalParams {
/// network_type: NetworkType::ERC20,
/// contract_address: "0xdAC17F958D2ee523a2206206994597C13D831ec7".to_string(), // USDT
/// address: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e".to_string(),
/// amount: "10.5".to_string(),
/// ipn_url: "https://example.com/webhook".to_string(),
/// udf: Some("withdrawal-456".to_string()),
/// }).await?;
///
/// println!("Withdrawal ID: {}", withdrawal.id);
/// Ok(())
/// }
/// ```
pub async fn create_withdrawal(
&self,
params: CreateWithdrawalParams,
) -> Result<WithdrawalResponse, Error> {
self.post("/create-withdrawal", ¶ms).await
}
/// Check transaction status by address or ID
///
/// # Example
///
/// ```no_run
/// use sagapay::{Client, Config, TransactionType};
///
/// #[tokio::main]
/// async fn main() -> Result<(), sagapay::Error> {
/// let client = Client::new(Config {
/// api_key: "your-api-key".to_string(),
/// api_secret: "your-api-secret".to_string(),
/// ..Config::default()
/// });
///
/// // By address
/// let status = client.check_transaction_status(
/// TransactionType::Deposit,
/// Some("0x742d35Cc6634C0532925a3b844Bc454e4438f44e"),
/// None,
/// ).await?;
///
/// // By ID
/// let status = client.check_transaction_status(
/// TransactionType::Deposit,
/// None,
/// Some("deposit-uuid"),
/// ).await?;
///
/// println!("Transaction count: {}", status.count);
/// Ok(())
/// }
/// ```
pub async fn check_transaction_status(
&self,
transaction_type: TransactionType,
address: Option<&str>,
id: Option<&str>,
) -> Result<TransactionStatusResponse, Error> {
if address.is_none() && id.is_none() {
return Err(Error::MissingParameter("address or id".to_string()));
}
let tx_type = match transaction_type {
TransactionType::Deposit => "deposit",
TransactionType::Withdrawal => "withdrawal",
};
let mut params: Vec<(&str, &str)> = vec![("type", tx_type)];
if let Some(addr) = address {
params.push(("address", addr));
}
if let Some(txn_id) = id {
params.push(("id", txn_id));
}
self.get_with_query("/check-transaction-status", ¶ms)
.await
}
/// Fetch wallet balance
///
/// # Example
///
/// ```no_run
/// use sagapay::{Client, Config, NetworkType};
///
/// #[tokio::main]
/// async fn main() -> Result<(), sagapay::Error> {
/// let client = Client::new(Config {
/// api_key: "your-api-key".to_string(),
/// api_secret: "your-api-secret".to_string(),
/// ..Config::default()
/// });
///
/// let address = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e";
/// let network_type = NetworkType::ERC20;
/// let contract_address = Some("0xdAC17F958D2ee523a2206206994597C13D831ec7"); // USDT
///
/// let balance = client.fetch_wallet_balance(address, network_type, contract_address).await?;
///
/// println!("Token: {} ({})", balance.token.symbol, balance.token.name);
/// println!("Balance: {}", balance.balance.formatted);
///
/// // Pass `None` (or `Some("0")`) for the network's native coin
/// let native = client
/// .fetch_wallet_balance(address, NetworkType::ERC20, None)
/// .await?;
///
/// println!("Native balance: {}", native.balance.formatted);
///
/// Ok(())
/// }
/// ```
pub async fn fetch_wallet_balance(
&self,
address: &str,
network_type: NetworkType,
contract_address: Option<&str>,
) -> Result<WalletBalanceResponse, Error> {
let network_type = network_type.to_string();
let mut params: Vec<(&str, &str)> =
vec![("address", address), ("networkType", &network_type)];
if let Some(contract) = contract_address {
params.push(("contractAddress", contract));
}
self.get_with_query("/fetch-wallet-balance", ¶ms).await
}
/// Confirm an IPN with the SagaPay API
///
/// This is the primary way to validate a notification: the API re-checks
/// the transaction against its own records and answers with `verified`.
/// Unlike every other endpoint, `/verify-ipn` takes no auth headers — the
/// API key and secret are read from the client [`Config`] and sent in the
/// request body.
///
/// # Example
///
/// ```no_run
/// use sagapay::{Client, Config, IpnType, VerifyIpnParams};
///
/// #[tokio::main]
/// async fn main() -> Result<(), sagapay::Error> {
/// let client = Client::new(Config {
/// api_key: "your-api-key".to_string(),
/// api_secret: "your-api-secret".to_string(),
/// ..Config::default()
/// });
///
/// let result = client.verify_ipn(VerifyIpnParams {
/// txn_hash: "0xabc123".to_string(),
/// ipn_type: IpnType::Deposit,
/// amount: "1.5".to_string(),
/// address: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e".to_string(),
/// }).await?;
///
/// println!("Verified: {}", result.verified);
/// Ok(())
/// }
/// ```
pub async fn verify_ipn(&self, params: VerifyIpnParams) -> Result<VerifyIpnResponse, Error> {
/// Request body for `/verify-ipn`, which carries the credentials inline
#[derive(Serialize)]
struct VerifyIpnRequest<'a> {
#[serde(rename = "txnHash")]
txn_hash: &'a str,
#[serde(rename = "type")]
ipn_type: &'a IpnType,
amount: &'a str,
address: &'a str,
#[serde(rename = "apiKey")]
api_key: &'a str,
#[serde(rename = "apiSecret")]
api_secret: &'a str,
}
let body = VerifyIpnRequest {
txn_hash: ¶ms.txn_hash,
ipn_type: ¶ms.ipn_type,
amount: ¶ms.amount,
address: ¶ms.address,
api_key: &self.config.api_key,
api_secret: &self.config.api_secret,
};
let url = format!("{}/verify-ipn", self.config.base_url);
// Deliberately built without the x-api-key / x-api-secret headers
let response = self
.http_client
.post(&url)
.header(ACCEPT, HeaderValue::from_static("application/json"))
.json(&body)
.send()
.await?;
self.handle_response(response).await
}
/// Get the client's API key
pub fn api_key(&self) -> &str {
&self.config.api_key
}
/// Get the client's API secret
pub fn api_secret(&self) -> &str {
&self.config.api_secret
}
/// Make a GET request with query parameters
async fn get_with_query<T: DeserializeOwned, Q: Serialize + ?Sized>(
&self,
path: &str,
query: &Q,
) -> Result<T, Error> {
let url = format!("{}{}", self.config.base_url, path);
let headers = self.build_headers()?;
let response = self
.http_client
.get(&url)
.headers(headers)
.query(query)
.send()
.await?;
self.handle_response(response).await
}
/// Make a POST request
async fn post<T: DeserializeOwned, D: Serialize + ?Sized>(
&self,
path: &str,
data: &D,
) -> Result<T, Error> {
let url = format!("{}{}", self.config.base_url, path);
let headers = self.build_headers()?;
let response = self
.http_client
.post(&url)
.headers(headers)
.json(data)
.send()
.await?;
self.handle_response(response).await
}
/// Build common headers for requests
fn build_headers(&self) -> Result<HeaderMap, Error> {
let mut headers = HeaderMap::new();
headers.insert(
"x-api-key",
HeaderValue::from_str(&self.config.api_key)
.map_err(|e| Error::InvalidParameter(format!("api_key: {}", e)))?,
);
headers.insert(
"x-api-secret",
HeaderValue::from_str(&self.config.api_secret)
.map_err(|e| Error::InvalidParameter(format!("api_secret: {}", e)))?,
);
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
headers.insert(ACCEPT, HeaderValue::from_static("application/json"));
Ok(headers)
}
/// Handle API response
async fn handle_response<T: DeserializeOwned>(
&self,
response: reqwest::Response,
) -> Result<T, Error> {
let status = response.status();
let body = response.text().await?;
if !status.is_success() {
let error_response: serde_json::Value = match serde_json::from_str(&body) {
Ok(json) => json,
Err(_) => {
return Err(Error::ApiError {
status_code: status.as_u16(),
message: body,
error_code: None,
});
}
};
// Error bodies are shaped { "error": "<human readable message>" }
let message = error_response
.get("error")
.and_then(|m| m.as_str())
.unwrap_or("Unknown error")
.to_string();
return Err(Error::ApiError {
status_code: status.as_u16(),
message,
error_code: None,
});
}
match serde_json::from_str(&body) {
Ok(data) => Ok(data),
Err(e) => Err(Error::JsonError(e)),
}
}
}