From 1f3806fd23fba133a108d3f704461e6023947c3f Mon Sep 17 00:00:00 2001 From: Lucas Sunsi Abreu Date: Sat, 16 Dec 2023 11:45:40 -0300 Subject: [PATCH] feat(pay): add preliminary support for convert on pay callback --- src/client.rs | 3 ++- src/core/pay/client.rs | 30 ++++++++++++++++++++++++++++-- src/core/pay/server.rs | 16 +++++++++++++--- tests/lud06.rs | 6 +++++- tests/lud09.rs | 14 +++++++++++--- tests/lud11.rs | 4 ++-- tests/lud12.rs | 3 ++- 7 files changed, 63 insertions(+), 13 deletions(-) diff --git a/src/client.rs b/src/client.rs index 4417618..92146db 100644 --- a/src/client.rs +++ b/src/client.rs @@ -135,8 +135,9 @@ impl Pay<'_> { &self, amount: &crate::pay::Amount, comment: Option<&str>, + convert: Option<&str>, ) -> Result { - let callback = self.core.invoice(amount, comment); + let callback = self.core.invoice(amount, comment, convert); let response = self .client diff --git a/src/core/pay/client.rs b/src/core/pay/client.rs index 53454f0..6749d08 100644 --- a/src/core/pay/client.rs +++ b/src/core/pay/client.rs @@ -111,11 +111,13 @@ impl Entrypoint { &'a self, amount: &'a super::Amount, comment: Option<&'a str>, + convert: Option<&'a str>, ) -> Callback<'a> { Callback { url: &self.callback, amount, comment, + convert, } } } @@ -124,6 +126,7 @@ pub struct Callback<'a> { pub url: &'a url::Url, pub comment: Option<&'a str>, pub amount: &'a super::Amount, + pub convert: Option<&'a str>, } impl std::fmt::Display for Callback<'_> { @@ -131,6 +134,7 @@ impl std::fmt::Display for Callback<'_> { let query = ser::Callback { comment: self.comment, amount: self.amount, + convert: self.convert, }; let querystr = serde_urlencoded::to_string(query).map_err(|_| std::fmt::Error)?; @@ -186,6 +190,7 @@ mod ser { pub comment: Option<&'a str>, #[serde(with = "super::super::serde::amount")] pub amount: &'a super::super::Amount, + pub convert: Option<&'a str>, } } @@ -376,7 +381,7 @@ mod tests { assert_eq!( parsed - .invoice(&super::super::Amount::Millisatoshis(314), None) + .invoice(&super::super::Amount::Millisatoshis(314), None, None) .to_string(), "https://yuri/?o=callback&amount=314" ); @@ -397,7 +402,8 @@ mod tests { parsed .invoice( &super::super::Amount::Millisatoshis(314), - Some("comentario") + Some("comentario"), + None ) .to_string(), "https://yuri/?o=callback&comment=comentario&amount=314" @@ -419,6 +425,7 @@ mod tests { parsed .invoice( &super::super::Amount::Currency(String::from("BRL"), 314), + None, None ) .to_string(), @@ -426,6 +433,25 @@ mod tests { ); } + #[test] + fn callback_render_convert() { + let input = r#"{ + "metadata": "[[\"text/plain\", \"boneco do steve magal\"]]", + "callback": "https://yuri?o=callback", + "maxSendable": 315, + "minSendable": 314 + }"#; + + let parsed: super::Entrypoint = input.as_bytes().try_into().expect("parse"); + + assert_eq!( + parsed + .invoice(&super::super::Amount::Millisatoshis(314), None, Some("BRL")) + .to_string(), + "https://yuri/?o=callback&amount=314&convert=BRL" + ); + } + #[test] fn callback_response_parse_base() { let input = r#"{ "pr": "pierre" }"#; diff --git a/src/core/pay/server.rs b/src/core/pay/server.rs index e1d65c9..929d6ea 100644 --- a/src/core/pay/server.rs +++ b/src/core/pay/server.rs @@ -69,6 +69,7 @@ impl TryFrom for Vec { pub struct Callback { pub amount: super::Amount, pub comment: Option, + pub convert: Option, } impl<'a> TryFrom<&'a str> for Callback { @@ -77,9 +78,10 @@ impl<'a> TryFrom<&'a str> for Callback { fn try_from(s: &'a str) -> Result { serde_urlencoded::from_str::(s) .map_err(|_| "deserialize failed") - .map(|query| Callback { - amount: query.amount, - comment: query.comment.map(String::from), + .map(|cb| Callback { + amount: cb.amount, + comment: cb.comment.map(String::from), + convert: cb.convert.map(String::from), }) } } @@ -165,6 +167,7 @@ mod de { pub comment: Option<&'a str>, #[serde(with = "super::super::serde::amount")] pub amount: super::super::Amount, + pub convert: Option<&'a str>, } } @@ -377,6 +380,13 @@ mod tests { assert!(parsed.comment.is_none()); } + #[test] + fn callback_parse_convert() { + let input = "amount=314&convert=BRL"; + let parsed: super::Callback = input.try_into().expect("parse"); + assert_eq!(parsed.convert.unwrap(), "BRL"); + } + #[test] fn callback_response_render_base() { let input = super::CallbackResponse { diff --git a/tests/lud06.rs b/tests/lud06.rs index 91cbd94..4838dca 100644 --- a/tests/lud06.rs +++ b/tests/lud06.rs @@ -66,7 +66,11 @@ async fn test() { ); let invoice = pr - .invoice(&lnurlkit::pay::Amount::Millisatoshis(314), Some("comment")) + .invoice( + &lnurlkit::pay::Amount::Millisatoshis(314), + Some("comment"), + None, + ) .await .expect("callback"); diff --git a/tests/lud09.rs b/tests/lud09.rs index 8464449..8c33075 100644 --- a/tests/lud09.rs +++ b/tests/lud09.rs @@ -70,14 +70,18 @@ async fn test() { }; let invoice = pr - .invoice(&lnurlkit::pay::Amount::Millisatoshis(0), None) + .invoice(&lnurlkit::pay::Amount::Millisatoshis(0), None, None) .await .expect("callback"); assert!(invoice.success_action.is_none()); let invoice = pr - .invoice(&lnurlkit::pay::Amount::Millisatoshis(1), Some("mensagem")) + .invoice( + &lnurlkit::pay::Amount::Millisatoshis(1), + Some("mensagem"), + None, + ) .await .expect("callback"); @@ -88,7 +92,11 @@ async fn test() { assert_eq!(&m as &str, "mensagem"); let invoice = pr - .invoice(&lnurlkit::pay::Amount::Millisatoshis(2), Some("descricao")) + .invoice( + &lnurlkit::pay::Amount::Millisatoshis(2), + Some("descricao"), + None, + ) .await .expect("callback"); diff --git a/tests/lud11.rs b/tests/lud11.rs index 49e698a..4dee76e 100644 --- a/tests/lud11.rs +++ b/tests/lud11.rs @@ -58,14 +58,14 @@ async fn test() { }; let invoice = pr - .invoice(&lnurlkit::pay::Amount::Millisatoshis(314), None) + .invoice(&lnurlkit::pay::Amount::Millisatoshis(314), None, None) .await .expect("callback"); assert!(invoice.disposable); let invoice = pr - .invoice(&lnurlkit::pay::Amount::Millisatoshis(315), None) + .invoice(&lnurlkit::pay::Amount::Millisatoshis(315), None, None) .await .expect("callback"); assert!(!invoice.disposable); diff --git a/tests/lud12.rs b/tests/lud12.rs index ac57dcc..fafc069 100644 --- a/tests/lud12.rs +++ b/tests/lud12.rs @@ -60,7 +60,7 @@ async fn test() { assert_eq!(pr.core.comment_size.unwrap(), 140); let invoice = pr - .invoice(&lnurlkit::pay::Amount::Millisatoshis(314), None) + .invoice(&lnurlkit::pay::Amount::Millisatoshis(314), None, None) .await .expect("callback"); @@ -70,6 +70,7 @@ async fn test() { .invoice( &lnurlkit::pay::Amount::Millisatoshis(314), Some("comentario"), + None, ) .await .expect("callback");