use serde::Deserialize;
use crate::{
prelude::{ZarinResult, ZarinpalSendExtension},
Zarinpal,
};
use super::{result_code::ResultCode, RequestResult};
#[derive(Debug, Clone, Deserialize)]
pub struct Authorities {
authority: String,
amount: u64,
callback_url: String,
referer: String,
date: String,
}
impl Authorities {
pub fn authority(&self) -> &str {
self.authority.as_ref()
}
pub fn amount(&self) -> u64 {
self.amount
}
pub fn callback_url(&self) -> &str {
self.callback_url.as_ref()
}
pub fn referer(&self) -> &str {
self.referer.as_ref()
}
pub fn date(&self) -> &str {
self.date.as_ref()
}
pub async fn verify(&self, zarinpal: &Zarinpal) -> ZarinResult<crate::prelude::Verify> {
zarinpal
.verify_payment(self.authority(), self.amount())
.build()
.await
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct Unverified {
code: String,
message: String,
authorities: Vec<Authorities>,
}
impl Unverified {
pub fn authorities(&self) -> &[Authorities] {
self.authorities.as_ref()
}
}
impl RequestResult for Unverified {
fn code(&self) -> ResultCode {
self.code.parse::<i64>().unwrap().into()
}
fn message(&self) -> &str {
&self.message
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_deserialization() {
let inner_model = Unverified {
code: "100".to_string(),
message: "Success".to_string(),
authorities: vec![Authorities {
authority: "A00000000000000000000000000207288780".to_string(),
amount: 50500,
callback_url: "https://golroz.com/vpay".to_string(),
referer: "https://golroz.com/test-form/".to_string(),
date: "2020-07-01 17:33:25".to_string(),
}],
};
let from_json = serde_json::from_value::<crate::results::__private::ApiResult<Unverified>>(
serde_json::json!({
"data": {
"code": "100",
"message": "Success",
"authorities": [
{
"authority": "A00000000000000000000000000207288780",
"amount": 50500,
"callback_url": "https://golroz.com/vpay",
"referer": "https://golroz.com/test-form/",
"date": "2020-07-01 17:33:25"
},
]
}
}),
)
.unwrap();
let data: Option<Unverified> = from_json.data.into();
let data = data.unwrap();
assert_eq!(data.code, inner_model.code);
assert_eq!(data.message, inner_model.message);
assert_eq!(data.authorities.len(), 1)
}
}