diff --git a/crates/router/src/core/payments/operations/payment_response.rs b/crates/router/src/core/payments/operations/payment_response.rs index 91fdfae63fa..fbc2d2ee461 100644 --- a/crates/router/src/core/payments/operations/payment_response.rs +++ b/crates/router/src/core/payments/operations/payment_response.rs @@ -200,6 +200,7 @@ impl PostUpdateTracker, types::PaymentsAuthor payment_method_billing_address, business_profile, connector_mandate_reference_id.clone(), + merchant_connector_id.clone(), )); let is_connector_mandate = resp.request.customer_acceptance.is_some() @@ -315,6 +316,7 @@ impl PostUpdateTracker, types::PaymentsAuthor payment_method_billing_address.as_ref(), &business_profile, connector_mandate_reference_id, + merchant_connector_id.clone(), )) .await; @@ -1099,6 +1101,7 @@ impl PostUpdateTracker, types::SetupMandateRequestDa payment_method_billing_address, business_profile, connector_mandate_reference_id, + merchant_connector_id.clone(), )) .await?; diff --git a/crates/router/src/core/payments/tokenization.rs b/crates/router/src/core/payments/tokenization.rs index 94f221dd722..84f848ef0ab 100644 --- a/crates/router/src/core/payments/tokenization.rs +++ b/crates/router/src/core/payments/tokenization.rs @@ -83,6 +83,7 @@ pub async fn save_payment_method( payment_method_billing_address: Option<&hyperswitch_domain_models::address::Address>, business_profile: &domain::Profile, mut original_connector_mandate_reference_id: Option, + merchant_connector_id: Option, ) -> RouterResult where FData: mandate::MandateBehaviour + Clone, @@ -458,7 +459,41 @@ where resp.payment_method_id = payment_method_id; let existing_pm = match payment_method { - Ok(pm) => Ok(pm), + Ok(pm) => { + let mandate_details = pm + .connector_mandate_details + .clone() + .map(|val| { + val.parse_value::( + "PaymentsMandateReference", + ) + }) + .transpose() + .change_context(errors::ApiErrorResponse::InternalServerError) + .attach_printable("Failed to deserialize to Payment Mandate Reference ")?; + if let Some((mandate_details, merchant_connector_id)) = + mandate_details.zip(merchant_connector_id) + { + let connector_mandate_details = + update_connector_mandate_details_status( + merchant_connector_id, + mandate_details, + ConnectorMandateStatus::Inactive, + )?; + payment_methods::cards::update_payment_method_connector_mandate_details( + state, + key_store, + db, + pm.clone(), + connector_mandate_details, + merchant_account.storage_scheme, + ) + .await + .change_context(errors::ApiErrorResponse::InternalServerError) + .attach_printable("Failed to add payment method in db")?; + } + Ok(pm) + } Err(err) => { if err.current_context().is_db_not_found() { payment_methods::cards::create_payment_method( @@ -1260,3 +1295,37 @@ pub fn update_connector_mandate_details( Ok(connector_mandate_details) } + +#[cfg(feature = "v1")] +pub fn update_connector_mandate_details_status( + merchant_connector_id: id_type::MerchantConnectorAccountId, + mut payment_mandate_reference: diesel_models::PaymentsMandateReference, + status: ConnectorMandateStatus, +) -> RouterResult> { + let mandate_reference = { + payment_mandate_reference + .entry(merchant_connector_id) + .and_modify(|pm| { + let update_rec = diesel_models::PaymentsMandateReferenceRecord { + connector_mandate_id: pm.connector_mandate_id.clone(), + payment_method_type: pm.payment_method_type, + original_payment_authorized_amount: pm.original_payment_authorized_amount, + original_payment_authorized_currency: pm.original_payment_authorized_currency, + mandate_metadata: pm.mandate_metadata.clone(), + connector_mandate_status: Some(status), + connector_mandate_request_reference_id: pm + .connector_mandate_request_reference_id + .clone(), + }; + *pm = update_rec + }); + Some(payment_mandate_reference) + }; + let connector_mandate_details = mandate_reference + .map(|mandate| mandate.encode_to_value()) + .transpose() + .change_context(errors::ApiErrorResponse::InternalServerError) + .attach_printable("Unable to serialize customer acceptance to value")?; + + Ok(connector_mandate_details) +}