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
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
refactor(core): add connector mandate id in `payments_response` based on merchant config (#5999)
Amisha Prabhataf0d2a8cbdbf
diff --git a/api-reference-v2/openapi_spec.json b/api-reference-v2/openapi_spec.jsonindex cf930f4b4e..99462e95e0 100644--- a/api-reference-v2/openapi_spec.json+++ b/api-reference-v2/openapi_spec.json@@ -14100,6 +14100,11 @@}],"nullable": true+ },+ "connector_mandate_id": {+ "type": "string",+ "description": "Connector Identifier for the payment method",+ "nullable": true}}},@@ -15254,6 +15259,11 @@}],"nullable": true+ },+ "connector_mandate_id": {+ "type": "string",+ "description": "Connector Identifier for the payment method",+ "nullable": true}}},diff --git a/api-reference/openapi_spec.json b/api-reference/openapi_spec.jsonindex 75e240cff7..f5f01c8c49 100644--- a/api-reference/openapi_spec.json+++ b/api-reference/openapi_spec.json@@ -17424,6 +17424,11 @@}],"nullable": true+ },+ "connector_mandate_id": {+ "type": "string",+ "description": "Connector Identifier for the payment method",+ "nullable": true}}},@@ -18578,6 +18583,11 @@}],"nullable": true+ },+ "connector_mandate_id": {+ "type": "string",+ "description": "Connector Identifier for the payment method",+ "nullable": true}}},diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rsindex a2ae6e950f..c1cd995b72 100644--- a/crates/api_models/src/payments.rs+++ b/crates/api_models/src/payments.rs@@ -4217,6 +4217,9 @@ pub struct PaymentsResponse {pub merchant_order_reference_id: Option<String>,/// order tax amount calculated by tax connectorspub order_tax_amount: Option<MinorUnit>,++ /// Connector Identifier for the payment method+ pub connector_mandate_id: Option<String>,}/// Fee information to be charged on the payment being collecteddiff --git a/crates/router/src/core/payments/operations/payment_response.rs b/crates/router/src/core/payments/operations/payment_response.rsindex e0324438b4..538f6d70c4 100644--- a/crates/router/src/core/payments/operations/payment_response.rs+++ b/crates/router/src/core/payments/operations/payment_response.rs@@ -14,7 +14,7 @@ use router_env::{instrument, logger, metrics::add_attributes, tracing};use storage_impl::DataModelExt;use tracing_futures::Instrument;-use super::{Operation, PostUpdateTracker};+use super::{Operation, OperationSessionSetters, PostUpdateTracker};use crate::{connector::utils::PaymentResponseRouterData,consts,@@ -198,7 +198,9 @@ impl<F: Send + Clone> PostUpdateTracker<F, PaymentData<F>, types::PaymentsAuthorif is_legacy_mandate {// Mandate is created on the application side and at the connector.- let (payment_method_id, _payment_method_status) = save_payment_call_future.await?;+ let tokenization::SavePaymentMethodDataResponse {+ payment_method_id, ..+ } = save_payment_call_future.await?;let mandate_id = mandate::mandate_procedure(state,@@ -212,11 +214,22 @@ impl<F: Send + Clone> PostUpdateTracker<F, PaymentData<F>, types::PaymentsAuthor.await?;payment_data.payment_attempt.payment_method_id = payment_method_id;payment_data.payment_attempt.mandate_id = mandate_id;+Ok(())} else if is_connector_mandate {// The mandate is created on connector's end.- let (payment_method_id, _payment_method_status) = save_payment_call_future.await?;+ let tokenization::SavePaymentMethodDataResponse {+ payment_method_id,+ mandate_reference_id,+ ..+ } = save_payment_call_future.await?;+payment_data.payment_attempt.payment_method_id = payment_method_id;++ payment_data.set_mandate_id(api_models::payments::MandateIds {+ mandate_id: None,+ mandate_reference_id,+ });Ok(())} else if should_avoid_saving {if let Some(pm_info) = &payment_data.payment_method_info {@@ -266,7 +279,11 @@ impl<F: Send + Clone> PostUpdateTracker<F, PaymentData<F>, types::PaymentsAuthorif let Err(err) = result {logger::error!("Asynchronously saving card in locker failed : {:?}", err);- } else if let Ok((payment_method_id, _pm_status)) = result {+ } else if let Ok(tokenization::SavePaymentMethodDataResponse {+ payment_method_id,+ ..+ }) = result+ {let payment_attempt_update =storage::PaymentAttemptUpdate::PaymentMethodDetailsUpdate {payment_method_id,@@ -828,23 +845,24 @@ impl<F: Clone> PostUpdateTracker<F, PaymentData<F>, types::SetupMandateRequestDa}})?;let merchant_connector_id = payment_data.payment_attempt.merchant_connector_id.clone();- let (payment_method_id, _payment_method_status) =- Box::pin(tokenization::save_payment_method(- state,- connector_name,- merchant_connector_id.clone(),- save_payment_data,- customer_id.clone(),- merchant_account,- resp.request.payment_method_type,- key_store,- resp.request.amount,- Some(resp.request.currency),- billing_name,- None,- business_profile,- ))- .await?;+ let tokenization::SavePaymentMethodDataResponse {+ payment_method_id, ..+ } = Box::pin(tokenization::save_payment_method(+ state,+ connector_name,+ merchant_connector_id.clone(),+ save_payment_data,+ customer_id.clone(),+ merchant_account,+ resp.request.payment_method_type,+ key_store,+ resp.request.amount,+ Some(resp.request.currency),+ billing_name,+ None,+ business_profile,+ ))+ .await?;let mandate_id = mandate::mandate_procedure(state,diff --git a/crates/router/src/core/payments/tokenization.rs b/crates/router/src/core/payments/tokenization.rsindex ca8c6718aa..6815d2dfc4 100644--- a/crates/router/src/core/payments/tokenization.rs+++ b/crates/router/src/core/payments/tokenization.rs@@ -5,6 +5,7 @@ use std::collections::HashMap;not(feature = "payment_methods_v2")))]use api_models::payment_methods::PaymentMethodsData;+use api_models::payments::{ConnectorMandateReferenceId, MandateReferenceId};use common_enums::PaymentMethod;use common_utils::{crypto::Encryptable,@@ -57,7 +58,11 @@ impl<F, Req: Clone> From<&types::RouterData<F, Req, types::PaymentsResponseData>}}}-+pub struct SavePaymentMethodDataResponse {+ pub payment_method_id: Option<String>,+ pub payment_method_status: Option<common_enums::PaymentMethodStatus>,+ pub mandate_reference_id: Option<MandateReferenceId>,+}#[cfg(all(any(feature = "v1", feature = "v2"),not(feature = "payment_methods_v2")@@ -78,7 +83,7 @@ pub async fn save_payment_method<FData>(billing_name: Option<Secret<String>>,payment_method_billing_address: Option<&api::Address>,business_profile: &domain::Profile,-) -> RouterResult<(Option<String>, Option<common_enums::PaymentMethodStatus>)>+) -> RouterResult<SavePaymentMethodDataResponse>whereFData: mandate::MandateBehaviour + Clone,{@@ -737,9 +742,36 @@ where} else {None};- Ok((pm_id, pm_status))+ let cmid_config = db+ .find_config_by_key_unwrap_or(+ format!("{}_should_show_connector_mandate_id_in_payments_response", merchant_account.get_id().get_string_repr().to_owned()).as_str(),+ Some("false".to_string()),+ )+ .await.map_err(|err| services::logger::error!(message="Failed to fetch the config", connector_mandate_details_population=?err)).ok();++ let mandate_reference_id = match cmid_config {+ Some(config) if config.config == "true" => Some(+ MandateReferenceId::ConnectorMandateId(ConnectorMandateReferenceId {+ connector_mandate_id: connector_mandate_id.clone(),+ payment_method_id: pm_id.clone(),+ update_history: None,+ mandate_metadata: mandate_metadata.clone(),+ }),+ ),+ _ => None,+ };++ Ok(SavePaymentMethodDataResponse {+ payment_method_id: pm_id,+ payment_method_status: pm_status,+ mandate_reference_id,+ })}- Err(_) => Ok((None, None)),+ Err(_) => Ok(SavePaymentMethodDataResponse {+ payment_method_id: None,+ payment_method_status: None,+ mandate_reference_id: None,+ }),}}@@ -761,7 +793,7 @@ pub async fn save_payment_method<FData>(_billing_name: Option<Secret<String>>,_payment_method_billing_address: Option<&api::Address>,_business_profile: &domain::Profile,-) -> RouterResult<(Option<String>, Option<common_enums::PaymentMethodStatus>)>+) -> RouterResult<SavePaymentMethodDataResponse>whereFData: mandate::MandateBehaviour + Clone,{diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rsindex d8c5f88dde..6b84ceb975 100644--- a/crates/router/src/core/payments/transformers.rs+++ b/crates/router/src/core/payments/transformers.rs@@ -1128,6 +1128,17 @@ where.or_else(|| tax.default.map(|a| a.order_tax_amount))})});+ let connector_mandate_id = payment_data.get_mandate_id().and_then(|mandate| {+ mandate+ .mandate_reference_id+ .as_ref()+ .and_then(|mandate_ref| match mandate_ref {+ api_models::payments::MandateReferenceId::ConnectorMandateId(+ connector_mandate_reference_id,+ ) => connector_mandate_reference_id.connector_mandate_id.clone(),+ _ => None,+ })+ });let payments_response = api::PaymentsResponse {payment_id: payment_intent.payment_id,@@ -1227,6 +1238,7 @@ wherefrm_metadata: payment_intent.frm_metadata,merchant_order_reference_id: payment_intent.merchant_order_reference_id,order_tax_amount,+ connector_mandate_id,};services::ApplicationResponse::JsonWithHeaders((payments_response, headers))@@ -1480,6 +1492,7 @@ impl ForeignFrom<(storage::PaymentIntent, storage::PaymentAttempt)> for api::Paycharges: None,frm_metadata: None,order_tax_amount: None,+ connector_mandate_id:None,}}}diff --git a/crates/router/tests/payments.rs b/crates/router/tests/payments.rsindex 11d7b644c0..435e39acae 100644--- a/crates/router/tests/payments.rs+++ b/crates/router/tests/payments.rs@@ -445,6 +445,7 @@ async fn payments_create_core() {frm_metadata: None,merchant_order_reference_id: None,order_tax_amount: None,+ connector_mandate_id: None,};let expected_response =services::ApplicationResponse::JsonWithHeaders((expected_response, vec![]));@@ -702,6 +703,7 @@ async fn payments_create_core_adyen_no_redirect() {frm_metadata: None,merchant_order_reference_id: None,order_tax_amount: None,+ connector_mandate_id: None,},vec![],));diff --git a/crates/router/tests/payments2.rs b/crates/router/tests/payments2.rsindex 28759f7111..b6c22c469d 100644--- a/crates/router/tests/payments2.rs+++ b/crates/router/tests/payments2.rs@@ -206,7 +206,9 @@ async fn payments_create_core() {frm_metadata: None,merchant_order_reference_id: None,order_tax_amount: None,+ connector_mandate_id: None,};+let expected_response =services::ApplicationResponse::JsonWithHeaders((expected_response, vec![]));let actual_response = Box::pin(payments::payments_core::<@@ -471,6 +473,7 @@ async fn payments_create_core_adyen_no_redirect() {frm_metadata: None,merchant_order_reference_id: None,order_tax_amount: None,+ connector_mandate_id: None,},vec![],));