Merge remote-tracking branch 'origin/master'

# Conflicts:
#	pay_u/src/lib.rs
This commit is contained in:
eraden 2022-04-26 19:56:10 +02:00
commit 9aeb78d64e
3 changed files with 83 additions and 45 deletions

View File

@ -3,8 +3,7 @@ use std::sync::Arc;
use parking_lot::Mutex;
use pay_u::{MerchantPosId, OrderCreateRequest};
use crate::model;
use crate::model::Product;
use crate::model::{Price, Quantity};
#[macro_export]
macro_rules! pay_async_handler {
@ -63,6 +62,7 @@ pub struct PaymentResult {
pub payu_order_id: String,
}
#[derive(Debug)]
pub struct Buyer {
/// Required customer e-mail
pub email: String,
@ -82,16 +82,23 @@ impl From<Buyer> for pay_u::Buyer {
}
}
impl From<model::Product> for pay_u::Product {
#[derive(Debug)]
pub struct Product {
pub name: String,
pub unit_price: Price,
pub quantity: Quantity,
}
impl From<Product> for pay_u::Product {
fn from(p: Product) -> Self {
todo!()
pay_u::Product::new(p.name, **p.unit_price, **p.quantity as u32)
}
}
#[derive(Debug, actix::Message)]
#[rtype(result = "Result<PaymentResult>")]
pub struct RequestPayment {
pub products: Vec<model::Product>,
pub products: Vec<Product>,
pub redirect_uri: String,
pub currency: String,
pub description: String,

View File

@ -131,12 +131,12 @@ impl Default for Audience {
}
}
#[derive(sqlx::Type, Serialize, Deserialize, Deref, From)]
#[derive(sqlx::Type, Serialize, Deserialize, Debug, Deref, From)]
#[sqlx(transparent)]
#[serde(transparent)]
pub struct Price(NonNegative);
#[derive(sqlx::Type, Serialize, Deserialize, Default, Deref, From)]
#[derive(sqlx::Type, Serialize, Deserialize, Default, Debug, Deref, From)]
#[sqlx(transparent)]
#[serde(transparent)]
pub struct Quantity(NonNegative);
@ -200,7 +200,7 @@ impl<'de> serde::Deserialize<'de> for Email {
}
}
#[derive(sqlx::Type, Serialize, Default, Deref, Display)]
#[derive(sqlx::Type, Serialize, Default, Debug, Deref, Display)]
#[sqlx(transparent)]
#[serde(transparent)]
pub struct NonNegative(i32);

View File

@ -40,8 +40,13 @@ pub enum Error {
Refund,
#[error("Create order returned invalid response")]
CreateOrder,
<<<<<<< HEAD
#[error("Operation failed with {0:?}")]
OpFailed(Status),
=======
#[error("Failed to fetch order transactions")]
OrderTransactions,
>>>>>>> 3ec4b209cd6a2ebefb4419083f9379794784d11c
}
pub type Result<T> = std::result::Result<T, Error>;
@ -541,9 +546,12 @@ impl Status {
}
}
#[derive(Deserialize, Debug)]
pub mod result {
use crate::{Refund, Status};
#[derive(serde::Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CreateOrderResult {
pub struct CreateOrder {
/// Http status as a text
pub status: Status,
/// Client should be redirected to this URI
@ -554,13 +562,14 @@ pub struct CreateOrderResult {
pub ext_order_id: Option<String>,
}
#[derive(Deserialize, Debug)]
#[derive(serde::Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PartialRefundResult {
pub struct PartialRefund {
pub order_id: Option<String>,
pub refund: Option<Refund>,
pub status: Status,
}
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
@ -583,6 +592,7 @@ impl RefundRequest {
pub fn description(&self) -> &str {
&self.description
}
pub fn amount(&self) -> Price {
self.amount
}
@ -606,6 +616,8 @@ pub mod notify {
use crate::OrderId;
use super::deserialize;
/// Payment notification object received on [super::Order].[notify_url]
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
@ -647,20 +659,31 @@ pub mod notify {
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Order {
#[serde(flatten)]
pub order: super::OrderCreateRequest,
pub notify_url: Option<String>,
/// Customer client IP address
pub customer_ip: String,
/// Secret pos ip. This is connected to PayU account
#[serde(deserialize_with = "deserialize::deserialize_i32")]
pub merchant_pos_id: super::MerchantPosId,
/// Transaction description
pub description: String,
/// 3 characters currency identifier, ex. PLN
pub currency_code: String,
/// Total price of the order in pennies (e.g. 1000 is 10.00 EUR).
/// Applies also to currencies without subunits (e.g. 1000 is 10
/// HUF).
#[serde(deserialize_with = "deserialize::deserialize_i32")]
pub total_amount: super::Price,
/// @see [Buyer]
pub buyer: Option<super::Buyer>,
/// List of products
pub products: Vec<super::Product>,
#[serde(skip_serializing)]
pub order_create_date: Option<String>,
pub pay_method: Option<super::PayMethod>,
pub status: super::PaymentStatus,
}
impl std::ops::Deref for Order {
type Target = super::OrderCreateRequest;
fn deref(&self) -> &Self::Target {
&self.order
}
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Prop {
@ -833,7 +856,7 @@ impl Client {
/// .await;
/// }
/// ```
pub async fn create_order(&mut self, order: OrderCreateRequest) -> Result<CreateOrderResult> {
pub async fn create_order(&mut self, order: OrderCreateRequest) -> Result<result::CreateOrder> {
self.authorize().await?;
if order.total_amount
!= order
@ -864,10 +887,14 @@ impl Client {
.text()
.await?;
log::trace!("Response: {}", text);
serde_json::from_str(&text).map_err(|e| {
let res: result::CreateOrder = serde_json::from_str(&text).map_err(|e| {
log::error!("{e:?}");
Error::CreateOrder
})
})?;
if res.status.status_code != "Success" {
return Err(Error::OpFailed(res.status));
}
Ok(res)
}
/// The PayU system fully supports refunds for processed payments, the
@ -910,7 +937,7 @@ impl Client {
&mut self,
order_id: OrderId,
refund: RefundRequest,
) -> Result<PartialRefundResult> {
) -> Result<result::PartialRefund> {
self.authorize().await?;
if refund.description().trim().is_empty() {
return Err(Error::NoDescription);
@ -928,10 +955,14 @@ impl Client {
.text()
.await?;
log::trace!("Response: {}", text);
serde_json::from_str::<'_, PartialRefundResult>(&text).map_err(|e| {
let res: result::PartialRefund = serde_json::from_str(&text).map_err(|e| {
log::error!("Invalid PayU response {e:?}");
Error::Refund
})
})?;
if res.status.status_code.as_str() != "Success" {
return Err(Error::OpFailed(res.status));
}
Ok(res)
}
/// The transaction retrieve request message enables you to retrieve the
@ -1077,7 +1108,7 @@ mod tests {
#[test]
fn check_accepted_refund_json() {
let res = serde_json::from_str::<PartialRefundResult>(include_str!(
let res = serde_json::from_str::<result::PartialRefund>(include_str!(
"../tests/responses/accepted_refund.json"
));
assert!(res.is_ok());
@ -1119,14 +1150,14 @@ mod tests {
}
#[test]
fn check_rejection_json() {
let res = serde_json::from_str::<PartialRefundResult>(include_str!(
let res = serde_json::from_str::<result::PartialRefund>(include_str!(
"../tests/responses/rejection.json"
));
assert!(res.is_ok());
}
#[test]
fn check_custom_literal_json() {
let res = serde_json::from_str::<PartialRefundResult>(include_str!(
let res = serde_json::from_str::<result::PartialRefund>(include_str!(
"../tests/responses/custom_code_literal.json"
));
assert!(res.is_ok());