mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-01 11:06:50 +08:00
feat(router): implement PaymentMethodInterface for MockDB (#1535)
This commit is contained in:
committed by
GitHub
parent
0bc1e043fe
commit
772fc84577
@ -102,6 +102,7 @@ pub struct MockDb {
|
|||||||
merchant_connector_accounts: Arc<Mutex<Vec<storage::MerchantConnectorAccount>>>,
|
merchant_connector_accounts: Arc<Mutex<Vec<storage::MerchantConnectorAccount>>>,
|
||||||
payment_attempts: Arc<Mutex<Vec<storage::PaymentAttempt>>>,
|
payment_attempts: Arc<Mutex<Vec<storage::PaymentAttempt>>>,
|
||||||
payment_intents: Arc<Mutex<Vec<storage::PaymentIntent>>>,
|
payment_intents: Arc<Mutex<Vec<storage::PaymentIntent>>>,
|
||||||
|
payment_methods: Arc<Mutex<Vec<storage::PaymentMethod>>>,
|
||||||
customers: Arc<Mutex<Vec<storage::Customer>>>,
|
customers: Arc<Mutex<Vec<storage::Customer>>>,
|
||||||
refunds: Arc<Mutex<Vec<storage::Refund>>>,
|
refunds: Arc<Mutex<Vec<storage::Refund>>>,
|
||||||
processes: Arc<Mutex<Vec<storage::ProcessTracker>>>,
|
processes: Arc<Mutex<Vec<storage::ProcessTracker>>>,
|
||||||
@ -124,6 +125,7 @@ impl MockDb {
|
|||||||
merchant_connector_accounts: Default::default(),
|
merchant_connector_accounts: Default::default(),
|
||||||
payment_attempts: Default::default(),
|
payment_attempts: Default::default(),
|
||||||
payment_intents: Default::default(),
|
payment_intents: Default::default(),
|
||||||
|
payment_methods: Default::default(),
|
||||||
customers: Default::default(),
|
customers: Default::default(),
|
||||||
refunds: Default::default(),
|
refunds: Default::default(),
|
||||||
processes: Default::default(),
|
processes: Default::default(),
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
use error_stack::IntoReport;
|
use error_stack::IntoReport;
|
||||||
|
use storage_models::payment_method::PaymentMethodUpdateInternal;
|
||||||
|
|
||||||
use super::{MockDb, Store};
|
use super::{MockDb, Store};
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -22,7 +23,7 @@ pub trait PaymentMethodInterface {
|
|||||||
|
|
||||||
async fn insert_payment_method(
|
async fn insert_payment_method(
|
||||||
&self,
|
&self,
|
||||||
m: storage::PaymentMethodNew,
|
payment_method_new: storage::PaymentMethodNew,
|
||||||
) -> CustomResult<storage::PaymentMethod, errors::StorageError>;
|
) -> CustomResult<storage::PaymentMethod, errors::StorageError>;
|
||||||
|
|
||||||
async fn update_payment_method(
|
async fn update_payment_method(
|
||||||
@ -53,10 +54,14 @@ impl PaymentMethodInterface for Store {
|
|||||||
|
|
||||||
async fn insert_payment_method(
|
async fn insert_payment_method(
|
||||||
&self,
|
&self,
|
||||||
m: storage::PaymentMethodNew,
|
payment_method_new: storage::PaymentMethodNew,
|
||||||
) -> CustomResult<storage::PaymentMethod, errors::StorageError> {
|
) -> CustomResult<storage::PaymentMethod, errors::StorageError> {
|
||||||
let conn = connection::pg_connection_write(self).await?;
|
let conn = connection::pg_connection_write(self).await?;
|
||||||
m.insert(&conn).await.map_err(Into::into).into_report()
|
payment_method_new
|
||||||
|
.insert(&conn)
|
||||||
|
.await
|
||||||
|
.map_err(Into::into)
|
||||||
|
.into_report()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_payment_method(
|
async fn update_payment_method(
|
||||||
@ -105,44 +110,122 @@ impl PaymentMethodInterface for Store {
|
|||||||
impl PaymentMethodInterface for MockDb {
|
impl PaymentMethodInterface for MockDb {
|
||||||
async fn find_payment_method(
|
async fn find_payment_method(
|
||||||
&self,
|
&self,
|
||||||
_payment_method_id: &str,
|
payment_method_id: &str,
|
||||||
) -> CustomResult<storage::PaymentMethod, errors::StorageError> {
|
) -> CustomResult<storage::PaymentMethod, errors::StorageError> {
|
||||||
// [#172]: Implement function for `MockDb`
|
let payment_methods = self.payment_methods.lock().await;
|
||||||
Err(errors::StorageError::MockDbError)?
|
let payment_method = payment_methods
|
||||||
|
.iter()
|
||||||
|
.find(|pm| pm.payment_method_id == payment_method_id)
|
||||||
|
.cloned();
|
||||||
|
|
||||||
|
match payment_method {
|
||||||
|
Some(pm) => Ok(pm),
|
||||||
|
None => Err(errors::StorageError::ValueNotFound(
|
||||||
|
"cannot find payment method".to_string(),
|
||||||
|
)
|
||||||
|
.into()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn insert_payment_method(
|
async fn insert_payment_method(
|
||||||
&self,
|
&self,
|
||||||
_m: storage::PaymentMethodNew,
|
payment_method_new: storage::PaymentMethodNew,
|
||||||
) -> CustomResult<storage::PaymentMethod, errors::StorageError> {
|
) -> CustomResult<storage::PaymentMethod, errors::StorageError> {
|
||||||
// [#172]: Implement function for `MockDb`
|
let mut payment_methods = self.payment_methods.lock().await;
|
||||||
Err(errors::StorageError::MockDbError)?
|
|
||||||
|
let payment_method = storage::PaymentMethod {
|
||||||
|
#[allow(clippy::as_conversions)]
|
||||||
|
id: payment_methods.len() as i32,
|
||||||
|
customer_id: payment_method_new.customer_id,
|
||||||
|
merchant_id: payment_method_new.merchant_id,
|
||||||
|
payment_method_id: payment_method_new.payment_method_id,
|
||||||
|
accepted_currency: payment_method_new.accepted_currency,
|
||||||
|
scheme: payment_method_new.scheme,
|
||||||
|
token: payment_method_new.token,
|
||||||
|
cardholder_name: payment_method_new.cardholder_name,
|
||||||
|
issuer_name: payment_method_new.issuer_name,
|
||||||
|
issuer_country: payment_method_new.issuer_country,
|
||||||
|
payer_country: payment_method_new.payer_country,
|
||||||
|
is_stored: payment_method_new.is_stored,
|
||||||
|
swift_code: payment_method_new.swift_code,
|
||||||
|
direct_debit_token: payment_method_new.direct_debit_token,
|
||||||
|
created_at: payment_method_new.created_at,
|
||||||
|
last_modified: payment_method_new.last_modified,
|
||||||
|
payment_method: payment_method_new.payment_method,
|
||||||
|
payment_method_type: payment_method_new.payment_method_type,
|
||||||
|
payment_method_issuer: payment_method_new.payment_method_issuer,
|
||||||
|
payment_method_issuer_code: payment_method_new.payment_method_issuer_code,
|
||||||
|
metadata: payment_method_new.metadata,
|
||||||
|
};
|
||||||
|
payment_methods.push(payment_method.clone());
|
||||||
|
Ok(payment_method)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find_payment_method_by_customer_id_merchant_id_list(
|
async fn find_payment_method_by_customer_id_merchant_id_list(
|
||||||
&self,
|
&self,
|
||||||
_customer_id: &str,
|
customer_id: &str,
|
||||||
_merchant_id: &str,
|
merchant_id: &str,
|
||||||
) -> CustomResult<Vec<storage::PaymentMethod>, errors::StorageError> {
|
) -> CustomResult<Vec<storage::PaymentMethod>, errors::StorageError> {
|
||||||
// [#172]: Implement function for `MockDb`
|
let payment_methods = self.payment_methods.lock().await;
|
||||||
Err(errors::StorageError::MockDbError)?
|
let payment_methods_found: Vec<storage::PaymentMethod> = payment_methods
|
||||||
|
.iter()
|
||||||
|
.filter(|pm| pm.customer_id == customer_id && pm.merchant_id == merchant_id)
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if payment_methods_found.is_empty() {
|
||||||
|
Err(
|
||||||
|
errors::StorageError::ValueNotFound("cannot find payment method".to_string())
|
||||||
|
.into(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Ok(payment_methods_found)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn delete_payment_method_by_merchant_id_payment_method_id(
|
async fn delete_payment_method_by_merchant_id_payment_method_id(
|
||||||
&self,
|
&self,
|
||||||
_merchant_id: &str,
|
merchant_id: &str,
|
||||||
_payment_method_id: &str,
|
payment_method_id: &str,
|
||||||
) -> CustomResult<storage::PaymentMethod, errors::StorageError> {
|
) -> CustomResult<storage::PaymentMethod, errors::StorageError> {
|
||||||
// [#172]: Implement function for `MockDb`
|
let mut payment_methods = self.payment_methods.lock().await;
|
||||||
Err(errors::StorageError::MockDbError)?
|
match payment_methods.iter().position(|pm| {
|
||||||
|
pm.merchant_id == merchant_id && pm.payment_method_id == payment_method_id
|
||||||
|
}) {
|
||||||
|
Some(index) => {
|
||||||
|
let deleted_payment_method = payment_methods.remove(index);
|
||||||
|
Ok(deleted_payment_method)
|
||||||
|
}
|
||||||
|
None => Err(errors::StorageError::ValueNotFound(
|
||||||
|
"cannot find payment method to delete".to_string(),
|
||||||
|
)
|
||||||
|
.into()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_payment_method(
|
async fn update_payment_method(
|
||||||
&self,
|
&self,
|
||||||
_payment_method: storage::PaymentMethod,
|
payment_method: storage::PaymentMethod,
|
||||||
_payment_method_update: storage::PaymentMethodUpdate,
|
payment_method_update: storage::PaymentMethodUpdate,
|
||||||
) -> CustomResult<storage::PaymentMethod, errors::StorageError> {
|
) -> CustomResult<storage::PaymentMethod, errors::StorageError> {
|
||||||
// [#172]: Implement function for `MockDb`
|
match self
|
||||||
Err(errors::StorageError::MockDbError)?
|
.payment_methods
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.iter_mut()
|
||||||
|
.find(|pm| pm.id == payment_method.id)
|
||||||
|
.map(|pm| {
|
||||||
|
let payment_method_updated =
|
||||||
|
PaymentMethodUpdateInternal::from(payment_method_update)
|
||||||
|
.create_payment_method(pm.clone());
|
||||||
|
*pm = payment_method_updated.clone();
|
||||||
|
payment_method_updated
|
||||||
|
}) {
|
||||||
|
Some(result) => Ok(result),
|
||||||
|
None => Err(errors::StorageError::ValueNotFound(
|
||||||
|
"cannot find payment method to update".to_string(),
|
||||||
|
)
|
||||||
|
.into()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -105,6 +105,14 @@ pub struct PaymentMethodUpdateInternal {
|
|||||||
metadata: Option<serde_json::Value>,
|
metadata: Option<serde_json::Value>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PaymentMethodUpdateInternal {
|
||||||
|
pub fn create_payment_method(self, source: PaymentMethod) -> PaymentMethod {
|
||||||
|
let metadata = self.metadata.map(Secret::new);
|
||||||
|
|
||||||
|
PaymentMethod { metadata, ..source }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<PaymentMethodUpdate> for PaymentMethodUpdateInternal {
|
impl From<PaymentMethodUpdate> for PaymentMethodUpdateInternal {
|
||||||
fn from(payment_method_update: PaymentMethodUpdate) -> Self {
|
fn from(payment_method_update: PaymentMethodUpdate) -> Self {
|
||||||
match payment_method_update {
|
match payment_method_update {
|
||||||
|
|||||||
Reference in New Issue
Block a user