khqr 0.1.0

Unofficial Rust SDK for Bakong KHQR
Documentation
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Bakong KHQR SDK
//!
//! Unified SDK for Bakong KHQR (Cambodia's centralized QR payment system).
//! Provides both QR code generation and Bakong API integration.

use std::sync::Arc;

use reqwest::Client;
use serde::de::DeserializeOwned;
use serde::Serialize;
use tracing::{debug, error};

use crate::config::{BakongConfig, Environment};
use crate::crc::calculate_crc16;
use crate::error::{BakongError, Result};
use crate::types::api::*;
use crate::types::individual::IndividualInfo;
use crate::types::merchant::MerchantInfo;
use crate::types::response::QRResult;
use crate::utils::{format_amount, format_sub_tag_length_value, format_tag_length_value, md5_hash};

#[derive(Clone)]
pub struct BakongKHQR {
    config: Arc<BakongConfig>,
    http_client: Client,
}

impl BakongKHQR {
    /// Create a new BakongKHQR instance with the given API token
    pub fn new(token: impl Into<String>) -> Self {
        Self::with_config(BakongConfig::sandbox(token))
    }

    /// Create with custom configuration
    pub fn with_config(config: BakongConfig) -> Self {
        let http_client = Client::builder()
            .timeout(std::time::Duration::from_secs(config.timeout_secs))
            .build()
            .expect("Failed to create HTTP client");

        Self {
            config: Arc::new(config),
            http_client,
        }
    }

    /// Get the current token
    pub fn token(&self) -> &str {
        &self.config.token
    }

    /// Get the environment (sandbox or production)
    pub fn environment(&self) -> Environment {
        self.config.environment
    }

    /// Get the base URL
    pub fn base_url(&self) -> &str {
        self.config
            .base_url
            .as_deref()
            .unwrap_or_else(|| self.config.environment.base_url())
    }

    /// Check if using sandbox environment
    pub fn is_sandbox(&self) -> bool {
        self.config.is_sandbox()
    }

    /// Generate QR code for individual account
    pub fn generate_qr(&self, info: IndividualInfo) -> Result<QRResult> {
        self.generate_individual(&info)
    }

    /// Generate QR code for merchant account
    pub fn generate_merchant_qr(&self, info: MerchantInfo) -> Result<QRResult> {
        self.generate_merchant_internal(&info)
    }

    fn generate_individual(&self, info: &IndividualInfo) -> Result<QRResult> {
        let bakong_account_id = info
            .bakong_account_id
            .as_ref()
            .ok_or_else(|| BakongError::RequiredField("bakong_account_id".to_string()))?;

        let merchant_name = info
            .merchant_name
            .as_ref()
            .ok_or_else(|| BakongError::RequiredField("merchant_name".to_string()))?;

        let merchant_city = info
            .merchant_city
            .as_ref()
            .map(|s| s.as_str())
            .unwrap_or("Phnom Penh");

        let currency = info.currency.as_ref().map(|s| s.as_str()).unwrap_or("KHR");

        let is_dynamic = info.amount.map(|a| a > 0.0).unwrap_or(false);
        let poi = if is_dynamic { "12" } else { "11" };

        let mut qr_parts = Vec::new();

        qr_parts.push(format_tag_length_value("00", "01"));
        qr_parts.push(format_tag_length_value("01", poi));

        let account_info =
            Self::build_individual_account_info(bakong_account_id, &info.account_information);
        qr_parts.push(format_tag_length_value("29", &account_info));

        qr_parts.push(format_tag_length_value("52", "0000"));

        let currency_code = if currency.to_uppercase() == "KHR" {
            "116"
        } else {
            "840"
        };
        qr_parts.push(format_tag_length_value("53", currency_code));

        if is_dynamic {
            if let Some(amount) = info.amount {
                let amount_str = format_amount(amount, currency);
                qr_parts.push(format_tag_length_value("54", &amount_str));
            }
        }

        qr_parts.push(format_tag_length_value("58", "KH"));
        qr_parts.push(format_tag_length_value("59", merchant_name));
        qr_parts.push(format_tag_length_value("60", merchant_city));

        let additional_data = Self::build_additional_data_individual(info);
        if !additional_data.is_empty() {
            qr_parts.push(format_tag_length_value("62", &additional_data));
        }

        let payload = qr_parts.join("");
        let crc = calculate_crc16(&payload);
        let qr_string = format!("{}63{}", payload, crc);

        let md5 = md5_hash(&qr_string);

        Ok(QRResult { qr: qr_string, md5 })
    }

    fn generate_merchant_internal(&self, info: &MerchantInfo) -> Result<QRResult> {
        let bakong_account_id = info
            .bakong_account_id
            .as_ref()
            .ok_or_else(|| BakongError::RequiredField("bakong_account_id".to_string()))?;

        let merchant_id = info
            .merchant_id
            .as_ref()
            .ok_or_else(|| BakongError::RequiredField("merchant_id".to_string()))?;

        let acquiring_bank = info
            .acquiring_bank
            .as_ref()
            .ok_or_else(|| BakongError::RequiredField("acquiring_bank".to_string()))?;

        let merchant_name = info
            .merchant_name
            .as_ref()
            .ok_or_else(|| BakongError::RequiredField("merchant_name".to_string()))?;

        let merchant_city = info
            .merchant_city
            .as_ref()
            .map(|s| s.as_str())
            .unwrap_or("Phnom Penh");

        let currency = info.currency.as_ref().map(|s| s.as_str()).unwrap_or("KHR");

        let is_dynamic = info.amount.map(|a| a > 0.0).unwrap_or(false);
        let poi = if is_dynamic { "12" } else { "11" };

        let mut qr_parts = Vec::new();

        qr_parts.push(format_tag_length_value("00", "01"));
        qr_parts.push(format_tag_length_value("01", poi));

        let account_info =
            Self::build_merchant_account_info(bakong_account_id, merchant_id, acquiring_bank);
        qr_parts.push(format_tag_length_value("30", &account_info));

        if let Some(mcc) = &info.merchant_category_code {
            qr_parts.push(format_tag_length_value("52", mcc));
        } else {
            qr_parts.push(format_tag_length_value("52", "0000"));
        }

        let currency_code = if currency.to_uppercase() == "KHR" {
            "116"
        } else {
            "840"
        };
        qr_parts.push(format_tag_length_value("53", currency_code));

        if is_dynamic {
            if let Some(amount) = info.amount {
                let amount_str = format_amount(amount, currency);
                qr_parts.push(format_tag_length_value("54", &amount_str));
            }
        }

        qr_parts.push(format_tag_length_value("58", "KH"));
        qr_parts.push(format_tag_length_value("59", merchant_name));
        qr_parts.push(format_tag_length_value("60", merchant_city));

        let additional_data = Self::build_additional_data_merchant(info);
        if !additional_data.is_empty() {
            qr_parts.push(format_tag_length_value("62", &additional_data));
        }

        let payload = qr_parts.join("");
        let crc = calculate_crc16(&payload);
        let qr_string = format!("{}63{}", payload, crc);

        let md5 = md5_hash(&qr_string);

        Ok(QRResult { qr: qr_string, md5 })
    }

    fn build_individual_account_info(bakong_id: &str, account_info: &Option<String>) -> String {
        let mut parts = Vec::new();
        parts.push(format_sub_tag_length_value("29", "00", bakong_id));

        if let Some(info) = account_info {
            parts.push(format_sub_tag_length_value("29", "01", info));
        }

        parts.join("")
    }

    fn build_merchant_account_info(
        bakong_id: &str,
        merchant_id: &str,
        acquiring_bank: &str,
    ) -> String {
        let mut parts = Vec::new();
        parts.push(format_sub_tag_length_value("30", "00", bakong_id));
        parts.push(format_sub_tag_length_value("30", "01", merchant_id));
        parts.push(format_sub_tag_length_value("30", "02", acquiring_bank));
        parts.join("")
    }

    fn build_additional_data_individual(info: &IndividualInfo) -> String {
        let mut parts = Vec::new();

        if let Some(bill) = &info.bill_number {
            parts.push(format_sub_tag_length_value("62", "01", bill));
        }
        if let Some(mobile) = &info.mobile_number {
            parts.push(format_sub_tag_length_value("62", "02", mobile));
        }
        if let Some(store) = &info.store_label {
            parts.push(format_sub_tag_length_value("62", "03", store));
        }
        if let Some(terminal) = &info.terminal_label {
            parts.push(format_sub_tag_length_value("62", "04", terminal));
        }
        if let Some(purpose) = &info.purpose_of_transaction {
            parts.push(format_sub_tag_length_value("62", "05", purpose));
        }

        let mut timestamp_parts = Vec::new();
        let now = chrono::Utc::now().timestamp_millis();
        timestamp_parts.push(format_sub_tag_length_value("62", "00", &now.to_string()));

        if let Some(expire) = info.expiration_timestamp {
            timestamp_parts.push(format_sub_tag_length_value("62", "01", &expire.to_string()));
        }

        if !timestamp_parts.is_empty() {
            parts.push(format_tag_length_value("68", &timestamp_parts.join("")));
        }

        parts.join("")
    }

    fn build_additional_data_merchant(info: &MerchantInfo) -> String {
        let mut parts = Vec::new();

        if let Some(bill) = &info.bill_number {
            parts.push(format_sub_tag_length_value("62", "01", bill));
        }
        if let Some(mobile) = &info.mobile_number {
            parts.push(format_sub_tag_length_value("62", "02", mobile));
        }
        if let Some(store) = &info.store_label {
            parts.push(format_sub_tag_length_value("62", "03", store));
        }
        if let Some(terminal) = &info.terminal_label {
            parts.push(format_sub_tag_length_value("62", "04", terminal));
        }
        if let Some(purpose) = &info.purpose_of_transaction {
            parts.push(format_sub_tag_length_value("62", "05", purpose));
        }

        let mut timestamp_parts = Vec::new();
        let now = chrono::Utc::now().timestamp_millis();
        timestamp_parts.push(format_sub_tag_length_value("62", "00", &now.to_string()));

        if let Some(expire) = info.expiration_timestamp {
            timestamp_parts.push(format_sub_tag_length_value("62", "01", &expire.to_string()));
        }

        if !timestamp_parts.is_empty() {
            parts.push(format_tag_length_value("68", &timestamp_parts.join("")));
        }

        parts.join("")
    }

    // ==================== API Methods ====================

    /// Check if a Bakong account exists
    pub async fn check_bakong_account(
        &self,
        account_id: &str,
    ) -> Result<CheckBakongAccountResponse> {
        let request = CheckBakongAccountRequest {
            account_id: account_id.to_string(),
        };

        self.post("/v1/check_bakong_account", &request).await
    }

    /// Check transaction status by MD5 hash
    pub async fn check_transaction_by_md5(
        &self,
        md5: &str,
    ) -> Result<CheckTransactionByMd5Response> {
        let request = CheckTransactionByMd5Request {
            md5: md5.to_string(),
        };

        self.post("/v1/check_transaction_by_md5", &request).await
    }

    /// Check transaction status by hash
    pub async fn check_transaction_by_hash(
        &self,
        hash: &str,
    ) -> Result<CheckTransactionByMd5Response> {
        #[derive(Serialize)]
        struct Request<'a> {
            hash: &'a str,
        }

        let request = Request { hash };
        self.post("/v1/check_transaction_by_hash", &request).await
    }

    /// Check transaction status by short hash
    pub async fn check_transaction_by_short_hash(
        &self,
        short_hash: &str,
    ) -> Result<CheckTransactionByMd5Response> {
        #[derive(Serialize)]
        struct Request<'a> {
            #[serde(rename = "shortHash")]
            short_hash: &'a str,
        }

        let request = Request { short_hash };
        self.post("/v1/check_transaction_by_short_hash", &request)
            .await
    }

    /// Generate deeplink from QR code
    pub async fn generate_deeplink(
        &self,
        qr: &str,
        source_info: SourceInfo,
    ) -> Result<GenerateDeeplinkResponse> {
        let request = GenerateDeeplinkRequest {
            qr: qr.to_string(),
            source_info,
        };

        self.post("/v1/generate_deeplink_by_qr", &request).await
    }

    /// Refresh/renew API token
    pub async fn refresh_token(&self, current_token: &str) -> Result<TokenRefreshResponse> {
        let request = TokenRefreshRequest {
            token: current_token.to_string(),
        };

        self.post("/v1/renew_token", &request).await
    }

    async fn post<T: Serialize + ?Sized, R: DeserializeOwned>(
        &self,
        endpoint: &str,
        body: &T,
    ) -> Result<R> {
        let base = self.base_url();
        let url = if base.ends_with('/') {
            format!("{}{}", base, endpoint)
        } else {
            format!("{}/{}", base, endpoint)
        };

        debug!("POST {}", url);

        let response = self
            .http_client
            .post(&url)
            .header("Content-Type", "application/json")
            .header("Authorization", format!("Bearer {}", self.config.token))
            .json(body)
            .send()
            .await?;

        let status = response.status();
        let response_text = response.text().await?;

        debug!("Response status: {}, body: {}", status, response_text);

        if !status.is_success() {
            error!("HTTP error: {} - {}", status, response_text);
            return Err(BakongError::HttpError(format!(
                "{}: {}",
                status, response_text
            )));
        }

        serde_json::from_str(&response_text).map_err(|e| {
            error!("JSON parse error: {}", e);
            BakongError::JsonError(e.to_string())
        })
    }
}

impl std::fmt::Debug for BakongKHQR {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BakongKHQR")
            .field("environment", &self.config.environment)
            .finish()
    }
}