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(modular_payment_method): implement status transition validation flow (#11762)

Shankar Singh C7868313c622a
diff --git a/api-reference/v1/openapi_spec_v1.json b/api-reference/v1/openapi_spec_v1.json
index e19e356e15..1868ca8e4a 100644
--- a/api-reference/v1/openapi_spec_v1.json
+++ b/api-reference/v1/openapi_spec_v1.json
@@ -29022,7 +29022,8 @@
"inactive",
"processing",
"awaiting_data",
- "new"
+ "new",
+ "redacted"
]
},
"PaymentMethodTokenizationDetails": {
diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.json
index 8114426a90..8ba8dc708b 100644
--- a/api-reference/v2/openapi_spec_v2.json
+++ b/api-reference/v2/openapi_spec_v2.json
@@ -22302,7 +22302,8 @@
"inactive",
"processing",
"awaiting_data",
- "new"
+ "new",
+ "redacted"
]
},
"PaymentMethodSubtypeSpecificData": {
diff --git a/crates/common_enums/src/enums.rs b/crates/common_enums/src/enums.rs
index 9d6c16bdf5..f5f1976f4d 100644
--- a/crates/common_enums/src/enums.rs
+++ b/crates/common_enums/src/enums.rs
@@ -2168,6 +2168,8 @@ pub enum PaymentMethodStatus {
AwaitingData,
/// Indicates that the payment method is in new state
New,
+ /// Indicates that the payment method has been redacted/deleted and cannot be used or recovered
+ Redacted,
}
impl From<AttemptStatus> for PaymentMethodStatus {
@@ -2204,6 +2206,19 @@ impl From<AttemptStatus> for PaymentMethodStatus {
}
}
+impl PaymentMethodStatus {
+ /// Checks if transitioning from `self` to `target` status is valid.
+ /// This defines the allowed status transitions for payment method updates.
+ pub fn can_transition_to(self, target: Self) -> bool {
+ match self {
+ Self::Processing | Self::AwaitingData | Self::Redacted => false,
+ Self::Active => false,
+ Self::Inactive => target == Self::Active || target == Self::New,
+ Self::New => target == Self::Active || target == Self::Inactive,
+ }
+ }
+}
+
/// To indicate the type of payment experience that the customer would go through
#[derive(
Eq,
@@ -11038,6 +11053,15 @@ pub enum AcknowledgementStatus {
Failed,
}
+impl From<AcknowledgementStatus> for PaymentMethodStatus {
+ fn from(ack: AcknowledgementStatus) -> Self {
+ match ack {
+ AcknowledgementStatus::Authenticated => Self::Active,
+ AcknowledgementStatus::Failed => Self::Inactive,
+ }
+ }
+}
+
/// Represents the type of retry for a payment attempt
#[derive(
Clone,
diff --git a/crates/hyperswitch_domain_models/src/payment_methods.rs b/crates/hyperswitch_domain_models/src/payment_methods.rs
index ebded81dcb..c5b535fe67 100644
--- a/crates/hyperswitch_domain_models/src/payment_methods.rs
+++ b/crates/hyperswitch_domain_models/src/payment_methods.rs
@@ -335,6 +335,7 @@ pub struct PaymentMethodUpdate {
pub acknowledgement_status: Option<common_enums::AcknowledgementStatus>,
pub network_tokenization: Option<common_types::payment_methods::NetworkTokenization>,
pub source_payment_method_data: Option<crate::vault::PaymentMethodVaultingData>,
+ pub status: Option<common_enums::PaymentMethodStatus>,
}
#[cfg(feature = "v2")]
@@ -347,6 +348,7 @@ impl From<payment_methods::PaymentMethodUpdate> for PaymentMethodUpdate {
acknowledgement_status: value.acknowledgement_status,
network_tokenization: None,
source_payment_method_data: None,
+ status: value.acknowledgement_status.map(|ack| ack.into()),
}
}
}
@@ -376,6 +378,7 @@ impl PaymentMethodUpdate {
|| self.connector_token_details.is_some()
|| self.network_transaction_id.is_some()
|| self.acknowledgement_status.is_some()
+ || self.status.is_some()
}
}
@@ -411,6 +414,7 @@ impl
acknowledgement_status: None,
network_tokenization: req.network_tokenization.clone(),
source_payment_method_data: Some(source_payment_method_data),
+ status: None,
}
}
}
diff --git a/crates/router/src/core/payment_methods.rs b/crates/router/src/core/payment_methods.rs
index 9ce0271b16..db89ac5331 100644
--- a/crates/router/src/core/payment_methods.rs
+++ b/crates/router/src/core/payment_methods.rs
@@ -1304,7 +1304,9 @@ async fn payment_method_resolver(
Box::new(existing_pm),
)))
}
- enums::PaymentMethodStatus::AwaitingData | enums::PaymentMethodStatus::Processing => {
+ enums::PaymentMethodStatus::AwaitingData
+ | enums::PaymentMethodStatus::Processing
+ | enums::PaymentMethodStatus::Redacted => {
// no payment method entry will be found with finger print id and awaiting data or processing state
logger::info!("Payment method is in awaiting data or processing state, no existing payment method entry found with fingerprint id");
Err(
@@ -1518,9 +1520,14 @@ impl PaymentMethodResolver {
None
};
- let update_payment_method_data: DomainPaymentMethodUpdate =
+ let mut update_payment_method_data: DomainPaymentMethodUpdate =
(req, source_payment_method_data).into();
+ // Reset Inactive status to New when customer re-adds the card
+ if payment_method.status == enums::PaymentMethodStatus::Inactive {
+ update_payment_method_data.status = Some(enums::PaymentMethodStatus::New);
+ }
+
let (response, updated_payment_method) = Box::pin(update_payment_method_core(
state,
platform,
@@ -4483,9 +4490,9 @@ pub async fn delete_payment_method_by_record(
profile: &domain::Profile,
payment_method: domain::PaymentMethod,
) -> RouterResult<()> {
- // Soft delete
+ // Soft delete - mark as Redacted (terminal state, no transitions allowed)
let pm_update = storage::PaymentMethodUpdate::StatusAndFingerprintUpdate {
- status: Some(enums::PaymentMethodStatus::Inactive),
+ status: Some(enums::PaymentMethodStatus::Redacted),
last_modified_by: platform
.get_initiator()
.and_then(|initiator| initiator.to_created_by())
@@ -5725,14 +5732,13 @@ impl<'a> pm_types::PaymentMethodUpdateHandler<'a> {
fn validate(&self) -> RouterResult<()> {
let payment_method = &self.payment_method;
when(
- payment_method.status == enums::PaymentMethodStatus::Inactive,
+ payment_method.status == enums::PaymentMethodStatus::Redacted,
|| {
Err(errors::ApiErrorResponse::InvalidRequestData {
- message: "Inactive Payment Method cannot be updated".to_string(),
+ message: "Redacted Payment Method cannot be updated".to_string(),
})
},
)?;
-
Ok(())
}
@@ -5890,15 +5896,15 @@ impl<'a> pm_types::PaymentMethodUpdateHandler<'a> {
return Ok(());
}
- let pm_status = match self.request.acknowledgement_status {
- Some(common_enums::AcknowledgementStatus::Authenticated) => {
- Some(common_enums::PaymentMethodStatus::Active)
- }
- Some(common_enums::AcknowledgementStatus::Failed) => {
- Some(common_enums::PaymentMethodStatus::Inactive)
- }
- None => None,
- };
+ // Get target status from request
+ // Domain model always uses PaymentMethodStatus - transformation from AcknowledgementStatus
+ // happens at the API boundary (From<api_models::PaymentMethodUpdate> impl)
+ let pm_status = self.request.status;
+
+ // Validate status transition
+ // Invalid transitions are silently ignored - status won't be updated
+ let pm_status = pm_status
+ .filter(|&new_status| self.payment_method.status.can_transition_to(new_status));
let db = self.state.store.as_ref();