chore(webhooks): ignore payment not found in webhooks (#1886)

This commit is contained in:
Nishant Joshi
2023-08-10 12:56:47 +05:30
committed by GitHub
parent 32b731d959
commit 29f068b205
4 changed files with 44 additions and 6 deletions

View File

@ -581,6 +581,14 @@ pub struct DrainerSettings {
#[serde(default)] #[serde(default)]
pub struct WebhooksSettings { pub struct WebhooksSettings {
pub outgoing_enabled: bool, pub outgoing_enabled: bool,
pub ignore_error: WebhookIgnoreErrorSettings,
}
#[derive(Debug, Clone, Deserialize, Default)]
#[serde(default)]
pub struct WebhookIgnoreErrorSettings {
pub event_type: Option<bool>,
pub payment_not_found: Option<bool>,
} }
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]

View File

@ -63,6 +63,7 @@ pub trait ConnectorErrorExt<T> {
#[track_caller] #[track_caller]
fn allow_webhook_event_type_not_found( fn allow_webhook_event_type_not_found(
self, self,
enabled: bool,
) -> error_stack::Result<Option<T>, errors::ConnectorError>; ) -> error_stack::Result<Option<T>, errors::ConnectorError>;
} }
@ -240,11 +241,14 @@ impl<T> ConnectorErrorExt<T> for error_stack::Result<T, errors::ConnectorError>
}) })
} }
fn allow_webhook_event_type_not_found(self) -> CustomResult<Option<T>, errors::ConnectorError> { fn allow_webhook_event_type_not_found(
self,
enabled: bool,
) -> CustomResult<Option<T>, errors::ConnectorError> {
match self { match self {
Ok(event_type) => Ok(Some(event_type)), Ok(event_type) => Ok(Some(event_type)),
Err(error) => match error.current_context() { Err(error) => match error.current_context() {
errors::ConnectorError::WebhookEventTypeNotFound => Ok(None), errors::ConnectorError::WebhookEventTypeNotFound if enabled => Ok(None),
_ => Err(error), _ => Err(error),
}, },
} }

View File

@ -39,6 +39,7 @@ counter_metric!(WEBHOOK_SOURCE_VERIFIED_COUNT, GLOBAL_METER);
counter_metric!(WEBHOOK_OUTGOING_COUNT, GLOBAL_METER); counter_metric!(WEBHOOK_OUTGOING_COUNT, GLOBAL_METER);
counter_metric!(WEBHOOK_OUTGOING_RECEIVED_COUNT, GLOBAL_METER); counter_metric!(WEBHOOK_OUTGOING_RECEIVED_COUNT, GLOBAL_METER);
counter_metric!(WEBHOOK_OUTGOING_NOT_RECEIVED_COUNT, GLOBAL_METER); counter_metric!(WEBHOOK_OUTGOING_NOT_RECEIVED_COUNT, GLOBAL_METER);
counter_metric!(WEBHOOK_PAYMENT_NOT_FOUND, GLOBAL_METER);
counter_metric!( counter_metric!(
WEBHOOK_EVENT_TYPE_IDENTIFICATION_FAILURE_COUNT, WEBHOOK_EVENT_TYPE_IDENTIFICATION_FAILURE_COUNT,
GLOBAL_METER GLOBAL_METER

View File

@ -14,7 +14,7 @@ use crate::{
payments, refunds, payments, refunds,
}, },
logger, logger,
routes::AppState, routes::{metrics::request::add_attributes, AppState},
services, services,
types::{ types::{
self as router_types, api, domain, self as router_types, api, domain,
@ -42,7 +42,7 @@ pub async fn payments_incoming_webhook_flow<W: types::OutgoingWebhookType>(
}; };
let payments_response = match webhook_details.object_reference_id { let payments_response = match webhook_details.object_reference_id {
api_models::webhooks::ObjectReferenceId::PaymentId(id) => { api_models::webhooks::ObjectReferenceId::PaymentId(id) => {
payments::payments_core::<api::PSync, api::PaymentsResponse, _, _, _>( let response = payments::payments_core::<api::PSync, api::PaymentsResponse, _, _, _>(
&state, &state,
merchant_account.clone(), merchant_account.clone(),
key_store, key_store,
@ -60,7 +60,30 @@ pub async fn payments_incoming_webhook_flow<W: types::OutgoingWebhookType>(
services::AuthFlow::Merchant, services::AuthFlow::Merchant,
consume_or_trigger_flow, consume_or_trigger_flow,
) )
.await? .await;
match response {
Ok(value) => value,
Err(err)
if matches!(
err.current_context(),
&errors::ApiErrorResponse::PaymentNotFound
) && state
.conf
.webhooks
.ignore_error
.payment_not_found
.unwrap_or(true) =>
{
metrics::WEBHOOK_PAYMENT_NOT_FOUND.add(
&metrics::CONTEXT,
1,
&[add_attributes("merchant_id", merchant_account.merchant_id)],
);
return Ok(());
}
error @ Err(_) => error?,
}
} }
_ => Err(errors::ApiErrorResponse::WebhookProcessingFailure) _ => Err(errors::ApiErrorResponse::WebhookProcessingFailure)
.into_report() .into_report()
@ -685,7 +708,9 @@ pub async fn webhooks_core<W: types::OutgoingWebhookType>(
let event_type = match connector let event_type = match connector
.get_webhook_event_type(&request_details) .get_webhook_event_type(&request_details)
.allow_webhook_event_type_not_found() .allow_webhook_event_type_not_found(
state.conf.webhooks.ignore_error.event_type.unwrap_or(true),
)
.switch() .switch()
.attach_printable("Could not find event type in incoming webhook body")? .attach_printable("Could not find event type in incoming webhook body")?
{ {