feat(router): make webhook events config disabled only and by default enable all the events (#2770)

This commit is contained in:
Sai Harsha Vardhan
2023-11-06 13:40:10 +05:30
committed by GitHub
parent d7b1673b85
commit d335879f92
2 changed files with 29 additions and 30 deletions

View File

@ -963,8 +963,13 @@ pub async fn webhooks_core<W: types::OutgoingWebhookType, Ctx: PaymentMethodRetr
return Ok((response, WebhookResponseTracker::NoEffect)); return Ok((response, WebhookResponseTracker::NoEffect));
} }
}; };
logger::info!(event_type=?event_type);
let process_webhook_further = utils::lookup_webhook_event( let is_webhook_event_supported = !matches!(
event_type,
api_models::webhooks::IncomingWebhookEvent::EventNotSupported
);
let is_webhook_event_enabled = !utils::is_webhook_event_disabled(
&*state.clone().store, &*state.clone().store,
connector_name.as_str(), connector_name.as_str(),
&merchant_account.merchant_id, &merchant_account.merchant_id,
@ -972,8 +977,10 @@ pub async fn webhooks_core<W: types::OutgoingWebhookType, Ctx: PaymentMethodRetr
) )
.await; .await;
//process webhook further only if webhook event is enabled and is not event_not_supported
let process_webhook_further = is_webhook_event_enabled && is_webhook_event_supported;
logger::info!(process_webhook=?process_webhook_further); logger::info!(process_webhook=?process_webhook_further);
logger::info!(event_type=?event_type);
let flow_type: api::WebhookFlow = event_type.to_owned().into(); let flow_type: api::WebhookFlow = event_type.to_owned().into();
let webhook_effect = if process_webhook_further let webhook_effect = if process_webhook_further

View File

@ -9,20 +9,10 @@ use crate::{
payments::helpers, payments::helpers,
}, },
db::{get_and_deserialize_key, StorageInterface}, db::{get_and_deserialize_key, StorageInterface},
services::logger,
types::{self, api, domain, PaymentAddress}, types::{self, api, domain, PaymentAddress},
}; };
fn default_webhook_config() -> api::MerchantWebhookConfig {
std::collections::HashSet::from([
api::IncomingWebhookEvent::PaymentIntentSuccess,
api::IncomingWebhookEvent::PaymentIntentFailure,
api::IncomingWebhookEvent::PaymentIntentProcessing,
api::IncomingWebhookEvent::PaymentIntentCancelled,
api::IncomingWebhookEvent::PaymentActionRequired,
api::IncomingWebhookEvent::RefundSuccess,
])
}
const IRRELEVANT_PAYMENT_ID_IN_SOURCE_VERIFICATION_FLOW: &str = const IRRELEVANT_PAYMENT_ID_IN_SOURCE_VERIFICATION_FLOW: &str =
"irrelevant_payment_id_in_source_verification_flow"; "irrelevant_payment_id_in_source_verification_flow";
const IRRELEVANT_ATTEMPT_ID_IN_SOURCE_VERIFICATION_FLOW: &str = const IRRELEVANT_ATTEMPT_ID_IN_SOURCE_VERIFICATION_FLOW: &str =
@ -30,38 +20,40 @@ const IRRELEVANT_ATTEMPT_ID_IN_SOURCE_VERIFICATION_FLOW: &str =
const IRRELEVANT_CONNECTOR_REQUEST_REFERENCE_ID_IN_SOURCE_VERIFICATION_FLOW: &str = const IRRELEVANT_CONNECTOR_REQUEST_REFERENCE_ID_IN_SOURCE_VERIFICATION_FLOW: &str =
"irrelevant_connector_request_reference_id_in_source_verification_flow"; "irrelevant_connector_request_reference_id_in_source_verification_flow";
/// Check whether the merchant has configured to process the webhook `event` for the `connector` /// Check whether the merchant has configured to disable the webhook `event` for the `connector`
/// First check for the key "whconf_{merchant_id}_{connector_id}" in redis, /// First check for the key "whconf_{merchant_id}_{connector_id}" in redis,
/// if not found, fetch from configs table in database, if not found use default /// if not found, fetch from configs table in database
pub async fn lookup_webhook_event( pub async fn is_webhook_event_disabled(
db: &dyn StorageInterface, db: &dyn StorageInterface,
connector_id: &str, connector_id: &str,
merchant_id: &str, merchant_id: &str,
event: &api::IncomingWebhookEvent, event: &api::IncomingWebhookEvent,
) -> bool { ) -> bool {
let redis_key = format!("whconf_{merchant_id}_{connector_id}"); let redis_key = format!("whconf_disabled_events_{merchant_id}_{connector_id}");
let merchant_webhook_config_result = let merchant_webhook_disable_config_result: CustomResult<
get_and_deserialize_key(db, &redis_key, "MerchantWebhookConfig") api::MerchantWebhookConfig,
.await redis_interface::errors::RedisError,
.map(|h| &h | &default_webhook_config()); > = get_and_deserialize_key(db, &redis_key, "MerchantWebhookConfig").await;
match merchant_webhook_config_result { match merchant_webhook_disable_config_result {
Ok(merchant_webhook_config) => merchant_webhook_config.contains(event), Ok(merchant_webhook_config) => merchant_webhook_config.contains(event),
Err(..) => { Err(..) => {
//if failed to fetch from redis. fetch from db and populate redis //if failed to fetch from redis. fetch from db and populate redis
db.find_config_by_key(&redis_key) db.find_config_by_key(&redis_key)
.await .await
.map(|config| { .map(|config| {
if let Ok(set) = match serde_json::from_str::<api::MerchantWebhookConfig>(&config.config) {
serde_json::from_str::<api::MerchantWebhookConfig>(&config.config) Ok(set) => set.contains(event),
{ Err(err) => {
&set | &default_webhook_config() logger::warn!(?err, "error while parsing merchant webhook config");
} else { false
default_webhook_config() }
} }
}) })
.unwrap_or_else(|_| default_webhook_config()) .unwrap_or_else(|err| {
.contains(event) logger::warn!(?err, "error while fetching merchant webhook config");
false
})
} }
} }
} }