Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

feat(db): implement `EphemeralKeyInterface` for `MockDb` (#1285)

Karthik8c93904c3e34
diff --git a/crates/router/src/db.rs b/crates/router/src/db.rs
index 3cd510b33e..e6036922b1 100644
--- a/crates/router/src/db.rs
+++ b/crates/router/src/db.rs
@@ -108,6 +108,7 @@ pub struct MockDb {
connector_response: Arc<Mutex<Vec<storage::ConnectorResponse>>>,
redis: Arc<redis_interface::RedisConnectionPool>,
api_keys: Arc<Mutex<Vec<storage::ApiKey>>>,
+ ephemeral_keys: Arc<Mutex<Vec<storage::EphemeralKey>>>,
cards_info: Arc<Mutex<Vec<storage::CardInfo>>>,
events: Arc<Mutex<Vec<storage::Event>>>,
disputes: Arc<Mutex<Vec<storage::Dispute>>>,
@@ -129,6 +130,7 @@ impl MockDb {
connector_response: Default::default(),
redis: Arc::new(crate::connection::redis_connection(redis).await),
api_keys: Default::default(),
+ ephemeral_keys: Default::default(),
cards_info: Default::default(),
events: Default::default(),
disputes: Default::default(),
diff --git a/crates/router/src/db/ephemeral_key.rs b/crates/router/src/db/ephemeral_key.rs
index dc3e181340..a95ba436e1 100644
--- a/crates/router/src/db/ephemeral_key.rs
+++ b/crates/router/src/db/ephemeral_key.rs
@@ -1,3 +1,5 @@
+use time::ext::NumericalDuration;
+
use crate::{
core::errors::{self, CustomResult},
db::MockDb,
@@ -125,21 +127,53 @@ mod storage {
impl EphemeralKeyInterface for MockDb {
async fn create_ephemeral_key(
&self,
- _ek: EphemeralKeyNew,
- _validity: i64,
+ ek: EphemeralKeyNew,
+ validity: i64,
) -> CustomResult<EphemeralKey, errors::StorageError> {
- Err(errors::StorageError::KVError.into())
+ let mut ephemeral_keys = self.ephemeral_keys.lock().await;
+ let created_at = common_utils::date_time::now();
+ let expires = created_at.saturating_add(validity.hours());
+
+ let ephemeral_key = EphemeralKey {
+ id: ek.id,
+ merchant_id: ek.merchant_id,
+ customer_id: ek.customer_id,
+ created_at: created_at.assume_utc().unix_timestamp(),
+ expires: expires.assume_utc().unix_timestamp(),
+ secret: ek.secret,
+ };
+ ephemeral_keys.push(ephemeral_key.clone());
+ Ok(ephemeral_key)
}
async fn get_ephemeral_key(
&self,
- _key: &str,
+ key: &str,
) -> CustomResult<EphemeralKey, errors::StorageError> {
- Err(errors::StorageError::KVError.into())
+ match self
+ .ephemeral_keys
+ .lock()
+ .await
+ .iter()
+ .find(|ephemeral_key| ephemeral_key.secret.eq(key))
+ {
+ Some(ephemeral_key) => Ok(ephemeral_key.clone()),
+ None => Err(
+ errors::StorageError::ValueNotFound("ephemeral key not found".to_string()).into(),
+ ),
+ }
}
async fn delete_ephemeral_key(
&self,
- _id: &str,
+ id: &str,
) -> CustomResult<EphemeralKey, errors::StorageError> {
- Err(errors::StorageError::KVError.into())
+ let mut ephemeral_keys = self.ephemeral_keys.lock().await;
+ if let Some(pos) = ephemeral_keys.iter().position(|x| (*x.id).eq(id)) {
+ let ek = ephemeral_keys.remove(pos);
+ Ok(ek)
+ } else {
+ return Err(
+ errors::StorageError::ValueNotFound("ephemeral key not found".to_string()).into(),
+ );
+ }
}
}
diff --git a/crates/router/src/types/storage.rs b/crates/router/src/types/storage.rs
index e56d93b08a..781bd495cb 100644
--- a/crates/router/src/types/storage.rs
+++ b/crates/router/src/types/storage.rs
@@ -27,7 +27,7 @@ pub mod kv;
pub use self::{
address::*, api_keys::*, cards_info::*, configs::*, connector_response::*, customers::*,
- dispute::*, events::*, file::*, locker_mock_up::*, mandate::*, merchant_account::*,
- merchant_connector_account::*, payment_attempt::*, payment_intent::*, payment_method::*,
- process_tracker::*, refund::*, reverse_lookup::*,
+ dispute::*, ephemeral_key::*, events::*, file::*, locker_mock_up::*, mandate::*,
+ merchant_account::*, merchant_connector_account::*, payment_attempt::*, payment_intent::*,
+ payment_method::*, process_tracker::*, refund::*, reverse_lookup::*,
};