feat(payments_v2): payment intent diesel and domain models changes v2 (#5783)

Co-authored-by: hrithikesh026 <hrithikesh.vm@juspay.in>
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
Narayan Bhat
2024-09-13 13:11:38 +05:30
committed by GitHub
parent 71bf627fbc
commit 10ac089449
59 changed files with 2893 additions and 762 deletions

View File

@ -329,6 +329,21 @@ impl UniqueConstraints for diesel_models::Address {
}
}
#[cfg(all(feature = "v2", feature = "payment_v2"))]
impl UniqueConstraints for diesel_models::PaymentIntent {
fn unique_constraints(&self) -> Vec<String> {
vec![format!(
"pi_{}_{}",
self.merchant_id.get_string_repr(),
self.merchant_reference_id
)]
}
fn table_name(&self) -> &str {
"PaymentIntent"
}
}
#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "payment_v2")))]
impl UniqueConstraints for diesel_models::PaymentIntent {
fn unique_constraints(&self) -> Vec<String> {
vec![format!(

View File

@ -16,7 +16,11 @@ use super::MockDb;
#[async_trait::async_trait]
impl PaymentIntentInterface for MockDb {
#[cfg(feature = "olap")]
#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_v2"),
feature = "olap"
))]
async fn filter_payment_intent_by_constraints(
&self,
_state: &KeyManagerState,
@ -28,7 +32,11 @@ impl PaymentIntentInterface for MockDb {
// [#172]: Implement function for `MockDb`
Err(StorageError::MockDbError)?
}
#[cfg(feature = "olap")]
#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_v2"),
feature = "olap"
))]
async fn filter_payment_intents_by_time_range_constraints(
&self,
_state: &KeyManagerState,
@ -40,7 +48,11 @@ impl PaymentIntentInterface for MockDb {
// [#172]: Implement function for `MockDb`
Err(StorageError::MockDbError)?
}
#[cfg(feature = "olap")]
#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_v2"),
feature = "olap"
))]
async fn get_intent_status_with_count(
&self,
_merchant_id: &common_utils::id_type::MerchantId,
@ -50,7 +62,11 @@ impl PaymentIntentInterface for MockDb {
// [#172]: Implement function for `MockDb`
Err(StorageError::MockDbError)?
}
#[cfg(feature = "olap")]
#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_v2"),
feature = "olap"
))]
async fn get_filtered_active_attempt_ids_for_total_count(
&self,
_merchant_id: &common_utils::id_type::MerchantId,
@ -60,7 +76,11 @@ impl PaymentIntentInterface for MockDb {
// [#172]: Implement function for `MockDb`
Err(StorageError::MockDbError)?
}
#[cfg(feature = "olap")]
#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_v2"),
feature = "olap"
))]
async fn get_filtered_payment_intents_attempt(
&self,
_state: &KeyManagerState,
@ -99,7 +119,7 @@ impl PaymentIntentInterface for MockDb {
let mut payment_intents = self.payment_intents.lock().await;
let payment_intent = payment_intents
.iter_mut()
.find(|item| item.payment_id == this.payment_id && item.merchant_id == this.merchant_id)
.find(|item| item.get_id() == this.get_id() && item.merchant_id == this.merchant_id)
.unwrap();
let diesel_payment_intent_update = diesel_models::PaymentIntentUpdate::from(update);
@ -121,6 +141,7 @@ impl PaymentIntentInterface for MockDb {
Ok(payment_intent.clone())
}
#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "payment_v2")))]
// safety: only used for testing
#[allow(clippy::unwrap_used)]
async fn find_payment_intent_by_payment_id_merchant_id(
@ -136,13 +157,31 @@ impl PaymentIntentInterface for MockDb {
Ok(payment_intents
.iter()
.find(|payment_intent| {
payment_intent.payment_id == *payment_id
&& payment_intent.merchant_id.eq(merchant_id)
payment_intent.get_id() == payment_id && payment_intent.merchant_id.eq(merchant_id)
})
.cloned()
.unwrap())
}
#[cfg(all(feature = "v2", feature = "payment_v2"))]
async fn find_payment_intent_by_id(
&self,
_state: &KeyManagerState,
id: &common_utils::id_type::PaymentId,
_merchant_key_store: &MerchantKeyStore,
_storage_scheme: storage_enums::MerchantStorageScheme,
) -> error_stack::Result<PaymentIntent, StorageError> {
let payment_intents = self.payment_intents.lock().await;
let payment_intent = payment_intents
.iter()
.find(|payment_intent| payment_intent.get_id() == id)
.ok_or(StorageError::ValueNotFound(
"PaymentIntent not found".to_string(),
))?;
Ok(payment_intent.clone())
}
async fn get_active_payment_attempt(
&self,
payment: &mut PaymentIntent,

View File

@ -73,8 +73,8 @@ impl<T: DatabaseStore> PaymentIntentInterface for KVRouterStore<T> {
storage_scheme: MerchantStorageScheme,
) -> error_stack::Result<PaymentIntent, StorageError> {
let merchant_id = payment_intent.merchant_id.clone();
let payment_id = payment_intent.payment_id.clone();
let field = payment_intent.payment_id.get_hash_key_for_kv_store();
let payment_id = payment_intent.get_id().to_owned();
let field = payment_intent.get_id().get_hash_key_for_kv_store();
let key = PartitionKey::MerchantIdPaymentId {
merchant_id: &merchant_id,
payment_id: &payment_id,
@ -152,12 +152,12 @@ impl<T: DatabaseStore> PaymentIntentInterface for KVRouterStore<T> {
storage_scheme: MerchantStorageScheme,
) -> error_stack::Result<PaymentIntent, StorageError> {
let merchant_id = this.merchant_id.clone();
let payment_id = this.payment_id.clone();
let payment_id = this.get_id().to_owned();
let key = PartitionKey::MerchantIdPaymentId {
merchant_id: &merchant_id,
payment_id: &payment_id,
};
let field = format!("pi_{}", this.payment_id.get_string_repr());
let field = format!("pi_{}", this.get_id().get_string_repr());
let storage_scheme = Box::pin(decide_storage_scheme::<_, DieselPaymentIntent>(
self,
storage_scheme,
@ -229,6 +229,7 @@ impl<T: DatabaseStore> PaymentIntentInterface for KVRouterStore<T> {
}
}
#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "payment_v2")))]
#[instrument(skip_all)]
async fn find_payment_intent_by_payment_id_merchant_id(
&self,
@ -288,6 +289,35 @@ impl<T: DatabaseStore> PaymentIntentInterface for KVRouterStore<T> {
.change_context(StorageError::DecryptionError)
}
#[cfg(all(feature = "v2", feature = "payment_v2"))]
#[instrument(skip_all)]
async fn find_payment_intent_by_id(
&self,
state: &KeyManagerState,
id: &common_utils::id_type::PaymentId,
merchant_key_store: &MerchantKeyStore,
_storage_scheme: MerchantStorageScheme,
) -> error_stack::Result<PaymentIntent, StorageError> {
let conn = pg_connection_read(self).await?;
let diesel_payment_intent = DieselPaymentIntent::find_by_global_id(&conn, id)
.await
.map_err(|er| {
let new_err = diesel_error_to_data_error(er.current_context());
er.change_context(new_err)
})?;
let merchant_id = diesel_payment_intent.merchant_id.clone();
PaymentIntent::convert_back(
state,
diesel_payment_intent,
merchant_key_store.key.get_inner(),
merchant_id.to_owned().into(),
)
.await
.change_context(StorageError::DecryptionError)
}
async fn get_active_payment_attempt(
&self,
payment: &mut PaymentIntent,
@ -315,7 +345,11 @@ impl<T: DatabaseStore> PaymentIntentInterface for KVRouterStore<T> {
}
}
#[cfg(feature = "olap")]
#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_v2"),
feature = "olap"
))]
async fn filter_payment_intent_by_constraints(
&self,
state: &KeyManagerState,
@ -335,7 +369,11 @@ impl<T: DatabaseStore> PaymentIntentInterface for KVRouterStore<T> {
.await
}
#[cfg(feature = "olap")]
#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_v2"),
feature = "olap"
))]
async fn filter_payment_intents_by_time_range_constraints(
&self,
state: &KeyManagerState,
@ -354,7 +392,12 @@ impl<T: DatabaseStore> PaymentIntentInterface for KVRouterStore<T> {
)
.await
}
#[cfg(feature = "olap")]
#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_v2"),
feature = "olap"
))]
async fn get_intent_status_with_count(
&self,
merchant_id: &common_utils::id_type::MerchantId,
@ -366,7 +409,11 @@ impl<T: DatabaseStore> PaymentIntentInterface for KVRouterStore<T> {
.await
}
#[cfg(feature = "olap")]
#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_v2"),
feature = "olap"
))]
async fn get_filtered_payment_intents_attempt(
&self,
state: &KeyManagerState,
@ -386,7 +433,11 @@ impl<T: DatabaseStore> PaymentIntentInterface for KVRouterStore<T> {
.await
}
#[cfg(feature = "olap")]
#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_v2"),
feature = "olap"
))]
async fn get_filtered_active_attempt_ids_for_total_count(
&self,
merchant_id: &common_utils::id_type::MerchantId,
@ -468,6 +519,7 @@ impl<T: DatabaseStore> PaymentIntentInterface for crate::RouterStore<T> {
.change_context(StorageError::DecryptionError)
}
#[cfg(all(any(feature = "v1", feature = "v2"), not(feature = "payment_v2")))]
#[instrument(skip_all)]
async fn find_payment_intent_by_payment_id_merchant_id(
&self,
@ -498,6 +550,35 @@ impl<T: DatabaseStore> PaymentIntentInterface for crate::RouterStore<T> {
.await
}
#[cfg(all(feature = "v2", feature = "payment_v2"))]
#[instrument(skip_all)]
async fn find_payment_intent_by_id(
&self,
state: &KeyManagerState,
id: &common_utils::id_type::PaymentId,
merchant_key_store: &MerchantKeyStore,
_storage_scheme: MerchantStorageScheme,
) -> error_stack::Result<PaymentIntent, StorageError> {
let conn = pg_connection_read(self).await?;
let diesel_payment_intent = DieselPaymentIntent::find_by_global_id(&conn, id)
.await
.map_err(|er| {
let new_err = diesel_error_to_data_error(er.current_context());
er.change_context(new_err)
})?;
let merchant_id = diesel_payment_intent.merchant_id.clone();
PaymentIntent::convert_back(
state,
diesel_payment_intent,
merchant_key_store.key.get_inner(),
merchant_id.to_owned().into(),
)
.await
.change_context(StorageError::DecryptionError)
}
#[instrument(skip_all)]
async fn get_active_payment_attempt(
&self,
@ -526,7 +607,11 @@ impl<T: DatabaseStore> PaymentIntentInterface for crate::RouterStore<T> {
}
}
#[cfg(feature = "olap")]
#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_v2"),
feature = "olap"
))]
#[instrument(skip_all)]
async fn filter_payment_intent_by_constraints(
&self,
@ -652,7 +737,11 @@ impl<T: DatabaseStore> PaymentIntentInterface for crate::RouterStore<T> {
.await
}
#[cfg(feature = "olap")]
#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_v2"),
feature = "olap"
))]
#[instrument(skip_all)]
async fn filter_payment_intents_by_time_range_constraints(
&self,
@ -674,7 +763,11 @@ impl<T: DatabaseStore> PaymentIntentInterface for crate::RouterStore<T> {
.await
}
#[cfg(feature = "olap")]
#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_v2"),
feature = "olap"
))]
#[instrument(skip_all)]
async fn get_intent_status_with_count(
&self,
@ -718,7 +811,11 @@ impl<T: DatabaseStore> PaymentIntentInterface for crate::RouterStore<T> {
})
}
#[cfg(feature = "olap")]
#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_v2"),
feature = "olap"
))]
#[instrument(skip_all)]
async fn get_filtered_payment_intents_attempt(
&self,
@ -911,7 +1008,11 @@ impl<T: DatabaseStore> PaymentIntentInterface for crate::RouterStore<T> {
.await
}
#[cfg(feature = "olap")]
#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_v2"),
feature = "olap"
))]
#[instrument(skip_all)]
async fn get_filtered_active_attempt_ids_for_total_count(
&self,