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(manual-update): add amount captured (#13151)

Nithin Nb3f767b2bc69
diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rs
index 34892c3970..1e6900b3ec 100644
--- a/crates/api_models/src/payments.rs
+++ b/crates/api_models/src/payments.rs
@@ -11248,6 +11248,9 @@ pub struct PaymentsManualUpdateRequest {
pub connector_transaction_id: Option<String>,
/// The amount that can be captured on the payment.
pub amount_capturable: Option<MinorUnit>,
+ /// Whether to update amount_captured using amount_to_capture from the attempt.
+ /// When true, amount_captured will be set to amount_to_capture
+ pub update_amount_captured: Option<bool>,
}
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, ToSchema)]
@@ -11271,6 +11274,8 @@ pub struct PaymentsManualUpdateResponse {
pub connector_transaction_id: Option<String>,
/// The amount that can be captured on the payment.
pub amount_capturable: Option<MinorUnit>,
+ /// The amount that has been captured on the payment.
+ pub amount_captured: Option<MinorUnit>,
}
/// Request to manually update payment status from Review state (Dashboard API)
diff --git a/crates/diesel_models/src/payment_intent.rs b/crates/diesel_models/src/payment_intent.rs
index 182340877b..f693d0e62f 100644
--- a/crates/diesel_models/src/payment_intent.rs
+++ b/crates/diesel_models/src/payment_intent.rs
@@ -620,6 +620,7 @@ pub enum PaymentIntentUpdate {
ManualUpdate {
status: Option<storage_enums::IntentStatus>,
updated_by: String,
+ amount_captured: Option<MinorUnit>,
},
SessionResponseUpdate {
tax_details: TaxDetails,
@@ -1834,12 +1835,16 @@ impl From<PaymentIntentUpdate> for PaymentIntentUpdateInternal {
external_surcharge_strategy: None,
external_surcharge_applicable: None,
},
- PaymentIntentUpdate::ManualUpdate { status, updated_by } => Self {
+ PaymentIntentUpdate::ManualUpdate {
+ status,
+ updated_by,
+ amount_captured,
+ } => Self {
status,
updated_by,
amount: None,
currency: None,
- amount_captured: None,
+ amount_captured,
customer_id: None,
return_url: None,
setup_future_usage: None,
diff --git a/crates/hyperswitch_domain_models/src/payments/payment_intent.rs b/crates/hyperswitch_domain_models/src/payments/payment_intent.rs
index 8d4e3281b1..136db9d7bc 100644
--- a/crates/hyperswitch_domain_models/src/payments/payment_intent.rs
+++ b/crates/hyperswitch_domain_models/src/payments/payment_intent.rs
@@ -355,6 +355,7 @@ pub enum PaymentIntentUpdate {
ManualUpdate {
status: Option<common_enums::IntentStatus>,
updated_by: String,
+ amount_captured: Option<MinorUnit>,
},
SessionResponseUpdate {
tax_details: diesel_models::TaxDetails,
@@ -1260,10 +1261,15 @@ impl From<PaymentIntentUpdate> for PaymentIntentUpdateInternal {
shipping_address_id,
..Default::default()
},
- PaymentIntentUpdate::ManualUpdate { status, updated_by } => Self {
+ PaymentIntentUpdate::ManualUpdate {
+ status,
+ updated_by,
+ amount_captured,
+ } => Self {
status,
modified_at: Some(common_utils::date_time::now()),
updated_by,
+ amount_captured,
..Default::default()
},
PaymentIntentUpdate::SessionResponseUpdate {
@@ -1609,9 +1615,15 @@ impl From<PaymentIntentUpdate> for DieselPaymentIntentUpdate {
} => Self::CompleteAuthorizeUpdate {
shipping_address_id,
},
- PaymentIntentUpdate::ManualUpdate { status, updated_by } => {
- Self::ManualUpdate { status, updated_by }
- }
+ PaymentIntentUpdate::ManualUpdate {
+ status,
+ updated_by,
+ amount_captured,
+ } => Self::ManualUpdate {
+ status,
+ updated_by,
+ amount_captured,
+ },
PaymentIntentUpdate::SessionResponseUpdate {
tax_details,
shipping_address_id,
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs
index f9db6a9ed4..7a45ef1302 100644
--- a/crates/router/src/core/payments.rs
+++ b/crates/router/src/core/payments.rs
@@ -13120,6 +13120,7 @@ pub async fn payments_manual_update(
error_reason,
connector_transaction_id,
amount_capturable,
+ update_amount_captured,
} = req;
let key_store = state
.store
@@ -13174,6 +13175,47 @@ pub async fn payments_manual_update(
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
.attach_printable("Error while fetching the payment_intent by payment_id, merchant_id")?;
+ let calculated_amount_captured = if update_amount_captured == Some(true) {
+ if amount_capturable.is_some() {
+ return Err(errors::ApiErrorResponse::InvalidRequestData {
+ message: "amount_capturable cannot be provided when update_amount_captured is true"
+ .to_string(),
+ }
+ .into());
+ }
+
+ if let Some(status) = attempt_status {
+ utils::when(
+ !matches!(
+ status,
+ enums::AttemptStatus::Charged
+ | enums::AttemptStatus::PartialCharged
+ | enums::AttemptStatus::PartialChargedAndChargeable
+ | enums::AttemptStatus::Voided
+ | enums::AttemptStatus::VoidedPostCharge
+ ),
+ || {
+ Err(errors::ApiErrorResponse::InvalidRequestData {
+ message: "update_amount_captured can only be used for terminal statuses with captured funds: Charged, PartialCharged, PartialChargedAndChargeable, Voided, VoidedPostCharge".to_string(),
+ })
+ },
+ )?;
+ }
+ if let Some(existing_captured) = payment_intent.amount_captured {
+ if existing_captured > MinorUnit::zero() {
+ return Err(errors::ApiErrorResponse::InvalidRequestData {
+ message: "Cannot update amount_captured: payment intent already has amount_captured > 0".to_string(),
+ }.into());
+ }
+ }
+ let new_captured_amount = payment_attempt
+ .amount_to_capture
+ .unwrap_or(payment_attempt.net_amount.get_total_amount());
+ Some(new_captured_amount)
+ } else {
+ None
+ };
+
let option_gsm = if let Some(((code, message), connector_name)) = error_code
.as_ref()
.zip(error_message.as_ref())
@@ -13219,24 +13261,29 @@ pub async fn payments_manual_update(
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
.attach_printable("Error while updating the payment_attempt")?;
// If the payment_attempt is active attempt for an intent, update the intent status
- if payment_intent.active_attempt.get_id() == payment_attempt.attempt_id {
- let intent_status = enums::IntentStatus::foreign_from(updated_payment_attempt.status);
- let payment_intent_update = storage::PaymentIntentUpdate::ManualUpdate {
- status: Some(intent_status),
- updated_by: merchant_account.storage_scheme.to_string(),
+ let updated_amount_captured =
+ if payment_intent.active_attempt.get_id() == payment_attempt.attempt_id {
+ let intent_status = enums::IntentStatus::foreign_from(updated_payment_attempt.status);
+ let payment_intent_update = storage::PaymentIntentUpdate::ManualUpdate {
+ status: Some(intent_status),
+ updated_by: merchant_account.storage_scheme.to_string(),
+ amount_captured: calculated_amount_captured,
+ };
+ state
+ .store
+ .update_payment_intent(
+ payment_intent,
+ payment_intent_update,
+ &key_store,
+ merchant_account.storage_scheme,
+ )
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
+ .attach_printable("Error while updating payment_intent")?
+ .amount_captured
+ } else {
+ None
};
- state
- .store
- .update_payment_intent(
- payment_intent,
- payment_intent_update,
- &key_store,
- merchant_account.storage_scheme,
- )
- .await
- .to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
- .attach_printable("Error while updating payment_intent")?;
- }
Ok(services::ApplicationResponse::Json(
api_models::payments::PaymentsManualUpdateResponse {
payment_id: updated_payment_attempt.payment_id,
@@ -13248,6 +13295,7 @@ pub async fn payments_manual_update(
error_reason: updated_payment_attempt.error_reason,
connector_transaction_id: updated_payment_attempt.connector_transaction_id,
amount_capturable: Some(updated_payment_attempt.amount_capturable),
+ amount_captured: updated_amount_captured,
},
))
}
@@ -13354,6 +13402,7 @@ pub async fn payments_manual_status_update(
let intent_update = storage::PaymentIntentUpdate::ManualUpdate {
status: Some(intent_status),
updated_by: merchant_account.storage_scheme.to_string(),
+ amount_captured: None,
};
state