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
use crate::CommandId;
use serde::Deserialize;
use std::fmt::{Display, Formatter, Result as FmtResult};

#[derive(Debug)]
/// Payload to register the 3rd party’s confirmation and validation URLs to M-Pesa
/// See more here: https://developer.safaricom.co.ke/docs?shell#c2b-api
pub struct C2bRegisterPayload<'a> {
    pub validation_url: &'a str,
    pub confirmation_url: &'a str,
    pub response_type: ResponseType,
    pub short_code: &'a str,
}

#[derive(Debug, Deserialize)]
/// C2B register response
/// Field names deliberately in Pascal case to correctly deserialize the
/// response data
pub struct C2bRegisterResponse {
    pub ConversationID: String,
    pub OriginatorConversationID: String,
    pub ResponseDescription: String,
}

#[derive(Debug)]
/// C2B Register Response types
pub enum ResponseType {
    Complete,
    Cancelled,
}

impl ResponseType {
    /// Stringify response type
    fn response_type_string(&self) -> &'static str {
        match self {
            ResponseType::Cancelled => "Cancelled",
            ResponseType::Complete => "Complete",
        }
    }
}

impl Display for ResponseType {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        write!(f, "{:?}", self.response_type_string())
    }
}

#[derive(Debug)]
/// Payload to make payment requests from C2B.
/// See more: https://developer.safaricom.co.ke/docs#c2b-api
pub struct C2bSimulatePayload<'a> {
    pub command_id: CommandId,
    pub amount: u32,
    pub msisdn: &'a str,
    pub bill_ref_number: &'a str,
    pub short_code: &'a str,
}

#[derive(Debug, Deserialize)]
/// C2B payment response
pub struct C2bSimulateResponse {
    pub ConversationID: String,
    pub OriginatorCoversationID: String,
    pub ResponseDescription: String,
}