Request PayU

This commit is contained in:
eraden 2022-04-22 22:15:00 +02:00
parent 4943e370b6
commit 79b306206f
4 changed files with 83 additions and 15 deletions

View File

@ -1,5 +1,5 @@
[target.x86_64-unknown-linux-gnu] #[target.x86_64-unknown-linux-gnu]
linker = "clang" #linker = "clang"
rustflags = [ #rustflags = [
"-C", "link-arg=-fuse-ld=mold", # "-C", "link-arg=-fuse-ld=mold",
] #]

3
Cargo.lock generated
View File

@ -2143,6 +2143,8 @@ name = "pay_u"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"dotenv",
"reqwest",
"serde", "serde",
"serde_json", "serde_json",
"thiserror", "thiserror",
@ -3509,6 +3511,7 @@ checksum = "9399fa2f927a3d327187cbd201480cee55bee6ac5d3c77dd27f0c6814cff16d5"
dependencies = [ dependencies = [
"base64", "base64",
"chunked_transfer", "chunked_transfer",
"encoding_rs",
"flate2", "flate2",
"log", "log",
"once_cell", "once_cell",

View File

@ -4,7 +4,8 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [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 = { version = "1.0.136", features = ["derive"] }
serde_json = { version = "1.0.79" } serde_json = { version = "1.0.79" }
@ -15,3 +16,4 @@ thiserror = { version = "1.0.30" }
[dev-dependencies] [dev-dependencies]
tokio = { version = "1.17.0", features = ["full"] } tokio = { version = "1.17.0", features = ["full"] }
dotenv = { version = "0.15.0" }

View File

@ -152,7 +152,7 @@ pub struct Order {
/// Customer client IP address /// Customer client IP address
pub customer_ip: String, pub customer_ip: String,
/// Secret pos ip. This is connected to PayU account /// Secret pos ip. This is connected to PayU account
pub merchant_pos_ip: String, pub merchant_pos_id: String,
/// Transaction description /// Transaction description
pub description: String, pub description: String,
/// 3 characters currency identifier, ex. PLN /// 3 characters currency identifier, ex. PLN
@ -258,7 +258,6 @@ pub struct Client {
impl Client { impl Client {
pub fn create_order(&self, order: Order) -> Result<CreateOrderResult> { pub fn create_order(&self, order: Order) -> Result<CreateOrderResult> {
println!("{}", serde_json::to_string_pretty(&order).unwrap());
if order.total_amount if order.total_amount
!= order != order
.products .products
@ -278,11 +277,49 @@ impl Client {
.map(|s| format!("Bearer {s}")) .map(|s| format!("Bearer {s}"))
.ok_or(Error::NoToken)?, .ok_or(Error::NoToken)?,
); );
eprintln!("{:?}", req); // eprintln!("req {:?}", req);
let res = req.send_string(&serde_json::to_string_pretty(&order).unwrap())?; 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::<CreateOrderResult>().map_err(Into::into) res.into_json::<CreateOrderResult>().map_err(Into::into)
} }
pub async fn create_order2(&self, order: Order) -> Result<CreateOrderResult> {
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::<CreateOrderResult>()
// .await
// .expect("Invalid response"))
}
fn base_url(&self) -> &str { fn base_url(&self) -> &str {
if self.sandbox { if self.sandbox {
"https://secure.snd.payu.com/api/v2_1" "https://secure.snd.payu.com/api/v2_1"
@ -294,12 +331,12 @@ impl Client {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use tokio::test;
use super::*; use super::*;
#[test] #[test]
async fn create_order() { fn create_order() {
dotenv::dotenv().ok();
let client = Client { let client = Client {
bearer: Some(String::from("d9a4536e-62ba-4f60-8017-6053211d3f47")), bearer: Some(String::from("d9a4536e-62ba-4f60-8017-6053211d3f47")),
sandbox: true, sandbox: true,
@ -307,7 +344,7 @@ mod tests {
let res = client.create_order(Order { let res = client.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_ip: "300746".to_string(), merchant_pos_id: "300746".to_string(),
description: "RTV market".to_string(), description: "RTV market".to_string(),
currency_code: "PLN".to_string(), currency_code: "PLN".to_string(),
total_amount: 21000, total_amount: 21000,
@ -318,7 +355,33 @@ mod tests {
], ],
order_create_date: None, 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()); assert!(res.is_ok());
} }
} }