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
//! Tokenized Checkout: Agreement request and response types.
//!
//! An *agreement* is a recurring-billing mandate set up with a customer.
//! The bKash tokenized checkout flow is:
//!
//! 1. [`CreateAgreementRequest`] (`mode = "0000"`) → [`CreateAgreementResponse`]
//!    returns a `paymentID`.
//! 2. Customer completes the wallet-side approval on their device.
//! 3. [`ExecuteAgreementRequest`] → [`ExecuteAgreementResponse`] returns
//!    the `agreementID` once the customer has approved.
//! 4. Optionally [`QueryAgreementRequest`] → [`AgreementStatusResponse`] to
//!    inspect current state.
//! 5. [`CancelAgreementRequest`] → [`CancelAgreementResponse`] revokes an
//!    existing agreement.

use serde::{Deserialize, Serialize};

use crate::models::common::{Currency, Intent, Money};

/// `mode` discriminator value for `POST /tokenized/checkout/create` when
/// creating a **recurring billing agreement**.
pub const AGREEMENT_MODE: &str = "0000";

/// Request body for creating a tokenized checkout agreement.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CreateAgreementRequest {
    /// Discriminator. Always [`AGREEMENT_MODE`] (`"0000"`).
    #[serde(rename = "mode")]
    pub mode: String,

    /// Merchant's reference for the payer (e.g. internal customer ID).
    #[serde(rename = "payerReference")]
    pub payer_reference: String,

    /// URL bKash redirects to once the customer has completed the
    /// wallet-side approval flow.
    #[serde(rename = "callbackURL")]
    pub callback_url: String,

    /// Agreement amount (the maximum amount that can be charged per cycle).
    pub amount: Money,

    /// Currency. bKash currently only supports BDT.
    pub currency: Currency,

    /// Intent. Agreements must use `Sale`.
    pub intent: Intent,

    /// Optional merchant invoice number (max length enforced by bKash).
    #[serde(
        rename = "merchantInvoiceNumber",
        skip_serializing_if = "Option::is_none"
    )]
    pub merchant_invoice_number: Option<String>,
}

impl CreateAgreementRequest {
    /// Construct a new create-agreement request with sensible defaults.
    ///
    /// `mode` is automatically set to [`AGREEMENT_MODE`] (`"0000"`) and
    /// `intent` defaults to [`Intent::Sale`].
    #[must_use]
    pub fn new(
        payer_reference: impl Into<String>,
        callback_url: impl Into<String>,
        amount: Money,
        currency: Currency,
    ) -> Self {
        Self {
            mode: AGREEMENT_MODE.to_string(),
            payer_reference: payer_reference.into(),
            callback_url: callback_url.into(),
            amount,
            currency,
            intent: Intent::Sale,
            merchant_invoice_number: None,
        }
    }

    /// Override the merchant invoice number.
    #[must_use]
    pub fn with_merchant_invoice_number(mut self, n: impl Into<String>) -> Self {
        self.merchant_invoice_number = Some(n.into());
        self
    }
}

/// Response from creating an agreement. Contains the `paymentID` that the
/// client passes to [`execute_agreement`](super::super::super::tokenized::TokenizedCheckoutClient::execute_agreement).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CreateAgreementResponse {
    /// `paymentID` to be passed to the execute step.
    #[serde(rename = "paymentID")]
    pub payment_id: String,

    /// bKash-generated URL where the customer approves the agreement.
    #[serde(rename = "bkashURL", default)]
    pub bkash_url: String,

    /// Callback URL echoed back for verification.
    #[serde(rename = "callbackURL", default)]
    pub callback_url: String,

    /// Agreement creation timestamp (ISO-8601).
    #[serde(rename = "agreementCreateTime", default)]
    pub agreement_create_time: String,

    /// Echoed `payerReference`.
    #[serde(rename = "payerReference", default)]
    pub payer_reference: String,

    /// Echoed organization short code.
    #[serde(rename = "orgShortCode", default)]
    pub org_short_code: String,

    /// Echoed currency.
    #[serde(default)]
    pub currency: Currency,

    /// Echoed intent.
    #[serde(default)]
    pub intent: Intent,

    /// Echoed merchant invoice number.
    #[serde(rename = "merchantInvoiceNumber", default)]
    pub merchant_invoice_number: String,
}

/// Request body for executing an agreement.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ExecuteAgreementRequest {
    /// `paymentID` returned by [`CreateAgreementResponse::payment_id`].
    #[serde(rename = "paymentID")]
    pub payment_id: String,
}

impl ExecuteAgreementRequest {
    /// Construct a new execute-agreement request.
    #[must_use]
    pub fn new(payment_id: impl Into<String>) -> Self {
        Self {
            payment_id: payment_id.into(),
        }
    }
}

/// Response from executing an agreement. Carries the `agreementID` used in
/// subsequent payment or query operations.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ExecuteAgreementResponse {
    /// bKash-issued agreement ID. Persist this for future payments.
    #[serde(rename = "agreementID")]
    pub agreement_id: String,

    /// `paymentID` that was executed.
    #[serde(rename = "paymentID", default)]
    pub payment_id: String,

    /// Customer MSISDN that approved the agreement (when available).
    #[serde(rename = "customerMsisdn", default)]
    pub customer_msisdn: String,

    /// Echoed `payerReference`.
    #[serde(rename = "payerReference", default)]
    pub payer_reference: String,

    /// Organization short code.
    #[serde(rename = "orgShortCode", default)]
    pub org_short_code: String,

    /// Echoed merchant invoice number.
    #[serde(rename = "merchantInvoiceNumber", default)]
    pub merchant_invoice_number: String,

    /// Timestamp the agreement was executed (ISO-8601).
    #[serde(rename = "agreementExecuteTime", default)]
    pub agreement_execute_time: String,

    /// Final agreement status string returned by bKash (e.g. `"Completed"`).
    #[serde(rename = "agreementStatus", default)]
    pub agreement_status: String,
}

/// Request body for querying an agreement.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct QueryAgreementRequest {
    /// The agreement ID returned from [`ExecuteAgreementResponse`].
    #[serde(rename = "agreementID")]
    pub agreement_id: String,
}

impl QueryAgreementRequest {
    /// Construct a new query-agreement request.
    #[must_use]
    pub fn new(agreement_id: impl Into<String>) -> Self {
        Self {
            agreement_id: agreement_id.into(),
        }
    }
}

/// Response from the agreement-status query.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct AgreementStatusResponse {
    /// The agreement ID queried.
    #[serde(rename = "agreementID")]
    pub agreement_id: String,

    /// Current status (e.g. `"Initiated"`, `"Completed"`, `"Cancelled"`).
    #[serde(rename = "agreementStatus", default)]
    pub agreement_status: String,

    /// Echoed `payerReference`.
    #[serde(rename = "payerReference", default)]
    pub payer_reference: String,

    /// Customer MSISDN.
    #[serde(rename = "customerMsisdn", default)]
    pub customer_msisdn: String,

    /// Echoed callback URL.
    #[serde(rename = "callbackURL", default)]
    pub callback_url: String,

    /// Echoed amount.
    #[serde(default)]
    pub amount: Money,

    /// Echoed currency.
    #[serde(default)]
    pub currency: Currency,

    /// Echoed intent.
    #[serde(default)]
    pub intent: Intent,

    /// Echoed merchant invoice number.
    #[serde(rename = "merchantInvoiceNumber", default)]
    pub merchant_invoice_number: String,

    /// Echoed organization short code.
    #[serde(rename = "orgShortCode", default)]
    pub org_short_code: String,

    /// Agreement creation timestamp (ISO-8601).
    #[serde(rename = "agreementCreateTime", default)]
    pub agreement_create_time: String,

    /// Agreement execution timestamp (ISO-8601).
    #[serde(rename = "agreementExecuteTime", default)]
    pub agreement_execute_time: String,
}

/// Request body for cancelling an agreement.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CancelAgreementRequest {
    /// The agreement ID to cancel.
    #[serde(rename = "agreementID")]
    pub agreement_id: String,
}

impl CancelAgreementRequest {
    /// Construct a new cancel-agreement request.
    #[must_use]
    pub fn new(agreement_id: impl Into<String>) -> Self {
        Self {
            agreement_id: agreement_id.into(),
        }
    }
}

/// Response from cancelling an agreement.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CancelAgreementResponse {
    /// The cancelled agreement ID.
    #[serde(rename = "agreementID")]
    pub agreement_id: String,

    /// Updated status (typically `"Cancelled"`).
    #[serde(rename = "agreementStatus", default)]
    pub agreement_status: String,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::common::Currency;

    #[test]
    fn create_agreement_serialises_with_mode_0000_and_intent_sale() {
        let req = CreateAgreementRequest::new(
            "cust-1",
            "https://example.test/cb",
            Money::bdt("100.00"),
            Currency::Bdt,
        );
        let json: serde_json::Value = serde_json::to_value(&req).unwrap();
        assert_eq!(json["mode"], "0000");
        assert_eq!(json["intent"], "sale");
        assert_eq!(json["payerReference"], "cust-1");
        assert_eq!(json["callbackURL"], "https://example.test/cb");
        assert_eq!(json["amount"], "100.00");
        assert_eq!(json["currency"], "BDT");
        assert!(json.get("merchantInvoiceNumber").is_none());
    }

    #[test]
    fn create_agreement_with_invoice_serialises_optional() {
        let req = CreateAgreementRequest::new(
            "cust-1",
            "https://example.test/cb",
            Money::bdt("100.00"),
            Currency::Bdt,
        )
        .with_merchant_invoice_number("INV-1");
        let json: serde_json::Value = serde_json::to_value(&req).unwrap();
        assert_eq!(json["merchantInvoiceNumber"], "INV-1");
    }

    #[test]
    fn execute_agreement_request_serialises() {
        let req = ExecuteAgreementRequest::new("TR0001");
        let json: serde_json::Value = serde_json::to_value(&req).unwrap();
        assert_eq!(json["paymentID"], "TR0001");
    }

    #[test]
    fn cancel_agreement_request_serialises() {
        let req = CancelAgreementRequest::new("AG0001");
        let json: serde_json::Value = serde_json::to_value(&req).unwrap();
        assert_eq!(json["agreementID"], "AG0001");
    }

    #[test]
    fn create_agreement_response_parses_minimal() {
        let body = r#"{
            "paymentID": "TR0001",
            "bkashURL": "https://example.test/bkash",
            "callbackURL": "https://example.test/cb",
            "agreementCreateTime": "2026-06-22T10:00:00:000 GMT+06:00",
            "payerReference": "cust-1",
            "orgShortCode": "0123",
            "currency": "BDT",
            "intent": "sale",
            "merchantInvoiceNumber": ""
        }"#;
        let resp: CreateAgreementResponse = serde_json::from_str(body).unwrap();
        assert_eq!(resp.payment_id, "TR0001");
        assert_eq!(resp.intent, Intent::Sale);
        assert_eq!(resp.currency, Currency::Bdt);
    }

    #[test]
    fn execute_agreement_response_parses_minimal() {
        let body = r#"{
            "agreementID": "AG0001",
            "paymentID": "TR0001",
            "agreementExecuteTime": "2026-06-22T10:00:00:000 GMT+06:00",
            "agreementStatus": "Completed"
        }"#;
        let resp: ExecuteAgreementResponse = serde_json::from_str(body).unwrap();
        assert_eq!(resp.agreement_id, "AG0001");
        assert_eq!(resp.agreement_status, "Completed");
    }

    #[test]
    fn cancel_agreement_response_parses() {
        let body = r#"{
            "agreementID": "AG0001",
            "agreementStatus": "Cancelled"
        }"#;
        let resp: CancelAgreementResponse = serde_json::from_str(body).unwrap();
        assert_eq!(resp.agreement_id, "AG0001");
        assert_eq!(resp.agreement_status, "Cancelled");
    }

    // ---- proptest round-trips -----------------------------------------

    use proptest::prelude::*;

    proptest! {
        #[test]
        fn create_agreement_request_roundtrip(
            payer_ref in ".*",
            callback in ".*",
            amount in ".*",
            inv in proptest::option::of(".*"),
        ) {
            let mut req = CreateAgreementRequest::new(
                payer_ref,
                callback,
                Money::new(amount),
                Currency::Bdt,
            );
            req.merchant_invoice_number = inv;
            let json = serde_json::to_string(&req).unwrap();
            let back: CreateAgreementRequest = serde_json::from_str(&json).unwrap();
            let json2 = serde_json::to_string(&back).unwrap();
            prop_assert_eq!(json, json2);
        }
    }
}