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
#[derive(Clone, Debug)]
pub struct Response {
    pub callback: url::Url,
    pub short_description: String,
    pub long_description: Option<String>,
    pub identifier: Option<String>,
    pub email: Option<String>,
    pub jpeg: Option<Vec<u8>>,
    pub png: Option<Vec<u8>>,
    pub comment_size: Option<u64>,
    pub min: u64,
    pub max: u64,
}

impl TryFrom<Response> for Vec<u8> {
    type Error = &'static str;

    fn try_from(r: Response) -> Result<Self, Self::Error> {
        use base64::{prelude::BASE64_STANDARD, Engine};

        let metadata = serde_json::to_string(
            &[
                Some(("text/plain", r.short_description.clone())),
                r.long_description
                    .as_ref()
                    .map(|s| ("text/long-desc", s.clone())),
                r.jpeg
                    .as_ref()
                    .map(|s| ("image/jpeg;base64", BASE64_STANDARD.encode(s))),
                r.png
                    .as_ref()
                    .map(|s| ("image/png;base64", BASE64_STANDARD.encode(s))),
                r.identifier
                    .as_ref()
                    .map(|s| ("text/identifier", s.clone())),
                r.email.as_ref().map(|s| ("text/email", s.clone())),
            ]
            .into_iter()
            .flatten()
            .collect::<Vec<_>>(),
        )
        .map_err(|_| "serialize failed")?;

        serde_json::to_vec(&ser::Response {
            tag: super::TAG,
            metadata,
            callback: &r.callback,
            min_sendable: r.min,
            max_sendable: r.max,
            comment_allowed: r.comment_size.unwrap_or(0),
        })
        .map_err(|_| "serialize failed")
    }
}

pub struct CallbackQuery {
    pub millisatoshis: u64,
    pub comment: Option<String>,
}

impl<'a> TryFrom<&'a str> for CallbackQuery {
    type Error = &'static str;

    fn try_from(s: &'a str) -> Result<Self, Self::Error> {
        serde_urlencoded::from_str::<super::serde::CallbackQuery>(s)
            .map_err(|_| "deserialize failed")
            .map(|query| CallbackQuery {
                millisatoshis: query.amount,
                comment: query.comment.map(String::from),
            })
    }
}

#[derive(Clone, Debug)]
pub struct CallbackResponse {
    pub pr: String,
    pub disposable: bool,
    pub success_action: Option<SuccessAction>,
}

#[derive(Clone, Debug)]
pub enum SuccessAction {
    Url(url::Url, String),
    Message(String),
}

impl std::fmt::Display for CallbackResponse {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let success_action = self.success_action.as_ref().map(|sa| {
            let mut map = std::collections::BTreeMap::new();

            match sa {
                SuccessAction::Message(m) => {
                    map.insert("tag", "message");
                    map.insert("message", m);
                }
                SuccessAction::Url(u, d) => {
                    map.insert("tag", "url");
                    map.insert("description", d);
                    map.insert("url", u.as_str());
                }
            }

            map
        });

        let cr = ser::CallbackResponse {
            success_action,
            disposable: self.disposable,
            pr: &self.pr,
        };

        f.write_str(&serde_json::to_string(&cr).map_err(|_| std::fmt::Error)?)
    }
}

mod ser {
    use serde::Serialize;
    use std::collections::BTreeMap;
    use url::Url;

    #[derive(Serialize)]
    pub(super) struct Response<'a> {
        pub tag: &'static str,
        pub metadata: String,
        pub callback: &'a Url,
        #[serde(rename = "minSendable")]
        pub min_sendable: u64,
        #[serde(rename = "maxSendable")]
        pub max_sendable: u64,
        #[serde(rename = "commentAllowed")]
        pub comment_allowed: u64,
    }

    #[derive(Serialize)]
    pub(super) struct CallbackResponse<'a> {
        pub pr: &'a str,
        pub disposable: bool,
        #[serde(rename = "successAction")]
        pub success_action: Option<BTreeMap<&'static str, &'a str>>,
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn response_render_base() {
        let query = super::Response {
            callback: url::Url::parse("https://yuri?o=callback").expect("url"),
            short_description: String::from("boneco do steve magal"),
            long_description: None,
            jpeg: None,
            png: None,
            comment_size: None,
            min: 314,
            max: 315,
            identifier: None,
            email: None,
        };

        assert_eq!(
            Vec::<u8>::try_from(query).unwrap(),
            br#"{"tag":"payRequest","metadata":"[[\"text/plain\",\"boneco do steve magal\"]]","callback":"https://yuri/?o=callback","minSendable":314,"maxSendable":315,"commentAllowed":0}"#
        );
    }

    #[test]
    fn response_render_comment_size() {
        let query = super::Response {
            callback: url::Url::parse("https://yuri?o=callback").expect("url"),
            short_description: String::from("boneco do steve magal"),
            long_description: None,
            jpeg: None,
            png: None,
            comment_size: Some(140),
            min: 314,
            max: 315,
            identifier: None,
            email: None,
        };

        assert_eq!(
            Vec::<u8>::try_from(query).unwrap(),
            br#"{"tag":"payRequest","metadata":"[[\"text/plain\",\"boneco do steve magal\"]]","callback":"https://yuri/?o=callback","minSendable":314,"maxSendable":315,"commentAllowed":140}"#
        );
    }

    #[test]
    fn response_render_long_description() {
        let query = super::Response {
            callback: url::Url::parse("https://yuri?o=callback").expect("url"),
            short_description: String::from("boneco do steve magal"),
            long_description: Some(String::from("mochila a jato brutal incluida")),
            jpeg: None,
            png: None,
            comment_size: None,
            min: 314,
            max: 315,
            identifier: None,
            email: None,
        };

        assert_eq!(
            Vec::<u8>::try_from(query).unwrap(),
            br#"{"tag":"payRequest","metadata":"[[\"text/plain\",\"boneco do steve magal\"],[\"text/long-desc\",\"mochila a jato brutal incluida\"]]","callback":"https://yuri/?o=callback","minSendable":314,"maxSendable":315,"commentAllowed":0}"#
        );
    }

    #[test]
    fn response_render_images() {
        let query = super::Response {
            callback: url::Url::parse("https://yuri?o=callback").expect("url"),
            short_description: String::from("boneco do steve magal"),
            long_description: None,
            jpeg: Some(b"imagembrutal".to_vec()),
            png: Some(b"fotobrutal".to_vec()),
            comment_size: None,
            min: 314,
            max: 315,
            identifier: None,
            email: None,
        };

        assert_eq!(
            Vec::<u8>::try_from(query).unwrap(),
            br#"{"tag":"payRequest","metadata":"[[\"text/plain\",\"boneco do steve magal\"],[\"image/jpeg;base64\",\"aW1hZ2VtYnJ1dGFs\"],[\"image/png;base64\",\"Zm90b2JydXRhbA==\"]]","callback":"https://yuri/?o=callback","minSendable":314,"maxSendable":315,"commentAllowed":0}"#
        );
    }

    #[test]
    fn response_render_identifier() {
        let query = super::Response {
            callback: url::Url::parse("https://yuri?o=callback").expect("url"),
            short_description: String::from("boneco do steve magal"),
            long_description: None,
            jpeg: Some(b"imagembrutal".to_vec()),
            png: Some(b"fotobrutal".to_vec()),
            comment_size: None,
            min: 314,
            max: 315,
            identifier: Some(String::from("steve@magal.brutal")),
            email: None,
        };

        assert_eq!(
            Vec::<u8>::try_from(query).unwrap(),
            br#"{"tag":"payRequest","metadata":"[[\"text/plain\",\"boneco do steve magal\"],[\"image/jpeg;base64\",\"aW1hZ2VtYnJ1dGFs\"],[\"image/png;base64\",\"Zm90b2JydXRhbA==\"],[\"text/identifier\",\"steve@magal.brutal\"]]","callback":"https://yuri/?o=callback","minSendable":314,"maxSendable":315,"commentAllowed":0}"#
        );
    }

    #[test]
    fn response_render_email() {
        let query = super::Response {
            callback: url::Url::parse("https://yuri?o=callback").expect("url"),
            short_description: String::from("boneco do steve magal"),
            long_description: None,
            jpeg: Some(b"imagembrutal".to_vec()),
            png: Some(b"fotobrutal".to_vec()),
            comment_size: None,
            min: 314,
            max: 315,
            identifier: None,
            email: Some(String::from("steve@magal.brutal")),
        };

        assert_eq!(
            Vec::<u8>::try_from(query).unwrap(),
            br#"{"tag":"payRequest","metadata":"[[\"text/plain\",\"boneco do steve magal\"],[\"image/jpeg;base64\",\"aW1hZ2VtYnJ1dGFs\"],[\"image/png;base64\",\"Zm90b2JydXRhbA==\"],[\"text/email\",\"steve@magal.brutal\"]]","callback":"https://yuri/?o=callback","minSendable":314,"maxSendable":315,"commentAllowed":0}"#
        );
    }

    #[test]
    fn callback_query_parse_base() {
        let input = "amount=314";
        let parsed: super::CallbackQuery = input.try_into().expect("parse");

        assert_eq!(parsed.millisatoshis, 314);
        assert!(parsed.comment.is_none());
    }

    #[test]
    fn callback_query_parse_comment() {
        let input = "amount=314&comment=comentario";
        let parsed: super::CallbackQuery = input.try_into().expect("parse");

        assert_eq!(parsed.millisatoshis, 314);
        assert_eq!(parsed.comment.unwrap(), "comentario");
    }

    #[test]
    fn callback_response_render_base() {
        let input = super::CallbackResponse {
            pr: String::from("pierre"),
            success_action: None,
            disposable: true,
        };

        assert_eq!(
            input.to_string(),
            r#"{"pr":"pierre","disposable":true,"successAction":null}"#
        );
    }

    #[test]
    fn callback_response_render_disposable() {
        let input = super::CallbackResponse {
            pr: String::from("pierre"),
            success_action: None,
            disposable: false,
        };

        assert_eq!(
            input.to_string(),
            r#"{"pr":"pierre","disposable":false,"successAction":null}"#
        );
    }

    #[test]
    fn callback_response_render_success_actions() {
        let input = super::CallbackResponse {
            pr: String::from("pierre"),
            success_action: Some(super::SuccessAction::Message(String::from("obrigado!"))),
            disposable: false,
        };

        assert_eq!(
            input.to_string(),
            r#"{"pr":"pierre","disposable":false,"successAction":{"message":"obrigado!","tag":"message"}}"#
        );

        let input = super::CallbackResponse {
            pr: String::from("pierre"),
            success_action: Some(super::SuccessAction::Url(
                url::Url::parse("http://recibo").expect("url"),
                String::from("segue recibo"),
            )),
            disposable: false,
        };

        assert_eq!(
            input.to_string(),
            r#"{"pr":"pierre","disposable":false,"successAction":{"description":"segue recibo","tag":"url","url":"http://recibo/"}}"#
        );
    }
}