Add order transactions

This commit is contained in:
eraden 2022-04-26 16:49:06 +02:00
parent cbc2a30e0e
commit 3ec4b209cd
2 changed files with 128 additions and 7 deletions

View File

@ -76,7 +76,7 @@ pub struct Buyer {
pub language: String,
}
impl From<model::Buyer> for pay_u::Buyer {
impl From<Buyer> for pay_u::Buyer {
fn from(b: Buyer) -> Self {
pay_u::Buyer::new(b.email, b.phone, b.first_name, b.last_name, b.language)
}
@ -104,10 +104,13 @@ pub(crate) async fn request_payment(
client: PayUClient,
) -> Result<PaymentResult> {
let mut client = &mut *client.lock();
client.create_order(
OrderCreateRequest::new(msg.buyer.into(), msg.customer_ip, msg.currency)
.with_description(msg.description)
.with_notify_url(msg.redirect_uri)
.with_products(msg.products.into_iter().map(Into::into)),
)
let order = client
.create_order(
OrderCreateRequest::new(msg.buyer.into(), msg.customer_ip, msg.currency)
.with_description(msg.description)
.with_notify_url(msg.redirect_uri)
.with_products(msg.products.into_iter().map(Into::into)),
)
.await?;
Ok(order)
}

View File

@ -1,6 +1,7 @@
mod deserialize;
mod serialize;
use std::fmt::Display;
use std::sync::Arc;
use reqwest::redirect;
@ -39,6 +40,8 @@ pub enum Error {
Refund,
#[error("Create order returned invalid response")]
CreateOrder,
#[error("Failed to fetch order transactions")]
OrderTransactions,
}
pub type Result<T> = std::result::Result<T, Error>;
@ -654,6 +657,76 @@ pub mod notify {
}
}
pub mod res {
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransactionPayMethod {
pub value: String,
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum CardProfile {
Consumer,
Business,
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum CardClassification {
Debit,
Credit,
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransactionCartData {
/// // "543402******4014",
pub card_number_masked: String,
/// MC (MasterCard/Maestro), VS (Visa)
/// Example; "MC"
pub card_scheme: String,
pub card_profile: CardProfile,
pub card_classification: CardClassification,
/// Example: "000"
pub card_response_code: String,
/// Example: "000 - OK"
pub card_response_code_desc: String,
/// Example: "5"
pub card_eci_code: String,
/// Example: "AY",
pub card3ds_status: String,
/// Example: "PL",
pub card_bin_country: String,
/// Example: "MCC0111LL1121"
pub first_transaction_id: String,
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransactionCardInstallmentProposal {}
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransactionCart {
pub cart_data: TransactionCartData,
pub card_installment_proposal: TransactionCardInstallmentProposal,
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Transaction {
pub pay_method: TransactionPayMethod,
pub payment_flow: String,
}
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Transactions {
pub transactions: Vec<Transaction>,
}
}
pub struct Client {
sandbox: bool,
merchant_pos_id: MerchantPosId,
@ -852,6 +925,51 @@ impl Client {
})
}
/// The transaction retrieve request message enables you to retrieve the
/// details of transactions created for an order.
///
/// Using this endpoint is extremely useful if you would like to get bank
/// account details or card details.
///
/// > Please note that although card details are available right after
/// > transaction has been processed, the bank details may be available
/// > either after few minutes or on the next business day, depending on the
/// > bank.
/// # Examples
///
/// ```
/// # use pay_u::*;
/// async fn order_transactions() {
/// let mut client = Client::new("145227", "12f071174cb7eb79d4aac5bc2f07563f", 300746)
/// .with_bearer("d9a4536e-62ba-4f60-8017-6053211d3f47", 2000)
/// .sandbox();
/// let res = client
/// .order_transactions("H9LL64F37H160126GUEST000P01")
/// .await;
/// }
/// ```
pub async fn order_transactions<OrderId: Display>(
&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);
let client = get_client!(self);
let text = client
.post(path)
.bearer_auth(bearer)
.send()
.await?
.text()
.await?;
log::trace!("Response: {}", text);
serde_json::from_str(&text).map_err(|e| {
log::error!("{e:?}");
Error::OrderTransactions
})
}
/// Get or refresh token
pub async fn authorize(&mut self) -> Result<bool> {
use chrono::{Duration, Utc};