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(core): fix amount capturable in payments response (#1437)

Prasunna Soppa5bc1aaba5945
diff --git a/crates/router/src/core/payments/operations/payment_response.rs b/crates/router/src/core/payments/operations/payment_response.rs
index d0b171267d..6b5a6d19a5 100644
--- a/crates/router/src/core/payments/operations/payment_response.rs
+++ b/crates/router/src/core/payments/operations/payment_response.rs
@@ -284,7 +284,7 @@ impl<F: Clone> PostUpdateTracker<F, PaymentData<F>, types::CompleteAuthorizeData
}
}
-async fn payment_response_update_tracker<F: Clone, T>(
+async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
db: &dyn StorageInterface,
_payment_id: &api::PaymentIdType,
mut payment_data: PaymentData<F>,
@@ -428,10 +428,11 @@ async fn payment_response_update_tracker<F: Clone, T>(
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?,
None => payment_data.connector_response,
};
+ let amount = router_data.request.get_capture_amount();
let amount_captured = router_data.amount_captured.or_else(|| {
if router_data.status == enums::AttemptStatus::Charged {
- Some(payment_data.payment_intent.amount)
+ amount
} else {
None
}
diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs
index aba49974cf..95557c5cda 100644
--- a/crates/router/src/core/payments/transformers.rs
+++ b/crates/router/src/core/payments/transformers.rs
@@ -361,13 +361,15 @@ where
})
.transpose()
.unwrap_or_default();
+ let amount_captured = payment_intent.amount_captured.unwrap_or_default();
+ let amount_capturable = Some(payment_attempt.amount - amount_captured);
services::ApplicationResponse::Json(
response
.set_payment_id(Some(payment_attempt.payment_id))
.set_merchant_id(Some(payment_attempt.merchant_id))
.set_status(payment_intent.status.foreign_into())
.set_amount(payment_attempt.amount)
- .set_amount_capturable(None)
+ .set_amount_capturable(amount_capturable)
.set_amount_received(payment_intent.amount_captured)
.set_connector(routed_through)
.set_client_secret(payment_intent.client_secret.map(masking::Secret::new))
diff --git a/crates/router/src/types.rs b/crates/router/src/types.rs
index 228a4d7b9a..186dd2791a 100644
--- a/crates/router/src/types.rs
+++ b/crates/router/src/types.rs
@@ -347,6 +347,34 @@ pub struct AccessTokenRequestData {
// Add more keys if required
}
+pub trait Capturable {
+ fn get_capture_amount(&self) -> Option<i64> {
+ Some(0)
+ }
+}
+
+impl Capturable for PaymentsAuthorizeData {
+ fn get_capture_amount(&self) -> Option<i64> {
+ Some(self.amount)
+ }
+}
+
+impl Capturable for PaymentsCaptureData {
+ fn get_capture_amount(&self) -> Option<i64> {
+ Some(self.amount_to_capture)
+ }
+}
+
+impl Capturable for CompleteAuthorizeData {
+ fn get_capture_amount(&self) -> Option<i64> {
+ Some(self.amount)
+ }
+}
+impl Capturable for VerifyRequestData {}
+impl Capturable for PaymentsCancelData {}
+impl Capturable for PaymentsSessionData {}
+impl Capturable for PaymentsSyncData {}
+
pub struct AddAccessTokenResult {
pub access_token_result: Result<Option<AccessToken>, ErrorResponse>,
pub connector_supports_access_token: bool,