Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

feat(reports): Programmatic Report Delivery via Webhook (#11518)

Rohan Mali843d9ce8669f
diff --git a/crates/analytics/src/errors.rs b/crates/analytics/src/errors.rs
index 73b09ba291..05ea711d3c 100644
--- a/crates/analytics/src/errors.rs
+++ b/crates/analytics/src/errors.rs
@@ -16,6 +16,8 @@ pub enum AnalyticsError {
ForexFetchFailed,
#[error("Missing email")]
MissingEmail,
+ #[error("Invalid URL scheme: {0}")]
+ InvalidReturnUrl(String),
}
impl ErrorSwitch<ApiErrorResponse> for AnalyticsError {
@@ -48,6 +50,12 @@ impl ErrorSwitch<ApiErrorResponse> for AnalyticsError {
"Missing or invalid merchant email address.",
None,
)),
+ Self::InvalidReturnUrl(invalid_url_err) => ApiErrorResponse::BadRequest(ApiError::new(
+ "IR",
+ 6,
+ format!("Invalid return URL: {invalid_url_err}"),
+ None,
+ )),
}
}
}
diff --git a/crates/api_models/src/analytics.rs b/crates/api_models/src/analytics.rs
index e270c9bbbb..30973a9632 100644
--- a/crates/api_models/src/analytics.rs
+++ b/crates/api_models/src/analytics.rs
@@ -150,6 +150,7 @@ pub struct RefundDistributionBody {
pub struct ReportRequest {
pub time_range: TimeRange,
pub emails: Option<Vec<Email>>,
+ pub return_url: Option<MerchantWebhookUrl>,
#[cfg(feature = "v2")]
#[serde(default)]
pub report_type: ReportType,
@@ -558,3 +559,47 @@ pub enum ReportType {
V2Payments,
RevenueRecovery,
}
+
+#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq, Eq, Hash, PartialOrd)]
+/// This domain type is specifically for merchant webhook URLs with validation
+pub struct MerchantWebhookUrl(url::Url);
+
+impl MerchantWebhookUrl {
+ /// Get string representation of the URL
+ pub fn get_string_repr(&self) -> &str {
+ self.0.as_str()
+ }
+
+ /// Wrap a url::Url in MerchantWebhookUrl type
+ pub fn wrap(url: url::Url) -> Self {
+ Self(url)
+ }
+
+ /// Get the inner url::Url
+ pub fn into_inner(self) -> url::Url {
+ self.0
+ }
+
+ /// Verify the URL scheme based on runtime environment
+ pub fn verify_https_scheme(&self) -> Result<(), String> {
+ let scheme = self.0.scheme().to_lowercase();
+
+ #[cfg(debug_assertions)]
+ {
+ // Debug builds: allow HTTP
+ if scheme == "https" || scheme == "http" {
+ return Ok(());
+ }
+ }
+
+ #[cfg(not(debug_assertions))]
+ {
+ // Release builds: HTTPS only
+ if scheme == "https" {
+ return Ok(());
+ }
+ }
+
+ Err("URL scheme must be HTTPS".to_string())
+ }
+}
diff --git a/crates/router/src/analytics.rs b/crates/router/src/analytics.rs
index 72b8a18945..f54f33e16e 100644
--- a/crates/router/src/analytics.rs
+++ b/crates/router/src/analytics.rs
@@ -34,7 +34,7 @@ pub mod routes {
use router_env::logger;
use crate::{
- analytics_validator::request_validator,
+ analytics_validator::{request_validator, validate_report_request},
consts::opensearch::SEARCH_INDEXES,
core::{api_locking, errors::user::UserErrors, verification::utils},
db::user_role::ListUserRolesByUserIdPayload,
@@ -1792,6 +1792,7 @@ pub mod routes {
(user_email, payload.emails)
}
None => {
+ validate_report_request(&payload)?;
let (primary_email, other_emails) = payload
.emails
.and_then(|mut emails| {
@@ -1880,6 +1881,7 @@ pub mod routes {
(user_email, payload.emails)
}
None => {
+ validate_report_request(&payload)?;
let (primary_email, other_emails) = payload
.emails
.and_then(|mut emails| {
@@ -1968,6 +1970,7 @@ pub mod routes {
(user_email, payload.emails)
}
None => {
+ validate_report_request(&payload)?;
let (primary_email, other_emails) = payload
.emails
.and_then(|mut emails| {
@@ -2061,6 +2064,7 @@ pub mod routes {
(user_email, payload.emails)
}
None => {
+ validate_report_request(&payload)?;
let (primary_email, other_emails) = payload
.emails
.and_then(|mut emails| {
@@ -2148,6 +2152,7 @@ pub mod routes {
(user_email, payload.emails)
}
None => {
+ validate_report_request(&payload)?;
let (primary_email, other_emails) = payload
.emails
.and_then(|mut emails| {
@@ -2236,6 +2241,7 @@ pub mod routes {
(user_email, payload.emails)
}
None => {
+ validate_report_request(&payload)?;
let (primary_email, other_emails) = payload
.emails
.and_then(|mut emails| {
@@ -2330,6 +2336,7 @@ pub mod routes {
(user_email, payload.emails)
}
None => {
+ validate_report_request(&payload)?;
let (primary_email, other_emails) = payload
.emails
.and_then(|mut emails| {
@@ -2418,6 +2425,7 @@ pub mod routes {
(user_email, payload.emails)
}
None => {
+ validate_report_request(&payload)?;
let (primary_email, other_emails) = payload
.emails
.and_then(|mut emails| {
@@ -2506,6 +2514,7 @@ pub mod routes {
(user_email, payload.emails)
}
None => {
+ validate_report_request(&payload)?;
let (primary_email, other_emails) = payload
.emails
.and_then(|mut emails| {
@@ -2598,6 +2607,7 @@ pub mod routes {
(user_email, payload.emails)
}
None => {
+ validate_report_request(&payload)?;
let (primary_email, other_emails) = payload
.emails
.and_then(|mut emails| {
@@ -2685,6 +2695,7 @@ pub mod routes {
(user_email, payload.emails)
}
None => {
+ validate_report_request(&payload)?;
let (primary_email, other_emails) = payload
.emails
.and_then(|mut emails| {
@@ -2772,6 +2783,7 @@ pub mod routes {
(user_email, payload.emails)
}
None => {
+ validate_report_request(&payload)?;
let (primary_email, other_emails) = payload
.emails
.and_then(|mut emails| {
@@ -2873,6 +2885,7 @@ pub mod routes {
(user_email, payload.emails)
}
None => {
+ validate_report_request(&payload)?;
let (primary_email, other_emails) = payload
.emails
.and_then(|mut emails| {
@@ -2961,6 +2974,7 @@ pub mod routes {
(user_email, payload.emails)
}
None => {
+ validate_report_request(&payload)?;
let (primary_email, other_emails) = payload
.emails
.and_then(|mut emails| {
@@ -3049,6 +3063,7 @@ pub mod routes {
(user_email, payload.emails)
}
None => {
+ validate_report_request(&payload)?;
let (primary_email, other_emails) = payload
.emails
.and_then(|mut emails| {
diff --git a/crates/router/src/analytics_validator.rs b/crates/router/src/analytics_validator.rs
index d50308cc84..ea88f40d7e 100644
--- a/crates/router/src/analytics_validator.rs
+++ b/crates/router/src/analytics_validator.rs
@@ -1,5 +1,5 @@
use analytics::errors::AnalyticsError;
-use api_models::analytics::AnalyticsRequest;
+use api_models::analytics::{AnalyticsRequest, ReportRequest};
use common_utils::errors::CustomResult;
use currency_conversion::types::ExchangeRates;
use router_env::logger;
@@ -22,3 +22,12 @@ pub async fn request_validator(
Ok(ex_rates)
}
+
+pub fn validate_report_request(request: &ReportRequest) -> Result<(), AnalyticsError> {
+ match request.return_url {
+ Some(ref return_url) => return_url
+ .verify_https_scheme()
+ .map_err(AnalyticsError::InvalidReturnUrl),
+ None => Ok(()),
+ }
+}