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

refactor: Add support to trigger payout sync if source verification is false (#10199)

Swangi Kumari250a69370920
diff --git a/crates/hyperswitch_connectors/src/connectors/gigadat.rs b/crates/hyperswitch_connectors/src/connectors/gigadat.rs
index 1ecdb870d8..bb430fda34 100644
--- a/crates/hyperswitch_connectors/src/connectors/gigadat.rs
+++ b/crates/hyperswitch_connectors/src/connectors/gigadat.rs
@@ -933,7 +933,7 @@ fn get_webhook_query_params(
"transaction" => txn = Some(value.to_string()),
"status" => {
if let Ok(status) =
- transformers::GigadatPaymentStatus::try_from(value.to_string())
+ transformers::GigadatTransactionStatus::try_from(value.to_string())
{
sts = Some(status);
}
@@ -967,29 +967,23 @@ impl webhooks::IncomingWebhook for Gigadat {
})
.collect();
- let cpi_type_entry = details
+ let webhook_type = details
.iter()
- .find(|&entry| entry.key == "cpiType")
+ .find(|&entry| entry.key == "type")
.ok_or(errors::ConnectorError::WebhookBodyDecodingFailed)?;
- let reference_id = match cpi_type_entry.value.as_str() {
- #[cfg(feature = "payouts")]
- "ETO" | "RTO" | "RTX" | "ANR" | "ANX" => {
- api_models::webhooks::ObjectReferenceId::PayoutId(
- api_models::webhooks::PayoutIdType::ConnectorPayoutId(query_params.transaction),
+ let reference_id = match transformers::GigadatFlow::get_flow(webhook_type.value.as_str())? {
+ transformers::GigadatFlow::Payment => {
+ api_models::webhooks::ObjectReferenceId::PaymentId(
+ api_models::payments::PaymentIdType::ConnectorTransactionId(
+ query_params.transaction,
+ ),
)
}
- "ETI" | "RFM" => api_models::webhooks::ObjectReferenceId::PaymentId(
- api_models::payments::PaymentIdType::ConnectorTransactionId(
- query_params.transaction,
- ),
+ #[cfg(feature = "payouts")]
+ transformers::GigadatFlow::Payout => api_models::webhooks::ObjectReferenceId::PayoutId(
+ api_models::webhooks::PayoutIdType::ConnectorPayoutId(query_params.transaction),
),
- _ => {
- return Err(errors::ConnectorError::NotImplemented(
- "Invalid transaction type ".to_string(),
- )
- .into())
- }
};
Ok(reference_id)
}
@@ -999,7 +993,25 @@ impl webhooks::IncomingWebhook for Gigadat {
request: &webhooks::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api_models::webhooks::IncomingWebhookEvent, errors::ConnectorError> {
let query_params = get_webhook_query_params(request)?;
- let event_type = api_models::webhooks::IncomingWebhookEvent::from(query_params.status);
+ let body_str = std::str::from_utf8(request.body)
+ .change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
+
+ let details: Vec<transformers::GigadatWebhookKeyValue> =
+ form_urlencoded::parse(body_str.as_bytes())
+ .map(|(key, value)| transformers::GigadatWebhookKeyValue {
+ key: key.to_string(),
+ value: value.to_string(),
+ })
+ .collect();
+
+ let webhook_type = details
+ .iter()
+ .find(|&entry| entry.key == "type")
+ .ok_or(errors::ConnectorError::WebhookBodyDecodingFailed)?;
+
+ let flow_type = transformers::GigadatFlow::get_flow(webhook_type.value.as_str())?;
+ let event_type =
+ transformers::get_gigadat_webhook_event_type(query_params.status, flow_type);
Ok(event_type)
}
diff --git a/crates/hyperswitch_connectors/src/connectors/gigadat/transformers.rs b/crates/hyperswitch_connectors/src/connectors/gigadat/transformers.rs
index 72180348a1..1526c65087 100644
--- a/crates/hyperswitch_connectors/src/connectors/gigadat/transformers.rs
+++ b/crates/hyperswitch_connectors/src/connectors/gigadat/transformers.rs
@@ -1,3 +1,4 @@
+use api_models::webhooks::IncomingWebhookEvent;
#[cfg(feature = "payouts")]
use api_models::{
self,
@@ -180,7 +181,7 @@ pub struct GigadatPaymentData {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
-pub enum GigadatPaymentStatus {
+pub enum GigadatTransactionStatus {
StatusInited,
StatusSuccess,
StatusRejected,
@@ -191,39 +192,76 @@ pub enum GigadatPaymentStatus {
StatusFailed,
}
-impl From<GigadatPaymentStatus> for enums::AttemptStatus {
- fn from(item: GigadatPaymentStatus) -> Self {
+impl From<GigadatTransactionStatus> for enums::AttemptStatus {
+ fn from(item: GigadatTransactionStatus) -> Self {
match item {
- GigadatPaymentStatus::StatusSuccess => Self::Charged,
- GigadatPaymentStatus::StatusInited | GigadatPaymentStatus::StatusPending => {
+ GigadatTransactionStatus::StatusSuccess => Self::Charged,
+ GigadatTransactionStatus::StatusInited | GigadatTransactionStatus::StatusPending => {
Self::Pending
}
- GigadatPaymentStatus::StatusRejected
- | GigadatPaymentStatus::StatusExpired
- | GigadatPaymentStatus::StatusRejected1
- | GigadatPaymentStatus::StatusAborted1
- | GigadatPaymentStatus::StatusFailed => Self::Failure,
+ GigadatTransactionStatus::StatusRejected
+ | GigadatTransactionStatus::StatusExpired
+ | GigadatTransactionStatus::StatusRejected1
+ | GigadatTransactionStatus::StatusAborted1
+ | GigadatTransactionStatus::StatusFailed => Self::Failure,
}
}
}
-impl From<GigadatPaymentStatus> for api_models::webhooks::IncomingWebhookEvent {
- fn from(item: GigadatPaymentStatus) -> Self {
- match item {
- GigadatPaymentStatus::StatusSuccess => Self::PaymentIntentSuccess,
- GigadatPaymentStatus::StatusFailed
- | GigadatPaymentStatus::StatusRejected
- | GigadatPaymentStatus::StatusRejected1
- | GigadatPaymentStatus::StatusExpired
- | GigadatPaymentStatus::StatusAborted1 => Self::PaymentIntentFailure,
- GigadatPaymentStatus::StatusInited | GigadatPaymentStatus::StatusPending => {
- Self::PaymentIntentProcessing
- }
+pub enum GigadatFlow {
+ Payment,
+ #[cfg(feature = "payouts")]
+ Payout,
+}
+
+impl GigadatFlow {
+ pub fn get_flow(webhook_type: &str) -> Result<Self, errors::ConnectorError> {
+ match webhook_type {
+ #[cfg(feature = "payouts")]
+ "ETO" | "RTO" | "RTX" | "ANR" | "ANX" => Ok(Self::Payout),
+
+ "ETI" | "RFM" | "CPI" | "ACK" => Ok(Self::Payment),
+ _ => Err(errors::ConnectorError::NotImplemented(
+ "Invalid transaction type ".to_string(),
+ )),
}
}
}
-impl TryFrom<String> for GigadatPaymentStatus {
+pub fn get_gigadat_webhook_event_type(
+ status: GigadatTransactionStatus,
+ flow: GigadatFlow,
+) -> IncomingWebhookEvent {
+ match flow {
+ GigadatFlow::Payment => match status {
+ GigadatTransactionStatus::StatusSuccess => IncomingWebhookEvent::PaymentIntentSuccess,
+ GigadatTransactionStatus::StatusFailed
+ | GigadatTransactionStatus::StatusRejected
+ | GigadatTransactionStatus::StatusRejected1
+ | GigadatTransactionStatus::StatusExpired
+ | GigadatTransactionStatus::StatusAborted1 => {
+ IncomingWebhookEvent::PaymentIntentFailure
+ }
+ GigadatTransactionStatus::StatusInited | GigadatTransactionStatus::StatusPending => {
+ IncomingWebhookEvent::PaymentIntentProcessing
+ }
+ },
+ #[cfg(feature = "payouts")]
+ GigadatFlow::Payout => match status {
+ GigadatTransactionStatus::StatusSuccess => IncomingWebhookEvent::PayoutSuccess,
+ GigadatTransactionStatus::StatusFailed
+ | GigadatTransactionStatus::StatusRejected
+ | GigadatTransactionStatus::StatusRejected1
+ | GigadatTransactionStatus::StatusExpired
+ | GigadatTransactionStatus::StatusAborted1 => IncomingWebhookEvent::PayoutFailure,
+ GigadatTransactionStatus::StatusInited | GigadatTransactionStatus::StatusPending => {
+ IncomingWebhookEvent::PayoutProcessing
+ }
+ },
+ }
+}
+
+impl TryFrom<String> for GigadatTransactionStatus {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(value: String) -> Result<Self, Self::Error> {
match value.as_str() {
@@ -242,7 +280,7 @@ impl TryFrom<String> for GigadatPaymentStatus {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GigadatTransactionStatusResponse {
- pub status: GigadatPaymentStatus,
+ pub status: GigadatTransactionStatus,
}
impl<F, T> TryFrom<ResponseRouterData<F, GigadatPaymentResponse, T, PaymentsResponseData>>
@@ -581,7 +619,7 @@ pub struct Error {
#[derive(Debug, Deserialize)]
pub struct GigadatWebhookQueryParameters {
pub transaction: String,
- pub status: GigadatPaymentStatus,
+ pub status: GigadatTransactionStatus,
}
#[derive(Debug, Serialize, Deserialize)]
diff --git a/crates/hyperswitch_connectors/src/connectors/loonio.rs b/crates/hyperswitch_connectors/src/connectors/loonio.rs
index ba4f80cc9b..0d4a6acba5 100644
--- a/crates/hyperswitch_connectors/src/connectors/loonio.rs
+++ b/crates/hyperswitch_connectors/src/connectors/loonio.rs
@@ -778,12 +778,21 @@ impl webhooks::IncomingWebhook for Loonio {
.body
.parse_struct("LoonioWebhookBody")
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
-
- Ok(api_models::webhooks::ObjectReferenceId::PaymentId(
- api_models::payments::PaymentIdType::ConnectorTransactionId(
- webhook_body.api_transaction_id,
- ),
- ))
+ match webhook_body.transaction_type {
+ #[cfg(feature = "payouts")]
+ loonio::LoonioWebhookTransactionType::OutgoingNotVerified => {
+ Ok(api_models::webhooks::ObjectReferenceId::PayoutId(
+ api_models::webhooks::PayoutIdType::ConnectorPayoutId(
+ webhook_body.api_transaction_id,
+ ),
+ ))
+ }
+ _ => Ok(api_models::webhooks::ObjectReferenceId::PaymentId(
+ api_models::payments::PaymentIdType::ConnectorTransactionId(
+ webhook_body.api_transaction_id,
+ ),
+ )),
+ }
}
fn get_webhook_event_type(
@@ -794,7 +803,10 @@ impl webhooks::IncomingWebhook for Loonio {
.body
.parse_struct("LoonioWebhookBody")
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
- Ok((&webhook_body.event_code).into())
+ Ok(loonio::get_loonio_webhook_event(
+ &webhook_body.transaction_type,
+ &webhook_body.event_code,
+ ))
}
fn get_webhook_resource_object(
@@ -838,6 +850,8 @@ lazy_static! {
};
static ref LOONIO_SUPPORTED_WEBHOOK_FLOWS: Vec<enums::EventClass> = vec![
enums::EventClass::Payments,
+ #[cfg(feature = "payouts")]
+ enums::EventClass::Payouts,
];
}
diff --git a/crates/hyperswitch_connectors/src/connectors/loonio/transformers.rs b/crates/hyperswitch_connectors/src/connectors/loonio/transformers.rs
index 7e611d51d2..e7b5d5aee7 100644
--- a/crates/hyperswitch_connectors/src/connectors/loonio/transformers.rs
+++ b/crates/hyperswitch_connectors/src/connectors/loonio/transformers.rs
@@ -413,7 +413,6 @@ pub struct LoonioWebhookBody {
pub api_transaction_id: String,
pub signature: Option<String>,
pub event_code: LoonioWebhookEventCode,
- pub id: i32,
#[serde(rename = "type")]
pub transaction_type: LoonioWebhookTransactionType,
pub customer_info: Option<pii::SecretSerdeValue>,
@@ -444,6 +443,61 @@ impl From<&LoonioWebhookEventCode> for webhooks::IncomingWebhookEvent {
}
}
+pub(crate) fn get_loonio_webhook_event(
+ transaction_type: &LoonioWebhookTransactionType,
+ event_code: &LoonioWebhookEventCode,
+) -> webhooks::IncomingWebhookEvent {
+ match transaction_type {
+ LoonioWebhookTransactionType::OutgoingNotVerified => {
+ #[cfg(feature = "payouts")]
+ {
+ match event_code {
+ LoonioWebhookEventCode::TransactionPrepared => {
+ webhooks::IncomingWebhookEvent::PayoutCreated
+ }
+ LoonioWebhookEventCode::TransactionPending => {
+ webhooks::IncomingWebhookEvent::PayoutProcessing
+ }
+ LoonioWebhookEventCode::TransactionAvailable
+ | LoonioWebhookEventCode::TransactionSettled => {
+ webhooks::IncomingWebhookEvent::PayoutSuccess
+ }
+ LoonioWebhookEventCode::TransactionFailed
+ | LoonioWebhookEventCode::TransactionRejected => {
+ webhooks::IncomingWebhookEvent::PayoutFailure
+ }
+ _ => webhooks::IncomingWebhookEvent::EventNotSupported,
+ }
+ }
+
+ #[cfg(not(feature = "payouts"))]
+ {
+ webhooks::IncomingWebhookEvent::EventNotSupported
+ }
+ }
+
+ _ => match event_code {
+ LoonioWebhookEventCode::TransactionSettled
+ | LoonioWebhookEventCode::TransactionAvailable => {
+ webhooks::IncomingWebhookEvent::PaymentIntentSuccess
+ }
+ LoonioWebhookEventCode::TransactionPending
+ | LoonioWebhookEventCode::TransactionPrepared => {
+ webhooks::IncomingWebhookEvent::PaymentIntentProcessing
+ }
+ LoonioWebhookEventCode::TransactionFailed
+ | LoonioWebhookEventCode::TransactionRejected
+ | LoonioWebhookEventCode::TransactionStatusFileFailed
+ | LoonioWebhookEventCode::TransactionReturned
+ | LoonioWebhookEventCode::TransactionWrongDestination
+ | LoonioWebhookEventCode::TransactionNsf => {
+ webhooks::IncomingWebhookEvent::PaymentIntentFailure
+ }
+ _ => webhooks::IncomingWebhookEvent::EventNotSupported,
+ },
+ }
+}
+
impl From<&LoonioWebhookEventCode> for enums::AttemptStatus {
fn from(event_code: &LoonioWebhookEventCode) -> Self {
match event_code {
diff --git a/crates/router/src/core/webhooks/incoming.rs b/crates/router/src/core/webhooks/incoming.rs
index 3e386d2f5f..8375debf84 100644
--- a/crates/router/src/core/webhooks/incoming.rs
+++ b/crates/router/src/core/webhooks/incoming.rs
@@ -1480,44 +1480,57 @@ async fn payouts_incoming_webhook_flow(
connector: &ConnectorEnum,
) -> CustomResult<WebhookResponseTracker, errors::ApiErrorResponse> {
metrics::INCOMING_PAYOUT_WEBHOOK_METRIC.add(1, &[]);
- if source_verified {
- let db = &*state.store;
- //find payout_attempt by object_reference_id
- let payout_attempt = match webhook_details.object_reference_id {
- webhooks::ObjectReferenceId::PayoutId(payout_id_type) => match payout_id_type {
- webhooks::PayoutIdType::PayoutAttemptId(id) => db
- .find_payout_attempt_by_merchant_id_payout_attempt_id(
- merchant_context.get_merchant_account().get_id(),
- &id,
- merchant_context.get_merchant_account().storage_scheme,
- )
- .await
- .change_context(errors::ApiErrorResponse::WebhookResourceNotFound)
- .attach_printable("Failed to fetch the payout attempt")?,
- webhooks::PayoutIdType::ConnectorPayoutId(id) => db
- .find_payout_attempt_by_merchant_id_connector_payout_id(
- merchant_context.get_merchant_account().get_id(),
- &id,
- merchant_context.get_merchant_account().storage_scheme,
- )
- .await
- .change_context(errors::ApiErrorResponse::WebhookResourceNotFound)
- .attach_printable("Failed to fetch the payout attempt")?,
- },
- _ => Err(errors::ApiErrorResponse::WebhookProcessingFailure)
- .attach_printable("received a non-payout id when processing payout webhooks")?,
- };
+ let db = &*state.store;
+ //find payout_attempt by object_reference_id
+ let payout_attempt = match webhook_details.object_reference_id {
+ webhooks::ObjectReferenceId::PayoutId(payout_id_type) => match payout_id_type {
+ webhooks::PayoutIdType::PayoutAttemptId(id) => db
+ .find_payout_attempt_by_merchant_id_payout_attempt_id(
+ merchant_context.get_merchant_account().get_id(),
+ &id,
+ merchant_context.get_merchant_account().storage_scheme,
+ )
+ .await
+ .change_context(errors::ApiErrorResponse::WebhookResourceNotFound)
+ .attach_printable("Failed to fetch the payout attempt")?,
+ webhooks::PayoutIdType::ConnectorPayoutId(id) => db
+ .find_payout_attempt_by_merchant_id_connector_payout_id(
+ merchant_context.get_merchant_account().get_id(),
+ &id,
+ merchant_context.get_merchant_account().storage_scheme,
+ )
+ .await
+ .change_context(errors::ApiErrorResponse::WebhookResourceNotFound)
+ .attach_printable("Failed to fetch the payout attempt")?,
+ },
+ _ => Err(errors::ApiErrorResponse::WebhookProcessingFailure)
+ .attach_printable("received a non-payout id when processing payout webhooks")?,
+ };
- let payouts = db
- .find_payout_by_merchant_id_payout_id(
- merchant_context.get_merchant_account().get_id(),
- &payout_attempt.payout_id,
- merchant_context.get_merchant_account().storage_scheme,
- )
- .await
- .change_context(errors::ApiErrorResponse::WebhookResourceNotFound)
- .attach_printable("Failed to fetch the payout")?;
+ let payouts = db
+ .find_payout_by_merchant_id_payout_id(
+ merchant_context.get_merchant_account().get_id(),
+ &payout_attempt.payout_id,
+ merchant_context.get_merchant_account().storage_scheme,
+ )
+ .await
+ .change_context(errors::ApiErrorResponse::WebhookResourceNotFound)
+ .attach_printable("Failed to fetch the payout")?;
+ let action_req =
+ payout_models::PayoutRequest::PayoutActionRequest(payout_models::PayoutActionRequest {
+ payout_id: payouts.payout_id.clone(),
+ });
+ let mut payout_data = Box::pin(payouts::make_payout_data(
+ &state,
+ &merchant_context,
+ None,
+ &action_req,
+ common_utils::consts::DEFAULT_LOCALE,
+ ))
+ .await?;
+
+ if source_verified {
let status = common_enums::PayoutStatus::foreign_try_from(event_type)
.change_context(errors::ApiErrorResponse::WebhookProcessingFailure)
.attach_printable("failed payout status mapping from event type")?;
@@ -1552,20 +1565,6 @@ async fn payouts_incoming_webhook_flow(
}
};
- let action_req =
- payout_models::PayoutRequest::PayoutActionRequest(payout_models::PayoutActionRequest {
- payout_id: payouts.payout_id.clone(),
- });
-
- let mut payout_data = Box::pin(payouts::make_payout_data(
- &state,
- &merchant_context,
- None,
- &action_req,
- common_utils::consts::DEFAULT_LOCALE,
- ))
- .await?;
-
let updated_payout_attempt = db
.update_payout_attempt(
&payout_attempt,
@@ -1614,9 +1613,38 @@ async fn payouts_incoming_webhook_flow(
})
} else {
metrics::INCOMING_PAYOUT_WEBHOOK_SIGNATURE_FAILURE_METRIC.add(1, &[]);
- Err(report!(
- errors::ApiErrorResponse::WebhookAuthenticationFailed
+ // Form connector data
+ let connector_data = match &payout_attempt.connector {
+ Some(connector) => ConnectorData::get_payout_connector_by_name(
+ &state.conf.connectors,
+ connector,
+ GetToken::Connector,
+ payout_attempt.merchant_connector_id.clone(),
+ )
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to get the connector data")?,
+ _ => Err(errors::ApplicationError::InvalidConfigurationValueError(
+ "Connector not found in payout_attempt - should not reach here.".to_string(),
+ ))
+ .change_context(errors::ApiErrorResponse::MissingRequiredField {
+ field_name: "connector",
+ })
+ .attach_printable("Connector not found for payout fulfillment")?,
+ };
+
+ Box::pin(payouts::create_payout_retrieve(
+ &state,
+ &merchant_context,
+ &connector_data,
+ &mut payout_data,
))
+ .await
+ .attach_printable("Payout retrieval failed for given Payout request")?;
+
+ Ok(WebhookResponseTracker::Payout {
+ payout_id: payout_data.payout_attempt.payout_id,
+ status: payout_data.payout_attempt.status,
+ })
}
}