diff --git a/Cargo.lock b/Cargo.lock index 2e34a0a..ed48d89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -613,6 +613,7 @@ dependencies = [ "oauth2", "parking_lot 0.12.0", "password-hash", + "pay_u 0.1.2", "pretty_env_logger", "rand_core", "sendgrid", @@ -2135,6 +2136,20 @@ checksum = "0c520e05135d6e763148b6426a837e239041653ba7becd2e538c076c738025fc" [[package]] name = "pay_u" version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20aa37e1a7272d03e52e25dc66a9d5fb965f299123b415b7172256631d99a30a" +dependencies = [ + "chrono", + "log", + "reqwest", + "serde", + "serde_json", + "thiserror", +] + +[[package]] +name = "pay_u" +version = "0.1.3" dependencies = [ "chrono", "dotenv", diff --git a/api/Cargo.toml b/api/Cargo.toml index f08ddca..d76f275 100644 --- a/api/Cargo.toml +++ b/api/Cargo.toml @@ -64,3 +64,5 @@ async-trait = { version = "0.1.53", features = [] } jemallocator = { version = "0.3.2", features = [] } sendgrid = { version = "0.17.4", features = ["async"] } + +pay_u = { version = '0.1', features = ["single-client"] } diff --git a/api/src/actors/mod.rs b/api/src/actors/mod.rs index 0e24353..0e35f36 100644 --- a/api/src/actors/mod.rs +++ b/api/src/actors/mod.rs @@ -2,4 +2,5 @@ pub mod cart_manager; pub mod database; pub mod email_manager; pub mod order_manager; +pub mod payment_manager; pub mod token_manager; diff --git a/api/src/actors/payment_manager.rs b/api/src/actors/payment_manager.rs new file mode 100644 index 0000000..fe53476 --- /dev/null +++ b/api/src/actors/payment_manager.rs @@ -0,0 +1,20 @@ +use pay_u::MerchantPosId; + +pub struct PaymentManager { + client: pay_u::Client, +} + +impl PaymentManager { + pub fn new( + client_id: ClientId, + client_secret: ClientSecret, + merchant_pos_id: MerchantPosId, + ) -> Self + where + ClientId: Into, + ClientSecret: Into, + { + let mut client = pay_u::Client::new(client_id, client_secret, merchant_pos_id); + Self { client } + } +} diff --git a/pay_u/Cargo.toml b/pay_u/Cargo.toml index fc7d05e..828dc98 100644 --- a/pay_u/Cargo.toml +++ b/pay_u/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "pay_u" description = "PayU Rest API wrapper" -version = "0.1.2" +version = "0.1.3" edition = "2021" license = "MIT" diff --git a/pay_u/README.md b/pay_u/README.md index 3f16cbc..3c73bc0 100644 --- a/pay_u/README.md +++ b/pay_u/README.md @@ -14,6 +14,7 @@ async fn usage() { let client_secret = std::env::var("PAYU_CLIENT_SECRET").unwrap(); let merchant_id = std::env::var("PAYU_CLIENT_MERCHANT_ID").unwrap().parse().unwrap(); let mut client = Client::new(client_id, client_secret, merchant_id); + client.authorize().await.expect("Invalid credentials"); let _res = client.create_order( OrderCreateRequest::new( @@ -47,6 +48,26 @@ async fn usage() { } ``` +### Notification with Actix + +```rust +use actix_web::{*, web::*}; + +#[post("/pay_u/{own_order_id}/notify")] +async fn handle_notification(path: Path, Json(notify): Json) -> HttpResponse { + let status = notify.status(); + let order_id = path.into_inner(); + match handle_update(order_id, status, notify) { + Ok(_) => (), + Err(e) => { + // ALWAYS SEND OK! + log::error!("{e:?}"); + } + }; + HttpResponse::Ok().body("") +} +``` + ## Bugs Please report bugs here: https://todo.sr.ht/~tsumanu/payu-rs diff --git a/pay_u/src/lib.rs b/pay_u/src/lib.rs index 95450c4..f8de97d 100644 --- a/pay_u/src/lib.rs +++ b/pay_u/src/lib.rs @@ -853,7 +853,7 @@ impl Client { } /// Get or refresh token - pub(crate) async fn authorize(&mut self) -> Result { + pub async fn authorize(&mut self) -> Result { use chrono::{Duration, Utc}; if Utc::now() - Duration::seconds(1) < self.bearer_expires_at { return Ok(true);