1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
extern crate chrono;
extern crate sha1;

use std::iter;

use chrono::prelude::*;
use log::debug;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use reqwest::header::{HeaderMap, HeaderValue};
use uuid::Uuid;

use crate::hash::HashGenerator;
use crate::hash::IyziAuthV2Generator;
use crate::options::Options;

pub const CLIENT_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const CLIENT_TITLE: &str = env!("CARGO_PKG_NAME");

const AUTHORIZATION: &str = "Authorization";
const RANDOM_HEADER_NAME: &str = "x-iyzi-rnd";
const CLIENT_VERSION_HEADER_NAME: &str = "x-iyzi-client-version";
const RANDOM_STRING_SIZE: usize = 8;

#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct IyzipayResource {
    status: Option<String>,

    error_code: Option<String>,

    error_message: Option<String>,

    error_group: Option<String>,

    locale: Option<String>,

    system_time: Option<i64>,

    conversation_id: Option<String>,
}

impl IyzipayResource {
    pub fn new() -> IyzipayResource {
        IyzipayResource::default()
    }

    pub fn get_http_headers(request: String, options: &Options) -> HeaderMap {
        let mut headers = HeaderMap::new();
        let mut rng = thread_rng();
        let random_alpanumeric: String = iter::repeat(())
            .map(|()| rng.sample(Alphanumeric))
            .take(RANDOM_STRING_SIZE)
            .collect();
        let random_string = format!(
            "{}{}",
            IyzipayResource::get_unix_timestamp_ms(),
            random_alpanumeric
        );
        debug!("Request:{}", request);

        headers.insert(
            RANDOM_HEADER_NAME,
            HeaderValue::from_str(random_string.as_str()).unwrap(),
        );
        headers.insert(
            AUTHORIZATION,
            IyzipayResource::prepare_authorization_header(request, random_string, options),
        );

        IyzipayResource::put_client_version_header(&mut headers);

        debug!(
            "Header:{}:{:?}",
            RANDOM_HEADER_NAME,
            headers.get(RANDOM_HEADER_NAME).unwrap()
        );
        debug!(
            "Header:{}:{:?}",
            AUTHORIZATION,
            headers.get(AUTHORIZATION).unwrap()
        );
        debug!(
            "Header:{}:{:?}",
            CLIENT_VERSION_HEADER_NAME,
            headers.get(CLIENT_VERSION_HEADER_NAME).unwrap()
        );
        headers
    }

    pub fn prepare_authorization_header(
        request: String,
        random_string: String,
        options: &Options,
    ) -> HeaderValue {
        let auth_str = format!(
            "{} {}:{}",
            "IYZWS",
            options.api_key(),
            HashGenerator::generate_hash(
                options.api_key(),
                options.secret_key(),
                random_string.as_str(),
                request.as_str()
            )
        );
        HeaderValue::from_str(auth_str.as_str()).unwrap()
    }

    pub fn prepare_authorization_header_v2(
        uri: String,
        request: String,
        random_string: String,
        options: &Options,
    ) -> HeaderValue {
        let auth_str = format!(
            "{} {}",
            "IYZWSv2",
            IyziAuthV2Generator::generate_auth_content(
                uri.as_str(),
                options.api_key(),
                options.secret_key(),
                random_string.as_str(),
                request.as_str()
            )
        );
        HeaderValue::from_str(auth_str.as_str()).unwrap()
    }

    pub fn get_http_headers_v2(uri: String, request: String, options: &Options) -> HeaderMap {
        let mut headers = HeaderMap::new();
        let random_string = Uuid::new_v4().to_string();
        headers.insert(
            AUTHORIZATION,
            IyzipayResource::prepare_authorization_header_v2(uri, request, random_string, options),
        );
        IyzipayResource::put_client_version_header(&mut headers);

        debug!(
            "Header:{}:{:?}",
            AUTHORIZATION,
            headers.get(AUTHORIZATION).unwrap()
        );
        debug!(
            "Header:{}:{:?}",
            CLIENT_VERSION_HEADER_NAME,
            headers.get(CLIENT_VERSION_HEADER_NAME).unwrap()
        );

        headers
    }

    fn get_unix_timestamp_ms() -> i64 {
        let now = Utc::now();
        let seconds: i64 = now.timestamp();
        let nanoseconds: i64 = now.nanosecond() as i64;
        (seconds * 1000) + (nanoseconds / 1000 / 1000)
    }

    fn put_client_version_header(headers: &mut HeaderMap<HeaderValue>) {
        let client: String = format!("{}-{}", CLIENT_TITLE, CLIENT_VERSION);
        headers.insert(
            CLIENT_VERSION_HEADER_NAME,
            HeaderValue::from_str(client.as_str()).unwrap(),
        );
    }

    pub fn set_status<T: Into<String>>(&mut self, status: T) {
        self.status = Some(status.into());
    }

    pub fn set_error_code<T: Into<String>>(&mut self, error_code: T) {
        self.error_code = Some(error_code.into());
    }

    pub fn set_error_message<T: Into<String>>(&mut self, error_message: T) {
        self.error_message = Some(error_message.into());
    }

    pub fn set_error_group<T: Into<String>>(&mut self, error_group: T) {
        self.error_group = Some(error_group.into());
    }

    pub fn set_locale<T: Into<String>>(&mut self, locale: T) {
        self.locale = Some(locale.into());
    }

    pub fn set_system_time<T: Into<i64>>(&mut self, system_time: T) {
        self.system_time = Some(system_time.into());
    }

    pub fn set_conversation_id<T: Into<String>>(&mut self, conversation_id: T) {
        self.conversation_id = Some(conversation_id.into());
    }

    pub fn status(&self) -> Option<&String> {
        self.status.as_ref()
    }
    pub fn error_code(&self) -> Option<&String> {
        self.error_code.as_ref()
    }
    pub fn error_message(&self) -> Option<&String> {
        self.error_message.as_ref()
    }
    pub fn error_group(&self) -> Option<&String> {
        self.error_group.as_ref()
    }
    pub fn locale(&self) -> Option<&String> {
        self.locale.as_ref()
    }
    pub fn system_time(&self) -> Option<&i64> {
        self.system_time.as_ref()
    }
    pub fn conversation_id(&self) -> Option<&String> {
        self.conversation_id.as_ref()
    }
}