bayarcash 1.0.1

Rust SDK for the Bayarcash payment gateway — a feature-parity, idiomatic port of the official Bayarcash PHP SDK. Supports API v2 (default) and v3.
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! The [`Bayarcash`] client and its operations.

use std::collections::BTreeMap;
use std::time::Duration;

use reqwest::Method;
use serde::de::DeserializeOwned;
use serde_json::Value;

use crate::callback::{self, CallbackData};
use crate::channels;
use crate::checksum;
use crate::config::ApiVersion;
use crate::error::{Error, Result, ValidationErrors};
use crate::models::{
    FpxBank, Mandate, MandateApplication, PaymentIntent, Portal, Transaction, TransactionList,
};
use crate::requests::{
    FpxDirectDebitEnrolmentRequest, FpxDirectDebitMaintenanceRequest,
    FpxDirectDebitTerminationRequest, PaymentIntentRequest, TransactionFilters,
};

/// The Bayarcash API client.
///
/// Construct with [`Bayarcash::new`], then configure fluently:
///
/// ```
/// use bayarcash::{Bayarcash, ApiVersion};
///
/// let client = Bayarcash::new("YOUR_API_TOKEN")
///     .use_sandbox()          // remove in production
///     .set_api_version(ApiVersion::V3)
///     .set_timeout(60);
/// ```
#[derive(Debug, Clone)]
pub struct Bayarcash {
    pub(crate) token: String,
    pub(crate) sandbox: bool,
    api_version: ApiVersion,
    pub(crate) timeout: Duration,
    pub(crate) http: reqwest::Client,
    base_url_override: Option<String>,
}

impl Bayarcash {
    // -- Payment channel constants (mirror `Bayarcash::FPX` etc.) --------------

    /// FPX Online Banking.
    pub const FPX: i32 = channels::FPX;
    /// Manual Bank Transfer.
    pub const MANUAL_TRANSFER: i32 = channels::MANUAL_TRANSFER;
    /// FPX Direct Debit.
    pub const FPX_DIRECT_DEBIT: i32 = channels::FPX_DIRECT_DEBIT;
    /// FPX Line of Credit.
    pub const FPX_LINE_OF_CREDIT: i32 = channels::FPX_LINE_OF_CREDIT;
    /// DuitNow Online Banking / Wallets.
    pub const DUITNOW_DOBW: i32 = channels::DUITNOW_DOBW;
    /// DuitNow QR.
    pub const DUITNOW_QR: i32 = channels::DUITNOW_QR;
    /// ShopeePayLater.
    pub const SPAYLATER: i32 = channels::SPAYLATER;
    /// Boost PayFlex.
    pub const BOOST_PAYFLEX: i32 = channels::BOOST_PAYFLEX;
    /// QRIS Online Banking.
    pub const QRISOB: i32 = channels::QRISOB;
    /// QRIS Wallet.
    pub const QRISWALLET: i32 = channels::QRISWALLET;
    /// NETS.
    pub const NETS: i32 = channels::NETS;
    /// Credit Card.
    pub const CREDIT_CARD: i32 = channels::CREDIT_CARD;
    /// Alipay.
    pub const ALIPAY: i32 = channels::ALIPAY;
    /// WeChat Pay.
    pub const WECHATPAY: i32 = channels::WECHATPAY;
    /// PromptPay.
    pub const PROMPTPAY: i32 = channels::PROMPTPAY;
    /// Touch 'n Go eWallet.
    pub const TOUCH_N_GO: i32 = channels::TOUCH_N_GO;
    /// Boost Wallet.
    pub const BOOST_WALLET: i32 = channels::BOOST_WALLET;
    /// GrabPay.
    pub const GRABPAY: i32 = channels::GRABPAY;
    /// Grab PayLater.
    pub const GRABPL: i32 = channels::GRABPL;
    /// ShopeePay (note: there is no channel id 20).
    pub const SHOPEE_PAY: i32 = channels::SHOPEE_PAY;

    // -- Construction & configuration -----------------------------------------

    /// Create a new client with the given API token (production, v2, 30s timeout).
    pub fn new(token: impl Into<String>) -> Self {
        Bayarcash {
            token: token.into(),
            sandbox: false,
            api_version: ApiVersion::V2,
            timeout: Duration::from_secs(30),
            http: reqwest::Client::new(),
            base_url_override: None,
        }
    }

    /// Override the base URL (e.g. to route through a proxy or a mock server in
    /// tests). A trailing slash is added if missing. Pass this and requests go
    /// to `<base>/<endpoint>` instead of the computed gateway host.
    pub fn set_base_url(mut self, url: impl Into<String>) -> Self {
        let mut url = url.into();
        if !url.ends_with('/') {
            url.push('/');
        }
        self.base_url_override = Some(url);
        self
    }

    /// Replace the API token.
    pub fn set_token(mut self, token: impl Into<String>) -> Self {
        self.token = token.into();
        self
    }

    /// Switch to the sandbox environment.
    pub fn use_sandbox(mut self) -> Self {
        self.sandbox = true;
        self
    }

    /// Choose the API version (`v2` default, or `v3`).
    pub fn set_api_version(mut self, version: ApiVersion) -> Self {
        self.api_version = version;
        self
    }

    /// Set the per-request timeout, in seconds.
    pub fn set_timeout(mut self, seconds: u64) -> Self {
        self.timeout = Duration::from_secs(seconds);
        self
    }

    /// The current API version.
    pub fn api_version(&self) -> ApiVersion {
        self.api_version
    }

    /// The current timeout, in seconds.
    pub fn timeout(&self) -> u64 {
        self.timeout.as_secs()
    }

    /// Whether the sandbox environment is in use.
    pub fn is_sandbox(&self) -> bool {
        self.sandbox
    }

    /// The base URI for the current version and environment.
    pub fn base_uri(&self) -> &'static str {
        match (self.api_version, self.sandbox) {
            (ApiVersion::V3, false) => "https://api.console.bayar.cash/v3/",
            (ApiVersion::V3, true) => "https://api.console.bayarcash-sandbox.com/v3/",
            (ApiVersion::V2, false) => "https://console.bayar.cash/api/v2/",
            (ApiVersion::V2, true) => "https://console.bayarcash-sandbox.com/api/v2/",
        }
    }

    fn require_v3(&self, method: &str) -> Result<()> {
        if self.api_version != ApiVersion::V3 {
            return Err(Error::Unsupported(format!(
                "The {method} method is only available for API version v3."
            )));
        }
        Ok(())
    }

    // -- Low-level HTTP --------------------------------------------------------

    async fn send(
        &self,
        method: Method,
        uri: &str,
        form: Option<&[(String, String)]>,
    ) -> Result<reqwest::Response> {
        let base = self.base_url_override.as_deref().unwrap_or(self.base_uri());
        let url = format!("{base}{uri}");
        let mut builder = self
            .http
            .request(method, &url)
            .header(reqwest::header::ACCEPT, "application/json")
            .bearer_auth(&self.token)
            .timeout(self.timeout);

        if let Some(pairs) = form {
            builder = builder.form(pairs);
        }

        let response = builder.send().await?;
        if response.status().is_success() {
            Ok(response)
        } else {
            Err(map_response_error(response).await)
        }
    }

    async fn request_json<T: DeserializeOwned>(
        &self,
        method: Method,
        uri: &str,
        form: Option<&[(String, String)]>,
    ) -> Result<T> {
        let response = self.send(method, uri, form).await?;
        let bytes = response.bytes().await?;
        Ok(serde_json::from_slice(&bytes)?)
    }

    async fn request_value(
        &self,
        method: Method,
        uri: &str,
        form: Option<&[(String, String)]>,
    ) -> Result<Value> {
        self.request_json(method, uri, form).await
    }

    // -- Portals & banks -------------------------------------------------------

    /// List all portals for your account (`get portals`).
    pub async fn get_portals(&self) -> Result<Vec<Portal>> {
        let response: Value = self.request_value(Method::GET, "portals", None).await?;
        let items = match response.get("data") {
            Some(Value::Array(arr)) => arr.clone(),
            _ => match response {
                Value::Array(arr) => arr,
                other => vec![other],
            },
        };
        items
            .into_iter()
            .map(|v| serde_json::from_value(v).map_err(Error::from))
            .collect()
    }

    /// Payment channels available for a portal, matched by `portal_key`.
    ///
    /// Returns the raw channel objects (as the PHP SDK does), or an empty vec
    /// when the portal is not found.
    pub async fn get_channels(&self, portal_key: &str) -> Result<Vec<Value>> {
        let portals = self.get_portals().await?;
        for portal in portals {
            if portal.portal_key.as_deref() == Some(portal_key) {
                return Ok(portal.payment_channels.unwrap_or_default());
            }
        }
        Ok(Vec::new())
    }

    /// List FPX banks (`get banks`).
    pub async fn fpx_banks_list(&self) -> Result<Vec<FpxBank>> {
        self.request_json(Method::GET, "banks", None).await
    }

    // -- Payment intents -------------------------------------------------------

    /// Create a payment intent (`post payment-intents`).
    pub async fn create_payment_intent(
        &self,
        request: &PaymentIntentRequest,
    ) -> Result<PaymentIntent> {
        let form = request.to_form_pairs();
        self.request_json(Method::POST, "payment-intents", Some(&form))
            .await
    }

    /// Retrieve a payment intent by id (**v3 only**).
    pub async fn get_payment_intent(&self, payment_intent_id: &str) -> Result<PaymentIntent> {
        self.require_v3("getPaymentIntent")?;
        let uri = format!("payment-intents/{payment_intent_id}");
        self.request_json(Method::GET, &uri, None).await
    }

    /// Cancel a payment intent by id (**v3 only**).
    pub async fn cancel_payment_intent(&self, payment_intent_id: &str) -> Result<PaymentIntent> {
        self.require_v3("cancelPaymentIntent")?;
        let uri = format!("payment-intents/{payment_intent_id}");
        self.request_json(Method::DELETE, &uri, None).await
    }

    // -- Transactions ----------------------------------------------------------

    /// Retrieve a single transaction by id (v2 and v3).
    pub async fn get_transaction(&self, id: &str) -> Result<Transaction> {
        let uri = format!("transactions/{id}");
        self.request_json(Method::GET, &uri, None).await
    }

    /// Retrieve all transactions with optional filters (**v3 only**).
    pub async fn get_all_transactions(
        &self,
        filters: &TransactionFilters,
    ) -> Result<TransactionList> {
        self.require_v3("getAllTransactions")?;
        let query = build_query(&filters.query_pairs());
        let uri = if query.is_empty() {
            "transactions".to_string()
        } else {
            format!("transactions?{query}")
        };
        let response: Value = self.request_value(Method::GET, &uri, None).await?;
        let data = data_array(&response);
        let transactions = data
            .into_iter()
            .map(|v| serde_json::from_value(v).map_err(Error::from))
            .collect::<Result<Vec<Transaction>>>()?;
        let meta = response.get("meta").cloned().unwrap_or(Value::Null);
        Ok(TransactionList {
            data: transactions,
            meta,
        })
    }

    /// Retrieve transactions by order number (**v3 only**).
    pub async fn get_transaction_by_order_number(
        &self,
        order_number: &str,
    ) -> Result<Vec<Transaction>> {
        self.require_v3("getTransactionByOrderNumber")?;
        self.query_transactions("order_number", order_number).await
    }

    /// Retrieve transactions by payer email (**v3 only**).
    pub async fn get_transactions_by_payer_email(&self, email: &str) -> Result<Vec<Transaction>> {
        self.require_v3("getTransactionsByPayerEmail")?;
        self.query_transactions("payer_email", email).await
    }

    /// Retrieve transactions by status (**v3 only**).
    pub async fn get_transactions_by_status(&self, status: &str) -> Result<Vec<Transaction>> {
        self.require_v3("getTransactionsByStatus")?;
        self.query_transactions("status", status).await
    }

    /// Retrieve transactions by payment channel id (**v3 only**).
    pub async fn get_transactions_by_payment_channel(
        &self,
        channel: i32,
    ) -> Result<Vec<Transaction>> {
        self.require_v3("getTransactionsByPaymentChannel")?;
        self.query_transactions("payment_channel", &channel.to_string())
            .await
    }

    /// Retrieve a single transaction by exchange reference number (**v3 only**).
    ///
    /// Returns `None` when nothing matches.
    pub async fn get_transaction_by_reference_number(
        &self,
        reference_number: &str,
    ) -> Result<Option<Transaction>> {
        self.require_v3("getTransactionByReferenceNumber")?;
        let mut list = self
            .query_transactions("exchange_reference_number", reference_number)
            .await?;
        if list.is_empty() {
            Ok(None)
        } else {
            Ok(Some(list.remove(0)))
        }
    }

    async fn query_transactions(&self, key: &str, value: &str) -> Result<Vec<Transaction>> {
        let uri = format!("transactions?{}={}", key, urlencode(value));
        let response: Value = self.request_value(Method::GET, &uri, None).await?;
        data_array(&response)
            .into_iter()
            .map(|v| serde_json::from_value(v).map_err(Error::from))
            .collect()
    }

    // -- FPX Direct Debit ------------------------------------------------------

    /// Create an FPX Direct Debit enrolment (`post mandates`).
    pub async fn create_fpx_direct_debit_enrollment(
        &self,
        request: &FpxDirectDebitEnrolmentRequest,
    ) -> Result<MandateApplication> {
        let form = request.to_form_pairs();
        self.request_json(Method::POST, "mandates", Some(&form))
            .await
    }

    /// Update (maintain) an existing mandate (`put mandates/{id}`).
    pub async fn create_fpx_direct_debit_maintenance(
        &self,
        mandate_id: &str,
        request: &FpxDirectDebitMaintenanceRequest,
    ) -> Result<MandateApplication> {
        let form = request.to_form_pairs();
        let uri = format!("mandates/{mandate_id}");
        self.request_json(Method::PUT, &uri, Some(&form)).await
    }

    /// Terminate an existing mandate (`delete mandates/{id}`).
    pub async fn create_fpx_direct_debit_termination(
        &self,
        mandate_id: &str,
        request: &FpxDirectDebitTerminationRequest,
    ) -> Result<MandateApplication> {
        let form = request.to_form_pairs();
        let uri = format!("mandates/{mandate_id}");
        self.request_json(Method::DELETE, &uri, Some(&form)).await
    }

    /// Retrieve a mandate by id (`get mandates/{id}`).
    pub async fn get_fpx_direct_debit(&self, id: &str) -> Result<Mandate> {
        let uri = format!("mandates/{id}");
        self.request_json(Method::GET, &uri, None).await
    }

    /// Retrieve a mandate transaction by id (`get mandates/transactions/{id}`).
    pub async fn get_fpx_direct_debit_transaction(&self, id: &str) -> Result<Transaction> {
        let uri = format!("mandates/transactions/{id}");
        self.request_json(Method::GET, &uri, None).await
    }

    // -- Checksums (delegate to the `checksum` module) -------------------------

    /// Generic checksum: sort by key, join values with `|`, HMAC-SHA256.
    pub fn create_checksum_value(
        &self,
        secret_key: &str,
        payload: &BTreeMap<String, String>,
    ) -> String {
        checksum::create_checksum_value(secret_key, payload)
    }

    /// Payment-intent checksum.
    pub fn create_payment_intent_checksum_value(
        &self,
        secret_key: &str,
        request: &PaymentIntentRequest,
    ) -> String {
        checksum::create_payment_intent_checksum(secret_key, request)
    }

    /// FPX Direct Debit enrolment checksum.
    pub fn create_fpx_direct_debit_enrolment_checksum_value(
        &self,
        secret_key: &str,
        request: &FpxDirectDebitEnrolmentRequest,
    ) -> String {
        checksum::create_fpx_direct_debit_enrolment_checksum(secret_key, request)
    }

    /// FPX Direct Debit maintenance checksum.
    pub fn create_fpx_direct_debit_maintenance_checksum_value(
        &self,
        secret_key: &str,
        request: &FpxDirectDebitMaintenanceRequest,
    ) -> String {
        checksum::create_fpx_direct_debit_maintenance_checksum(secret_key, request)
    }

    // -- Callback verification (delegate to the `callback` module) -------------

    /// Verify a transaction callback (`callback_url`).
    pub fn verify_transaction_callback_data(
        &self,
        data: &impl CallbackData,
        secret_key: &str,
    ) -> bool {
        callback::verify_transaction_callback_data(data, secret_key)
    }

    /// Verify a return-URL callback (payer redirect).
    pub fn verify_return_url_callback_data(
        &self,
        data: &impl CallbackData,
        secret_key: &str,
    ) -> bool {
        callback::verify_return_url_callback_data(data, secret_key)
    }

    /// Verify a pre-transaction callback.
    pub fn verify_pre_transaction_callback_data(
        &self,
        data: &impl CallbackData,
        secret_key: &str,
    ) -> bool {
        callback::verify_pre_transaction_callback_data(data, secret_key)
    }

    /// Verify a direct-debit bank-approval callback.
    pub fn verify_direct_debit_bank_approval_callback_data(
        &self,
        data: &impl CallbackData,
        secret_key: &str,
    ) -> bool {
        callback::verify_direct_debit_bank_approval_callback_data(data, secret_key)
    }

    /// Verify a direct-debit authorization callback (includes `application_type`).
    pub fn verify_direct_debit_authorization_callback_data(
        &self,
        data: &impl CallbackData,
        secret_key: &str,
    ) -> bool {
        callback::verify_direct_debit_authorization_callback_data(data, secret_key)
    }

    /// Verify a direct-debit transaction callback.
    pub fn verify_direct_debit_transaction_callback_data(
        &self,
        data: &impl CallbackData,
        secret_key: &str,
    ) -> bool {
        callback::verify_direct_debit_transaction_callback_data(data, secret_key)
    }
}

// ---- shared helpers ---------------------------------------------------------

/// Map a non-2xx response to a typed [`Error`], mirroring `handleRequestError`.
pub(crate) async fn map_response_error(response: reqwest::Response) -> Error {
    let status = response.status().as_u16();
    let reset = response
        .headers()
        .get("x-ratelimit-reset")
        .and_then(|v| v.to_str().ok())
        .and_then(|s| s.trim().parse::<i64>().ok());
    let body = response.text().await.unwrap_or_default();

    match status {
        422 => {
            let value = serde_json::from_str(&body).unwrap_or(Value::Null);
            Error::Validation(ValidationErrors::from_value(value))
        }
        404 => Error::NotFound,
        400 => Error::FailedAction(extract_failed_action_message(&body)),
        429 => Error::RateLimitExceeded { resets_at: reset },
        _ => Error::Api { status, body },
    }
}

/// Extract the 400 error message the way the PHP SDK does:
/// prefer `message`, then `error`, else the raw body; array messages are JSON-encoded.
fn extract_failed_action_message(body: &str) -> String {
    match serde_json::from_str::<Value>(body) {
        Ok(Value::Object(map)) => {
            let candidate = map.get("message").or_else(|| map.get("error"));
            match candidate {
                Some(Value::String(s)) => s.clone(),
                Some(other) => other.to_string(),
                None => body.to_string(),
            }
        }
        _ => body.to_string(),
    }
}

/// `response["data"]` as an array, or an empty vec (matches `$response['data'] ?? []`).
fn data_array(response: &Value) -> Vec<Value> {
    response
        .get("data")
        .and_then(|d| d.as_array())
        .cloned()
        .unwrap_or_default()
}

/// Build a URL query string from `(key, value)` pairs (values percent-encoded).
fn build_query(pairs: &[(String, String)]) -> String {
    pairs
        .iter()
        .map(|(k, v)| format!("{}={}", urlencode(k), urlencode(v)))
        .collect::<Vec<_>>()
        .join("&")
}

/// Percent-encode a string like PHP's `urlencode` (RFC 1738: space -> `+`).
fn urlencode(input: &str) -> String {
    let mut out = String::with_capacity(input.len());
    for &byte in input.as_bytes() {
        match byte {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' => {
                out.push(byte as char);
            }
            b' ' => out.push('+'),
            _ => {
                out.push('%');
                out.push_str(&format!("{byte:02X}"));
            }
        }
    }
    out
}

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

    #[test]
    fn base_uri_matrix() {
        let v2 = Bayarcash::new("t");
        assert_eq!(v2.base_uri(), "https://console.bayar.cash/api/v2/");
        assert_eq!(
            v2.clone().use_sandbox().base_uri(),
            "https://console.bayarcash-sandbox.com/api/v2/"
        );

        let v3 = Bayarcash::new("t").set_api_version(ApiVersion::V3);
        assert_eq!(v3.base_uri(), "https://api.console.bayar.cash/v3/");
        assert_eq!(
            v3.use_sandbox().base_uri(),
            "https://api.console.bayarcash-sandbox.com/v3/"
        );
    }

    #[test]
    fn channel_constants() {
        assert_eq!(Bayarcash::FPX, 1);
        assert_eq!(Bayarcash::GRABPL, 19);
        assert_eq!(Bayarcash::SHOPEE_PAY, 21);
    }

    #[test]
    fn v3_only_methods_rejected_on_v2() {
        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();
        let client = Bayarcash::new("t");
        let err = rt.block_on(client.get_payment_intent("pi_1")).unwrap_err();
        match err {
            Error::Unsupported(msg) => {
                assert!(msg.contains("getPaymentIntent"));
                assert!(msg.contains("v3"));
            }
            other => panic!("expected Unsupported, got {other:?}"),
        }
    }

    #[test]
    fn urlencode_matches_php() {
        assert_eq!(urlencode("ahmad@example.com"), "ahmad%40example.com");
        assert_eq!(urlencode("INV-1001"), "INV-1001");
        assert_eq!(urlencode("a b"), "a+b");
    }
}