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(payment_methods): add update_last_used_at flag (#14881)

Shankar Singh C377d70988808
diff --git a/api-reference/v1/openapi_spec_v1.json b/api-reference/v1/openapi_spec_v1.json
index 9246c5f98a..30e19d68e9 100644
--- a/api-reference/v1/openapi_spec_v1.json
+++ b/api-reference/v1/openapi_spec_v1.json
@@ -35138,6 +35138,11 @@
"PaymentMethodUpdate": {
"type": "object",
"properties": {
+ "update_last_used_at": {
+ "type": "boolean",
+ "example": true,
+ "nullable": true
+ },
"card": {
"allOf": [
{
diff --git a/api-reference/v1/payment-methods/payment-method--update.mdx b/api-reference/v1/payment-methods/payment-method--update.mdx
index c4ff42b87a..41718a1f69 100644
--- a/api-reference/v1/payment-methods/payment-method--update.mdx
+++ b/api-reference/v1/payment-methods/payment-method--update.mdx
@@ -1,3 +1,8 @@
---
openapi: post /payment_methods/{method_id}/update
----
\ No newline at end of file
+---
+
+Pass `"update_last_used_at": true` to set `last_used_at` to the current server UTC time.
+Passing `false`, `null`, or omitting the field leaves the timestamp unchanged.
+The flag can be sent on its own or alongside other payment method updates.
+The response returns the stored `last_used_at` timestamp.
diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.json
index 1a1ba27bb7..c972b1c747 100644
--- a/api-reference/v2/openapi_spec_v2.json
+++ b/api-reference/v2/openapi_spec_v2.json
@@ -25533,6 +25533,11 @@
"PaymentMethodUpdate": {
"type": "object",
"properties": {
+ "update_last_used_at": {
+ "type": "boolean",
+ "example": true,
+ "nullable": true
+ },
"payment_method_data": {
"allOf": [
{
diff --git a/api-reference/v2/payment-methods/payment-method--update-v1.mdx b/api-reference/v2/payment-methods/payment-method--update-v1.mdx
index 4152b11004..a4a3d24e7a 100644
--- a/api-reference/v2/payment-methods/payment-method--update-v1.mdx
+++ b/api-reference/v2/payment-methods/payment-method--update-v1.mdx
@@ -1,3 +1,8 @@
---
openapi: put /v1/payment-methods/{id}/update-saved-payment-method
----
\ No newline at end of file
+---
+
+Pass `"update_last_used_at": true` to set `last_used_at` to the current server UTC time.
+Passing `false`, `null`, or omitting the field leaves the timestamp unchanged.
+The flag can be sent on its own or alongside other payment method updates.
+The response returns the stored `last_used_at` timestamp.
diff --git a/api-reference/v2/payment-methods/payment-method--update.mdx b/api-reference/v2/payment-methods/payment-method--update.mdx
index 7f5f41668f..ef216c79cd 100644
--- a/api-reference/v2/payment-methods/payment-method--update.mdx
+++ b/api-reference/v2/payment-methods/payment-method--update.mdx
@@ -1,3 +1,8 @@
---
openapi: put /v2/payment-methods/{id}/update-saved-payment-method
----
\ No newline at end of file
+---
+
+Pass `"update_last_used_at": true` to set `last_used_at` to the current server UTC time.
+Passing `false`, `null`, or omitting the field leaves the timestamp unchanged.
+The flag can be sent on its own or alongside other payment method updates.
+The response returns the stored `last_used_at` timestamp.
diff --git a/crates/api_models/src/payment_methods.rs b/crates/api_models/src/payment_methods.rs
index c42f75beac..ca5eb60e76 100644
--- a/crates/api_models/src/payment_methods.rs
+++ b/crates/api_models/src/payment_methods.rs
@@ -551,6 +551,9 @@ impl PaymentMethodCreate {
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct PaymentMethodUpdate {
+ #[schema(example = true)]
+ pub update_last_used_at: Option<bool>,
+
/// Card Details
#[schema(example = json!({
"card_number": "4111111145551142",
@@ -570,6 +573,9 @@ pub struct PaymentMethodUpdate {
#[cfg(feature = "v2")]
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, ToSchema)]
pub struct PaymentMethodUpdate {
+ #[schema(example = true)]
+ pub update_last_used_at: Option<bool>,
+
/// Payment method details to be updated for the payment_method
pub payment_method_data: Option<PaymentMethodUpdateData>,
diff --git a/crates/diesel_models/src/payment_method.rs b/crates/diesel_models/src/payment_method.rs
index 833ad9d4e2..62c8d81952 100644
--- a/crates/diesel_models/src/payment_method.rs
+++ b/crates/diesel_models/src/payment_method.rs
@@ -404,6 +404,7 @@ pub enum PaymentMethodUpdate {
last_modified_by: Option<String>,
},
GenericUpdate {
+ last_used_at: Option<PrimitiveDateTime>,
payment_method_data: Option<Encryption>,
status: Option<storage_enums::PaymentMethodStatus>,
locker_id: Option<String>,
@@ -1219,6 +1220,7 @@ impl From<PaymentMethodUpdate> for PaymentMethodUpdateInternal {
compatibility_updated_at: Some(now),
},
PaymentMethodUpdate::GenericUpdate {
+ last_used_at,
payment_method_data,
status,
locker_id,
@@ -1235,7 +1237,7 @@ impl From<PaymentMethodUpdate> for PaymentMethodUpdateInternal {
last_modified_by,
} => Self {
payment_method_data,
- last_used_at: None,
+ last_used_at,
status,
locker_id,
payment_method: None,
diff --git a/crates/hyperswitch_connectors/src/connectors/adyen.rs b/crates/hyperswitch_connectors/src/connectors/adyen.rs
index 8ff07e3106..2549ed735a 100644
--- a/crates/hyperswitch_connectors/src/connectors/adyen.rs
+++ b/crates/hyperswitch_connectors/src/connectors/adyen.rs
@@ -2144,6 +2144,7 @@ impl IncomingWebhook for Adyen {
let year_str = expiry.year();
Ok(Some(api_models::payment_methods::PaymentMethodUpdate {
+ update_last_used_at: Some(true),
card: Some(api_models::payment_methods::CardDetailUpdate {
card_exp_month: Some(month_str),
card_exp_year: Some(year_str),
diff --git a/crates/hyperswitch_domain_models/src/payment_methods.rs b/crates/hyperswitch_domain_models/src/payment_methods.rs
index a6f08c39da..d0f8fcd0b5 100644
--- a/crates/hyperswitch_domain_models/src/payment_methods.rs
+++ b/crates/hyperswitch_domain_models/src/payment_methods.rs
@@ -341,6 +341,7 @@ impl PaymentMethod {
#[cfg(feature = "v2")]
#[derive(Clone, Debug)]
pub struct PaymentMethodUpdate {
+ pub update_last_used_at: Option<bool>,
pub payment_method_data: Option<payment_methods::PaymentMethodUpdateData>,
pub connector_token_details: Option<payment_methods::ConnectorTokenDetails>,
pub network_transaction_id: Option<Secret<String>>,
@@ -355,6 +356,7 @@ pub struct PaymentMethodUpdate {
impl From<payment_methods::PaymentMethodUpdate> for PaymentMethodUpdate {
fn from(value: payment_methods::PaymentMethodUpdate) -> Self {
Self {
+ update_last_used_at: value.update_last_used_at,
payment_method_data: value.payment_method_data,
connector_token_details: value.connector_token_details,
network_transaction_id: value.network_transaction_id,
@@ -409,7 +411,8 @@ impl PaymentMethodUpdate {
}
pub fn is_payment_method_update_required(&self) -> bool {
- self.is_payment_method_metadata_update()
+ self.update_last_used_at == Some(true)
+ || self.is_payment_method_metadata_update()
|| self.connector_token_details.is_some()
|| self.network_transaction_id.is_some()
|| self.acknowledgement_status.is_some()
@@ -458,6 +461,7 @@ impl
Self {
payment_method_data,
+ update_last_used_at: None,
connector_token_details: None,
network_transaction_id: None,
acknowledgement_status: None,
diff --git a/crates/payment_methods/src/client/update.rs b/crates/payment_methods/src/client/update.rs
index 6d9162d61b..b1bad8cbac 100644
--- a/crates/payment_methods/src/client/update.rs
+++ b/crates/payment_methods/src/client/update.rs
@@ -35,6 +35,7 @@ pub struct UpdatePaymentMethodV1Request {
/// V1-facing update payload.
#[derive(Clone, Debug, Serialize)]
pub struct UpdatePaymentMethodV1Payload {
+ pub update_last_used_at: Option<bool>,
/// Payment method details to update.
pub payment_method_data: Option<PaymentMethodUpdateData>,
/// Connector token details to update.
@@ -49,6 +50,7 @@ pub struct UpdatePaymentMethodV1Payload {
/// Modular service update request payload.
#[derive(Clone, Debug, Serialize)]
pub struct ModularPMUpdateRequest {
+ pub update_last_used_at: Option<bool>,
/// Payment method details to update.
pub payment_method_data: Option<PaymentMethodUpdateData>,
/// Connector token details to update.
@@ -161,6 +163,7 @@ impl TryFrom<&UpdatePaymentMethodV1Request> for ModularPMUpdateRequest {
fn try_from(value: &UpdatePaymentMethodV1Request) -> Result<Self, Self::Error> {
Ok(Self {
+ update_last_used_at: value.payload.update_last_used_at,
payment_method_data: value.payload.payment_method_data.clone(),
connector_token_details: value.payload.connector_token_details.clone(),
network_transaction_id: value.payload.network_transaction_id.clone(),
diff --git a/crates/router/src/core/payment_methods.rs b/crates/router/src/core/payment_methods.rs
index dc4f5e7293..62c57e4e41 100644
--- a/crates/router/src/core/payment_methods.rs
+++ b/crates/router/src/core/payment_methods.rs
@@ -3374,6 +3374,7 @@ async fn execute_payment_method_create(
payment_method_subtype,
external_vault_source,
Some(enums::PaymentMethodStatus::New),
+ None,
platform.get_initiator(),
)
.await
@@ -4074,6 +4075,7 @@ pub async fn generate_network_token_for_payment_method(
// network token columns. Its changeset skips `None` fields, so only the network token
// columns (and last_modified) are written.
let pm_update = storage::PaymentMethodUpdate::GenericUpdate {
+ last_used_at: None,
payment_method_data: None,
status: None,
locker_id: None,
@@ -5390,6 +5392,7 @@ pub async fn create_pm_additional_data_update(
payment_method_subtype: Option<common_enums::PaymentMethodType>,
external_vault_source: Option<id_type::MerchantConnectorAccountId>,
status: Option<storage_enums::PaymentMethodStatus>,
+ last_used_at: Option<time::PrimitiveDateTime>,
initiator: Option<&domain::Initiator>,
) -> RouterResult<storage::PaymentMethodUpdate> {
let encrypted_payment_method_data = payment_methods_data
@@ -5425,6 +5428,7 @@ pub async fn create_pm_additional_data_update(
.map(From::from);
let pm_update = storage::PaymentMethodUpdate::GenericUpdate {
+ last_used_at,
// A new payment method is created with inactive state
// It will be marked active after payment succeeds
status,
@@ -8871,6 +8875,9 @@ impl<'a> pm_types::PaymentMethodUpdateHandler<'a> {
false => (vault_request_data, vault_resp),
};
+ let last_used_at =
+ (self.request.update_last_used_at == Some(true)).then(common_utils::date_time::now);
+
let updated_payment_method = match self.insert_promoted_record {
// The record has only ever lived in redis, and already carries the card, the
// fingerprints and the acceptance. Writing the vault id and the connector
@@ -8878,6 +8885,7 @@ impl<'a> pm_types::PaymentMethodUpdateHandler<'a> {
// tokenization is scheduled separately and never arrives here.
true => {
let payment_method = domain::PaymentMethod {
+ last_used_at: last_used_at.unwrap_or(self.payment_method.last_used_at),
locker_id: vault_resp
.as_ref()
.map(|vault_resp| vault_resp.vault_id.clone()),
@@ -8975,6 +8983,7 @@ impl<'a> pm_types::PaymentMethodUpdateHandler<'a> {
None,
None,
pm_status,
+ last_used_at,
self.platform.get_initiator(),
)
.await
diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs
index 3ef24b30c0..cd3ebf5d2b 100644
--- a/crates/router/src/core/payment_methods/cards.rs
+++ b/crates/router/src/core/payment_methods/cards.rs
@@ -2818,53 +2818,44 @@ pub async fn update_customer_payment_method(
if let Some(cs) = &req.client_secret {
let is_client_secret_expired = authenticate_pm_client_secret_and_check_expiry(cs, &pm)?;
- if is_client_secret_expired {
- return Err((errors::ApiErrorResponse::ClientSecretExpired).into());
- };
+ utils::when(is_client_secret_expired, || {
+ Err(errors::ApiErrorResponse::ClientSecretExpired)
+ })
+ .attach_printable("Cannot update payment method with an expired client secret")?;
};
// Currently update is supported only for cards and wallets
- if let Some(card_update) = req.card.clone() {
- if pm.status == enums::PaymentMethodStatus::AwaitingData {
- return Err(report!(errors::ApiErrorResponse::NotSupported {
- message: "Payment method is awaiting data so it cannot be updated".into()
- }));
- }
+ let (pm, response) = if let Some(card_update) = req.card.clone() {
+ utils::when(
+ pm.status == enums::PaymentMethodStatus::AwaitingData,
+ || {
+ Err(errors::ApiErrorResponse::NotSupported {
+ message: "Payment method is awaiting data so it cannot be updated".into(),
+ })
+ },
+ )
+ .attach_printable("Cannot update a payment method awaiting data")?;
- if pm.payment_method_data.is_none() {
- return Err(report!(errors::ApiErrorResponse::GenericNotFoundError {
- message: "payment_method_data not found".to_string()
- }));
- }
+ utils::when(pm.payment_method_data.is_none(), || {
+ Err(errors::ApiErrorResponse::GenericNotFoundError {
+ message: "payment_method_data not found".to_string(),
+ })
+ })
+ .attach_printable("Cannot update card details without payment method data")?;
// Fetch the existing payment method data from db
- let existing_card_data =
- pm.payment_method_data
- .clone()
- .map(|x| x.into_inner().expose())
- .map(
- |value| -> Result<
- PaymentMethodsData,
- error_stack::Report<errors::ApiErrorResponse>,
- > {
- value
- .parse_value::<PaymentMethodsData>("PaymentMethodsData")
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Failed to deserialize payment methods data")
- },
- )
- .transpose()?
- .and_then(|pmd| match pmd {
- PaymentMethodsData::Card(crd) => Some(api::CardDetailFromLocker::from(crd)),
- _ => None,
- })
- .ok_or(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Failed to obtain decrypted card object from db")?;
+ let existing_card_data = deserialize_saved_payment_method_data(&pm)?
+ .and_then(|pmd| match pmd {
+ PaymentMethodsData::Card(crd) => Some(api::CardDetailFromLocker::from(crd)),
+ _ => None,
+ })
+ .ok_or(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to obtain decrypted card object from db")?;
let is_card_updation_required =
validate_payment_method_update(card_update.clone(), existing_card_data.clone());
- let response = if is_card_updation_required {
+ if is_card_updation_required {
// Fetch the existing card data from locker for getting card number
let locker_card_response = Box::pin(get_card_from_locker(
&state,
@@ -2929,7 +2920,7 @@ pub async fn update_customer_payment_method(
.await?;
// Add the updated payment method data to locker
- let (mut add_card_resp, _) = Box::pin(cards.add_card_to_locker(
+ let (add_card_resp, _) = Box::pin(cards.add_card_to_locker(
new_pm.clone(),
&updated_card_details,
&customer_id,
@@ -2998,10 +2989,6 @@ pub async fn update_customer_payment_method(
.map(|last_modified_by| last_modified_by.to_string()),
};
- add_card_resp
- .payment_method_id
- .clone_from(&pm.payment_method_id);
-
let compat_action = super::payment_method_modular_forward_compat_action(
&state,
&pm.merchant_id,
@@ -3010,21 +2997,22 @@ pub async fn update_customer_payment_method(
)
.await;
- db.update_payment_method(
- provider.get_key_store(),
- pm.clone(),
- pm_update,
- provider.get_account().storage_scheme,
- compat_action,
- )
- .await
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Failed to update payment method in db")?;
+ let pm = db
+ .update_payment_method(
+ provider.get_key_store(),
+ pm.clone(),
+ pm_update,
+ provider.get_account().storage_scheme,
+ compat_action,
+ )
+ .await
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to update payment method in db")?;
- api::CustomerPaymentMethodUpdateResponse {
+ let response = api::CustomerPaymentMethodUpdateResponse {
merchant_id: add_card_resp.merchant_id,
customer_id: add_card_resp.customer_id,
- payment_method_id: add_card_resp.payment_method_id,
+ payment_method_id: pm.payment_method_id.clone(),
payment_method: add_card_resp.payment_method,
payment_method_type: add_card_resp.payment_method_type,
#[cfg(feature = "payouts")]
@@ -3036,40 +3024,27 @@ pub async fn update_customer_payment_method(
recurring_enabled: add_card_resp.recurring_enabled,
installment_payment_enabled: add_card_resp.installment_payment_enabled,
payment_experience: add_card_resp.payment_experience,
- last_used_at: add_card_resp.last_used_at,
+ last_used_at: Some(pm.last_used_at),
client_secret: add_card_resp.client_secret,
- }
+ };
+ Ok((pm, response))
} else {
// Return existing payment method data as response without any changes
- api::CustomerPaymentMethodUpdateResponse {
- merchant_id: pm.merchant_id.to_owned(),
- customer_id: pm.customer_id.clone(),
- payment_method_id: pm.payment_method_id.clone(),
- payment_method: pm.get_payment_method_type(),
- payment_method_type: pm.get_payment_method_subtype(),
- #[cfg(feature = "payouts")]
- bank_transfer: None,
- card: Some(existing_card_data),
- wallet: None,
- // since locker is not called, no metadata is merged to payment method metadata
- metadata: pm.get_payment_method_metadata(None),
- created: Some(pm.created_at),
- recurring_enabled: Some(false),
- installment_payment_enabled: Some(false),
- payment_experience: Some(vec![api_models::enums::PaymentExperience::RedirectToUrl]),
- last_used_at: Some(common_utils::date_time::now()),
- client_secret: pm.client_secret.clone(),
- }
- };
-
- Ok(services::ApplicationResponse::Json(response))
- } else if let Some(wallet_update) = req.wallet.clone() {
- if pm.payment_method != Some(common_enums::PaymentMethod::Wallet) {
- return Err((errors::ApiErrorResponse::InvalidRequestData {
- message: "The Payment Method is not wallet".to_string(),
- })
- .into());
+ // since locker is not called, no metadata is merged to payment method metadata
+ let response =
+ customer_payment_method_update_response(&pm, Some(existing_card_data), None);
+ Ok((pm, response))
}
+ } else if let Some(wallet_update) = req.wallet.clone() {
+ utils::when(
+ pm.payment_method != Some(common_enums::PaymentMethod::Wallet),
+ || {
+ Err(errors::ApiErrorResponse::InvalidRequestData {
+ message: "The Payment Method is not wallet".to_string(),
+ })
+ },
+ )
+ .attach_printable("Cannot update wallet details for a non-wallet payment method")?;
let updated_pmd = PaymentMethodsData::WalletDetails(wallet_update);
let key_manager_state = (&state).into();
@@ -3102,7 +3077,7 @@ pub async fn update_customer_payment_method(
let pm = db
.update_payment_method(
provider.get_key_store(),
- pm,
+ pm.clone(),
pm_update,
provider.get_account().storage_scheme,
compat_action,
@@ -3111,30 +3086,86 @@ pub async fn update_customer_payment_method(
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to update payment method in db")?;
- Ok(services::ApplicationResponse::Json(
- api::CustomerPaymentMethodUpdateResponse {
- merchant_id: pm.merchant_id.to_owned(),
- customer_id: pm.customer_id.clone(),
- payment_method_id: pm.payment_method_id.clone(),
- payment_method: pm.get_payment_method_type(),
- payment_method_type: pm.get_payment_method_subtype(),
- #[cfg(feature = "payouts")]
- bank_transfer: None,
- card: None,
- wallet: req.wallet.clone(),
- metadata: pm.metadata,
- created: Some(pm.created_at),
- recurring_enabled: Some(false),
- installment_payment_enabled: Some(false),
- payment_experience: Some(vec![api_models::enums::PaymentExperience::RedirectToUrl]),
- last_used_at: Some(common_utils::date_time::now()),
- client_secret: pm.client_secret.clone(),
- },
- ))
+ let response = customer_payment_method_update_response(&pm, None, req.wallet.clone());
+ Ok((pm, response))
+ } else if req.update_last_used_at.is_some() {
+ let payment_method_data = deserialize_saved_payment_method_data(&pm)?;
+ let (card, wallet) = match payment_method_data {
+ Some(PaymentMethodsData::Card(card)) => {
+ (Some(api::CardDetailFromLocker::from(card)), None)
+ }
+ Some(PaymentMethodsData::WalletDetails(wallet)) => (None, Some(wallet)),
+ _ => (None, None),
+ };
+ let response = customer_payment_method_update_response(&pm, card, wallet);
+ Ok((pm, response))
} else {
Err(report!(errors::ApiErrorResponse::NotSupported {
message: "Payment method update for the given payment method is not supported".into()
}))
+ .attach_printable("No supported payment method update was provided")
+ }?;
+
+ let response = if req.update_last_used_at == Some(true) {
+ let updated_payment_method = update_last_used_at(
+ &pm,
+ &state,
+ provider.get_account().storage_scheme,
+ provider.get_key_store(),
+ )
+ .await
+ .attach_printable("Failed to update saved payment method last_used_at")?;
+ api::CustomerPaymentMethodUpdateResponse {
+ last_used_at: Some(updated_payment_method.last_used_at),
+ ..response
+ }
+ } else {
+ response
+ };
+
+ Ok(services::ApplicationResponse::Json(response))
+}
+
+#[cfg(feature = "v1")]
+fn deserialize_saved_payment_method_data(
+ pm: &domain::PaymentMethod,
+) -> errors::RouterResult<Option<PaymentMethodsData>> {
+ pm.payment_method_data
+ .as_ref()
+ .map(|data| {
+ data.get_inner()
+ .clone()
+ .expose()
+ .parse_value::<PaymentMethodsData>("PaymentMethodsData")
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to deserialize saved payment method data")
+ })
+ .transpose()
+}
+
+#[cfg(feature = "v1")]
+fn customer_payment_method_update_response(
+ pm: &domain::PaymentMethod,
+ card: Option<api::CardDetailFromLocker>,
+ wallet: Option<PaymentMethodDataWalletInfo>,
+) -> api::CustomerPaymentMethodUpdateResponse {
+ api::CustomerPaymentMethodUpdateResponse {
+ merchant_id: pm.merchant_id.clone(),
+ customer_id: pm.customer_id.clone(),
+ payment_method_id: pm.payment_method_id.clone(),
+ payment_method: pm.get_payment_method_type(),
+ payment_method_type: pm.get_payment_method_subtype(),
+ #[cfg(feature = "payouts")]
+ bank_transfer: None,
+ card,
+ wallet,
+ metadata: pm.get_payment_method_metadata(None),
+ created: Some(pm.created_at),
+ recurring_enabled: Some(false),
+ installment_payment_enabled: Some(false),
+ payment_experience: Some(vec![api_models::enums::PaymentExperience::RedirectToUrl]),
+ last_used_at: Some(pm.last_used_at),
+ client_secret: pm.client_secret.clone(),
}
}
@@ -7391,7 +7422,7 @@ pub async fn update_last_used_at(
state: &routes::SessionState,
storage_scheme: MerchantStorageScheme,
key_store: &domain::MerchantKeyStore,
-) -> errors::RouterResult<()> {
+) -> errors::RouterResult<domain::PaymentMethod> {
let update_last_used = storage::PaymentMethodUpdate::LastUsedUpdate {
last_used_at: common_utils::date_time::now(),
};
@@ -7408,9 +7439,7 @@ pub async fn update_last_used_at(
)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Failed to update the last_used_at in db")?;
-
- Ok(())
+ .attach_printable("Failed to update the last_used_at in db")
}
#[cfg(feature = "payouts")]
pub async fn get_bank_from_vault(
diff --git a/crates/router/src/core/payments/operations/payment_response.rs b/crates/router/src/core/payments/operations/payment_response.rs
index 62b4291e0f..527868ad2e 100644
--- a/crates/router/src/core/payments/operations/payment_response.rs
+++ b/crates/router/src/core/payments/operations/payment_response.rs
@@ -324,6 +324,7 @@ where
Some(common_enums::AcknowledgementStatus::Authenticated);
let payload = UpdatePaymentMethodV1Payload {
+ update_last_used_at: Some(true),
payment_method_data,
connector_token_details,
network_transaction_id: network_transaction_id
@@ -342,6 +343,7 @@ where
// #3 - Execute the modular payment-method update call if there is something to be updated
if payload.payment_method_data.is_some()
+ || payload.update_last_used_at == Some(true)
|| payload.connector_token_details.is_some()
|| payload.network_transaction_id.is_some()
|| payload.acknowledgement_status.is_some()
@@ -5054,6 +5056,7 @@ impl<F: Clone> PostUpdateTracker<F, PaymentConfirmData<F>, types::SetupMandateRe
let payment_method_update_request =
api_models::payment_methods::PaymentMethodUpdate {
+ update_last_used_at: Some(true),
payment_method_data: None,
connector_token_details: Some(
connector_token_details_for_payment_method_update,
diff --git a/crates/router/src/workflows/payment_method_modular_backward_compat.rs b/crates/router/src/workflows/payment_method_modular_backward_compat.rs
index 26af6c46de..ffe7a15559 100644
--- a/crates/router/src/workflows/payment_method_modular_backward_compat.rs
+++ b/crates/router/src/workflows/payment_method_modular_backward_compat.rs
@@ -756,6 +756,7 @@ impl BackwardCompatWorkflowBuilder<BackwardDbCompatPrepared> {
if older_locker_id.as_deref() != Some(updated_card_reference.as_str()) {
let pm_update = storage::PaymentMethodUpdate::GenericUpdate {
+ last_used_at: None,
payment_method_data: None,
status: None,
locker_id: Some(updated_card_reference.clone()),