Skip to main content

rust_nicepay/utils/
config.rs

1// use serde::{Debug, Deserialize, Serialize};
2use std::collections::HashMap;
3
4pub struct Config {
5    pub is_production: bool,
6    pub private_key: String,
7    pub client_secret: String,
8    pub client_id: String,
9    pub is_cloud_server: bool,
10    pub merchant_key: String,
11    pub channel_id: String,
12}
13
14impl Config {
15    pub const SNAP_DEV_BASE_URL: &'static str = "https://dev.nicepay.co.id/nicepay";
16    pub const SNAP_PROD_BASE_URL: &'static str = "https://www.nicepay.co.id/nicepay";
17    pub const SNAP_DEV_CLOUD_BASE_URL: &'static str = "https://dev-services.nicepay.co.id/nicepay";
18    pub const SNAP_PROD_CLOUD_BASE_URL: &'static str = "https://services.nicepay.co.id/nicepay";
19
20    pub fn new() -> Self {
21        Self {
22            is_production: false,
23            private_key: String::new(),
24            client_secret: String::new(),
25            client_id: String::new(),
26            is_cloud_server: false,
27            merchant_key: String::new(),
28            channel_id: String::new(),
29        }
30    }
31
32    pub fn with_options(options: &HashMap<&str, String>) -> Self {
33        let mut config = Self::new();
34        config.set_configuration(options);
35        config
36    }
37
38    pub fn get_configuration(&self) -> HashMap<String, String> {
39        let mut config = HashMap::new();
40        config.insert("is_production".to_string(), self.is_production.to_string());
41        config.insert("private_key".to_string(), self.private_key.clone());
42        config.insert("client_secret".to_string(), self.client_secret.clone());
43        config.insert("client_id".to_string(), self.client_id.clone());
44        config.insert(
45            "is_cloud_server".to_string(),
46            self.is_cloud_server.to_string(),
47        );
48        config.insert("merchant_key".to_string(), self.merchant_key.clone());
49        config.insert("channel_id".to_string(), self.channel_id.clone());
50        config
51    }
52
53    pub fn set_configuration(&mut self, options: &HashMap<&str, String>) {
54        if let Some(is_production) = options.get("is_production") {
55            self.is_production = is_production.parse().unwrap_or(false);
56        }
57        if let Some(private_key) = options.get("private_key") {
58            self.private_key = private_key.clone();
59        }
60        if let Some(client_secret) = options.get("client_secret") {
61            self.client_secret = client_secret.clone();
62        }
63        if let Some(client_id) = options.get("client_id") {
64            self.client_id = client_id.clone();
65        }
66        if let Some(is_cloud_server) = options.get("is_cloud_server") {
67            self.is_cloud_server = is_cloud_server.parse().unwrap_or(false);
68        }
69        if let Some(merchant_key) = options.get("merchant_key") {
70            self.merchant_key = merchant_key.clone();
71        }
72        if let Some(channel_id) = options.get("channel_id") {
73            self.channel_id = channel_id.clone();
74        }
75    }
76
77    pub fn get_snap_api_base_url(&self) -> &'static str {
78        if self.is_cloud_server {
79            if self.is_production {
80                Self::SNAP_PROD_CLOUD_BASE_URL
81            } else {
82                Self::SNAP_DEV_CLOUD_BASE_URL
83            }
84        } else {
85            if self.is_production {
86                Self::SNAP_PROD_BASE_URL
87            } else {
88                Self::SNAP_DEV_BASE_URL
89            }
90        }
91    }
92}