Skip to content

Commit

Permalink
fix(router/webhooks): correct webhook error mapping and make source v…
Browse files Browse the repository at this point in the history
…erification optional for all connectors (#1333)
  • Loading branch information
vspecky authored Jun 2, 2023
1 parent b681f78 commit 7131509
Show file tree
Hide file tree
Showing 23 changed files with 191 additions and 114 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions crates/router/src/connector/adyen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use storage_models::enums as storage_enums;
use self::transformers as adyen;
use crate::{
configs::settings,
connector::utils as conn_utils,
consts,
core::errors::{self, CustomResult},
db::StorageInterface,
Expand Down Expand Up @@ -797,13 +798,17 @@ impl api::IncomingWebhook for Adyen {
db: &dyn StorageInterface,
merchant_id: &str,
) -> CustomResult<Vec<u8>, errors::ConnectorError> {
let key = format!("whsec_verification_{}_{}", self.id(), merchant_id);
let secret = db
.get_key(&key)
.await
.change_context(errors::ConnectorError::WebhookVerificationSecretNotFound)?;

Ok(secret)
let key = conn_utils::get_webhook_merchant_secret_key(self.id(), merchant_id);
let secret = match db.find_config_by_key(&key).await {
Ok(config) => Some(config),
Err(e) => {
crate::logger::warn!("Unable to fetch merchant webhook secret from DB: {:#?}", e);
None
}
};
Ok(secret
.map(|conf| conf.config.into_bytes())
.unwrap_or_default())
}

fn get_webhook_object_reference_id(
Expand Down
18 changes: 12 additions & 6 deletions crates/router/src/connector/airwallex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use transformers as airwallex;
use super::utils::{AccessTokenRequestInfo, RefundsRequestData};
use crate::{
configs::settings,
connector::utils as conn_utils,
core::{
errors::{self, CustomResult},
payments,
Expand Down Expand Up @@ -947,12 +948,17 @@ impl api::IncomingWebhook for Airwallex {
db: &dyn StorageInterface,
merchant_id: &str,
) -> CustomResult<Vec<u8>, errors::ConnectorError> {
let key = format!("whsec_verification_{}_{}", self.id(), merchant_id);
let secret = db
.find_config_by_key(&key)
.await
.change_context(errors::ConnectorError::WebhookVerificationSecretNotFound)?;
Ok(secret.config.into_bytes())
let key = conn_utils::get_webhook_merchant_secret_key(self.id(), merchant_id);
let secret = match db.find_config_by_key(&key).await {
Ok(config) => Some(config),
Err(e) => {
crate::logger::warn!("Unable to fetch merchant webhook secret from DB: {:#?}", e);
None
}
};
Ok(secret
.map(|conf| conf.config.into_bytes())
.unwrap_or_default())
}

fn get_webhook_object_reference_id(
Expand Down
3 changes: 2 additions & 1 deletion crates/router/src/connector/authorizedotnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use transformers as authorizedotnet;

use crate::{
configs::settings,
connector::utils as conn_utils,
consts,
core::errors::{self, CustomResult},
db::StorageInterface,
Expand Down Expand Up @@ -684,7 +685,7 @@ impl api::IncomingWebhook for Authorizedotnet {
db: &dyn StorageInterface,
merchant_id: &str,
) -> CustomResult<Vec<u8>, errors::ConnectorError> {
let key = format!("whsec_verification_{}_{}", self.id(), merchant_id);
let key = conn_utils::get_webhook_merchant_secret_key(self.id(), merchant_id);
let secret = match db.find_config_by_key(&key).await {
Ok(config) => Some(config),
Err(e) => {
Expand Down
19 changes: 12 additions & 7 deletions crates/router/src/connector/bluesnap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use super::utils::{
};
use crate::{
configs::settings,
connector::utils as conn_utils,
consts,
core::{
errors::{self, CustomResult},
Expand Down Expand Up @@ -975,13 +976,17 @@ impl api::IncomingWebhook for Bluesnap {
db: &dyn StorageInterface,
merchant_id: &str,
) -> CustomResult<Vec<u8>, errors::ConnectorError> {
let key = format!("whsec_verification_{}_{}", self.id(), merchant_id);
let secret = db
.find_config_by_key(&key)
.await
.change_context(errors::ConnectorError::WebhookVerificationSecretNotFound)?;

Ok(secret.config.into_bytes())
let key = conn_utils::get_webhook_merchant_secret_key(self.id(), merchant_id);
let secret = match db.find_config_by_key(&key).await {
Ok(config) => Some(config),
Err(e) => {
crate::logger::warn!("Unable to fetch merchant webhook secret from DB: {:#?}", e);
None
}
};
Ok(secret
.map(|conf| conf.config.into_bytes())
.unwrap_or_default())
}

async fn verify_webhook_source(
Expand Down
8 changes: 4 additions & 4 deletions crates/router/src/connector/braintree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod transformers;

use std::fmt::Debug;

use error_stack::ResultExt;
use error_stack::{IntoReport, ResultExt};

use self::transformers as braintree;
use crate::{
Expand Down Expand Up @@ -688,20 +688,20 @@ impl api::IncomingWebhook for Braintree {
&self,
_request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api_models::webhooks::ObjectReferenceId, errors::ConnectorError> {
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
Err(errors::ConnectorError::WebhooksNotImplemented).into_report()
}

fn get_webhook_event_type(
&self,
_request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api::IncomingWebhookEvent, errors::ConnectorError> {
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
Err(errors::ConnectorError::WebhooksNotImplemented).into_report()
}

fn get_webhook_resource_object(
&self,
_request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<serde_json::Value, errors::ConnectorError> {
Err(errors::ConnectorError::NotImplemented("braintree".to_string()).into())
Err(errors::ConnectorError::WebhooksNotImplemented).into_report()
}
}
2 changes: 1 addition & 1 deletion crates/router/src/connector/checkout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ impl api::IncomingWebhook for Checkout {
db: &dyn StorageInterface,
merchant_id: &str,
) -> CustomResult<Vec<u8>, errors::ConnectorError> {
let key = format!("whsec_verification_{}_{}", self.id(), merchant_id);
let key = conn_utils::get_webhook_merchant_secret_key(self.id(), merchant_id);
let secret = match db.find_config_by_key(&key).await {
Ok(config) => Some(config),
Err(e) => {
Expand Down
18 changes: 11 additions & 7 deletions crates/router/src/connector/coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,17 @@ impl api::IncomingWebhook for Coinbase {
db: &dyn db::StorageInterface,
merchant_id: &str,
) -> CustomResult<Vec<u8>, errors::ConnectorError> {
let key = format!("whsec_verification_{}_{}", self.id(), merchant_id);
let secret = db
.get_key(&key)
.await
.change_context(errors::ConnectorError::WebhookVerificationSecretNotFound)?;

Ok(secret)
let key = utils::get_webhook_merchant_secret_key(self.id(), merchant_id);
let secret = match db.find_config_by_key(&key).await {
Ok(config) => Some(config),
Err(e) => {
crate::logger::warn!("Unable to fetch merchant webhook secret from DB: {:#?}", e);
None
}
};
Ok(secret
.map(|conf| conf.config.into_bytes())
.unwrap_or_default())
}

fn get_webhook_object_reference_id(
Expand Down
6 changes: 3 additions & 3 deletions crates/router/src/connector/cybersource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,20 +717,20 @@ impl api::IncomingWebhook for Cybersource {
&self,
_request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api_models::webhooks::ObjectReferenceId, errors::ConnectorError> {
Err(errors::ConnectorError::NotImplemented("cybersource".to_string()).into())
Err(errors::ConnectorError::WebhooksNotImplemented).into_report()
}

fn get_webhook_event_type(
&self,
_request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api::IncomingWebhookEvent, errors::ConnectorError> {
Err(errors::ConnectorError::NotImplemented("cybersource".to_string()).into())
Err(errors::ConnectorError::WebhooksNotImplemented).into_report()
}

fn get_webhook_resource_object(
&self,
_request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<serde_json::Value, errors::ConnectorError> {
Err(errors::ConnectorError::NotImplemented("cybersource".to_string()).into())
Err(errors::ConnectorError::WebhooksNotImplemented).into_report()
}
}
8 changes: 4 additions & 4 deletions crates/router/src/connector/fiserv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod transformers;
use std::fmt::Debug;

use base64::Engine;
use error_stack::ResultExt;
use error_stack::{IntoReport, ResultExt};
use ring::hmac;
use time::OffsetDateTime;
use transformers as fiserv;
Expand Down Expand Up @@ -684,20 +684,20 @@ impl api::IncomingWebhook for Fiserv {
&self,
_request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api_models::webhooks::ObjectReferenceId, errors::ConnectorError> {
Err(errors::ConnectorError::NotImplemented("fiserv".to_string()).into())
Err(errors::ConnectorError::WebhooksNotImplemented).into_report()
}

fn get_webhook_event_type(
&self,
_request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api::IncomingWebhookEvent, errors::ConnectorError> {
Err(errors::ConnectorError::NotImplemented("fiserv".to_string()).into())
Err(errors::ConnectorError::WebhooksNotImplemented).into_report()
}

fn get_webhook_resource_object(
&self,
_request: &api::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<serde_json::Value, errors::ConnectorError> {
Err(errors::ConnectorError::NotImplemented("fiserv".to_string()).into())
Err(errors::ConnectorError::WebhooksNotImplemented).into_report()
}
}
17 changes: 11 additions & 6 deletions crates/router/src/connector/globalpay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,12 +808,17 @@ impl api::IncomingWebhook for Globalpay {
db: &dyn db::StorageInterface,
merchant_id: &str,
) -> CustomResult<Vec<u8>, errors::ConnectorError> {
let key = format!("whsec_verification_{}_{}", self.id(), merchant_id);
let secret = db
.find_config_by_key(&key)
.await
.change_context(errors::ConnectorError::WebhookVerificationSecretNotFound)?;
Ok(secret.config.into_bytes())
let key = conn_utils::get_webhook_merchant_secret_key(self.id(), merchant_id);
let secret = match db.find_config_by_key(&key).await {
Ok(config) => Some(config),
Err(e) => {
crate::logger::warn!("Unable to fetch merchant webhook secret from DB: {:#?}", e);
None
}
};
Ok(secret
.map(|conf| conf.config.into_bytes())
.unwrap_or_default())
}

fn get_webhook_source_verification_signature(
Expand Down
18 changes: 11 additions & 7 deletions crates/router/src/connector/iatapay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,13 +633,17 @@ impl api::IncomingWebhook for Iatapay {
db: &dyn db::StorageInterface,
merchant_id: &str,
) -> CustomResult<Vec<u8>, errors::ConnectorError> {
let key = format!("whsec_verification_{}_{}", self.id(), merchant_id);
let secret = db
.get_key(&key)
.await
.change_context(errors::ConnectorError::WebhookVerificationSecretNotFound)?;

Ok(secret)
let key = utils::get_webhook_merchant_secret_key(self.id(), merchant_id);
let secret = match db.find_config_by_key(&key).await {
Ok(config) => Some(config),
Err(e) => {
crate::logger::warn!("Unable to fetch merchant webhook secret from DB: {:#?}", e);
None
}
};
Ok(secret
.map(|conf| conf.config.into_bytes())
.unwrap_or_default())
}

fn get_webhook_object_reference_id(
Expand Down
17 changes: 11 additions & 6 deletions crates/router/src/connector/nuvei.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,12 +852,17 @@ impl api::IncomingWebhook for Nuvei {
db: &dyn StorageInterface,
merchant_id: &str,
) -> CustomResult<Vec<u8>, errors::ConnectorError> {
let key = format!("wh_mer_sec_verification_{}_{}", self.id(), merchant_id);
let secret = db
.get_key(&key)
.await
.change_context(errors::ConnectorError::WebhookVerificationSecretNotFound)?;
Ok(secret)
let key = utils::get_webhook_merchant_secret_key(self.id(), merchant_id);
let secret = match db.find_config_by_key(&key).await {
Ok(config) => Some(config),
Err(e) => {
crate::logger::warn!("Unable to fetch merchant webhook secret from DB: {:#?}", e);
None
}
};
Ok(secret
.map(|conf| conf.config.into_bytes())
.unwrap_or_default())
}

fn get_webhook_source_verification_message(
Expand Down
19 changes: 12 additions & 7 deletions crates/router/src/connector/opennode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use transformers as opennode;
use self::opennode::OpennodeWebhookDetails;
use crate::{
configs::settings,
connector::utils as conn_utils,
core::errors::{self, CustomResult},
db, headers,
services::{self, ConnectorIntegration},
Expand Down Expand Up @@ -527,13 +528,17 @@ impl api::IncomingWebhook for Opennode {
db: &dyn db::StorageInterface,
merchant_id: &str,
) -> CustomResult<Vec<u8>, errors::ConnectorError> {
let key = format!("whsec_verification_{}_{}", self.id(), merchant_id);
let secret = db
.get_key(&key)
.await
.change_context(errors::ConnectorError::WebhookVerificationSecretNotFound)?;

Ok(secret)
let key = conn_utils::get_webhook_merchant_secret_key(self.id(), merchant_id);
let secret = match db.find_config_by_key(&key).await {
Ok(config) => Some(config),
Err(e) => {
crate::logger::warn!("Unable to fetch merchant webhook secret from DB: {:#?}", e);
None
}
};
Ok(secret
.map(|conf| conf.config.into_bytes())
.unwrap_or_default())
}

fn get_webhook_object_reference_id(
Expand Down
Loading

0 comments on commit 7131509

Please sign in to comment.