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
//! Request builders for the write operations.
//!
//! Each builder produces the exact `application/x-www-form-urlencoded` field
//! set the PHP SDK sends, and knows how to compute its own checksum payload so
//! the generated checksum is byte-compatible with the gateway.

use std::path::PathBuf;

use crate::checksum;

/// A payment channel selection: either a single channel id or several.
///
/// Mirrors the PHP behaviour where `payment_channel` may be a single int or an
/// array of ints. For the checksum the ids are comma-joined; for the request
/// body a single id is sent as `payment_channel=<id>` and multiple ids as
/// `payment_channel[0]=<id>&payment_channel[1]=<id>...`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PaymentChannel {
    /// A single channel id.
    Single(i32),
    /// Multiple channel ids.
    Multiple(Vec<i32>),
}

impl PaymentChannel {
    /// The comma-joined value used inside the checksum payload.
    pub fn checksum_value(&self) -> String {
        match self {
            PaymentChannel::Single(id) => id.to_string(),
            PaymentChannel::Multiple(ids) => ids
                .iter()
                .map(|id| id.to_string())
                .collect::<Vec<_>>()
                .join(","),
        }
    }

    fn form_pairs(&self) -> Vec<(String, String)> {
        match self {
            PaymentChannel::Single(id) => vec![("payment_channel".to_string(), id.to_string())],
            PaymentChannel::Multiple(ids) => ids
                .iter()
                .enumerate()
                .map(|(i, id)| (format!("payment_channel[{i}]"), id.to_string()))
                .collect(),
        }
    }
}

impl From<i32> for PaymentChannel {
    fn from(id: i32) -> Self {
        PaymentChannel::Single(id)
    }
}

impl From<Vec<i32>> for PaymentChannel {
    fn from(ids: Vec<i32>) -> Self {
        PaymentChannel::Multiple(ids)
    }
}

impl From<&[i32]> for PaymentChannel {
    fn from(ids: &[i32]) -> Self {
        PaymentChannel::Multiple(ids.to_vec())
    }
}

fn push_opt(pairs: &mut Vec<(String, String)>, key: &str, value: &Option<String>) {
    if let Some(v) = value {
        pairs.push((key.to_string(), v.clone()));
    }
}

/// Builder for [`crate::Bayarcash::create_payment_intent`].
///
/// The five required fields map onto the checksum payload
/// (`payment_channel`, `order_number`, `amount`, `payer_name`, `payer_email`).
#[derive(Debug, Clone, Default)]
pub struct PaymentIntentRequest {
    /// Your portal key.
    pub portal_key: String,
    /// One or more payment channels. `None` lets the payer choose on Bayarcash.
    pub payment_channel: Option<PaymentChannel>,
    /// Your order/reference number (max 30 chars).
    pub order_number: String,
    /// Amount as a string with up to 2 decimals, e.g. `"10.00"`.
    pub amount: String,
    /// Payer name (max 150 chars).
    pub payer_name: String,
    /// Payer email (max 250 chars).
    pub payer_email: String,
    /// Payer telephone number (required for some e-wallet/DuitNow channels).
    pub payer_telephone_number: Option<String>,
    /// Browser redirect URL after payment.
    pub return_url: Option<String>,
    /// Server-to-server callback URL.
    pub callback_url: Option<String>,
    /// Arbitrary metadata echoed back by the gateway.
    pub metadata: Option<String>,
    /// Any additional form fields to send verbatim.
    pub extra: Vec<(String, String)>,
    /// The request checksum. Set it via [`PaymentIntentRequest::signed`] or
    /// [`crate::Bayarcash::create_payment_intent_checksum_value`].
    pub checksum: Option<String>,
}

impl PaymentIntentRequest {
    /// Construct a request from the five required fields.
    pub fn new(
        portal_key: impl Into<String>,
        order_number: impl Into<String>,
        amount: impl Into<String>,
        payer_name: impl Into<String>,
        payer_email: impl Into<String>,
    ) -> Self {
        PaymentIntentRequest {
            portal_key: portal_key.into(),
            order_number: order_number.into(),
            amount: amount.into(),
            payer_name: payer_name.into(),
            payer_email: payer_email.into(),
            ..Default::default()
        }
    }

    /// Set the payment channel(s). Accepts an `i32`, `Vec<i32>` or [`PaymentChannel`].
    pub fn payment_channel(mut self, channel: impl Into<PaymentChannel>) -> Self {
        self.payment_channel = Some(channel.into());
        self
    }

    /// Set the payer telephone number.
    pub fn payer_telephone_number(mut self, value: impl Into<String>) -> Self {
        self.payer_telephone_number = Some(value.into());
        self
    }

    /// Set the return URL.
    pub fn return_url(mut self, value: impl Into<String>) -> Self {
        self.return_url = Some(value.into());
        self
    }

    /// Set the callback URL.
    pub fn callback_url(mut self, value: impl Into<String>) -> Self {
        self.callback_url = Some(value.into());
        self
    }

    /// Set the metadata value.
    pub fn metadata(mut self, value: impl Into<String>) -> Self {
        self.metadata = Some(value.into());
        self
    }

    /// Add an arbitrary extra form field.
    pub fn extra(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.extra.push((key.into(), value.into()));
        self
    }

    /// Set the checksum explicitly.
    pub fn checksum(mut self, checksum: impl Into<String>) -> Self {
        self.checksum = Some(checksum.into());
        self
    }

    /// Compute and attach the checksum using the given secret key.
    pub fn signed(mut self, secret_key: &str) -> Self {
        self.checksum = Some(checksum::create_payment_intent_checksum(secret_key, &self));
        self
    }

    /// The ordered `(key, value)` pairs sent as the form body.
    pub fn to_form_pairs(&self) -> Vec<(String, String)> {
        let mut pairs = Vec::new();
        pairs.push(("portal_key".to_string(), self.portal_key.clone()));
        if let Some(channel) = &self.payment_channel {
            pairs.extend(channel.form_pairs());
        }
        pairs.push(("order_number".to_string(), self.order_number.clone()));
        pairs.push(("amount".to_string(), self.amount.clone()));
        pairs.push(("payer_name".to_string(), self.payer_name.clone()));
        pairs.push(("payer_email".to_string(), self.payer_email.clone()));
        push_opt(&mut pairs, "payer_telephone_number", &self.payer_telephone_number);
        push_opt(&mut pairs, "return_url", &self.return_url);
        push_opt(&mut pairs, "callback_url", &self.callback_url);
        push_opt(&mut pairs, "metadata", &self.metadata);
        pairs.extend(self.extra.iter().cloned());
        push_opt(&mut pairs, "checksum", &self.checksum);
        pairs
    }
}

/// Builder for [`crate::Bayarcash::create_fpx_direct_debit_enrollment`].
#[derive(Debug, Clone, Default)]
pub struct FpxDirectDebitEnrolmentRequest {
    /// Your portal key.
    pub portal_key: String,
    /// Your order/reference number.
    pub order_number: String,
    /// Amount as a string, e.g. `"10.00"` (range 5.00–30000.00).
    pub amount: String,
    /// Payer name.
    pub payer_name: String,
    /// Payer ID type (see [`crate::FpxDirectDebit`] constants).
    pub payer_id_type: i32,
    /// Payer ID value (e.g. NRIC number).
    pub payer_id: String,
    /// Payer email (max 27 chars).
    pub payer_email: String,
    /// Payer telephone number.
    pub payer_telephone_number: String,
    /// Reason for the mandate application.
    pub application_reason: String,
    /// Frequency mode (see [`crate::FpxDirectDebit`] `MODE_*`).
    pub frequency_mode: String,
    /// Optional effective date (`YYYY-MM-DD`).
    pub effective_date: Option<String>,
    /// Optional expiry date (`YYYY-MM-DD`).
    pub expiry_date: Option<String>,
    /// Browser redirect URL after enrolment.
    pub return_url: Option<String>,
    /// Any additional form fields.
    pub extra: Vec<(String, String)>,
    /// The request checksum.
    pub checksum: Option<String>,
}

impl FpxDirectDebitEnrolmentRequest {
    /// Add an arbitrary extra form field.
    pub fn extra(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.extra.push((key.into(), value.into()));
        self
    }

    /// Set the checksum explicitly.
    pub fn checksum(mut self, checksum: impl Into<String>) -> Self {
        self.checksum = Some(checksum.into());
        self
    }

    /// Compute and attach the enrolment checksum using the given secret key.
    pub fn signed(mut self, secret_key: &str) -> Self {
        self.checksum = Some(checksum::create_fpx_direct_debit_enrolment_checksum(
            secret_key, &self,
        ));
        self
    }

    /// The ordered `(key, value)` pairs sent as the form body.
    pub fn to_form_pairs(&self) -> Vec<(String, String)> {
        let mut pairs = vec![
            ("portal_key".to_string(), self.portal_key.clone()),
            ("order_number".to_string(), self.order_number.clone()),
            ("amount".to_string(), self.amount.clone()),
            ("payer_name".to_string(), self.payer_name.clone()),
            ("payer_id_type".to_string(), self.payer_id_type.to_string()),
            ("payer_id".to_string(), self.payer_id.clone()),
            ("payer_email".to_string(), self.payer_email.clone()),
            (
                "payer_telephone_number".to_string(),
                self.payer_telephone_number.clone(),
            ),
            ("application_reason".to_string(), self.application_reason.clone()),
            ("frequency_mode".to_string(), self.frequency_mode.clone()),
        ];
        push_opt(&mut pairs, "effective_date", &self.effective_date);
        push_opt(&mut pairs, "expiry_date", &self.expiry_date);
        push_opt(&mut pairs, "return_url", &self.return_url);
        pairs.extend(self.extra.iter().cloned());
        push_opt(&mut pairs, "checksum", &self.checksum);
        pairs
    }
}

/// Builder for [`crate::Bayarcash::create_fpx_direct_debit_maintenance`].
#[derive(Debug, Clone, Default)]
pub struct FpxDirectDebitMaintenanceRequest {
    /// New amount.
    pub amount: String,
    /// Payer email.
    pub payer_email: String,
    /// Payer telephone number.
    pub payer_telephone_number: String,
    /// Reason for the maintenance.
    pub application_reason: String,
    /// Frequency mode (see [`crate::FpxDirectDebit`] `MODE_*`).
    pub frequency_mode: String,
    /// Any additional form fields.
    pub extra: Vec<(String, String)>,
    /// The request checksum.
    pub checksum: Option<String>,
}

impl FpxDirectDebitMaintenanceRequest {
    /// Add an arbitrary extra form field.
    pub fn extra(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.extra.push((key.into(), value.into()));
        self
    }

    /// Set the checksum explicitly.
    pub fn checksum(mut self, checksum: impl Into<String>) -> Self {
        self.checksum = Some(checksum.into());
        self
    }

    /// Compute and attach the maintenance checksum using the given secret key.
    pub fn signed(mut self, secret_key: &str) -> Self {
        self.checksum = Some(checksum::create_fpx_direct_debit_maintenance_checksum(
            secret_key, &self,
        ));
        self
    }

    /// The ordered `(key, value)` pairs sent as the form body.
    pub fn to_form_pairs(&self) -> Vec<(String, String)> {
        let mut pairs = vec![
            ("amount".to_string(), self.amount.clone()),
            ("payer_email".to_string(), self.payer_email.clone()),
            (
                "payer_telephone_number".to_string(),
                self.payer_telephone_number.clone(),
            ),
            ("application_reason".to_string(), self.application_reason.clone()),
            ("frequency_mode".to_string(), self.frequency_mode.clone()),
        ];
        pairs.extend(self.extra.iter().cloned());
        push_opt(&mut pairs, "checksum", &self.checksum);
        pairs
    }
}

/// Builder for [`crate::Bayarcash::create_fpx_direct_debit_termination`].
#[derive(Debug, Clone, Default)]
pub struct FpxDirectDebitTerminationRequest {
    /// Reason for the termination.
    pub application_reason: String,
    /// Any additional form fields.
    pub extra: Vec<(String, String)>,
}

impl FpxDirectDebitTerminationRequest {
    /// Construct a termination request from a reason.
    pub fn new(application_reason: impl Into<String>) -> Self {
        FpxDirectDebitTerminationRequest {
            application_reason: application_reason.into(),
            extra: Vec::new(),
        }
    }

    /// Add an arbitrary extra form field.
    pub fn extra(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.extra.push((key.into(), value.into()));
        self
    }

    /// The ordered `(key, value)` pairs sent as the form body.
    pub fn to_form_pairs(&self) -> Vec<(String, String)> {
        let mut pairs = vec![("application_reason".to_string(), self.application_reason.clone())];
        pairs.extend(self.extra.iter().cloned());
        pairs
    }
}

/// Filters for [`crate::Bayarcash::get_all_transactions`] (v3 only).
///
/// Only these five parameters are forwarded to the gateway, matching the PHP
/// `getAllTransactions` allow-list.
#[derive(Debug, Clone, Default)]
pub struct TransactionFilters {
    /// Filter by order number.
    pub order_number: Option<String>,
    /// Filter by status code (as a string, e.g. `"3"`).
    pub status: Option<String>,
    /// Filter by payment channel id.
    pub payment_channel: Option<i32>,
    /// Filter by exchange reference number.
    pub exchange_reference_number: Option<String>,
    /// Filter by payer email.
    pub payer_email: Option<String>,
}

impl TransactionFilters {
    /// A new, empty filter set.
    pub fn new() -> Self {
        Self::default()
    }

    /// Filter by order number.
    pub fn order_number(mut self, value: impl Into<String>) -> Self {
        self.order_number = Some(value.into());
        self
    }

    /// Filter by status.
    pub fn status(mut self, value: impl Into<String>) -> Self {
        self.status = Some(value.into());
        self
    }

    /// Filter by payment channel id.
    pub fn payment_channel(mut self, value: i32) -> Self {
        self.payment_channel = Some(value);
        self
    }

    /// Filter by exchange reference number.
    pub fn exchange_reference_number(mut self, value: impl Into<String>) -> Self {
        self.exchange_reference_number = Some(value.into());
        self
    }

    /// Filter by payer email.
    pub fn payer_email(mut self, value: impl Into<String>) -> Self {
        self.payer_email = Some(value.into());
        self
    }

    /// The allow-listed query pairs, in a stable order.
    pub(crate) fn query_pairs(&self) -> Vec<(String, String)> {
        let mut pairs = Vec::new();
        if let Some(v) = &self.order_number {
            pairs.push(("order_number".to_string(), v.clone()));
        }
        if let Some(v) = &self.status {
            pairs.push(("status".to_string(), v.clone()));
        }
        if let Some(v) = &self.payment_channel {
            pairs.push(("payment_channel".to_string(), v.to_string()));
        }
        if let Some(v) = &self.exchange_reference_number {
            pairs.push(("exchange_reference_number".to_string(), v.clone()));
        }
        if let Some(v) = &self.payer_email {
            pairs.push(("payer_email".to_string(), v.clone()));
        }
        pairs
    }
}

/// Builder for [`crate::Bayarcash::create_manual_bank_transfer`].
///
/// `payment_gateway` must be `2` ([`crate::channels::MANUAL_TRANSFER`]).
#[derive(Debug, Clone, Default)]
pub struct ManualBankTransferRequest {
    /// Your portal key.
    pub portal_key: String,
    /// Must be `2` (Manual Bank Transfer).
    pub payment_gateway: i32,
    /// Your order number.
    pub order_no: String,
    /// Buyer name.
    pub buyer_name: String,
    /// Buyer email.
    pub buyer_email: String,
    /// Buyer telephone number (optional).
    pub buyer_tel_no: Option<String>,
    /// Order amount, e.g. `"10.00"`.
    pub order_amount: String,
    /// Destination bank name.
    pub merchant_bank_name: String,
    /// Destination bank account number.
    pub merchant_bank_account: String,
    /// Destination bank account holder name.
    pub merchant_bank_account_holder: String,
    /// Transfer type, e.g. `"Internet Banking"` or `"Cash Deposit Machine (CDM)"`.
    pub bank_transfer_type: String,
    /// Free-form notes.
    pub bank_transfer_notes: String,
    /// Transfer date (`YYYY-MM-DD`). Defaults to today when `None`.
    pub bank_transfer_date: Option<String>,
    /// Path to the proof-of-payment file (jpeg/png/gif/pdf, max 10 MB).
    pub proof_of_payment: Option<PathBuf>,
    /// Any additional text form fields.
    pub extra: Vec<(String, String)>,
}

impl ManualBankTransferRequest {
    /// Add an arbitrary extra text field.
    pub fn extra(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.extra.push((key.into(), value.into()));
        self
    }

    /// The non-file text fields, in insertion order, with defaults applied.
    pub(crate) fn text_fields(&self) -> Vec<(String, String)> {
        let mut pairs = vec![
            ("portal_key".to_string(), self.portal_key.clone()),
            ("payment_gateway".to_string(), self.payment_gateway.to_string()),
            ("order_no".to_string(), self.order_no.clone()),
            ("buyer_name".to_string(), self.buyer_name.clone()),
            ("buyer_email".to_string(), self.buyer_email.clone()),
        ];
        if let Some(tel) = &self.buyer_tel_no {
            pairs.push(("buyer_tel_no".to_string(), tel.clone()));
        }
        pairs.push(("order_amount".to_string(), self.order_amount.clone()));
        pairs.push(("merchant_bank_name".to_string(), self.merchant_bank_name.clone()));
        pairs.push((
            "merchant_bank_account".to_string(),
            self.merchant_bank_account.clone(),
        ));
        pairs.push((
            "merchant_bank_account_holder".to_string(),
            self.merchant_bank_account_holder.clone(),
        ));
        pairs.push(("bank_transfer_type".to_string(), self.bank_transfer_type.clone()));
        pairs.push(("bank_transfer_notes".to_string(), self.bank_transfer_notes.clone()));
        pairs.extend(self.extra.iter().cloned());
        pairs
    }
}