bkash-rs 0.2.1

Idiomatic async-first Rust client for the bKash Payment Gateway API.
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! HTTP transport layer built on `reqwest`.
//!
//! The [`Transport`] owns a [`reqwest::Client`] and a [`Config`], and
//! handles:
//!
//! - Attaching `Authorization: Bearer <id_token>` and `X-APP-Key` headers.
//! - Decoding the bKash response envelope (success: `statusCode == "0000"` /
//!   no `errorCode`; failure: any other body).
//! - Mapping HTTP / API errors to [`Error`].
//! - Retrying once on HTTP 401 after a force-regrant.
//! - Retrying transient failures (network, 5xx, `503` errorCode) with
//!   exponential backoff.
//! - Granting / refreshing OAuth tokens via [`TokenManager`].

use std::sync::Arc;
use std::time::Duration;

use reqwest::header::{HeaderMap, HeaderName, HeaderValue, AUTHORIZATION};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use tracing::{debug, warn};

use crate::config::{Config, Product};
use crate::error::{Error, ErrorCode};
use crate::token::{TokenCache, TokenManager, TokenTransport};

/// Header name used by bKash to carry the app key.
pub const X_APP_KEY: HeaderName = HeaderName::from_static("x-app-key");
/// Header name used by bKash to carry the merchant username.
///
/// bKash requires this header on **every** request, including the
/// token-grant call. (The official docs describe it inconsistently as a
/// body field; the live server accepts only the header form.)
pub const USERNAME: HeaderName = HeaderName::from_static("username");
/// Header name used by bKash to carry the merchant password.
pub const PASSWORD: HeaderName = HeaderName::from_static("password");
/// Header name used by bKash to carry the bearer token.
const BEARER_PREFIX: &str = "Bearer ";

/// Per-request options.
#[derive(Debug, Default, Clone)]
pub struct RequestOptions {
    /// Skip transient retries (used internally for the post-401 retry).
    pub skip_retry: bool,
    /// Override the configured timeout for this single request.
    pub timeout: Option<Duration>,
}

impl RequestOptions {
    /// Create options with no overrides.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }
}

/// HTTP transport. Cheap to clone (it owns an `Arc` internally).
#[derive(Clone)]
pub struct Transport {
    inner: Arc<TransportInner>,
}

struct TransportInner {
    http: reqwest::Client,
    config: Config,
    token_cache: TokenCache,
}

impl std::fmt::Debug for Transport {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Transport")
            .field("config", &self.inner.config)
            .field("http", &"<reqwest::Client>")
            .finish_non_exhaustive()
    }
}

impl Transport {
    /// Build a [`Transport`] from a [`Config`]. The HTTP client is created
    /// (or reused) per the config.
    pub async fn new(config: Config) -> Result<Self, Error> {
        let http = if let Some(c) = &config.http_client {
            c.clone()
        } else {
            reqwest::Client::builder()
                .timeout(config.timeout)
                .build()
                .map_err(Error::from)?
        };
        Ok(Self {
            inner: Arc::new(TransportInner {
                http,
                config,
                token_cache: TokenCache::new(),
            }),
        })
    }

    /// Access the configuration.
    #[must_use]
    pub fn config(&self) -> &Config {
        &self.inner.config
    }

    /// Access the shared token cache.
    #[must_use]
    pub fn token_cache(&self) -> &TokenCache {
        &self.inner.token_cache
    }

    /// Construct a [`TokenManager`] backed by this transport.
    #[must_use]
    pub fn token_manager(&self) -> TokenManager<Transport> {
        TokenManager::new(
            self.inner.config.clone(),
            self.inner.token_cache.clone(),
            Arc::new(self.clone()),
        )
    }

    /// Build the full URL for a `(product, path)` pair.
    pub fn url_for(&self, product: Product, path: &str) -> String {
        let base = self
            .inner
            .config
            .base_url
            .clone()
            .unwrap_or_else(|| self.inner.config.environment.base_url(product));
        let trimmed_base = base.trim_end_matches('/');
        let trimmed_path = path.trim_start_matches('/');
        let url = format!("{trimmed_base}/{trimmed_path}");
        tracing::debug!(%url, "url_for");
        url
    }

    /// Send an authenticated request. The token is obtained (and cached)
    /// before the request is sent.
    pub async fn request<P, R>(
        &self,
        product: Product,
        method: reqwest::Method,
        path: &str,
        body: Option<&P>,
    ) -> Result<R, Error>
    where
        P: Serialize + Send + Sync,
        R: DeserializeOwned,
    {
        self.request_with(product, method, path, body, &RequestOptions::default())
            .await
    }

    /// Send an authenticated request with a dynamically-formatted path.
    ///
    /// Identical to [`request`](Self::request) except the path is taken as
    /// an owned `String` so callers can interpolate path parameters
    /// (e.g. `format!("/tokenized/checkout/execute/{paymentID}")`).
    pub async fn request_path<P, R>(
        &self,
        product: Product,
        method: reqwest::Method,
        path: String,
        body: Option<&P>,
    ) -> Result<R, Error>
    where
        P: Serialize + Send + Sync,
        R: DeserializeOwned,
    {
        self.request_with_path(product, method, path, body, &RequestOptions::default())
            .await
    }

    /// Send an authenticated request with extra per-request options.
    pub async fn request_with<P, R>(
        &self,
        product: Product,
        method: reqwest::Method,
        path: &str,
        body: Option<&P>,
        options: &RequestOptions,
    ) -> Result<R, Error>
    where
        P: Serialize + Send + Sync,
        R: DeserializeOwned,
    {
        self.request_with_path(product, method, path.to_string(), body, options)
            .await
    }

    /// Send an authenticated request with extra per-request options and a
    /// dynamically-formatted path.
    pub async fn request_with_path<P, R>(
        &self,
        product: Product,
        method: reqwest::Method,
        path: String,
        body: Option<&P>,
        options: &RequestOptions,
    ) -> Result<R, Error>
    where
        P: Serialize + Send + Sync,
        R: DeserializeOwned,
    {
        let tm = self.token_manager();
        let token = tm.ensure_token(product).await?;

        let max_attempts = if options.skip_retry {
            1
        } else {
            1 + self.inner.config.max_retries
        };

        let mut attempt: u32 = 0;
        let mut force_regrant_done = false;
        let mut current_token = token;
        loop {
            attempt += 1;
            match self
                .send_once::<P, R>(
                    product,
                    method.clone(),
                    &path,
                    body,
                    &current_token.id_token,
                    options,
                )
                .await
            {
                Ok(r) => return Ok(r),
                Err(Error::Api { status: 401, .. }) if !force_regrant_done => {
                    warn!(%path, "401 from bKash; forcing re-grant and retrying");
                    self.inner.token_cache.clear().await;
                    current_token = tm.force_grant(product).await?;
                    force_regrant_done = true;
                    // The post-regrant call still participates in the
                    // transient retry loop below; do not return here.
                    continue;
                }
                Err(Error::Api {
                    status: 401,
                    message,
                    ..
                }) if force_regrant_done => {
                    // We already force-regranted once and still got 401;
                    // surface as a credential failure rather than retrying.
                    return Err(Error::Auth(format!("401 after force-regrant: {message}")));
                }
                Err(e) if e.is_transient() && attempt < max_attempts => {
                    let backoff = backoff_for(attempt);
                    warn!(
                        error = %e,
                        attempt,
                        backoff_ms = backoff.as_millis() as u64,
                        "transient error; retrying"
                    );
                    tokio::time::sleep(backoff).await;
                    continue;
                }
                Err(e) => return Err(e),
            }
        }
    }

    async fn send_once<P, R>(
        &self,
        product: Product,
        method: reqwest::Method,
        path: &str,
        body: Option<&P>,
        id_token: &str,
        options: &RequestOptions,
    ) -> Result<R, Error>
    where
        P: Serialize + Send + Sync,
        R: DeserializeOwned,
    {
        let url = self.url_for(product, path);
        let mut headers = HeaderMap::new();
        let auth_value = format!("{BEARER_PREFIX}{id_token}");
        headers.insert(
            AUTHORIZATION,
            HeaderValue::from_str(&auth_value)
                .map_err(|_| Error::Auth("invalid token characters".into()))?,
        );
        headers.insert(
            X_APP_KEY,
            HeaderValue::from_str(&self.inner.config.app_key)
                .map_err(|_| Error::Config("invalid app_key characters".into()))?,
        );
        // bKash requires `username` and `password` headers on every request,
        // including the token-grant call. The official docs sometimes show
        // them as body fields; the live server accepts only the header form.
        headers.insert(
            USERNAME,
            HeaderValue::from_str(&self.inner.config.username)
                .map_err(|_| Error::Config("invalid username characters".into()))?,
        );
        headers.insert(
            PASSWORD,
            HeaderValue::from_str(&self.inner.config.password)
                .map_err(|_| Error::Config("invalid password characters".into()))?,
        );
        headers.insert(
            reqwest::header::CONTENT_TYPE,
            HeaderValue::from_static("application/json"),
        );
        headers.insert(
            reqwest::header::ACCEPT,
            HeaderValue::from_static("application/json"),
        );

        let mut req = self.inner.http.request(method, &url).headers(headers);
        if let Some(t) = options.timeout {
            req = req.timeout(t);
        }
        if let Some(b) = body {
            req = req.json(b);
        }

        debug!(%url, "sending bKash request");
        let resp = req.send().await?;
        let status = resp.status();
        let bytes = resp.bytes().await?;

        // 401 — separate path so retry logic above can detect.
        if status.as_u16() == 401 {
            return Err(Error::Api {
                code: "401".to_string(),
                message: "Unauthorized".to_string(),
                status: 401,
            });
        }

        if status.is_success() {
            return decode_envelope(&bytes, status.as_u16());
        }

        if let Ok(env) = serde_json::from_slice::<ApiResponse<serde_json::Value>>(&bytes) {
            let code = env
                .error_code
                .clone()
                .unwrap_or_else(|| status.as_u16().to_string());
            let message = env
                .error_message
                .or(env.status_message)
                .unwrap_or_else(|| status.to_string());
            if ErrorCode::from_code(&code).is_auth() {
                return Err(Error::Auth(format!("{code}: {message}")));
            }
            return Err(Error::Api {
                code,
                message,
                status: status.as_u16(),
            });
        }

        Err(Error::Api {
            code: status.as_u16().to_string(),
            message: status.to_string(),
            status: status.as_u16(),
        })
    }
}

#[async_trait::async_trait]
impl TokenTransport for Transport {
    async fn execute_raw<P, R>(
        &self,
        product: Product,
        method: reqwest::Method,
        path: &str,
        body: Option<&P>,
    ) -> Result<R, Error>
    where
        P: Serialize + Send + Sync,
        R: DeserializeOwned,
    {
        let url = self.url_for(product, path);
        let mut headers = HeaderMap::new();
        headers.insert(
            reqwest::header::CONTENT_TYPE,
            HeaderValue::from_static("application/json"),
        );
        headers.insert(
            reqwest::header::ACCEPT,
            HeaderValue::from_static("application/json"),
        );
        // bKash requires `username` and `password` headers on the token-grant
        // call as well. The app credentials still go in the body (as a
        // `grant_type` + `app_key` + `app_secret` form), but these two
        // headers must be present or the server rejects the call with
        // HTTP 400 "Missing required request parameters: [password, username]".
        headers.insert(
            USERNAME,
            HeaderValue::from_str(&self.inner.config.username)
                .map_err(|_| Error::Config("invalid username characters".into()))?,
        );
        headers.insert(
            PASSWORD,
            HeaderValue::from_str(&self.inner.config.password)
                .map_err(|_| Error::Config("invalid password characters".into()))?,
        );

        let mut req = self.inner.http.request(method, &url).headers(headers);
        if let Some(b) = body {
            req = req.json(b);
        }
        let resp = req.send().await?;
        let status = resp.status();
        let bytes = resp.bytes().await?;
        if !status.is_success() {
            return Err(Error::Api {
                code: status.as_u16().to_string(),
                message: String::from_utf8_lossy(&bytes).into_owned(),
                status: status.as_u16(),
            });
        }
        decode_envelope(&bytes, status.as_u16())
    }
}

/// Internal response envelope. bKash success bodies set `statusCode`; failure
/// bodies set `errorCode`. Either is optional in this deserialiser to
/// tolerate the refund shape.
#[derive(Debug, Deserialize)]
struct ApiResponse<T> {
    #[serde(default, rename = "statusCode")]
    status_code: Option<String>,
    #[serde(default, rename = "statusMessage")]
    status_message: Option<String>,
    #[serde(default, rename = "errorCode")]
    error_code: Option<String>,
    #[serde(default, rename = "errorMessage")]
    error_message: Option<String>,
    #[serde(flatten)]
    data: T,
}

fn decode_envelope<R: DeserializeOwned>(bytes: &[u8], http_status: u16) -> Result<R, Error> {
    if bytes.is_empty() {
        return Err(Error::Api {
            code: "empty".into(),
            message: "empty response body".into(),
            status: http_status,
        });
    }
    let env: ApiResponse<serde_json::Value> =
        serde_json::from_slice(bytes).map_err(Error::Decode)?;
    if let Some(err_code) = env.error_code.as_deref() {
        let message = env
            .error_message
            .clone()
            .or(env.status_message.clone())
            .unwrap_or_default();
        if ErrorCode::from_code(err_code).is_auth() {
            return Err(Error::Auth(format!("{err_code}: {message}")));
        }
        return Err(Error::Api {
            code: err_code.to_string(),
            message,
            status: http_status,
        });
    }
    if env.status_code.as_deref() != Some("0000") {
        let code = env.status_code.clone().unwrap_or_else(|| "unknown".into());
        let message = env
            .status_message
            .clone()
            .unwrap_or_else(|| "no statusMessage".into());
        return Err(Error::Api {
            code,
            message,
            status: http_status,
        });
    }
    // The body has statusCode == 0000 — decode the flattened data.
    let value = env.data;
    serde_json::from_value::<R>(value).map_err(Error::Decode)
}

/// Compute exponential-backoff delay for attempt number (1-indexed).
fn backoff_for(attempt: u32) -> Duration {
    // 200ms, 400ms, 800ms, ...
    let exp = 2u64.saturating_pow(attempt.saturating_sub(1));
    Duration::from_millis(200u64 * exp)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn backoff_grows() {
        assert_eq!(backoff_for(1), Duration::from_millis(200));
        assert_eq!(backoff_for(2), Duration::from_millis(400));
        assert_eq!(backoff_for(3), Duration::from_millis(800));
    }

    #[test]
    fn request_options_default() {
        let opts = RequestOptions::default();
        assert!(!opts.skip_retry);
        assert!(opts.timeout.is_none());
    }

    #[test]
    fn decode_envelope_succeeds_on_0000() {
        let body = r#"{"statusCode":"0000","statusMessage":"OK","foo":"bar"}"#;
        let v: serde_json::Value = decode_envelope(body.as_bytes(), 200).unwrap();
        assert_eq!(v["foo"], "bar");
    }

    #[test]
    fn decode_envelope_maps_error_code() {
        let body = r#"{"errorCode":"2001","errorMessage":"Invalid App Key"}"#;
        let err = decode_envelope::<serde_json::Value>(body.as_bytes(), 200).unwrap_err();
        match err {
            Error::Auth(msg) => assert!(msg.contains("2001")),
            other => panic!("expected Auth, got {other:?}"),
        }
    }

    #[test]
    fn decode_envelope_maps_non_0000_status() {
        let body = r#"{"statusCode":"9999","statusMessage":"weird"}"#;
        let err = decode_envelope::<serde_json::Value>(body.as_bytes(), 200).unwrap_err();
        assert!(matches!(err, Error::Api { ref code, .. } if code == "9999"));
    }
}