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"
dependencies = [
"chrono",
"derive_more",
"dotenv",
"log",
"reqwest",

View File

@ -102,7 +102,7 @@ pub struct RequestPayment {
pub(crate) async fn request_payment(
msg: RequestPayment,
client: PayUClient,
) -> Result<PaymentResult> {
) -> Result<pay_u::OrderId> {
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)
}

View File

@ -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" }

View File

@ -46,6 +46,16 @@ pub enum 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.
///
/// 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<OrderId>(
pub async fn partial_refund(
&mut self,
order_id: OrderId,
refund: RefundRequest,
) -> Result<PartialRefundResult>
where
OrderId: std::fmt::Display,
{
) -> Result<PartialRefundResult> {
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<OrderId: Display>(
&mut self,
order_id: OrderId,
) -> Result<res::Transactions> {
pub async fn order_transactions(&mut self, order_id: OrderId) -> Result<res::Transactions> {
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;