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(payouts): add manual update api for payouts (#10539)

Sakil Mostak0e85d50e65b8
diff --git a/crates/api_models/src/events/payouts.rs b/crates/api_models/src/events/payouts.rs
index 3e6e80d010..cf19a91e40 100644
--- a/crates/api_models/src/events/payouts.rs
+++ b/crates/api_models/src/events/payouts.rs
@@ -4,6 +4,7 @@ use crate::payouts::{
PayoutActionRequest, PayoutCreateRequest, PayoutCreateResponse, PayoutLinkInitiateRequest,
PayoutListConstraints, PayoutListFilterConstraints, PayoutListFilters, PayoutListFiltersV2,
PayoutListResponse, PayoutRetrieveRequest, PayoutsAggregateResponse,
+ PayoutsManualUpdateRequest, PayoutsManualUpdateResponse,
};
impl ApiEventMetric for PayoutRetrieveRequest {
@@ -81,3 +82,19 @@ impl ApiEventMetric for PayoutsAggregateResponse {
Some(ApiEventsType::ResourceListAPI)
}
}
+
+impl ApiEventMetric for PayoutsManualUpdateRequest {
+ fn get_api_event_type(&self) -> Option<ApiEventsType> {
+ Some(ApiEventsType::Payout {
+ payout_id: self.payout_id.to_owned(),
+ })
+ }
+}
+
+impl ApiEventMetric for PayoutsManualUpdateResponse {
+ fn get_api_event_type(&self) -> Option<ApiEventsType> {
+ Some(ApiEventsType::Payout {
+ payout_id: self.payout_id.to_owned(),
+ })
+ }
+}
diff --git a/crates/api_models/src/payouts.rs b/crates/api_models/src/payouts.rs
index 8dafdcc93a..8c2a45304a 100644
--- a/crates/api_models/src/payouts.rs
+++ b/crates/api_models/src/payouts.rs
@@ -1124,3 +1124,54 @@ pub struct PayoutsAggregateResponse {
/// The list of intent status with their count
pub status_with_count: HashMap<common_enums::PayoutStatus, i64>,
}
+
+#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, ToSchema)]
+pub struct PayoutsManualUpdateRequest {
+ /// The identifier for the payout
+ #[schema(value_type = String)]
+ pub payout_id: id_type::PayoutId,
+ /// The identifier for the payout attempt
+ pub payout_attempt_id: String,
+ /// Merchant ID
+ #[schema(value_type = String)]
+ pub merchant_id: id_type::MerchantId,
+ /// The status of the payout attempt
+ #[schema(value_type = Option<PayoutStatus>)]
+ pub status: Option<api_enums::PayoutStatus>,
+ /// Error code of the connector
+ pub error_code: Option<String>,
+ /// Error message of the connector
+ pub error_message: Option<String>,
+ /// A unique identifier for a payout provided by the connector
+ pub connector_payout_id: Option<String>,
+}
+
+impl PayoutsManualUpdateRequest {
+ pub fn is_update_parameter_present(&self) -> bool {
+ self.status.is_some()
+ || self.error_code.is_some()
+ || self.error_message.is_some()
+ || self.connector_payout_id.is_some()
+ }
+}
+
+#[derive(Debug, serde::Serialize, Clone, ToSchema)]
+pub struct PayoutsManualUpdateResponse {
+ /// The identifier for the payout
+ #[schema(value_type = String)]
+ pub payout_id: id_type::PayoutId,
+ /// The identifier for the payout attempt
+ pub payout_attempt_id: String,
+ /// Merchant ID
+ #[schema(value_type = String)]
+ pub merchant_id: id_type::MerchantId,
+ /// The status of the payout attempt
+ #[schema(value_type = PayoutStatus)]
+ pub attempt_status: api_enums::PayoutStatus,
+ /// Error code of the connector
+ pub error_code: Option<String>,
+ /// Error message of the connector
+ pub error_message: Option<String>,
+ /// A unique identifier for a payout provided by the connector
+ pub connector_payout_id: Option<String>,
+}
diff --git a/crates/diesel_models/src/payout_attempt.rs b/crates/diesel_models/src/payout_attempt.rs
index 834539e2e3..81fdbf45a9 100644
--- a/crates/diesel_models/src/payout_attempt.rs
+++ b/crates/diesel_models/src/payout_attempt.rs
@@ -111,6 +111,14 @@ pub enum PayoutAttemptUpdate {
AdditionalPayoutMethodDataUpdate {
additional_payout_method_data: Option<payout_method_utils::AdditionalPayoutMethodData>,
},
+ ManualUpdate {
+ status: Option<storage_enums::PayoutStatus>,
+ error_code: Option<String>,
+ error_message: Option<String>,
+ unified_code: Option<UnifiedCode>,
+ unified_message: Option<UnifiedMessage>,
+ connector_payout_id: Option<String>,
+ },
}
#[derive(Clone, Debug, AsChangeset, router_derive::DebugAsDisplay)]
@@ -218,6 +226,22 @@ impl From<PayoutAttemptUpdate> for PayoutAttemptUpdateInternal {
additional_payout_method_data,
..Default::default()
},
+ PayoutAttemptUpdate::ManualUpdate {
+ status,
+ error_code,
+ error_message,
+ unified_code,
+ unified_message,
+ connector_payout_id,
+ } => Self {
+ status,
+ error_code,
+ error_message,
+ unified_code,
+ unified_message,
+ connector_payout_id,
+ ..Default::default()
+ },
}
}
}
diff --git a/crates/diesel_models/src/payouts.rs b/crates/diesel_models/src/payouts.rs
index abc692a36f..3e9fd2891c 100644
--- a/crates/diesel_models/src/payouts.rs
+++ b/crates/diesel_models/src/payouts.rs
@@ -113,6 +113,9 @@ pub enum PayoutsUpdate {
StatusUpdate {
status: storage_enums::PayoutStatus,
},
+ ManualUpdate {
+ status: Option<storage_enums::PayoutStatus>,
+ },
}
#[derive(Clone, Debug, AsChangeset, router_derive::DebugAsDisplay)]
@@ -216,6 +219,10 @@ impl From<PayoutsUpdate> for PayoutsUpdateInternal {
status: Some(status),
..Default::default()
},
+ PayoutsUpdate::ManualUpdate { status } => Self {
+ status,
+ ..Default::default()
+ },
}
}
}
diff --git a/crates/hyperswitch_domain_models/src/payouts/payout_attempt.rs b/crates/hyperswitch_domain_models/src/payouts/payout_attempt.rs
index 3b7223febd..dd113a0345 100644
--- a/crates/hyperswitch_domain_models/src/payouts/payout_attempt.rs
+++ b/crates/hyperswitch_domain_models/src/payouts/payout_attempt.rs
@@ -153,6 +153,14 @@ pub enum PayoutAttemptUpdate {
AdditionalPayoutMethodDataUpdate {
additional_payout_method_data: Option<payout_method_utils::AdditionalPayoutMethodData>,
},
+ ManualUpdate {
+ status: Option<storage_enums::PayoutStatus>,
+ error_code: Option<String>,
+ error_message: Option<String>,
+ unified_code: Option<UnifiedCode>,
+ unified_message: Option<UnifiedMessage>,
+ connector_payout_id: Option<String>,
+ },
}
#[derive(Clone, Debug, Default)]
@@ -231,6 +239,22 @@ impl From<PayoutAttemptUpdate> for PayoutAttemptUpdateInternal {
additional_payout_method_data,
..Default::default()
},
+ PayoutAttemptUpdate::ManualUpdate {
+ status,
+ error_code,
+ error_message,
+ unified_code,
+ unified_message,
+ connector_payout_id,
+ } => Self {
+ status,
+ error_code,
+ error_message,
+ unified_code,
+ unified_message,
+ connector_payout_id,
+ ..Default::default()
+ },
}
}
}
diff --git a/crates/hyperswitch_domain_models/src/payouts/payouts.rs b/crates/hyperswitch_domain_models/src/payouts/payouts.rs
index d33dd54acd..14a6b148d4 100644
--- a/crates/hyperswitch_domain_models/src/payouts/payouts.rs
+++ b/crates/hyperswitch_domain_models/src/payouts/payouts.rs
@@ -188,6 +188,9 @@ pub enum PayoutsUpdate {
StatusUpdate {
status: storage_enums::PayoutStatus,
},
+ ManualUpdate {
+ status: Option<storage_enums::PayoutStatus>,
+ },
}
#[derive(Clone, Debug, Default)]
@@ -264,6 +267,10 @@ impl From<PayoutsUpdate> for PayoutsUpdateInternal {
status: Some(status),
..Default::default()
},
+ PayoutsUpdate::ManualUpdate { status } => Self {
+ status,
+ ..Default::default()
+ },
}
}
}
diff --git a/crates/router/src/core/payouts.rs b/crates/router/src/core/payouts.rs
index 0a905d4794..e98b38bc3e 100644
--- a/crates/router/src/core/payouts.rs
+++ b/crates/router/src/core/payouts.rs
@@ -39,6 +39,8 @@ use router_env::{instrument, logger, tracing, Env};
use scheduler::utils as pt_utils;
use time::Duration;
+#[cfg(all(feature = "olap", feature = "payouts"))]
+use crate::consts as payout_consts;
#[cfg(feature = "olap")]
use crate::types::domain::behaviour::Conversion;
#[cfg(feature = "olap")]
@@ -3472,3 +3474,143 @@ pub async fn get_aggregates_for_payouts(
},
))
}
+
+#[cfg(all(feature = "olap", feature = "payouts"))]
+pub async fn payouts_manual_update_core(
+ state: SessionState,
+ req: api_models::payouts::PayoutsManualUpdateRequest,
+) -> RouterResponse<api_models::payouts::PayoutsManualUpdateResponse> {
+ if req.is_update_parameter_present() {
+ let api_models::payouts::PayoutsManualUpdateRequest {
+ payout_id,
+ payout_attempt_id,
+ merchant_id,
+ status,
+ error_code,
+ error_message,
+ connector_payout_id,
+ } = req;
+
+ let key_store = state
+ .store
+ .get_merchant_key_store_by_merchant_id(
+ &merchant_id,
+ &state.store.get_master_key().to_vec().into(),
+ )
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Error while fetching the key store by merchant_id")?;
+
+ let merchant_account = state
+ .store
+ .find_merchant_account_by_merchant_id(&merchant_id, &key_store)
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
+ .attach_printable("Error while fetching the merchant_account by merchant_id")?;
+
+ let payout_attempt = state
+ .store
+ .find_payout_attempt_by_merchant_id_payout_attempt_id(
+ &merchant_id,
+ &payout_attempt_id,
+ merchant_account.storage_scheme,
+ )
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::PayoutNotFound)
+ .attach_printable(
+ "Error while fetching the payout_attempt by merchant_id and attempt_id",
+ )?;
+
+ let payouts = state
+ .store
+ .find_payout_by_merchant_id_payout_id(
+ &merchant_id,
+ &payout_id,
+ merchant_account.storage_scheme,
+ )
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::PayoutNotFound)
+ .attach_printable("Error while fetching the payout by merchant_id and payout_id")?;
+
+ let option_gsm = if let Some(((code, message), connector_name)) = error_code
+ .as_ref()
+ .zip(error_message.as_ref())
+ .zip(payout_attempt.connector.as_ref())
+ {
+ helpers::get_gsm_record(
+ &state,
+ Some(code.to_string()),
+ Some(message.to_string()),
+ Some(connector_name.to_string()),
+ consts::PAYOUT_FLOW_STR,
+ payout_consts::DEFAULT_SUBFLOW_STR,
+ )
+ .await
+ } else {
+ None
+ };
+
+ // Update the payout_attempt
+ let attempt_update = storage::PayoutAttemptUpdate::ManualUpdate {
+ status,
+ error_code: error_code.clone(),
+ error_message: error_message.clone(),
+ unified_code: option_gsm
+ .as_ref()
+ .and_then(|gsm| gsm.unified_code.clone())
+ .and_then(|unified_code| UnifiedCode::try_from(unified_code).ok()),
+ unified_message: option_gsm
+ .and_then(|gsm| gsm.unified_message)
+ .and_then(|unified_code| UnifiedMessage::try_from(unified_code).ok()),
+ connector_payout_id: connector_payout_id.clone(),
+ };
+
+ let updated_payout_attempt = state
+ .store
+ .update_payout_attempt(
+ &payout_attempt,
+ attempt_update,
+ &payouts,
+ merchant_account.storage_scheme,
+ )
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::PayoutNotFound)
+ .attach_printable("Error while updating the payout_attempt")?;
+
+ let active_attempt_id =
+ utils::get_payout_attempt_id(payout_id.get_string_repr(), payouts.attempt_count);
+
+ // If the payout_attempt is the active attempt for the payout, update the payout status
+ if active_attempt_id == payout_attempt_id {
+ let payout_update = storage::PayoutsUpdate::ManualUpdate { status };
+ state
+ .store
+ .update_payout(
+ &payouts,
+ payout_update,
+ &updated_payout_attempt,
+ merchant_account.storage_scheme,
+ )
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::PayoutNotFound)
+ .attach_printable("Error while updating payout")?;
+ }
+
+ Ok(services::ApplicationResponse::Json(
+ api_models::payouts::PayoutsManualUpdateResponse {
+ payout_id: updated_payout_attempt.payout_id,
+ payout_attempt_id: updated_payout_attempt.payout_attempt_id,
+ merchant_id: updated_payout_attempt.merchant_id,
+ attempt_status: updated_payout_attempt.status,
+ error_code,
+ error_message,
+ connector_payout_id,
+ },
+ ))
+ } else {
+ Err(errors::ApiErrorResponse::UnprocessableEntity {
+ message: "Request must contain atleast one parameter to update".to_string(),
+ }
+ .into())
+ }
+}
diff --git a/crates/router/src/routes/app.rs b/crates/router/src/routes/app.rs
index 2fe88b4ab9..3a953bd540 100644
--- a/crates/router/src/routes/app.rs
+++ b/crates/router/src/routes/app.rs
@@ -1536,6 +1536,10 @@ impl Payouts {
.service(
web::resource("/profile/filter")
.route(web::post().to(payouts_list_available_filters_for_profile)),
+ )
+ .service(
+ web::resource("/{payout_id}/manual-update")
+ .route(web::put().to(payouts_manual_update)),
);
}
route = route
diff --git a/crates/router/src/routes/lock_utils.rs b/crates/router/src/routes/lock_utils.rs
index 867fa9f96f..2ff3fc96ce 100644
--- a/crates/router/src/routes/lock_utils.rs
+++ b/crates/router/src/routes/lock_utils.rs
@@ -188,6 +188,7 @@ impl From<Flow> for ApiIdentifier {
| Flow::PayoutsFilter
| Flow::PayoutsAccounts
| Flow::PayoutsConfirm
+ | Flow::PayoutsManualUpdate
| Flow::PayoutLinkInitiate
| Flow::PayoutsAggregate => Self::Payouts,
Flow::RefundsCreate
diff --git a/crates/router/src/routes/payouts.rs b/crates/router/src/routes/payouts.rs
index 915ed9e1a2..49ac8adec8 100644
--- a/crates/router/src/routes/payouts.rs
+++ b/crates/router/src/routes/payouts.rs
@@ -12,8 +12,13 @@ use router_env::{instrument, tracing, Flow};
use super::app::AppState;
use crate::{
- core::{api_locking, errors::RouterResult, payouts::*},
+ core::{
+ api_locking::{self, GetLockingInput},
+ errors::RouterResult,
+ payouts::*,
+ },
logger,
+ routes::lock_utils,
services::{
api,
authentication::{self as auth},
@@ -620,3 +625,50 @@ pub async fn get_payouts_aggregates_profile(
))
.await
}
+
+#[cfg(all(feature = "olap", feature = "payouts"))]
+impl GetLockingInput for payout_types::PayoutsManualUpdateRequest {
+ fn get_locking_input<F>(&self, flow: F) -> api_locking::LockAction
+ where
+ F: router_env::types::FlowMetric,
+ lock_utils::ApiIdentifier: From<F>,
+ {
+ api_locking::LockAction::Hold {
+ input: api_locking::LockingInput {
+ unique_locking_key: self.payout_id.get_string_repr().to_owned(),
+ api_identifier: lock_utils::ApiIdentifier::from(flow),
+ override_lock_retries: None,
+ },
+ }
+ }
+}
+
+#[cfg(all(feature = "olap", feature = "payouts"))]
+#[instrument(skip_all, fields(flow = ?Flow::PayoutsManualUpdate))]
+pub async fn payouts_manual_update(
+ state: web::Data<AppState>,
+ req: HttpRequest,
+ json_payload: web::Json<payout_types::PayoutsManualUpdateRequest>,
+ path: web::Path<id_type::PayoutId>,
+) -> HttpResponse {
+ let flow = Flow::PayoutsManualUpdate;
+ let mut payload = json_payload.into_inner();
+ let payout_id = path.into_inner();
+
+ let locking_action = payload.get_locking_input(flow.clone());
+
+ tracing::Span::current().record("payout_id", payout_id.get_string_repr());
+
+ payload.payout_id = payout_id;
+
+ Box::pin(api::server_wrap(
+ flow,
+ state,
+ &req,
+ payload,
+ |state, _auth, req, _req_state| payouts_manual_update_core(state, req),
+ &auth::AdminApiAuth,
+ locking_action,
+ ))
+ .await
+}
diff --git a/crates/router/src/types/api/payouts.rs b/crates/router/src/types/api/payouts.rs
index 40da5ce60d..a823dd54c4 100644
--- a/crates/router/src/types/api/payouts.rs
+++ b/crates/router/src/types/api/payouts.rs
@@ -5,8 +5,8 @@ pub use api_models::payouts::{
PayoutEnabledPaymentMethodsInfo, PayoutLinkResponse, PayoutListConstraints,
PayoutListFilterConstraints, PayoutListFilters, PayoutListFiltersV2, PayoutListResponse,
PayoutMethodData, PayoutMethodDataResponse, PayoutRequest, PayoutRetrieveBody,
- PayoutRetrieveRequest, PixBankTransfer, RequiredFieldsOverrideRequest, SepaBankTransfer,
- Wallet as WalletPayout,
+ PayoutRetrieveRequest, PayoutsManualUpdateRequest, PixBankTransfer,
+ RequiredFieldsOverrideRequest, SepaBankTransfer, Wallet as WalletPayout,
};
pub use hyperswitch_domain_models::router_flow_types::payouts::{
PoCancel, PoCreate, PoEligibility, PoFulfill, PoQuote, PoRecipient, PoRecipientAccount, PoSync,
diff --git a/crates/router_env/src/logger/types.rs b/crates/router_env/src/logger/types.rs
index 7741b18bff..05b46c8a6a 100644
--- a/crates/router_env/src/logger/types.rs
+++ b/crates/router_env/src/logger/types.rs
@@ -603,6 +603,9 @@ pub enum Flow {
RefundsManualUpdate,
/// Manually update the payment details like status, error code, error message etc.
PaymentsManualUpdate,
+ #[cfg(feature = "payouts")]
+ /// Manually update the payout details like status, error code, error message etc.
+ PayoutsManualUpdate,
/// Dynamic Tax Calcultion
SessionUpdateTaxCalculation,
ProxyConfirmIntent,
diff --git a/crates/storage_impl/src/payouts/payout_attempt.rs b/crates/storage_impl/src/payouts/payout_attempt.rs
index 536320bb55..e94736c134 100644
--- a/crates/storage_impl/src/payouts/payout_attempt.rs
+++ b/crates/storage_impl/src/payouts/payout_attempt.rs
@@ -720,6 +720,21 @@ impl DataModelExt for PayoutAttemptUpdate {
} => DieselPayoutAttemptUpdate::AdditionalPayoutMethodDataUpdate {
additional_payout_method_data,
},
+ Self::ManualUpdate {
+ status,
+ error_code,
+ error_message,
+ unified_code,
+ unified_message,
+ connector_payout_id,
+ } => DieselPayoutAttemptUpdate::ManualUpdate {
+ status,
+ error_code,
+ error_message,
+ unified_code,
+ unified_message,
+ connector_payout_id,
+ },
}
}
diff --git a/crates/storage_impl/src/payouts/payouts.rs b/crates/storage_impl/src/payouts/payouts.rs
index 8a8e73a8e1..fa5f3a1c63 100644
--- a/crates/storage_impl/src/payouts/payouts.rs
+++ b/crates/storage_impl/src/payouts/payouts.rs
@@ -1124,6 +1124,7 @@ impl DataModelExt for PayoutsUpdate {
DieselPayoutsUpdate::AttemptCountUpdate { attempt_count }
}
Self::StatusUpdate { status } => DieselPayoutsUpdate::StatusUpdate { status },
+ Self::ManualUpdate { status } => DieselPayoutsUpdate::ManualUpdate { status },
}
}