1use ring::digest::{digest, SHA512};
2use std::collections::HashMap;
3use std::str::from_utf8;
4
5pub mod bharat_qr;
6pub mod bin;
7pub mod checkout;
8pub mod downtime;
9pub mod emi;
10pub mod invoice;
11pub mod offers;
12pub mod payments;
13pub mod payouts;
14pub mod refunds;
15pub mod settlements;
16pub mod upi;
17
18#[derive(Debug, Copy, Clone)]
19pub struct PayuApiClient {
20 pub base_url: &'static str,
21 pub merchant_key: &'static str,
22 pub merchant_salt_v2: &'static str,
23}
24
25pub enum ApiEnv {
26 Test,
27 Production,
28}
29
30impl PayuApiClient {
31 pub fn new(env: ApiEnv, merchant_key: &'static str, merchant_salt_v2: &'static str) -> Self {
32 return Self {
33 base_url: match env {
34 ApiEnv::Test => "https://test.payu.in",
35 ApiEnv::Production => "https://info.payu.in",
36 },
37 merchant_key,
38 merchant_salt_v2,
39 };
40 }
41
42 fn generate_hash(
43 self,
44 mut vars: HashMap<String, String>,
45 ) -> Result<HashMap<String, String>, anyhow::Error> {
46 let command = vars.remove("command").unwrap();
47 let var = vars.iter().map(|kv| kv.1.to_string()).collect::<Vec<String>>().join("|");
48 let data = format!(
49 "{}|{}|{}|{}",
50 self.merchant_key, command, var, self.merchant_salt_v2
51 );
52 let digest = digest(&SHA512, data.as_bytes());
53 let hash = from_utf8(digest.as_ref()).unwrap().to_string();
54 let mut map: HashMap<String, String> = HashMap::new();
55 map.insert("key".to_string(), self.merchant_key.to_string());
56 map.insert("command".to_string(), command.to_string());
57 map.insert("hash".to_string(), hash);
58 return Ok(map);
59 }
60
61 pub fn validate_hash(self, vars: HashMap<String, String>, hash: &'static str) -> bool {
62 let gen_hash = self.generate_hash(vars).unwrap();
63 let generated_hash = gen_hash.get("hash").unwrap();
64 if generated_hash.to_string() == hash.to_string() {
65 return true;
66 }
67 return false;
68 }
69}