1use reqwest::{Client, Error};
2use uuid::Uuid;
3
4
5pub mod models;
6use models::{PaymentInitializeResponse, PaymentInitializePayload, PaymentVerifyResponse};
7
8const PAYSTACK_BASE_URL: &str = "https://api.paystack.co";
9
10
11pub struct PaystackClient{
12 client : Client,
13 secret_key : String
14}
15
16
17impl PaystackClient{
18 pub fn new<S : Into<String>> (secret_key : S) -> Self {
19 Self {
20 client : Client::new(),
21 secret_key : secret_key.into()
22 }
23
24 }
25
26 pub fn generate_refrence(&self) -> String {
27 Uuid::new_v4().to_string()
28 }
29
30 pub async fn initialize_payment(
31 &self,
32 email: &str,
33 amount: f64,
34 callback_url: &str) -> Result<(String, PaymentInitializeResponse), Error>{
35
36 let reference: String = self.generate_refrence();
37
38 let payload = PaymentInitializePayload {
39 email: email.to_string(),
40 amount: (amount * 100.0) as i64,
41 reference: reference.clone(),
42 callback_url: callback_url.to_string(),
43 };
44
45 let res = self
46 .client
47 .post(format!("{}/transaction/initialize", PAYSTACK_BASE_URL))
48 .bearer_auth(&self.secret_key)
49 .json(&payload)
50 .send()
51 .await?
52 .json::<PaymentInitializeResponse>()
53 .await?;
54
55 Ok((reference, res))
56
57 }
58
59
60 pub async fn verify_payment(&self, reference:&str) -> Result<PaymentVerifyResponse, Error> {
61 let res = self
62 .client
63 .get(format!("{}/transaction/verify/{}", PAYSTACK_BASE_URL, reference))
64 .bearer_auth(&self.secret_key)
65 .send()
66 .await?
67 .json::<PaymentVerifyResponse>()
68 .await?;
69
70 Ok(res)
71
72 }
73
74
75
76}