diff --git a/.cargo/config.toml b/.cargo/config.toml index 3e26d32..2519d1b 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,5 +1,5 @@ -[target.x86_64-unknown-linux-gnu] -linker = "clang" -rustflags = [ - "-C", "link-arg=-fuse-ld=mold", -] +#[target.x86_64-unknown-linux-gnu] +#linker = "clang" +#rustflags = [ +# "-C", "link-arg=-fuse-ld=mold", +#] diff --git a/Cargo.lock b/Cargo.lock index 1298121..d99a021 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2143,6 +2143,8 @@ name = "pay_u" version = "0.1.0" dependencies = [ "chrono", + "dotenv", + "reqwest", "serde", "serde_json", "thiserror", @@ -3509,6 +3511,7 @@ checksum = "9399fa2f927a3d327187cbd201480cee55bee6ac5d3c77dd27f0c6814cff16d5" dependencies = [ "base64", "chunked_transfer", + "encoding_rs", "flate2", "log", "once_cell", diff --git a/pay_u/Cargo.toml b/pay_u/Cargo.toml index ebc186e..d8a4a2d 100644 --- a/pay_u/Cargo.toml +++ b/pay_u/Cargo.toml @@ -4,7 +4,8 @@ version = "0.1.0" edition = "2021" [dependencies] -ureq = { version = "2.4.0", features = ["json", "serde_json"] } +ureq = { version = "2.4.0", features = ["json", "serde_json", "charset", "tls", "rustls"] } +reqwest = { version = "0.11.10", features = ["default", "rustls", "rustls-tls", "json"] } serde = { version = "1.0.136", features = ["derive"] } serde_json = { version = "1.0.79" } @@ -15,3 +16,4 @@ thiserror = { version = "1.0.30" } [dev-dependencies] tokio = { version = "1.17.0", features = ["full"] } +dotenv = { version = "0.15.0" } diff --git a/pay_u/src/lib.rs b/pay_u/src/lib.rs index c3f2e8b..b1fc0e0 100644 --- a/pay_u/src/lib.rs +++ b/pay_u/src/lib.rs @@ -152,7 +152,7 @@ pub struct Order { /// Customer client IP address pub customer_ip: String, /// Secret pos ip. This is connected to PayU account - pub merchant_pos_ip: String, + pub merchant_pos_id: String, /// Transaction description pub description: String, /// 3 characters currency identifier, ex. PLN @@ -258,7 +258,6 @@ pub struct Client { impl Client { pub fn create_order(&self, order: Order) -> Result { - println!("{}", serde_json::to_string_pretty(&order).unwrap()); if order.total_amount != order .products @@ -278,11 +277,49 @@ impl Client { .map(|s| format!("Bearer {s}")) .ok_or(Error::NoToken)?, ); - eprintln!("{:?}", req); - let res = req.send_string(&serde_json::to_string_pretty(&order).unwrap())?; + // eprintln!("req {:?}", req); + let res = req.send_json(order)?; + // eprintln!("res {:?}", res); + // let r = res.into_string(); + // let x = r.is_ok(); + // let s = r.expect("Not a string"); + // eprintln!("s {:}", s); res.into_json::().map_err(Into::into) } + pub async fn create_order2(&self, order: Order) -> Result { + if order.total_amount + != order + .products + .iter() + .fold(0, |memo, p| memo + (p.unit_price * p.quantity as i32)) + { + return Err(Error::IncorrectTotal); + } + let client = reqwest::ClientBuilder::default() + .https_only(true) + .referer(true) + .use_rustls_tls() + .build() + .expect("Failed to create client"); + let bearer = self.bearer.as_ref().cloned().unwrap_or_default(); + eprintln!("bearer {bearer:?}"); + let res = client + .post(format!("{}/orders", self.base_url())) + .bearer_auth(bearer) + .json(&order) + .send() + .await + .expect("Request failed"); + let res = res.text().await.unwrap(); + eprintln!("{:?}", res); + Ok(serde_json::from_str(&res).unwrap()) + // Ok(res + // .json::() + // .await + // .expect("Invalid response")) + } + fn base_url(&self) -> &str { if self.sandbox { "https://secure.snd.payu.com/api/v2_1" @@ -294,12 +331,12 @@ impl Client { #[cfg(test)] mod tests { - use tokio::test; - use super::*; #[test] - async fn create_order() { + fn create_order() { + dotenv::dotenv().ok(); + let client = Client { bearer: Some(String::from("d9a4536e-62ba-4f60-8017-6053211d3f47")), sandbox: true, @@ -307,7 +344,7 @@ mod tests { let res = client.create_order(Order { notify_url: Some(String::from("https://your.eshop.com/notify")), customer_ip: "127.0.0.1".to_string(), - merchant_pos_ip: "300746".to_string(), + merchant_pos_id: "300746".to_string(), description: "RTV market".to_string(), currency_code: "PLN".to_string(), total_amount: 21000, @@ -318,7 +355,33 @@ mod tests { ], order_create_date: None, }); - eprintln!("{:#?}", res); + assert!(res.is_ok()); + } + + #[tokio::test] + async fn create_order2() { + dotenv::dotenv().ok(); + + let client = Client { + bearer: Some(String::from("d9a4536e-62ba-4f60-8017-6053211d3f47")), + sandbox: true, + }; + let res = client + .create_order2(Order { + notify_url: Some(String::from("https://your.eshop.com/notify")), + customer_ip: "127.0.0.1".to_string(), + merchant_pos_id: "300746".to_string(), + description: "RTV market".to_string(), + currency_code: "PLN".to_string(), + total_amount: 21000, + buyer: Buyer::new("john.doe@example.com", "654111654", "John", "Doe", "pl"), + products: vec![ + Product::new("Wireless Mouse for Laptop", 15000, 1), + Product::new("HDMI cable", 6000, 1), + ], + order_create_date: None, + }) + .await; assert!(res.is_ok()); } }