Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(connector): add config cleanup on payment connector deletion #5998

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/common_enums/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3304,6 +3304,16 @@ pub enum PresenceOfCustomerDuringPayment {
Absent,
}

impl From<ConnectorType> for TransactionType {
fn from(connector_type: ConnectorType) -> Self {
match connector_type {
#[cfg(feature = "payouts")]
ConnectorType::PayoutProcessor => Self::Payout,
_ => Self::Payment,
}
}
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize, Default, ToSchema)]
pub enum TaxCalculationOverride {
/// Skip calling the external tax provider
Expand Down
147 changes: 146 additions & 1 deletion crates/router/src/core/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1923,6 +1923,65 @@ impl<'a> MerchantDefaultConfigUpdate<'a> {
}
Ok(())
}

async fn retrieve_and_delete_from_default_fallback_routing_algorithm_if_routable_connector_exists(
&self,
) -> RouterResult<()> {
let mut default_routing_config = routing::helpers::get_merchant_default_config(
noagbmn marked this conversation as resolved.
Show resolved Hide resolved
self.store,
self.merchant_id.get_string_repr(),
self.transaction_type,
)
.await?;

let mut default_routing_config_for_profile = routing::helpers::get_merchant_default_config(
self.store,
self.profile_id.get_string_repr(),
self.transaction_type,
)
.await?;

if let Some(routable_connector_val) = self.routable_connector {
let choice = routing_types::RoutableConnectorChoice {
choice_kind: routing_types::RoutableChoiceKind::FullStruct,
connector: *routable_connector_val,
merchant_connector_id: Some(self.merchant_connector_id.clone()),
};
if default_routing_config.contains(&choice) {
default_routing_config.retain(|mca| {
mca.merchant_connector_id
.as_ref()
.map_or(true, |merchant_connector_id| {
merchant_connector_id != self.merchant_connector_id
})
});
routing::helpers::update_merchant_default_config(
self.store,
self.merchant_id.get_string_repr(),
default_routing_config.clone(),
self.transaction_type,
)
.await?;
}
if default_routing_config_for_profile.contains(&choice.clone()) {
default_routing_config_for_profile.retain(|mca| {
mca.merchant_connector_id
.as_ref()
.map_or(true, |merchant_connector_id| {
merchant_connector_id != self.merchant_connector_id
})
});
routing::helpers::update_merchant_default_config(
self.store,
self.profile_id.get_string_repr(),
default_routing_config_for_profile.clone(),
self.transaction_type,
)
.await?;
}
}
Ok(())
}
}
#[cfg(feature = "v2")]
struct DefaultFallbackRoutingConfigUpdate<'a> {
Expand Down Expand Up @@ -1962,6 +2021,40 @@ impl<'a> DefaultFallbackRoutingConfigUpdate<'a> {
}
Ok(())
}

async fn retrieve_and_delete_from_default_fallback_routing_algorithm_if_routable_connector_exists(
&self,
) -> RouterResult<()> {
let profile_wrapper = ProfileWrapper::new(self.business_profile.clone());
let default_routing_config_for_profile =
&mut profile_wrapper.get_default_fallback_list_of_connector_under_profile()?;
if let Some(routable_connector_val) = self.routable_connector {
let choice = routing_types::RoutableConnectorChoice {
choice_kind: routing_types::RoutableChoiceKind::FullStruct,
connector: *routable_connector_val,
merchant_connector_id: Some(self.merchant_connector_id.clone()),
};
if default_routing_config_for_profile.contains(&choice.clone()) {
default_routing_config_for_profile.retain(|mca| {
mca.merchant_connector_id
.as_ref()
.map_or(true, |merchant_connector_id| {
merchant_connector_id != self.merchant_connector_id
})
});

profile_wrapper
.update_default_fallback_routing_of_connectors_under_profile(
self.store,
default_routing_config_for_profile,
self.key_manager_state,
&self.key_store,
)
.await?
}
}
Ok(())
}
}
#[cfg(any(feature = "v1", feature = "v2", feature = "olap"))]
#[async_trait::async_trait]
Expand Down Expand Up @@ -3131,7 +3224,7 @@ pub async fn delete_connector(
.await
.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;

let _mca = db
let mca = db
.find_by_merchant_connector_account_merchant_id_merchant_connector_id(
key_manager_state,
&merchant_id,
Expand All @@ -3153,6 +3246,26 @@ pub async fn delete_connector(
id: merchant_connector_id.get_string_repr().to_string(),
})?;

// delete the mca from the config as well
let merchant_default_config_delete = MerchantDefaultConfigUpdate {
routable_connector: &Some(
common_enums::RoutableConnectors::from_str(&mca.connector_name).map_err(|_| {
errors::ApiErrorResponse::InvalidDataValue {
field_name: "connector_name",
}
})?,
),
merchant_connector_id: &mca.get_id(),
store: db,
merchant_id: &merchant_id,
profile_id: &mca.profile_id,
transaction_type: &mca.connector_type.into(),
};

merchant_default_config_delete
.retrieve_and_delete_from_default_fallback_routing_algorithm_if_routable_connector_exists()
.await?;

let response = api::MerchantConnectorDeleteResponse {
merchant_id,
merchant_connector_id,
Expand Down Expand Up @@ -3199,6 +3312,38 @@ pub async fn delete_connector(
id: id.clone().get_string_repr().to_string(),
})?;

let business_profile = crate::core::utils::validate_and_get_business_profile(
db,
key_manager_state,
&key_store,
Some(&mca.profile_id),
merchant_id,
)
.await?
.get_required_value("Profile")
.change_context(errors::ApiErrorResponse::ProfileNotFound {
id: mca.profile_id.get_string_repr().to_owned(),
})?;

let merchant_default_config_delete = DefaultFallbackRoutingConfigUpdate {
routable_connector: &Some(
common_enums::RoutableConnectors::from_str(&mca.connector_name).map_err(|_| {
errors::ApiErrorResponse::InvalidDataValue {
field_name: "connector_name",
}
})?,
),
merchant_connector_id: &mca.get_id(),
store: db,
business_profile,
key_store,
key_manager_state,
};

merchant_default_config_delete
.retrieve_and_delete_from_default_fallback_routing_algorithm_if_routable_connector_exists()
.await?;

let response = api::MerchantConnectorDeleteResponse {
merchant_id: merchant_id.clone(),
id,
Expand Down
Loading