From 35a3f2dee6b1c66d12060a95a11032c523241a86 Mon Sep 17 00:00:00 2001 From: eraden Date: Tue, 26 Apr 2022 19:55:50 +0200 Subject: [PATCH] Add OrderId type --- Cargo.lock | 1 + api/src/actors/payment_manager.rs | 4 ++-- pay_u/Cargo.toml | 2 ++ pay_u/src/lib.rs | 28 +++++++++++++++++----------- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f25a38d..ad63d22 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2138,6 +2138,7 @@ name = "pay_u" version = "0.1.4" dependencies = [ "chrono", + "derive_more", "dotenv", "log", "reqwest", diff --git a/api/src/actors/payment_manager.rs b/api/src/actors/payment_manager.rs index a404913..df0c1a0 100644 --- a/api/src/actors/payment_manager.rs +++ b/api/src/actors/payment_manager.rs @@ -102,7 +102,7 @@ pub struct RequestPayment { pub(crate) async fn request_payment( msg: RequestPayment, client: PayUClient, -) -> Result { +) -> Result { let mut client = &mut *client.lock(); let order = client .create_order( @@ -112,5 +112,5 @@ pub(crate) async fn request_payment( .with_products(msg.products.into_iter().map(Into::into)), ) .await?; - Ok(order) + Ok(order.order_id) } diff --git a/pay_u/Cargo.toml b/pay_u/Cargo.toml index a977a26..71cd86b 100644 --- a/pay_u/Cargo.toml +++ b/pay_u/Cargo.toml @@ -21,6 +21,8 @@ thiserror = { version = "1.0.30" } log = { version = "0.4.16" } +derive_more = { version = "0.99.17" } + [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 a51ab00..57f5013 100644 --- a/pay_u/src/lib.rs +++ b/pay_u/src/lib.rs @@ -46,6 +46,16 @@ pub enum Error { pub type Result = std::result::Result; +/// PayU internal order id +#[derive(derive_more::Display, derive_more::From, derive_more::Deref)] +pub struct OrderId(pub String); + +impl OrderId { + pub fn new>(id: S) -> Self { + Self(id.into()) + } +} + /// PayU payment status. /// /// Each payment is initially Pending and can change according to following @@ -594,6 +604,8 @@ pub struct Refund { pub mod notify { use serde::Deserialize; + use crate::OrderId; + /// Payment notification object received on [super::Order].[notify_url] #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] @@ -615,7 +627,7 @@ pub mod notify { #[serde(rename_all = "camelCase")] pub struct RefundUpdate { pub ext_order_id: String, - pub order_id: String, + pub order_id: OrderId, pub refund: Refund, } @@ -894,14 +906,11 @@ impl Client { /// .await; /// } /// ``` - pub async fn partial_refund( + pub async fn partial_refund( &mut self, order_id: OrderId, refund: RefundRequest, - ) -> Result - where - OrderId: std::fmt::Display, - { + ) -> Result { self.authorize().await?; if refund.description().trim().is_empty() { return Err(Error::NoDescription); @@ -948,10 +957,7 @@ impl Client { /// .await; /// } /// ``` - pub async fn order_transactions( - &mut self, - order_id: OrderId, - ) -> Result { + pub async fn order_transactions(&mut self, order_id: OrderId) -> Result { self.authorize().await?; let bearer = self.bearer.as_ref().cloned().unwrap_or_default(); let path = format!("{}/orders/{}/transactions", self.base_url(), order_id); @@ -1058,7 +1064,7 @@ mod tests { async fn partial_refund() { let res = build_client() .partial_refund( - "H9LL64F37H160126GUEST000P01", + OrderId::new("H9LL64F37H160126GUEST000P01"), RefundRequest::new("Refund", 1000), ) .await;