Working create order

This commit is contained in:
eraden 2022-04-22 23:10:37 +02:00
parent 79b306206f
commit bb69885361
3 changed files with 55 additions and 104 deletions

27
Cargo.lock generated
View File

@ -815,12 +815,6 @@ dependencies = [
"phf_codegen", "phf_codegen",
] ]
[[package]]
name = "chunked_transfer"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fff857943da45f546682664a79488be82e69e43c1a7a2307679ab9afb3a66d2e"
[[package]] [[package]]
name = "cipher" name = "cipher"
version = "0.3.0" version = "0.3.0"
@ -2149,7 +2143,6 @@ dependencies = [
"serde_json", "serde_json",
"thiserror", "thiserror",
"tokio", "tokio",
"ureq",
] ]
[[package]] [[package]]
@ -3503,26 +3496,6 @@ version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
[[package]]
name = "ureq"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9399fa2f927a3d327187cbd201480cee55bee6ac5d3c77dd27f0c6814cff16d5"
dependencies = [
"base64",
"chunked_transfer",
"encoding_rs",
"flate2",
"log",
"once_cell",
"rustls 0.20.4",
"serde",
"serde_json",
"url",
"webpki 0.22.0",
"webpki-roots 0.22.3",
]
[[package]] [[package]]
name = "url" name = "url"
version = "2.2.2" version = "2.2.2"

View File

@ -4,8 +4,7 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
ureq = { version = "2.4.0", features = ["json", "serde_json", "charset", "tls", "rustls"] } reqwest = { version = "0.11.10", features = ["default", "native-tls", "json"] }
reqwest = { version = "0.11.10", features = ["default", "rustls", "rustls-tls", "json"] }
serde = { version = "1.0.136", features = ["derive"] } serde = { version = "1.0.136", features = ["derive"] }
serde_json = { version = "1.0.79" } serde_json = { version = "1.0.79" }

View File

@ -1,3 +1,4 @@
use reqwest::redirect;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
@ -5,11 +6,11 @@ pub enum Error {
#[error("Client is not authorized. No bearer token available")] #[error("Client is not authorized. No bearer token available")]
NoToken, NoToken,
#[error("{0}")] #[error("{0}")]
UReq(#[from] ureq::Error),
#[error("{0}")]
Io(#[from] std::io::Error), Io(#[from] std::io::Error),
#[error("Total value is not sum of products price")] #[error("Total value is not sum of products price")]
IncorrectTotal, IncorrectTotal,
#[error("{0}")]
Reqwest(#[from] reqwest::Error),
} }
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
@ -214,7 +215,7 @@ pub struct CreateOrderResult {
/// This should be connected to your own order /// This should be connected to your own order
pub order_id: String, pub order_id: String,
/// This is YOUR_EXT_ORDER_ID /// This is YOUR_EXT_ORDER_ID
pub ext_order_id: String, pub ext_order_id: Option<String>,
} }
pub mod notify { pub mod notify {
@ -257,37 +258,43 @@ pub struct Client {
} }
impl Client { impl Client {
pub fn create_order(&self, order: Order) -> Result<CreateOrderResult> { /// Create new order in PayU
if order.total_amount ///
!= order /// ### IMPORTANT:
.products /// Do not follow redirect for any reason. Location points to
.iter() /// payment page which is useless in this context
.fold(0, |memo, p| memo + (p.unit_price * p.quantity as i32)) ///
{ /// ### IMPORTANT:
return Err(Error::IncorrectTotal); /// Do not use rustls. It'll freeze application!
} ///
/// # Examples
let req = ureq::post(&format!("{}/orders", self.base_url())) ///
.set("Content-Type", "application/json") /// ```rust
.set( /// # use pay_u::{Client, Order, Product, Buyer};
"Authorization", /// async fn pay() {
&self /// let client = Client {
.bearer /// bearer: Some(String::from("d9a4536e-62ba-4f60-8017-6053211d3f47")),
.as_ref() /// sandbox: true,
.map(|s| format!("Bearer {s}")) /// };
.ok_or(Error::NoToken)?, /// let res = client
); /// .create_order(Order {
// eprintln!("req {:?}", req); /// notify_url: Some(String::from("https://your.eshop.com/notify")),
let res = req.send_json(order)?; /// customer_ip: "127.0.0.1".to_string(),
// eprintln!("res {:?}", res); /// merchant_pos_id: "300746".to_string(),
// let r = res.into_string(); /// description: "RTV market".to_string(),
// let x = r.is_ok(); /// currency_code: "PLN".to_string(),
// let s = r.expect("Not a string"); /// total_amount: 21000,
// eprintln!("s {:}", s); /// buyer: Buyer::new("john.doe@example.com", "654111654", "John", "Doe", "pl"),
res.into_json::<CreateOrderResult>().map_err(Into::into) /// products: vec![
} /// Product::new("Wireless Mouse for Laptop", 15000, 1),
/// Product::new("HDMI cable", 6000, 1),
pub async fn create_order2(&self, order: Order) -> Result<CreateOrderResult> { /// ],
/// order_create_date: None,
/// })
/// .await;
/// }
/// ```
pub async fn create_order(&self, order: Order) -> Result<CreateOrderResult> {
if order.total_amount if order.total_amount
!= order != order
.products .products
@ -297,27 +304,24 @@ impl Client {
return Err(Error::IncorrectTotal); return Err(Error::IncorrectTotal);
} }
let client = reqwest::ClientBuilder::default() let client = reqwest::ClientBuilder::default()
.https_only(true) .user_agent("curl/7.82.0")
.referer(true) .use_native_tls()
.use_rustls_tls() // Do not follow!
.redirect(redirect::Policy::none())
.connection_verbose(true)
.build() .build()
.expect("Failed to create client"); .expect("Failed to create client");
let bearer = self.bearer.as_ref().cloned().unwrap_or_default(); let bearer = self.bearer.as_ref().cloned().unwrap_or_default();
eprintln!("bearer {bearer:?}"); let path = format!("{}/orders", self.base_url());
let res = client client
.post(format!("{}/orders", self.base_url())) .post(path)
.bearer_auth(bearer) .bearer_auth(bearer)
.json(&order) .json(&order)
.send() .send()
.await?
.json::<CreateOrderResult>()
.await .await
.expect("Request failed"); .map_err(Into::into)
let res = res.text().await.unwrap();
eprintln!("{:?}", res);
Ok(serde_json::from_str(&res).unwrap())
// Ok(res
// .json::<CreateOrderResult>()
// .await
// .expect("Invalid response"))
} }
fn base_url(&self) -> &str { fn base_url(&self) -> &str {
@ -333,33 +337,8 @@ impl Client {
mod tests { mod tests {
use super::*; use super::*;
#[test]
fn create_order() {
dotenv::dotenv().ok();
let client = Client {
bearer: Some(String::from("d9a4536e-62ba-4f60-8017-6053211d3f47")),
sandbox: true,
};
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_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,
});
assert!(res.is_ok());
}
#[tokio::test] #[tokio::test]
async fn create_order2() { async fn create_order() {
dotenv::dotenv().ok(); dotenv::dotenv().ok();
let client = Client { let client = Client {
@ -367,7 +346,7 @@ mod tests {
sandbox: true, sandbox: true,
}; };
let res = client let res = client
.create_order2(Order { .create_order(Order {
notify_url: Some(String::from("https://your.eshop.com/notify")), notify_url: Some(String::from("https://your.eshop.com/notify")),
customer_ip: "127.0.0.1".to_string(), customer_ip: "127.0.0.1".to_string(),
merchant_pos_id: "300746".to_string(), merchant_pos_id: "300746".to_string(),