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
use serde::{Deserialize, Serialize};

use crate::client::Mpesa;
use crate::constants::SendRemindersTypes;
use crate::environment::ApiEnvironment;
use crate::errors::{MpesaError, MpesaResult};

#[derive(Debug, Serialize)]
/// Payload to opt you in as a biller to the bill manager features.
struct OnboardPayload<'mpesa> {
    #[serde(rename(serialize = "callbackUrl"))]
    callback_url: &'mpesa str,
    #[serde(rename(serialize = "email"))]
    email: &'mpesa str,
    #[serde(rename(serialize = "logo"))]
    logo: &'mpesa str,
    #[serde(rename(serialize = "officialContact"))]
    official_contact: &'mpesa str,
    #[serde(rename(serialize = "sendReminders"))]
    send_reminders: SendRemindersTypes,
    #[serde(rename(serialize = "shortcode"))]
    short_code: &'mpesa str,
}

#[derive(Clone, Debug, Deserialize)]
pub struct OnboardResponse {
    #[serde(rename(deserialize = "app_key"))]
    pub app_key: String,
    #[serde(rename(deserialize = "rescode"))]
    pub response_code: String,
    #[serde(rename(deserialize = "resmsg"))]
    pub response_message: String,
}

#[derive(Debug)]
pub struct OnboardBuilder<'mpesa, Env: ApiEnvironment> {
    client: &'mpesa Mpesa<Env>,
    callback_url: Option<&'mpesa str>,
    email: Option<&'mpesa str>,
    logo: Option<&'mpesa str>,
    official_contact: Option<&'mpesa str>,
    send_reminders: Option<SendRemindersTypes>,
    short_code: Option<&'mpesa str>,
}

impl<'mpesa, Env: ApiEnvironment> OnboardBuilder<'mpesa, Env> {
    /// Creates a new Bill Manager Onboard builder
    pub fn new(client: &'mpesa Mpesa<Env>) -> OnboardBuilder<'mpesa, Env> {
        OnboardBuilder {
            client,
            callback_url: None,
            email: None,
            logo: None,
            official_contact: None,
            send_reminders: None,
            short_code: None,
        }
    }

    /// Adds `callbackUrl`.
    ///
    /// # Errors
    /// If 'callbackUrl` is not provided.
    pub fn callback_url(mut self, callback_url: &'mpesa str) -> OnboardBuilder<'mpesa, Env> {
        self.callback_url = Some(callback_url);
        self
    }

    /// Adds an `email` address to the request.
    ///
    /// # Errors
    /// If `email` is not provided.
    pub fn email(mut self, email: &'mpesa str) -> OnboardBuilder<'mpesa, Env> {
        self.email = Some(email);
        self
    }

    /// Adds `logo`; a file with your organizions's logo.
    ///
    /// # Errors
    /// If `logo` is not provided.
    pub fn logo(mut self, logo: &'mpesa str) -> OnboardBuilder<'mpesa, Env> {
        self.logo = Some(logo);
        self
    }

    /// Adds `officialContact` to the request; must be in the format `07XXXXXXXX`
    ///
    /// # Errors
    /// If `officialContact` is invalid or not provided.
    pub fn official_contact(
        mut self,
        official_contact: &'mpesa str,
    ) -> OnboardBuilder<'mpesa, Env> {
        self.official_contact = Some(official_contact);
        self
    }

    /// Adds `sendReminders`. Defaults to `SendRemindersTypes::Disable` if no value is explicitely passed.
    ///
    /// # Errors
    /// If `sendReminders` is not valid.
    pub fn send_reminders(
        mut self,
        send_reminders: SendRemindersTypes,
    ) -> OnboardBuilder<'mpesa, Env> {
        self.send_reminders = Some(send_reminders);
        self
    }

    /// Adds `ShortCode`; the 6 digit MPESA Till Number or PayBill Number
    ///
    /// # Errors
    /// If Till or PayBill number is invalid or not provided
    pub fn short_code(mut self, short_code: &'mpesa str) -> OnboardBuilder<'mpesa, Env> {
        self.short_code = Some(short_code);
        self
    }

    /// # Bill Manager Onboarding API
    ///
    /// Opt in as a biller to mpesa's bill manager features.
    ///
    /// A successful request returns a `OnboardResponse` type
    ///
    /// # Errors
    /// Returns an `MpesaError` on failure
    #[allow(clippy::unnecessary_lazy_evaluations)]
    pub async fn send(self) -> MpesaResult<OnboardResponse> {
        let url = format!(
            "{}/v1/billmanager-invoice/optin",
            self.client.environment.base_url()
        );

        let payload = OnboardPayload {
            callback_url: self
                .callback_url
                .ok_or(MpesaError::Message("callback_url is required"))?,
            email: self.email.ok_or(MpesaError::Message("email is required"))?,
            logo: self.logo.ok_or(MpesaError::Message("logo is required"))?,
            official_contact: self
                .official_contact
                .ok_or(MpesaError::Message("official_contact is required"))?,
            send_reminders: self
                .send_reminders
                .unwrap_or_else(|| SendRemindersTypes::Disable),
            short_code: self
                .short_code
                .ok_or(MpesaError::Message("short_code is required"))?,
        };

        let response = self
            .client
            .http_client
            .post(&url)
            .bearer_auth(self.client.auth().await?)
            .json(&payload)
            .send()
            .await?;

        if response.status().is_success() {
            let value = response.json().await?;
            return Ok(value);
        }

        let value = response.json().await?;
        Err(MpesaError::OnboardError(value))
    }
}