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
use crate::errors::Result;
use crate::types::*;
use uuid::Uuid;

/// Product invoice
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Invoice {
    #[doc(hidden)]
    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
    extra: Option<String>,
    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
    client_id: Option<i32>,
    /// ISO 4217 currency code

    #[serde(default)]
    currency: String,
    /// A list of objects used to calculate the total price of the product

    #[serde(default)]
    price_parts: Vec<LabeledPricePart>,
    /// The maximum allowed amount of tip in the smallest units of the currency

    #[serde(default)]
    max_tip_amount: i64,
    /// Suggested amounts of tip in the smallest units of the currency

    #[serde(default)]
    suggested_tip_amounts: Vec<i64>,
    /// True, if the payment is a test payment

    #[serde(default)]
    is_test: bool,
    /// True, if the user's name is needed for payment

    #[serde(default)]
    need_name: bool,
    /// True, if the user's phone number is needed for payment

    #[serde(default)]
    need_phone_number: bool,
    /// True, if the user's email address is needed for payment

    #[serde(default)]
    need_email_address: bool,
    /// True, if the user's shipping address is needed for payment

    #[serde(default)]
    need_shipping_address: bool,
    /// True, if the user's phone number will be sent to the provider

    #[serde(default)]
    send_phone_number_to_provider: bool,
    /// True, if the user's email address will be sent to the provider

    #[serde(default)]
    send_email_address_to_provider: bool,
    /// True, if the total price depends on the shipping method

    #[serde(default)]
    is_flexible: bool,
}

impl RObject for Invoice {
    #[doc(hidden)]
    fn extra(&self) -> Option<&str> {
        self.extra.as_deref()
    }
    #[doc(hidden)]
    fn client_id(&self) -> Option<i32> {
        self.client_id
    }
}

impl Invoice {
    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
        Ok(serde_json::from_str(json.as_ref())?)
    }
    pub fn builder() -> InvoiceBuilder {
        let mut inner = Invoice::default();
        inner.extra = Some(Uuid::new_v4().to_string());

        InvoiceBuilder { inner }
    }

    pub fn currency(&self) -> &String {
        &self.currency
    }

    pub fn price_parts(&self) -> &Vec<LabeledPricePart> {
        &self.price_parts
    }

    pub fn max_tip_amount(&self) -> i64 {
        self.max_tip_amount
    }

    pub fn suggested_tip_amounts(&self) -> &Vec<i64> {
        &self.suggested_tip_amounts
    }

    pub fn is_test(&self) -> bool {
        self.is_test
    }

    pub fn need_name(&self) -> bool {
        self.need_name
    }

    pub fn need_phone_number(&self) -> bool {
        self.need_phone_number
    }

    pub fn need_email_address(&self) -> bool {
        self.need_email_address
    }

    pub fn need_shipping_address(&self) -> bool {
        self.need_shipping_address
    }

    pub fn send_phone_number_to_provider(&self) -> bool {
        self.send_phone_number_to_provider
    }

    pub fn send_email_address_to_provider(&self) -> bool {
        self.send_email_address_to_provider
    }

    pub fn is_flexible(&self) -> bool {
        self.is_flexible
    }
}

#[doc(hidden)]
pub struct InvoiceBuilder {
    inner: Invoice,
}

#[deprecated]
pub type RTDInvoiceBuilder = InvoiceBuilder;

impl InvoiceBuilder {
    pub fn build(&self) -> Invoice {
        self.inner.clone()
    }

    pub fn currency<T: AsRef<str>>(&mut self, currency: T) -> &mut Self {
        self.inner.currency = currency.as_ref().to_string();
        self
    }

    pub fn price_parts(&mut self, price_parts: Vec<LabeledPricePart>) -> &mut Self {
        self.inner.price_parts = price_parts;
        self
    }

    pub fn max_tip_amount(&mut self, max_tip_amount: i64) -> &mut Self {
        self.inner.max_tip_amount = max_tip_amount;
        self
    }

    pub fn suggested_tip_amounts(&mut self, suggested_tip_amounts: Vec<i64>) -> &mut Self {
        self.inner.suggested_tip_amounts = suggested_tip_amounts;
        self
    }

    pub fn is_test(&mut self, is_test: bool) -> &mut Self {
        self.inner.is_test = is_test;
        self
    }

    pub fn need_name(&mut self, need_name: bool) -> &mut Self {
        self.inner.need_name = need_name;
        self
    }

    pub fn need_phone_number(&mut self, need_phone_number: bool) -> &mut Self {
        self.inner.need_phone_number = need_phone_number;
        self
    }

    pub fn need_email_address(&mut self, need_email_address: bool) -> &mut Self {
        self.inner.need_email_address = need_email_address;
        self
    }

    pub fn need_shipping_address(&mut self, need_shipping_address: bool) -> &mut Self {
        self.inner.need_shipping_address = need_shipping_address;
        self
    }

    pub fn send_phone_number_to_provider(
        &mut self,
        send_phone_number_to_provider: bool,
    ) -> &mut Self {
        self.inner.send_phone_number_to_provider = send_phone_number_to_provider;
        self
    }

    pub fn send_email_address_to_provider(
        &mut self,
        send_email_address_to_provider: bool,
    ) -> &mut Self {
        self.inner.send_email_address_to_provider = send_email_address_to_provider;
        self
    }

    pub fn is_flexible(&mut self, is_flexible: bool) -> &mut Self {
        self.inner.is_flexible = is_flexible;
        self
    }
}

impl AsRef<Invoice> for Invoice {
    fn as_ref(&self) -> &Invoice {
        self
    }
}

impl AsRef<Invoice> for InvoiceBuilder {
    fn as_ref(&self) -> &Invoice {
        &self.inner
    }
}