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

fix(payouts): concurrency issue in payout webhooks (#10523)

Sakil Mostak38b9d689f21b
diff --git a/crates/router/src/core/webhooks/incoming.rs b/crates/router/src/core/webhooks/incoming.rs
index 670ca0cbe9..1d2ac0f935 100644
--- a/crates/router/src/core/webhooks/incoming.rs
+++ b/crates/router/src/core/webhooks/incoming.rs
@@ -17,7 +17,7 @@ use common_utils::{
use diesel_models::{refund as diesel_refund, ConnectorMandateReferenceId};
use error_stack::{report, ResultExt};
#[cfg(feature = "payouts")]
-use hyperswitch_domain_models::payouts::{payout_attempt::PayoutAttempt, payouts::PayoutsUpdate};
+use hyperswitch_domain_models::payouts::payouts::PayoutsUpdate;
use hyperswitch_domain_models::{
mandates::CommonMandateReference,
merchant_key_store::MerchantKeyStore,
@@ -1477,10 +1477,89 @@ async fn payouts_incoming_webhook_flow(
request_details: &IncomingWebhookRequestDetails<'_>,
connector: &ConnectorEnum,
) -> CustomResult<WebhookResponseTracker, errors::ApiErrorResponse> {
- metrics::INCOMING_PAYOUT_WEBHOOK_METRIC.add(1, &[]);
+ let payout_id =
+ get_payout_id_from_object_reference_id(&state, &platform, &webhook_details).await?;
+
+ let lock_action = api_locking::LockAction::Hold {
+ input: api_locking::LockingInput {
+ unique_locking_key: payout_id.get_string_repr().to_owned(),
+ api_identifier: lock_utils::ApiIdentifier::Payouts,
+ override_lock_retries: None,
+ },
+ };
+
+ lock_action
+ .clone()
+ .perform_locking_action(
+ &state,
+ platform.get_processor().get_account().get_id().to_owned(),
+ )
+ .await?;
+
+ let payout_response = Box::pin(process_payout_incoming_webhook(
+ state.clone(),
+ platform.clone(),
+ business_profile,
+ event_type,
+ payout_id,
+ source_verified,
+ request_details,
+ connector,
+ ))
+ .await;
+
+ lock_action
+ .free_lock_action(
+ &state,
+ platform.get_processor().get_account().get_id().to_owned(),
+ )
+ .await?;
+
+ payout_response
+}
+
+#[cfg(feature = "payouts")]
+struct PayoutWebhookPreCondition {
+ is_non_terminal_status: bool,
+ is_source_verified: bool,
+}
+
+#[cfg(feature = "payouts")]
+enum PaoyoutWebhookAction {
+ UpdateStatus,
+ RetrieveStatus,
+ NoAction,
+}
+
+#[cfg(feature = "payouts")]
+impl PayoutWebhookPreCondition {
+ pub fn new(is_non_terminal_status: bool, is_source_verified: bool) -> Self {
+ Self {
+ is_non_terminal_status,
+ is_source_verified,
+ }
+ }
+
+ // only update if the payout is in non-terminal status
+ // if source verified, update the payout attempt and trigger outgoing webhook
+ // if not source verified, do a payout retrieve call and update the status
+ pub fn get_payout_webhook_action(&self) -> PaoyoutWebhookAction {
+ match (self.is_non_terminal_status, self.is_source_verified) {
+ (true, true) => PaoyoutWebhookAction::UpdateStatus,
+ (true, false) => PaoyoutWebhookAction::RetrieveStatus,
+ (false, true) | (false, false) => PaoyoutWebhookAction::NoAction,
+ }
+ }
+}
+
+#[cfg(feature = "payouts")]
+async fn get_payout_id_from_object_reference_id(
+ state: &SessionState,
+ platform: &domain::Platform,
+ webhook_details: &api::IncomingWebhookDetails,
+) -> CustomResult<common_utils::id_type::PayoutId, errors::ApiErrorResponse> {
let db = &*state.store;
- //find payout_attempt by object_reference_id
- let payout_attempt = match webhook_details.object_reference_id {
+ let payout_attempt = match webhook_details.object_reference_id.clone() {
webhooks::ObjectReferenceId::PayoutId(payout_id_type) => match payout_id_type {
webhooks::PayoutIdType::PayoutAttemptId(id) => db
.find_payout_attempt_by_merchant_id_payout_attempt_id(
@@ -1505,18 +1584,27 @@ async fn payouts_incoming_webhook_flow(
.attach_printable("received a non-payout id when processing payout webhooks")?,
};
- let payouts = db
- .find_payout_by_merchant_id_payout_id(
- platform.get_processor().get_account().get_id(),
- &payout_attempt.payout_id,
- platform.get_processor().get_account().storage_scheme,
- )
- .await
- .change_context(errors::ApiErrorResponse::WebhookResourceNotFound)
- .attach_printable("Failed to fetch the payout")?;
+ Ok(payout_attempt.payout_id)
+}
+
+#[cfg(feature = "payouts")]
+#[instrument(skip_all)]
+#[allow(clippy::too_many_arguments)]
+async fn process_payout_incoming_webhook(
+ state: SessionState,
+ platform: domain::Platform,
+ business_profile: domain::Profile,
+ event_type: webhooks::IncomingWebhookEvent,
+ payout_id: common_utils::id_type::PayoutId,
+ source_verified: bool,
+ request_details: &IncomingWebhookRequestDetails<'_>,
+ connector: &ConnectorEnum,
+) -> CustomResult<WebhookResponseTracker, errors::ApiErrorResponse> {
+ metrics::INCOMING_PAYOUT_WEBHOOK_METRIC.add(1, &[]);
+
let action_req =
payout_models::PayoutRequest::PayoutActionRequest(payout_models::PayoutActionRequest {
- payout_id: payouts.payout_id.clone(),
+ payout_id,
});
let mut payout_data = Box::pin(payouts::make_payout_data(
@@ -1528,10 +1616,11 @@ async fn payouts_incoming_webhook_flow(
))
.await?;
- let payout_webhook_action = get_payout_webhook_action(
- payout_attempt.status.is_non_terminal_status(),
+ let payout_webhook_action = PayoutWebhookPreCondition::new(
+ payout_data.payout_attempt.status.is_non_terminal_status(),
source_verified,
- );
+ )
+ .get_payout_webhook_action();
match payout_webhook_action {
PaoyoutWebhookAction::UpdateStatus => {
payout_incoming_webhook_update_status(
@@ -1541,19 +1630,12 @@ async fn payouts_incoming_webhook_flow(
event_type,
request_details,
connector,
- payout_attempt,
&mut payout_data,
)
.await
}
PaoyoutWebhookAction::RetrieveStatus => {
- payout_incoming_webhook_retrieve_status(
- state,
- platform,
- payout_attempt,
- &mut payout_data,
- )
- .await
+ payout_incoming_webhook_retrieve_status(state, platform, &mut payout_data).await
}
PaoyoutWebhookAction::NoAction => Ok(WebhookResponseTracker::Payout {
payout_id: payout_data.payout_attempt.payout_id,
@@ -1562,26 +1644,6 @@ async fn payouts_incoming_webhook_flow(
}
}
-enum PaoyoutWebhookAction {
- UpdateStatus,
- RetrieveStatus,
- NoAction,
-}
-
-// only update if the payout is in non-terminal status
-// if source verified, update the payout attempt and trigger outgoing webhook
-// if not source verified, do a payout retrieve call and update the status
-fn get_payout_webhook_action(
- is_non_terminal_status: bool,
- is_source_verified: bool,
-) -> PaoyoutWebhookAction {
- match (is_non_terminal_status, is_source_verified) {
- (true, true) => PaoyoutWebhookAction::UpdateStatus,
- (true, false) => PaoyoutWebhookAction::RetrieveStatus,
- (false, true) | (false, false) => PaoyoutWebhookAction::NoAction,
- }
-}
-
#[cfg(feature = "payouts")]
#[instrument(skip_all)]
#[allow(clippy::too_many_arguments)]
@@ -1592,9 +1654,9 @@ async fn payout_incoming_webhook_update_status(
event_type: webhooks::IncomingWebhookEvent,
request_details: &IncomingWebhookRequestDetails<'_>,
connector: &ConnectorEnum,
- payout_attempt: PayoutAttempt,
payout_data: &mut payouts::PayoutData,
) -> CustomResult<WebhookResponseTracker, errors::ApiErrorResponse> {
+ let payout_attempt = &payout_data.payout_attempt;
let db = &*state.store;
let status = common_enums::PayoutStatus::foreign_try_from(event_type)
.change_context(errors::ApiErrorResponse::WebhookProcessingFailure)
@@ -1611,7 +1673,7 @@ async fn payout_incoming_webhook_update_status(
.update_payout(
&payout_data.payouts,
payouts_update,
- &payout_attempt,
+ payout_attempt,
platform.get_processor().get_account().storage_scheme,
)
.await
@@ -1642,7 +1704,7 @@ async fn payout_incoming_webhook_update_status(
status,
error_message: None,
error_code: None,
- is_eligible: payout_attempt.is_eligible,
+ is_eligible: payout_data.payout_attempt.is_eligible,
unified_code: None,
unified_message: None,
payout_connector_metadata: payout_attempt.payout_connector_metadata.clone(),
@@ -1651,7 +1713,7 @@ async fn payout_incoming_webhook_update_status(
let updated_payout_attempt = db
.update_payout_attempt(
- &payout_attempt,
+ payout_attempt,
payout_attempt_update,
&payout_data.payouts,
platform.get_processor().get_account().storage_scheme,
@@ -1703,17 +1765,16 @@ async fn payout_incoming_webhook_update_status(
async fn payout_incoming_webhook_retrieve_status(
state: SessionState,
platform: domain::Platform,
- payout_attempt: PayoutAttempt,
payout_data: &mut payouts::PayoutData,
) -> CustomResult<WebhookResponseTracker, errors::ApiErrorResponse> {
metrics::INCOMING_PAYOUT_WEBHOOK_SIGNATURE_FAILURE_METRIC.add(1, &[]);
// Form connector data
- let connector_data = match &payout_attempt.connector {
+ let connector_data = match &payout_data.payout_attempt.connector {
Some(connector) => ConnectorData::get_payout_connector_by_name(
&state.conf.connectors,
connector,
GetToken::Connector,
- payout_attempt.merchant_connector_id.clone(),
+ payout_data.payout_attempt.merchant_connector_id.clone(),
)
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to get the connector data")?,