mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-02 04:04:43 +08:00
refactor(common_utils): move serde implementations and date-time utils to common_utils crate (#40)
This commit is contained in:
52
Cargo.lock
generated
52
Cargo.lock
generated
@ -841,6 +841,7 @@ dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_urlencoded",
|
||||
"time",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1856,20 +1857,11 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num_threads"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.15.0"
|
||||
version = "1.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e82dad04139b71a90c080c8463fe0dc7902db5192d939bd0950f074d014339e1"
|
||||
checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
|
||||
|
||||
[[package]]
|
||||
name = "opaque-debug"
|
||||
@ -2686,18 +2678,18 @@ checksum = "e25dfac463d778e353db5be2449d1cce89bd6fd23c9f1ea21310ce6e5a1b29c4"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.145"
|
||||
version = "1.0.148"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b"
|
||||
checksum = "e53f64bb4ba0191d6d0676e1b141ca55047d83b74f5607e6d8eb88126c52c2dc"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.145"
|
||||
version = "1.0.148"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "81fa1584d3d1bcacd84c277a0dfe21f5b0f6accf4a23d04d4c6d61f1af522b4c"
|
||||
checksum = "a55492425aa53521babf6137309e7d34c20bbfbbfcfe2c7f3a047fd1f6b92c0c"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@ -2706,9 +2698,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.85"
|
||||
version = "1.0.89"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44"
|
||||
checksum = "020ff22c755c2ed3f8cf162dbb41a7268d934702f3ed3631656ea597e08fc3db"
|
||||
dependencies = [
|
||||
"itoa 1.0.3",
|
||||
"ryu",
|
||||
@ -2886,9 +2878,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.101"
|
||||
version = "1.0.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e90cde112c4b9690b8cbe810cba9ddd8bc1d7472e2cae317b69e9438c1cba7d2"
|
||||
checksum = "4ae548ec36cf198c0ef7710d3c230987c2d6d7bd98ad6edc0274462724c585ce"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@ -2972,22 +2964,30 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.3.14"
|
||||
version = "0.3.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c3f9a28b618c3a6b9251b6908e9c99e04b9e5c02e6581ccbb67d59c34ef7f9b"
|
||||
checksum = "a561bf4617eebd33bca6434b988f39ed798e527f51a1e797d0ee4f61c0a38376"
|
||||
dependencies = [
|
||||
"itoa 1.0.3",
|
||||
"libc",
|
||||
"num_threads",
|
||||
"serde",
|
||||
"time-core",
|
||||
"time-macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time-macros"
|
||||
version = "0.2.4"
|
||||
name = "time-core"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42657b1a6f4d817cda8e7a0ace261fe0cc946cf3a80314390b22cc61ae080792"
|
||||
checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd"
|
||||
|
||||
[[package]]
|
||||
name = "time-macros"
|
||||
version = "0.2.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d967f99f534ca7e495c575c62638eebc2898a8c84c119b89e250477bc4ba16b2"
|
||||
dependencies = [
|
||||
"time-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec"
|
||||
|
||||
@ -10,6 +10,7 @@ error-stack = "0.2.1"
|
||||
serde = { version = "1.0.145", features = ["derive"] }
|
||||
serde_json = "1.0.85"
|
||||
serde_urlencoded = "0.7.1"
|
||||
time = { version = "0.3.17", features = ["serde", "serde-well-known", "std"] }
|
||||
|
||||
# First party crates
|
||||
masking = { version = "0.1.0", path = "../masking" }
|
||||
|
||||
87
crates/common_utils/src/custom_serde.rs
Normal file
87
crates/common_utils/src/custom_serde.rs
Normal file
@ -0,0 +1,87 @@
|
||||
//! Custom serialization/deserialization implementations.
|
||||
|
||||
/// Use the well-known ISO 8601 format when serializing and deserializing an
|
||||
/// [`PrimitiveDateTime`][PrimitiveDateTime].
|
||||
///
|
||||
/// [PrimitiveDateTime]: ::time::PrimitiveDateTime
|
||||
pub mod iso8601 {
|
||||
use std::num::NonZeroU8;
|
||||
|
||||
use serde::{ser::Error as _, Deserializer, Serialize, Serializer};
|
||||
use time::{
|
||||
format_description::well_known::{
|
||||
iso8601::{Config, EncodedConfig, TimePrecision},
|
||||
Iso8601,
|
||||
},
|
||||
serde::iso8601,
|
||||
PrimitiveDateTime, UtcOffset,
|
||||
};
|
||||
|
||||
const FORMAT_CONFIG: EncodedConfig = Config::DEFAULT
|
||||
.set_time_precision(TimePrecision::Second {
|
||||
decimal_digits: NonZeroU8::new(3),
|
||||
})
|
||||
.encode();
|
||||
|
||||
/// Serialize a [`PrimitiveDateTime`] using the well-known ISO 8601 format.
|
||||
pub fn serialize<S>(date_time: &PrimitiveDateTime, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
date_time
|
||||
.assume_utc()
|
||||
.format(&Iso8601::<FORMAT_CONFIG>)
|
||||
.map_err(S::Error::custom)?
|
||||
.serialize(serializer)
|
||||
}
|
||||
|
||||
/// Deserialize an [`PrimitiveDateTime`] from its ISO 8601 representation.
|
||||
pub fn deserialize<'a, D>(deserializer: D) -> Result<PrimitiveDateTime, D::Error>
|
||||
where
|
||||
D: Deserializer<'a>,
|
||||
{
|
||||
iso8601::deserialize(deserializer).map(|offset_date_time| {
|
||||
let utc_date_time = offset_date_time.to_offset(UtcOffset::UTC);
|
||||
PrimitiveDateTime::new(utc_date_time.date(), utc_date_time.time())
|
||||
})
|
||||
}
|
||||
|
||||
/// Use the well-known ISO 8601 format when serializing and deserializing an
|
||||
/// [`Option<PrimitiveDateTime>`][PrimitiveDateTime].
|
||||
///
|
||||
/// [PrimitiveDateTime]: ::time::PrimitiveDateTime
|
||||
pub mod option {
|
||||
use serde::Serialize;
|
||||
use time::format_description::well_known::Iso8601;
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Serialize an [`Option<PrimitiveDateTime>`] using the well-known ISO 8601 format.
|
||||
pub fn serialize<S>(
|
||||
date_time: &Option<PrimitiveDateTime>,
|
||||
serializer: S,
|
||||
) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
date_time
|
||||
.map(|date_time| date_time.assume_utc().format(&Iso8601::<FORMAT_CONFIG>))
|
||||
.transpose()
|
||||
.map_err(S::Error::custom)?
|
||||
.serialize(serializer)
|
||||
}
|
||||
|
||||
/// Deserialize an [`Option<PrimitiveDateTime>`] from its ISO 8601 representation.
|
||||
pub fn deserialize<'a, D>(deserializer: D) -> Result<Option<PrimitiveDateTime>, D::Error>
|
||||
where
|
||||
D: Deserializer<'a>,
|
||||
{
|
||||
iso8601::option::deserialize(deserializer).map(|option_offset_date_time| {
|
||||
option_offset_date_time.map(|offset_date_time| {
|
||||
let utc_date_time = offset_date_time.to_offset(UtcOffset::UTC);
|
||||
PrimitiveDateTime::new(utc_date_time.date(), utc_date_time.time())
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -13,5 +13,17 @@
|
||||
)]
|
||||
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR" ), "/", "README.md"))]
|
||||
|
||||
pub mod custom_serde;
|
||||
pub mod errors;
|
||||
pub mod ext_traits;
|
||||
|
||||
/// Date-time utilities.
|
||||
pub mod date_time {
|
||||
use time::{OffsetDateTime, PrimitiveDateTime};
|
||||
|
||||
/// Create a new [`PrimitiveDateTime`] with the current date and time in UTC.
|
||||
pub fn now() -> PrimitiveDateTime {
|
||||
let utc_date_time = OffsetDateTime::now_utc();
|
||||
PrimitiveDateTime::new(utc_date_time.date(), utc_date_time.time())
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
use common_utils::custom_serde;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
@ -12,7 +13,6 @@ use crate::{
|
||||
},
|
||||
storage::enums::{self, FutureUsage, IntentStatus, PaymentMethodType},
|
||||
},
|
||||
utils::custom_serde,
|
||||
};
|
||||
|
||||
#[derive(Default, Serialize, PartialEq, Eq, Deserialize, Clone)]
|
||||
|
||||
@ -82,7 +82,7 @@ pub async fn add_payment_method(
|
||||
payment_method_issuer: req.payment_method_issuer,
|
||||
card: None,
|
||||
metadata: req.metadata,
|
||||
created: Some(crate::utils::date_time::now()),
|
||||
created: Some(common_utils::date_time::now()),
|
||||
payment_method_issuer_code: req.payment_method_issuer_code,
|
||||
recurring_enabled: false, //TODO
|
||||
installment_payment_enabled: false, //TODO
|
||||
@ -504,7 +504,7 @@ pub async fn get_tempcard_from_payment_method(
|
||||
let card_info_val = get_card_info_value(&state.conf.keys, card_info).await?;
|
||||
let temp_card = storage::TempCard {
|
||||
card_info: Some(card_info_val),
|
||||
date_created: crate::utils::date_time::now(),
|
||||
date_created: common_utils::date_time::now(),
|
||||
txn_id: None,
|
||||
id: payment_token,
|
||||
};
|
||||
|
||||
@ -112,7 +112,7 @@ pub fn mk_add_card_response(
|
||||
payment_method_issuer: req.payment_method_issuer,
|
||||
card: Some(card),
|
||||
metadata: req.metadata,
|
||||
created: Some(crate::utils::date_time::now()),
|
||||
created: Some(common_utils::date_time::now()),
|
||||
payment_method_issuer_code: req.payment_method_issuer_code,
|
||||
recurring_enabled: false, //TODO
|
||||
installment_payment_enabled: false, //TODO
|
||||
|
||||
@ -678,7 +678,7 @@ pub async fn create_temp_card(
|
||||
let card_info_val = cards::get_card_info_value(&state.conf.keys, card_info).await?;
|
||||
temp_card = storage::TempCardNew {
|
||||
card_info: Some(card_info_val),
|
||||
date_created: crate::utils::date_time::now(),
|
||||
date_created: common_utils::date_time::now(),
|
||||
txn_id: Some(txn_id.to_string()),
|
||||
id: None,
|
||||
};
|
||||
|
||||
@ -299,7 +299,7 @@ impl PaymentCreate {
|
||||
request: &api::PaymentsRequest,
|
||||
browser_info: Option<serde_json::Value>,
|
||||
) -> storage::PaymentAttemptNew {
|
||||
let created_at @ modified_at @ last_synced = Some(crate::utils::date_time::now());
|
||||
let created_at @ modified_at @ last_synced = Some(common_utils::date_time::now());
|
||||
let status =
|
||||
helpers::payment_attempt_status_fsm(&request.payment_method_data, request.confirm);
|
||||
let (amount, currency) = (money.0, Some(money.1));
|
||||
@ -334,7 +334,7 @@ impl PaymentCreate {
|
||||
shipping_address_id: Option<String>,
|
||||
billing_address_id: Option<String>,
|
||||
) -> storage::PaymentIntentNew {
|
||||
let created_at @ modified_at @ last_synced = Some(crate::utils::date_time::now());
|
||||
let created_at @ modified_at @ last_synced = Some(common_utils::date_time::now());
|
||||
let status =
|
||||
helpers::payment_intent_status_fsm(&request.payment_method_data, request.confirm);
|
||||
let client_secret =
|
||||
|
||||
@ -414,7 +414,7 @@ fn mk_new_refund(
|
||||
merchant_id: &str,
|
||||
refund_amount: i32,
|
||||
) -> storage::RefundNew {
|
||||
let current_time = crate::utils::date_time::now();
|
||||
let current_time = common_utils::date_time::now();
|
||||
let connecter_transaction_id = match &payment_attempt.connector_transaction_id {
|
||||
Some(id) => id,
|
||||
None => "",
|
||||
@ -681,7 +681,7 @@ pub async fn add_refund_sync_task(
|
||||
refund: &storage::Refund,
|
||||
runner: &str,
|
||||
) -> RouterResult<storage::ProcessTracker> {
|
||||
let current_time = crate::utils::date_time::now();
|
||||
let current_time = common_utils::date_time::now();
|
||||
let refund_workflow_model = serde_json::to_value(refund_to_refund_core_workflow_model(refund))
|
||||
.into_report()
|
||||
.change_context(errors::ApiErrorResponse::InternalServerError)
|
||||
@ -693,7 +693,7 @@ pub async fn add_refund_sync_task(
|
||||
tag: vec![String::from("REFUND")],
|
||||
runner: Some(String::from(runner)),
|
||||
retry_count: 0,
|
||||
schedule_time: Some(crate::utils::date_time::now()),
|
||||
schedule_time: Some(common_utils::date_time::now()),
|
||||
rule: String::new(),
|
||||
tracking_data: refund_workflow_model,
|
||||
business_status: String::from("Pending"),
|
||||
@ -717,7 +717,7 @@ pub async fn add_refund_execute_task(
|
||||
runner: &str,
|
||||
) -> RouterResult<storage::ProcessTracker> {
|
||||
let task = "EXECUTE_REFUND";
|
||||
let current_time = crate::utils::date_time::now();
|
||||
let current_time = common_utils::date_time::now();
|
||||
let refund_workflow_model = serde_json::to_value(refund_to_refund_core_workflow_model(refund))
|
||||
.into_report()
|
||||
.change_context(errors::ApiErrorResponse::InternalServerError)
|
||||
@ -728,7 +728,7 @@ pub async fn add_refund_execute_task(
|
||||
tag: vec![String::from("REFUND")],
|
||||
runner: Some(String::from(runner)),
|
||||
retry_count: 0,
|
||||
schedule_time: Some(crate::utils::date_time::now()),
|
||||
schedule_time: Some(common_utils::date_time::now()),
|
||||
rule: String::new(),
|
||||
tracking_data: refund_workflow_model,
|
||||
business_status: String::from("Pending"),
|
||||
|
||||
@ -70,7 +70,7 @@ pub fn validate_refund_amount(
|
||||
pub fn validate_payment_order_age(
|
||||
created_at: &PrimitiveDateTime,
|
||||
) -> CustomResult<(), RefundValidationError> {
|
||||
let current_time = utils::date_time::now();
|
||||
let current_time = common_utils::date_time::now();
|
||||
|
||||
utils::when(
|
||||
(current_time - *created_at).whole_days() > REFUND_MAX_AGE,
|
||||
|
||||
@ -146,6 +146,7 @@ mod storage {
|
||||
|
||||
#[cfg(feature = "kv_store")]
|
||||
mod storage {
|
||||
use common_utils::date_time;
|
||||
use error_stack::{IntoReport, ResultExt};
|
||||
use fred::prelude::*;
|
||||
use redis_interface::RedisEntryId;
|
||||
@ -156,7 +157,7 @@ mod storage {
|
||||
core::errors::{self, CustomResult},
|
||||
services::Store,
|
||||
types::storage::{enums, payment_attempt::*},
|
||||
utils::{date_time, storage_partitioning::KvStorePartition},
|
||||
utils::storage_partitioning::KvStorePartition,
|
||||
};
|
||||
|
||||
#[async_trait::async_trait]
|
||||
|
||||
@ -34,6 +34,7 @@ pub trait IPaymentIntent {
|
||||
|
||||
#[cfg(feature = "kv_store")]
|
||||
mod storage {
|
||||
use common_utils::date_time;
|
||||
use error_stack::{IntoReport, ResultExt};
|
||||
use fred::prelude::{RedisErrorKind, *};
|
||||
use redis_interface::RedisEntryId;
|
||||
@ -44,7 +45,7 @@ mod storage {
|
||||
core::errors::{self, CustomResult},
|
||||
services::Store,
|
||||
types::{api, storage::payment_intent::*},
|
||||
utils::{date_time, storage_partitioning::KvStorePartition},
|
||||
utils::storage_partitioning::KvStorePartition,
|
||||
};
|
||||
|
||||
#[async_trait::async_trait]
|
||||
|
||||
@ -23,7 +23,6 @@ use crate::{
|
||||
routes::AppState,
|
||||
scheduler::utils as pt_utils,
|
||||
types::storage::{self, enums},
|
||||
utils::date_time,
|
||||
};
|
||||
|
||||
// Valid consumer business statuses
|
||||
@ -114,7 +113,7 @@ pub async fn consumer_operations(
|
||||
let mut handler = vec![];
|
||||
|
||||
for task in tasks.iter_mut() {
|
||||
let pickup_time = date_time::now();
|
||||
let pickup_time = common_utils::date_time::now();
|
||||
|
||||
pt_utils::add_histogram_metrics(&pickup_time, task, &stream_name);
|
||||
|
||||
|
||||
@ -13,7 +13,6 @@ use crate::{
|
||||
routes::AppState,
|
||||
scheduler::{utils::*, SchedulerFlow, SchedulerOptions},
|
||||
types::storage::{self, enums::ProcessTrackerStatus},
|
||||
utils,
|
||||
};
|
||||
|
||||
#[instrument(skip_all)]
|
||||
@ -87,7 +86,7 @@ pub async fn fetch_producer_tasks(
|
||||
) -> CustomResult<Vec<storage::ProcessTracker>, errors::ProcessTrackerError> {
|
||||
let upper = conf.producer.upper_fetch_limit;
|
||||
let lower = conf.producer.lower_fetch_limit;
|
||||
let now = utils::date_time::now();
|
||||
let now = common_utils::date_time::now();
|
||||
// Add these to validations
|
||||
let time_upper_limit = now.checked_add(Duration::seconds(upper)).ok_or_else(|| {
|
||||
report!(errors::ProcessTrackerError::ConfigurationError)
|
||||
|
||||
@ -16,7 +16,7 @@ use crate::{
|
||||
routes::AppState,
|
||||
scheduler::{ProcessTrackerBatch, SchedulerFlow},
|
||||
types::storage::{self, enums::ProcessTrackerStatus},
|
||||
utils::{self, date_time, OptionExt, StringExt},
|
||||
utils::{OptionExt, StringExt},
|
||||
};
|
||||
|
||||
pub async fn acquire_pt_lock(
|
||||
@ -120,7 +120,7 @@ pub async fn update_status_and_append(
|
||||
SchedulerFlow::Cleaner => {
|
||||
let res = state
|
||||
.store
|
||||
.reinitialize_limbo_processes(process_ids, utils::date_time::now())
|
||||
.reinitialize_limbo_processes(process_ids, common_utils::date_time::now())
|
||||
.await;
|
||||
match res {
|
||||
Ok(count) => {
|
||||
@ -157,7 +157,7 @@ pub fn divide(
|
||||
tasks: Vec<storage::ProcessTracker>,
|
||||
conf: &SchedulerSettings,
|
||||
) -> Vec<ProcessTrackerBatch> {
|
||||
let now = utils::date_time::now();
|
||||
let now = common_utils::date_time::now();
|
||||
let batch_size = conf.producer.batch_size;
|
||||
divide_into_batches(batch_size, tasks, now, conf)
|
||||
}
|
||||
@ -257,7 +257,7 @@ pub fn get_process_tracker_id<'a>(
|
||||
}
|
||||
|
||||
pub fn get_time_from_delta(delta: Option<i32>) -> Option<time::PrimitiveDateTime> {
|
||||
delta.map(|t| date_time::now().saturating_add(time::Duration::seconds(t.into())))
|
||||
delta.map(|t| common_utils::date_time::now().saturating_add(time::Duration::seconds(t.into())))
|
||||
}
|
||||
|
||||
pub async fn consumer_operation_handler<E>(
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use common_utils::custom_serde;
|
||||
use error_stack::report;
|
||||
use literally::hmap;
|
||||
use once_cell::sync::Lazy;
|
||||
@ -10,7 +11,6 @@ use crate::{
|
||||
core::errors::{self, RouterResult},
|
||||
pii::{self, Secret},
|
||||
types::storage::enums,
|
||||
utils::custom_serde,
|
||||
};
|
||||
|
||||
/// Static collection that contains valid Payment Method Type and Payment Method SubType
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
use common_utils::custom_serde;
|
||||
use error_stack::{IntoReport, ResultExt};
|
||||
use masking::{PeekInterface, Secret};
|
||||
use router_derive::Setter;
|
||||
@ -9,7 +10,6 @@ use crate::{
|
||||
pii,
|
||||
services::api,
|
||||
types::{self, api as api_types, enums, storage},
|
||||
utils::custom_serde,
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
@ -24,7 +24,7 @@ pub enum PaymentOp {
|
||||
pub struct PaymentsRequest {
|
||||
#[serde(
|
||||
default,
|
||||
deserialize_with = "custom_serde::payment_id_type::deserialize_option"
|
||||
deserialize_with = "crate::utils::custom_serde::payment_id_type::deserialize_option"
|
||||
)]
|
||||
pub payment_id: Option<PaymentIdType>,
|
||||
pub merchant_id: Option<String>,
|
||||
@ -109,7 +109,7 @@ impl CustomerAcceptance {
|
||||
}
|
||||
pub fn get_accepted_at(&self) -> PrimitiveDateTime {
|
||||
self.accepted_at
|
||||
.unwrap_or_else(crate::utils::date_time::now)
|
||||
.unwrap_or_else(common_utils::date_time::now)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
use common_utils::custom_serde;
|
||||
use error_stack::ResultExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use time::PrimitiveDateTime;
|
||||
@ -8,7 +9,7 @@ use crate::{
|
||||
core::errors::{self, CustomResult},
|
||||
services,
|
||||
types::{api, storage::enums},
|
||||
utils::{crypto, custom_serde},
|
||||
utils::crypto,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
|
||||
@ -1,14 +1,10 @@
|
||||
use common_utils::custom_serde;
|
||||
use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
|
||||
use masking::Secret;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use time::{OffsetDateTime, PrimitiveDateTime};
|
||||
|
||||
use crate::{
|
||||
consts,
|
||||
schema::address,
|
||||
types::api,
|
||||
utils::{custom_serde, generate_id},
|
||||
};
|
||||
use crate::{consts, schema::address, types::api, utils::generate_id};
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, Insertable, router_derive::DebugAsDisplay)]
|
||||
#[diesel(table_name = address)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
|
||||
@ -77,7 +77,7 @@ impl From<ConnectorResponseUpdate> for ConnectorResponseUpdateInternal {
|
||||
connector_transaction_id,
|
||||
authentication_data,
|
||||
encoded_data,
|
||||
modified_at: crate::utils::date_time::now(),
|
||||
modified_at: common_utils::date_time::now(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
use common_utils::custom_serde;
|
||||
use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
|
||||
use error_stack::ResultExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -8,7 +9,7 @@ use crate::{
|
||||
core::errors::{self, RouterResult},
|
||||
pii::{self, PeekInterface, Secret},
|
||||
schema::customers,
|
||||
utils::{self, custom_serde, ValidateCall},
|
||||
utils::{self, ValidateCall},
|
||||
};
|
||||
|
||||
#[derive(
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
use common_utils::custom_serde;
|
||||
use diesel::{Identifiable, Insertable, Queryable};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use time::PrimitiveDateTime;
|
||||
|
||||
use crate::{schema::events, types::storage::enums, utils::custom_serde};
|
||||
use crate::{schema::events, types::storage::enums};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, Insertable, router_derive::DebugAsDisplay)]
|
||||
#[diesel(table_name = events)]
|
||||
|
||||
@ -2,7 +2,7 @@ use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use time::PrimitiveDateTime;
|
||||
|
||||
use crate::{schema::payment_attempt, types::enums, utils::date_time};
|
||||
use crate::{schema::payment_attempt, types::enums};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Identifiable, Queryable, Serialize, Deserialize)]
|
||||
#[diesel(table_name = payment_attempt)]
|
||||
@ -142,7 +142,7 @@ impl PaymentAttemptUpdate {
|
||||
payment_method_id: pa_update
|
||||
.payment_method_id
|
||||
.unwrap_or(source.payment_method_id),
|
||||
modified_at: date_time::now(),
|
||||
modified_at: common_utils::date_time::now(),
|
||||
..source
|
||||
}
|
||||
}
|
||||
@ -165,14 +165,14 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
|
||||
// connector_transaction_id,
|
||||
authentication_type,
|
||||
payment_method,
|
||||
modified_at: Some(crate::utils::date_time::now()),
|
||||
modified_at: Some(common_utils::date_time::now()),
|
||||
..Default::default()
|
||||
},
|
||||
PaymentAttemptUpdate::AuthenticationTypeUpdate {
|
||||
authentication_type,
|
||||
} => Self {
|
||||
authentication_type: Some(authentication_type),
|
||||
modified_at: Some(crate::utils::date_time::now()),
|
||||
modified_at: Some(common_utils::date_time::now()),
|
||||
..Default::default()
|
||||
},
|
||||
PaymentAttemptUpdate::ConfirmUpdate {
|
||||
@ -181,7 +181,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
|
||||
} => Self {
|
||||
status: Some(status),
|
||||
payment_method,
|
||||
modified_at: Some(crate::utils::date_time::now()),
|
||||
modified_at: Some(common_utils::date_time::now()),
|
||||
..Default::default()
|
||||
},
|
||||
PaymentAttemptUpdate::VoidUpdate {
|
||||
@ -204,7 +204,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
|
||||
connector_transaction_id,
|
||||
authentication_type,
|
||||
payment_method_id,
|
||||
modified_at: Some(crate::utils::date_time::now()),
|
||||
modified_at: Some(common_utils::date_time::now()),
|
||||
redirect,
|
||||
mandate_id,
|
||||
..Default::default()
|
||||
@ -215,7 +215,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
|
||||
} => Self {
|
||||
status: Some(status),
|
||||
error_message,
|
||||
modified_at: Some(crate::utils::date_time::now()),
|
||||
modified_at: Some(common_utils::date_time::now()),
|
||||
..Default::default()
|
||||
},
|
||||
PaymentAttemptUpdate::StatusUpdate { status } => Self {
|
||||
@ -247,7 +247,7 @@ mod tests {
|
||||
|
||||
// let conn = config.conn.get();
|
||||
|
||||
let current_time = crate::utils::date_time::now();
|
||||
let current_time = common_utils::date_time::now();
|
||||
let payment_attempt = PaymentAttemptNew {
|
||||
payment_id: "1".to_string(),
|
||||
connector: types::Connector::Dummy.to_string(),
|
||||
@ -276,7 +276,7 @@ mod tests {
|
||||
store: Store::new(&conf).await,
|
||||
conf,
|
||||
};
|
||||
let current_time = crate::utils::date_time::now();
|
||||
let current_time = common_utils::date_time::now();
|
||||
|
||||
let payment_attempt = PaymentAttemptNew {
|
||||
payment_id: "1".to_string(),
|
||||
@ -313,7 +313,7 @@ mod tests {
|
||||
store: Store::new(&conf).await,
|
||||
conf,
|
||||
};
|
||||
let current_time = crate::utils::date_time::now();
|
||||
let current_time = common_utils::date_time::now();
|
||||
|
||||
let payment_attempt = PaymentAttemptNew {
|
||||
payment_id: uuid.clone(),
|
||||
|
||||
@ -2,7 +2,7 @@ use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use time::PrimitiveDateTime;
|
||||
|
||||
use crate::{schema::payment_intent, types::enums, utils::date_time};
|
||||
use crate::{schema::payment_intent, types::enums};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Identifiable, Queryable, Serialize, Deserialize)]
|
||||
#[diesel(table_name = payment_intent)]
|
||||
@ -134,7 +134,7 @@ impl PaymentIntentUpdate {
|
||||
shipping_address_id: internal_update
|
||||
.shipping_address_id
|
||||
.or(source.shipping_address_id),
|
||||
modified_at: date_time::now(),
|
||||
modified_at: common_utils::date_time::now(),
|
||||
..source
|
||||
}
|
||||
}
|
||||
@ -158,12 +158,12 @@ impl From<PaymentIntentUpdate> for PaymentIntentUpdateInternal {
|
||||
client_secret: make_client_secret_null_if_success(Some(status)),
|
||||
shipping_address_id,
|
||||
billing_address_id,
|
||||
modified_at: Some(crate::utils::date_time::now()),
|
||||
modified_at: Some(common_utils::date_time::now()),
|
||||
..Default::default()
|
||||
},
|
||||
PaymentIntentUpdate::MetadataUpdate { metadata } => Self {
|
||||
metadata: Some(metadata),
|
||||
modified_at: Some(crate::utils::date_time::now()),
|
||||
modified_at: Some(common_utils::date_time::now()),
|
||||
..Default::default()
|
||||
},
|
||||
PaymentIntentUpdate::ReturnUrlUpdate {
|
||||
@ -179,12 +179,12 @@ impl From<PaymentIntentUpdate> for PaymentIntentUpdateInternal {
|
||||
customer_id,
|
||||
shipping_address_id,
|
||||
billing_address_id,
|
||||
modified_at: Some(crate::utils::date_time::now()),
|
||||
modified_at: Some(common_utils::date_time::now()),
|
||||
..Default::default()
|
||||
},
|
||||
PaymentIntentUpdate::PGStatusUpdate { status } => Self {
|
||||
status: Some(status),
|
||||
modified_at: Some(crate::utils::date_time::now()),
|
||||
modified_at: Some(common_utils::date_time::now()),
|
||||
..Default::default()
|
||||
},
|
||||
PaymentIntentUpdate::MerchantStatusUpdate {
|
||||
@ -196,7 +196,7 @@ impl From<PaymentIntentUpdate> for PaymentIntentUpdateInternal {
|
||||
client_secret: make_client_secret_null_if_success(Some(status)),
|
||||
shipping_address_id,
|
||||
billing_address_id,
|
||||
modified_at: Some(crate::utils::date_time::now()),
|
||||
modified_at: Some(common_utils::date_time::now()),
|
||||
..Default::default()
|
||||
},
|
||||
PaymentIntentUpdate::ResponseUpdate {
|
||||
@ -214,7 +214,7 @@ impl From<PaymentIntentUpdate> for PaymentIntentUpdateInternal {
|
||||
// customer_id,
|
||||
return_url,
|
||||
client_secret: make_client_secret_null_if_success(Some(status)),
|
||||
modified_at: Some(crate::utils::date_time::now()),
|
||||
modified_at: Some(common_utils::date_time::now()),
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
use diesel::{Identifiable, Insertable, Queryable};
|
||||
use time::PrimitiveDateTime;
|
||||
|
||||
use crate::{pii::Secret, schema::payment_methods, types::storage::enums, utils};
|
||||
use crate::{pii::Secret, schema::payment_methods, types::storage::enums};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Identifiable, Queryable)]
|
||||
#[diesel(table_name = payment_methods)]
|
||||
@ -58,7 +58,7 @@ pub struct PaymentMethodNew {
|
||||
|
||||
impl Default for PaymentMethodNew {
|
||||
fn default() -> Self {
|
||||
let now = utils::date_time::now();
|
||||
let now = common_utils::date_time::now();
|
||||
|
||||
Self {
|
||||
customer_id: String::default(),
|
||||
|
||||
@ -5,9 +5,7 @@ use error_stack::ResultExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use time::PrimitiveDateTime;
|
||||
|
||||
use crate::{
|
||||
core::errors, db, scheduler::metrics, schema::process_tracker, types::storage::enums, utils,
|
||||
};
|
||||
use crate::{core::errors, db, scheduler::metrics, schema::process_tracker, types::storage::enums};
|
||||
|
||||
#[derive(
|
||||
Clone,
|
||||
@ -53,7 +51,7 @@ impl ProcessTracker {
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let current_time = crate::utils::date_time::now();
|
||||
let current_time = common_utils::date_time::now();
|
||||
Ok(ProcessTrackerNew {
|
||||
id: process_tracker_id,
|
||||
name: Some(String::from(task)),
|
||||
@ -170,7 +168,7 @@ impl Default for ProcessTrackerUpdateInternal {
|
||||
tracking_data: Option::default(),
|
||||
business_status: Option::default(),
|
||||
status: Option::default(),
|
||||
updated_at: Some(utils::date_time::now()),
|
||||
updated_at: Some(common_utils::date_time::now()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,15 +16,6 @@ pub(crate) use self::{
|
||||
};
|
||||
use crate::consts;
|
||||
|
||||
pub mod date_time {
|
||||
use time::{OffsetDateTime, PrimitiveDateTime};
|
||||
|
||||
pub fn now() -> PrimitiveDateTime {
|
||||
let utc_date_time = OffsetDateTime::now_utc();
|
||||
PrimitiveDateTime::new(utc_date_time.date(), utc_date_time.time())
|
||||
}
|
||||
}
|
||||
|
||||
pub mod error_parser {
|
||||
use std::fmt::Display;
|
||||
|
||||
|
||||
@ -1,94 +1,3 @@
|
||||
/// Use the well-known ISO 8601 format when serializing and deserializing an
|
||||
/// [`PrimitiveDateTime`][PrimitiveDateTime].
|
||||
///
|
||||
/// [PrimitiveDateTime]: ::time::PrimitiveDateTime
|
||||
pub(crate) mod iso8601 {
|
||||
use std::num::NonZeroU8;
|
||||
|
||||
use serde::{ser::Error as _, Deserializer, Serialize, Serializer};
|
||||
use time::{
|
||||
format_description::well_known::{
|
||||
iso8601::{Config, EncodedConfig, TimePrecision},
|
||||
Iso8601,
|
||||
},
|
||||
serde::iso8601,
|
||||
PrimitiveDateTime, UtcOffset,
|
||||
};
|
||||
const FORMAT_CONFIG: EncodedConfig = Config::DEFAULT
|
||||
.set_time_precision(TimePrecision::Second {
|
||||
decimal_digits: NonZeroU8::new(3),
|
||||
})
|
||||
.encode();
|
||||
|
||||
/// Serialize a [`PrimitiveDateTime`] using the well-known ISO 8601 format.
|
||||
pub(crate) fn serialize<S>(
|
||||
date_time: &PrimitiveDateTime,
|
||||
serializer: S,
|
||||
) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
// iso8601::serialize(&date_time.assume_utc(), serializer)
|
||||
date_time
|
||||
.assume_utc()
|
||||
.format(&Iso8601::<FORMAT_CONFIG>)
|
||||
.map_err(S::Error::custom)?
|
||||
.serialize(serializer)
|
||||
}
|
||||
|
||||
/// Deserialize an [`PrimitiveDateTime`] from its ISO 8601 representation.
|
||||
pub(crate) fn deserialize<'a, D>(deserializer: D) -> Result<PrimitiveDateTime, D::Error>
|
||||
where
|
||||
D: Deserializer<'a>,
|
||||
{
|
||||
iso8601::deserialize(deserializer).map(|offset_date_time| {
|
||||
let utc_date_time = offset_date_time.to_offset(UtcOffset::UTC);
|
||||
PrimitiveDateTime::new(utc_date_time.date(), utc_date_time.time())
|
||||
})
|
||||
}
|
||||
|
||||
/// Use the well-known ISO 8601 format when serializing and deserializing an
|
||||
/// [`Option<PrimitiveDateTime>`][PrimitiveDateTime].
|
||||
///
|
||||
/// [PrimitiveDateTime]: ::time::PrimitiveDateTime
|
||||
pub(crate) mod option {
|
||||
use serde::Serialize;
|
||||
use time::format_description::well_known::Iso8601;
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Serialize an [`Option<PrimitiveDateTime>`] using the well-known ISO 8601 format.
|
||||
pub(crate) fn serialize<S>(
|
||||
date_time: &Option<PrimitiveDateTime>,
|
||||
serializer: S,
|
||||
) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
date_time
|
||||
.map(|date_time| date_time.assume_utc().format(&Iso8601::<FORMAT_CONFIG>))
|
||||
.transpose()
|
||||
.map_err(S::Error::custom)?
|
||||
.serialize(serializer)
|
||||
}
|
||||
|
||||
/// Deserialize an [`Option<PrimitiveDateTime>`] from its ISO 8601 representation.
|
||||
pub(crate) fn deserialize<'a, D>(
|
||||
deserializer: D,
|
||||
) -> Result<Option<PrimitiveDateTime>, D::Error>
|
||||
where
|
||||
D: Deserializer<'a>,
|
||||
{
|
||||
iso8601::option::deserialize(deserializer).map(|option_offset_date_time| {
|
||||
option_offset_date_time.map(|offset_date_time| {
|
||||
let utc_date_time = offset_date_time.to_offset(UtcOffset::UTC);
|
||||
PrimitiveDateTime::new(utc_date_time.date(), utc_date_time.time())
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) mod payment_id_type {
|
||||
use std::fmt;
|
||||
|
||||
|
||||
@ -22,7 +22,7 @@ impl TestApp {
|
||||
}
|
||||
|
||||
fn mk_merchant_account() -> serde_json::Value {
|
||||
let timestamp = router::utils::date_time::now();
|
||||
let timestamp = common_utils::date_time::now();
|
||||
|
||||
serde_json::json!({
|
||||
"merchant_id": format!("merchant_{timestamp}"),
|
||||
|
||||
@ -25,7 +25,7 @@ impl TestApp {
|
||||
#[actix_web::test]
|
||||
async fn test_webhook_config_lookup() {
|
||||
let app = TestApp::init().await;
|
||||
let timestamp = router::utils::date_time::now();
|
||||
let timestamp = common_utils::date_time::now();
|
||||
|
||||
let merchant_id = format!("merchant_{timestamp}");
|
||||
let connector_id = "stripe";
|
||||
|
||||
Reference in New Issue
Block a user