zarinpal/
extensions.rs

1//! Extension traits for [`Zarinpal`].
2
3use crate::{
4    methods::{request::RequestPayment, unverified::UnverifiedRequests, verify::VerifyPayment},
5    ZarinpalClient,
6};
7
8pub trait ZarinpalSendExtension: ZarinpalClient + Sized {
9    /// Request a payment through Zarinpal payments gateway.
10    fn request_payment<'z>(
11        &'z self,
12        amount: u64,
13        callback_url: reqwest::Url,
14        description: impl Into<String>,
15    ) -> crate::methods::request::RequestPaymentBuilder<
16        '_,
17        Self,
18        (
19            (),
20            (),
21            (u64,),
22            (String,),
23            (String,),
24            (),
25            (),
26            (Option<&Self>,),
27        ),
28    > {
29        RequestPayment::builder()
30            .zarinpal(self)
31            .amount(amount)
32            .callback_url(callback_url)
33            .description(description)
34    }
35
36    /// Verify a previously made payment requests through Zarinpal payments gateway.
37    fn verify_payment<'z>(
38        &'z self,
39        authority: impl Into<String>,
40        amount: u64,
41    ) -> crate::methods::verify::VerifyPaymentBuilder<
42        '_,
43        Self,
44        ((), (u64,), (String,), (Option<&Self>,)),
45    > {
46        VerifyPayment::builder()
47            .zarinpal(self)
48            .amount(amount)
49            .authority(authority)
50    }
51
52    /// Returns a list of at most 100 recent unverified payment requests.
53    fn unverified_requests<'z>(
54        &'z self,
55    ) -> crate::methods::unverified::UnverifiedRequestsBuilder<'_, Self, ((), (Option<&Self>,))>
56    {
57        UnverifiedRequests::builder().zarinpal(self)
58    }
59}
60
61impl<T> ZarinpalSendExtension for T where T: ZarinpalClient {}
62
63#[cfg(test)]
64mod tests {
65    use crate::{
66        methods::request::{Currency, Metadata},
67        prelude::ZarinpalSendExtension,
68        Zarinpal,
69    };
70
71    #[tokio::test]
72    async fn test_1() {
73        let zarinpal = Zarinpal::new_test().unwrap();
74
75        let unverified = zarinpal.unverified_requests().build().await;
76        println!("{unverified:#?}")
77    }
78
79    #[tokio::test]
80    async fn test_2() {
81        let zarinpal = Zarinpal::new_test().unwrap();
82
83        let unverified = zarinpal
84            .request_payment(
85                10000,
86                "https://google.com/".parse().unwrap(),
87                "Test Payment 1",
88            )
89            .build()
90            .await;
91
92        println!("{unverified:#?}")
93    }
94
95    #[tokio::test]
96    async fn test_3() {
97        let zarinpal = Zarinpal::new_test().unwrap();
98
99        let unverified = zarinpal
100            .request_payment(
101                10000,
102                "https://google.com/".parse().unwrap(),
103                "Test Payment 1",
104            )
105            // Setting some optional field
106            .currency(Currency::IRT)
107            .metadata(Metadata::builder().mobile("mobile").email("email").build())
108            .build()
109            .await;
110
111        println!("{unverified:#?}")
112    }
113}