feat(router): allow enabling/disabling of outgoing webhooks based on env configuration (#476)

This commit is contained in:
ItsMeShashank
2023-01-30 18:34:20 +05:30
committed by GitHub
parent 39adbbc183
commit fa7d087c0d
5 changed files with 40 additions and 20 deletions

View File

@ -49,6 +49,9 @@ cards = ["stripe","adyen","authorizedotnet","checkout","braintree","aci","shift4
max_attempts = 10 max_attempts = 10
max_age = 365 max_age = 365
[webhooks]
outgoing_enabled = true
[eph_key] [eph_key]
validity = 1 validity = 1

View File

@ -97,6 +97,9 @@ locker_decryption_key2 = "" # private key 2 in pem format, corresponding public
max_attempts = 10 # Number of refund attempts allowed max_attempts = 10 # Number of refund attempts allowed
max_age = 365 # Max age of a refund in days. max_age = 365 # Max age of a refund in days.
[webhooks]
outgoing_enabled = true
# Validity of an Ephemeral Key in Hours # Validity of an Ephemeral Key in Hours
[eph_key] [eph_key]
validity = 1 validity = 1

View File

@ -50,6 +50,7 @@ pub struct Settings {
#[cfg(feature = "kv_store")] #[cfg(feature = "kv_store")]
pub drainer: DrainerSettings, pub drainer: DrainerSettings,
pub jwekey: Jwekey, pub jwekey: Jwekey,
pub webhooks: WebhooksSettings,
} }
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]
@ -195,6 +196,12 @@ pub struct DrainerSettings {
pub max_read_count: u64, pub max_read_count: u64,
} }
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
pub struct WebhooksSettings {
pub outgoing_enabled: bool,
}
impl Settings { impl Settings {
pub fn new() -> ApplicationResult<Self> { pub fn new() -> ApplicationResult<Self> {
Self::with_config_path(None) Self::with_config_path(None)

View File

@ -72,6 +72,7 @@ async fn payments_incoming_webhook_flow(
.change_context(errors::WebhooksFlowError::PaymentsCoreFailed)?; .change_context(errors::WebhooksFlowError::PaymentsCoreFailed)?;
create_event_and_trigger_outgoing_webhook( create_event_and_trigger_outgoing_webhook(
state,
merchant_account, merchant_account,
event_type, event_type,
enums::EventClass::Payments, enums::EventClass::Payments,
@ -79,7 +80,6 @@ async fn payments_incoming_webhook_flow(
payment_id, payment_id,
enums::EventObjectType::PaymentDetails, enums::EventObjectType::PaymentDetails,
api::OutgoingWebhookContent::PaymentDetails(payments_response), api::OutgoingWebhookContent::PaymentDetails(payments_response),
state.store,
) )
.await?; .await?;
} }
@ -93,6 +93,7 @@ async fn payments_incoming_webhook_flow(
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
#[instrument(skip_all)] #[instrument(skip_all)]
async fn create_event_and_trigger_outgoing_webhook( async fn create_event_and_trigger_outgoing_webhook(
state: AppState,
merchant_account: storage::MerchantAccount, merchant_account: storage::MerchantAccount,
event_type: enums::EventType, event_type: enums::EventType,
event_class: enums::EventClass, event_class: enums::EventClass,
@ -100,12 +101,7 @@ async fn create_event_and_trigger_outgoing_webhook(
primary_object_id: String, primary_object_id: String,
primary_object_type: enums::EventObjectType, primary_object_type: enums::EventObjectType,
content: api::OutgoingWebhookContent, content: api::OutgoingWebhookContent,
db: Box<dyn StorageInterface>,
) -> CustomResult<(), errors::WebhooksFlowError> { ) -> CustomResult<(), errors::WebhooksFlowError> {
let arbiter = actix::Arbiter::try_current()
.ok_or(errors::WebhooksFlowError::ForkFlowFailed)
.into_report()?;
let new_event = storage::EventNew { let new_event = storage::EventNew {
event_id: generate_id(consts::ID_LENGTH, "evt"), event_id: generate_id(consts::ID_LENGTH, "evt"),
event_type, event_type,
@ -116,11 +112,17 @@ async fn create_event_and_trigger_outgoing_webhook(
primary_object_type, primary_object_type,
}; };
let event = db let event = state
.store
.insert_event(new_event) .insert_event(new_event)
.await .await
.change_context(errors::WebhooksFlowError::WebhookEventCreationFailed)?; .change_context(errors::WebhooksFlowError::WebhookEventCreationFailed)?;
if state.conf.webhooks.outgoing_enabled {
let arbiter = actix::Arbiter::try_current()
.ok_or(errors::WebhooksFlowError::ForkFlowFailed)
.into_report()?;
let outgoing_webhook = api::OutgoingWebhook { let outgoing_webhook = api::OutgoingWebhook {
merchant_id: merchant_account.merchant_id.clone(), merchant_id: merchant_account.merchant_id.clone(),
event_id: event.event_id, event_id: event.event_id,
@ -130,12 +132,14 @@ async fn create_event_and_trigger_outgoing_webhook(
}; };
arbiter.spawn(async move { arbiter.spawn(async move {
let result = trigger_webhook_to_merchant(merchant_account, outgoing_webhook, db).await; let result =
trigger_webhook_to_merchant(merchant_account, outgoing_webhook, state.store).await;
if let Err(e) = result { if let Err(e) = result {
logger::error!(?e); logger::error!(?e);
} }
}); });
}
Ok(()) Ok(())
} }

View File

@ -45,6 +45,9 @@ locker_encryption_key2 = ""
locker_decryption_key1 = "" locker_decryption_key1 = ""
locker_decryption_key2 = "" locker_decryption_key2 = ""
[webhooks]
outgoing_enabled = true
[connectors.aci] [connectors.aci]
base_url = "https://eu-test.oppwa.com/" base_url = "https://eu-test.oppwa.com/"