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(relay): add relay report generation endpoints and configuration (#12897)

Aniket Burman18862c621fea
diff --git a/config/deployments/env_specific.toml b/config/deployments/env_specific.toml
index a524cf3d74..c77ffea7dc 100644
--- a/config/deployments/env_specific.toml
+++ b/config/deployments/env_specific.toml
@@ -257,6 +257,7 @@ dispute_function = "report_download_config_dispute_function" # Config to downloa
payment_function = "report_download_config_payment_function" # Config to download payment report
refund_function = "report_download_config_refund_function" # Config to download refund report
payout_function = "report_download_config_payout_function" # Config to download payout report
+relay_function = "report_download_config_relay_function" # Config to download relay report
region = "report_download_config_region" # Region of the bucket
[opensearch]
diff --git a/crates/analytics/src/lib.rs b/crates/analytics/src/lib.rs
index 315a4fe311..5524519ef9 100644
--- a/crates/analytics/src/lib.rs
+++ b/crates/analytics/src/lib.rs
@@ -1139,6 +1139,7 @@ pub struct ReportConfig {
pub dispute_function: String,
pub authentication_function: String,
pub payout_function: String,
+ pub relay_function: String,
pub region: String,
}
@@ -1170,6 +1171,7 @@ pub enum AnalyticsFlow {
GenerateRefundReport,
GenerateAuthenticationReport,
GeneratePayoutReport,
+ GenerateRelayReport,
GetApiEventMetrics,
GetApiEventFilters,
GetConnectorEvents,
diff --git a/crates/router/src/analytics.rs b/crates/router/src/analytics.rs
index c91be0a9bc..a52a6b8383 100644
--- a/crates/router/src/analytics.rs
+++ b/crates/router/src/analytics.rs
@@ -267,6 +267,10 @@ pub mod routes {
web::resource("report/refunds")
.route(web::post().to(generate_merchant_refund_report)),
)
+ .service(
+ web::resource("report/relay")
+ .route(web::post().to(generate_merchant_relay_report)),
+ )
.service(
web::resource("report/payments")
.route(web::post().to(generate_merchant_payment_report)),
@@ -444,6 +448,10 @@ pub mod routes {
web::resource("report/refunds")
.route(web::post().to(generate_profile_refund_report)),
)
+ .service(
+ web::resource("report/relay")
+ .route(web::post().to(generate_profile_relay_report)),
+ )
.service(
web::resource("report/payments")
.route(web::post().to(generate_profile_payment_report)),
@@ -2150,6 +2158,247 @@ pub mod routes {
))
.await
}
+ #[cfg(feature = "v1")]
+ pub async fn generate_merchant_relay_report(
+ state: web::Data<AppState>,
+ req: actix_web::HttpRequest,
+ json_payload: web::Json<ReportRequest>,
+ ) -> impl Responder {
+ let flow = AnalyticsFlow::GenerateRelayReport;
+ Box::pin(api::server_wrap(
+ flow,
+ state.clone(),
+ &req,
+ json_payload.into_inner(),
+ |state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_active_user_by_user_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
+
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
+
+ (user_email, payload.emails)
+ }
+ None => {
+ validate_report_request(&payload)?;
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
+
+ (primary_email, Some(other_emails))
+ }
+ };
+
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
+
+ let auth_info = auth.platform.to_merchant_level_auth_info();
+ let hash_key = match &payload.return_url {
+ Some(_) => {
+ get_payment_response_hash_key(
+ state.store.as_ref(),
+ auth.platform.get_processor().get_key_store(),
+ &auth_info,
+ )
+ .await?
+ }
+ None => None,
+ };
+
+ let lambda_req = GenerateReportRequest {
+ request: payload,
+ merchant_id: Some(auth.platform.get_processor().get_account().get_id().clone()),
+ auth: auth_info,
+ email: user_email,
+ payment_response_hash_key: hash_key,
+ };
+
+ let json_bytes =
+ serde_json::to_vec(&lambda_req).map_err(|_| AnalyticsError::UnknownError)?;
+
+ let lambda_result = invoke_lambda(
+ &state.conf.report_download_config.relay_function,
+ &state.conf.report_download_config.region,
+ &json_bytes,
+ )
+ .await;
+
+ match &lambda_result {
+ Ok(_) => logger::info!(
+ merchant_id = ?lambda_req.merchant_id,
+ "Successfully invoked relay report lambda function"
+ ),
+ Err(error) => logger::error!(
+ merchant_id = ?lambda_req.merchant_id,
+ ?error,
+ "Failed to invoke relay report lambda function"
+ ),
+ }
+
+ lambda_result.map(ApplicationResponse::Json)
+ },
+ auth::auth_type(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::MerchantReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
+ api_locking::LockAction::NotApplicable,
+ ))
+ .await
+ }
+
+ #[cfg(feature = "v1")]
+ pub async fn generate_profile_relay_report(
+ state: web::Data<AppState>,
+ req: actix_web::HttpRequest,
+ json_payload: web::Json<ReportRequest>,
+ ) -> impl Responder {
+ let flow = AnalyticsFlow::GenerateRelayReport;
+ Box::pin(api::server_wrap(
+ flow,
+ state.clone(),
+ &req,
+ json_payload.into_inner(),
+ |state, (auth, user_id): auth::AuthenticationDataWithUserId, payload, _| async move {
+ let (user_email, optional_emails) = match user_id {
+ Some(user_id) => {
+ let user = state
+ .global_store
+ .find_active_user_by_user_id(&user_id)
+ .await
+ .change_context(AnalyticsError::UnknownError)?;
+
+ let user_email = Email::try_from(
+ UserEmail::from_pii_email(user.email)
+ .change_context(AnalyticsError::UnknownError)?
+ .get_secret()
+ .expose()
+ .to_string(),
+ )
+ .change_context(AnalyticsError::MissingEmail)?;
+
+ (user_email, payload.emails)
+ }
+ None => {
+ validate_report_request(&payload)?;
+ let (primary_email, other_emails) = payload
+ .emails
+ .and_then(|mut emails| {
+ if emails.is_empty() {
+ None
+ } else {
+ let primary_email = emails.remove(0);
+ Some((primary_email, emails))
+ }
+ })
+ .ok_or(AnalyticsError::MissingEmail)?;
+
+ (primary_email, Some(other_emails))
+ }
+ };
+
+ let payload = ReportRequest {
+ emails: optional_emails,
+ ..payload
+ };
+
+ let profile_id = auth
+ .profile
+ .ok_or(report!(UserErrors::JwtProfileIdMissing))
+ .change_context(AnalyticsError::AccessForbiddenError)?
+ .get_id()
+ .clone();
+
+ let auth_info = auth.platform.to_profile_level_auth_info(profile_id);
+ let hash_key = match &payload.return_url {
+ Some(_) => {
+ get_payment_response_hash_key(
+ state.store.as_ref(),
+ auth.platform.get_processor().get_key_store(),
+ &auth_info,
+ )
+ .await?
+ }
+ None => None,
+ };
+
+ let lambda_req = GenerateReportRequest {
+ request: payload,
+ merchant_id: Some(auth.platform.get_processor().get_account().get_id().clone()),
+ auth: auth_info,
+ email: user_email,
+ payment_response_hash_key: hash_key,
+ };
+
+ let json_bytes =
+ serde_json::to_vec(&lambda_req).map_err(|_| AnalyticsError::UnknownError)?;
+
+ let lambda_result = invoke_lambda(
+ &state.conf.report_download_config.relay_function,
+ &state.conf.report_download_config.region,
+ &json_bytes,
+ )
+ .await;
+
+ match &lambda_result {
+ Ok(_) => logger::info!(
+ merchant_id = ?lambda_req.merchant_id,
+ "Successfully invoked relay report lambda function"
+ ),
+ Err(error) => logger::error!(
+ merchant_id = ?lambda_req.merchant_id,
+ ?error,
+ "Failed to invoke relay report lambda function"
+ ),
+ }
+
+ lambda_result.map(ApplicationResponse::Json)
+ },
+ auth::auth_type(
+ &auth::ApiKeyAuth {
+ allow_connected_scope_operation: true,
+ allow_platform_self_operation: false,
+ },
+ &auth::JWTAuth {
+ permission: Permission::ProfileReportRead,
+ allow_connected: true,
+ allow_platform: false,
+ },
+ req.headers(),
+ ),
+ api_locking::LockAction::NotApplicable,
+ ))
+ .await
+ }
+
#[cfg(feature = "v1")]
pub async fn generate_merchant_dispute_report(
state: web::Data<AppState>,