Add OrderId type

This commit is contained in:
eraden 2022-04-26 19:55:50 +02:00
parent 3ec4b209cd
commit 35a3f2dee6
4 changed files with 22 additions and 13 deletions

1
Cargo.lock generated
View File

@ -2138,6 +2138,7 @@ name = "pay_u"
version = "0.1.4" version = "0.1.4"
dependencies = [ dependencies = [
"chrono", "chrono",
"derive_more",
"dotenv", "dotenv",
"log", "log",
"reqwest", "reqwest",

View File

@ -102,7 +102,7 @@ pub struct RequestPayment {
pub(crate) async fn request_payment( pub(crate) async fn request_payment(
msg: RequestPayment, msg: RequestPayment,
client: PayUClient, client: PayUClient,
) -> Result<PaymentResult> { ) -> Result<pay_u::OrderId> {
let mut client = &mut *client.lock(); let mut client = &mut *client.lock();
let order = client let order = client
.create_order( .create_order(
@ -112,5 +112,5 @@ pub(crate) async fn request_payment(
.with_products(msg.products.into_iter().map(Into::into)), .with_products(msg.products.into_iter().map(Into::into)),
) )
.await?; .await?;
Ok(order) Ok(order.order_id)
} }

View File

@ -21,6 +21,8 @@ thiserror = { version = "1.0.30" }
log = { version = "0.4.16" } log = { version = "0.4.16" }
derive_more = { version = "0.99.17" }
[dev-dependencies] [dev-dependencies]
tokio = { version = "1.17.0", features = ["full"] } tokio = { version = "1.17.0", features = ["full"] }
dotenv = { version = "0.15.0" } dotenv = { version = "0.15.0" }

View File

@ -46,6 +46,16 @@ pub enum Error {
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
/// PayU internal order id
#[derive(derive_more::Display, derive_more::From, derive_more::Deref)]
pub struct OrderId(pub String);
impl OrderId {
pub fn new<S: Into<String>>(id: S) -> Self {
Self(id.into())
}
}
/// PayU payment status. /// PayU payment status.
/// ///
/// Each payment is initially Pending and can change according to following /// Each payment is initially Pending and can change according to following
@ -594,6 +604,8 @@ pub struct Refund {
pub mod notify { pub mod notify {
use serde::Deserialize; use serde::Deserialize;
use crate::OrderId;
/// Payment notification object received on [super::Order].[notify_url] /// Payment notification object received on [super::Order].[notify_url]
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@ -615,7 +627,7 @@ pub mod notify {
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct RefundUpdate { pub struct RefundUpdate {
pub ext_order_id: String, pub ext_order_id: String,
pub order_id: String, pub order_id: OrderId,
pub refund: Refund, pub refund: Refund,
} }
@ -894,14 +906,11 @@ impl Client {
/// .await; /// .await;
/// } /// }
/// ``` /// ```
pub async fn partial_refund<OrderId>( pub async fn partial_refund(
&mut self, &mut self,
order_id: OrderId, order_id: OrderId,
refund: RefundRequest, refund: RefundRequest,
) -> Result<PartialRefundResult> ) -> Result<PartialRefundResult> {
where
OrderId: std::fmt::Display,
{
self.authorize().await?; self.authorize().await?;
if refund.description().trim().is_empty() { if refund.description().trim().is_empty() {
return Err(Error::NoDescription); return Err(Error::NoDescription);
@ -948,10 +957,7 @@ impl Client {
/// .await; /// .await;
/// } /// }
/// ``` /// ```
pub async fn order_transactions<OrderId: Display>( pub async fn order_transactions(&mut self, order_id: OrderId) -> Result<res::Transactions> {
&mut self,
order_id: OrderId,
) -> Result<res::Transactions> {
self.authorize().await?; self.authorize().await?;
let bearer = self.bearer.as_ref().cloned().unwrap_or_default(); let bearer = self.bearer.as_ref().cloned().unwrap_or_default();
let path = format!("{}/orders/{}/transactions", self.base_url(), order_id); let path = format!("{}/orders/{}/transactions", self.base_url(), order_id);
@ -1058,7 +1064,7 @@ mod tests {
async fn partial_refund() { async fn partial_refund() {
let res = build_client() let res = build_client()
.partial_refund( .partial_refund(
"H9LL64F37H160126GUEST000P01", OrderId::new("H9LL64F37H160126GUEST000P01"),
RefundRequest::new("Refund", 1000), RefundRequest::new("Refund", 1000),
) )
.await; .await;