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): store connector_mandate_details in PaymentMethods table (#3907)

Amisha Prabhatd220e815dc81
diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs
index 0adbca09cd..c0595f14f0 100644
--- a/crates/router/src/core/payment_methods/cards.rs
+++ b/crates/router/src/core/payment_methods/cards.rs
@@ -84,6 +84,7 @@ pub async fn create_payment_method(
customer_acceptance: Option<serde_json::Value>,
payment_method_data: Option<Encryption>,
key_store: &domain::MerchantKeyStore,
+ connector_mandate_details: Option<serde_json::Value>,
) -> errors::CustomResult<storage::PaymentMethod, errors::ApiErrorResponse> {
db.find_customer_by_customer_id_merchant_id(customer_id, merchant_id, key_store)
.await
@@ -101,6 +102,7 @@ pub async fn create_payment_method(
scheme: req.card_network.clone(),
metadata: pm_metadata.map(masking::Secret::new),
payment_method_data,
+ connector_mandate_details,
customer_acceptance: customer_acceptance.map(masking::Secret::new),
..storage::PaymentMethodNew::default()
})
@@ -187,6 +189,7 @@ pub async fn get_or_insert_payment_method(
resp.metadata.clone().map(|val| val.expose()),
None,
locker_id,
+ None,
)
.await
} else {
@@ -372,6 +375,7 @@ pub async fn add_payment_method(
pm_metadata.cloned(),
None,
locker_id,
+ None,
)
.await?;
}
@@ -391,6 +395,7 @@ pub async fn insert_payment_method(
pm_metadata: Option<serde_json::Value>,
customer_acceptance: Option<serde_json::Value>,
locker_id: Option<String>,
+ connector_mandate_details: Option<serde_json::Value>,
) -> errors::RouterResult<diesel_models::PaymentMethod> {
let pm_card_details = resp
.card
@@ -408,6 +413,7 @@ pub async fn insert_payment_method(
customer_acceptance,
pm_data_encrypted,
key_store,
+ connector_mandate_details,
)
.await
}
@@ -1249,12 +1255,16 @@ pub async fn list_payment_methods(
})
.await
.transpose()?;
-
+ let setup_future_usage = payment_intent.as_ref().and_then(|pi| pi.setup_future_usage);
let payment_type = payment_attempt.as_ref().map(|pa| {
let amount = api::Amount::from(pa.amount);
let mandate_type = if pa.mandate_id.is_some() {
Some(api::MandateTransactionType::RecurringMandateTransaction)
- } else if pa.mandate_details.is_some() {
+ } else if pa.mandate_details.is_some()
+ || setup_future_usage
+ .map(|future_usage| future_usage == api_enums::FutureUsage::OffSession)
+ .unwrap_or(false)
+ {
Some(api::MandateTransactionType::NewMandateTransaction)
} else {
None
diff --git a/crates/router/src/core/payments/tokenization.rs b/crates/router/src/core/payments/tokenization.rs
index 943550ef43..c449b76c60 100644
--- a/crates/router/src/core/payments/tokenization.rs
+++ b/crates/router/src/core/payments/tokenization.rs
@@ -1,3 +1,5 @@
+use std::collections::HashMap;
+
use api_models::payment_methods::PaymentMethodsData;
use common_enums::PaymentMethod;
use common_utils::{
@@ -42,7 +44,7 @@ where
FData: mandate::MandateBehaviour,
{
match resp.response {
- Ok(_) => {
+ Ok(responses) => {
let db = &*state.store;
let token_store = state
.conf
@@ -84,6 +86,22 @@ where
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Unable to serialize customer acceptance to value")?;
+ let connector_mandate_details = if resp.request.get_setup_mandate_details().is_none()
+ && resp
+ .request
+ .get_setup_future_usage()
+ .map(|future_usage| future_usage == storage_enums::FutureUsage::OffSession)
+ .unwrap_or(false)
+ {
+ add_connector_mandate_details_in_payment_method(responses, connector)
+ } else {
+ None
+ }
+ .map(|connector_mandate_data| connector_mandate_data.encode_to_value())
+ .transpose()
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Unable to serialize customer acceptance to value")?;
+
let pm_id = if resp.request.get_setup_future_usage().is_some()
&& customer_acceptance.is_some()
{
@@ -194,6 +212,7 @@ where
customer_acceptance,
pm_data_encrypted,
key_store,
+ connector_mandate_details,
)
.await
} else {
@@ -256,6 +275,7 @@ where
resp.metadata.clone().map(|val| val.expose()),
customer_acceptance,
locker_id,
+ connector_mandate_details,
)
.await
} else {
@@ -372,6 +392,7 @@ where
customer_acceptance,
pm_data_encrypted,
key_store,
+ connector_mandate_details,
)
.await?;
}
@@ -623,3 +644,30 @@ pub async fn add_payment_method_token<F: Clone, T: types::Tokenizable + Clone>(
_ => Ok(None),
}
}
+
+fn add_connector_mandate_details_in_payment_method(
+ resp: types::PaymentsResponseData,
+ connector: &api::ConnectorData,
+) -> Option<storage::PaymentsMandateReference> {
+ let mut mandate_details = HashMap::new();
+
+ let connector_mandate_id = match resp {
+ types::PaymentsResponseData::TransactionResponse {
+ mandate_reference, ..
+ } => {
+ if let Some(mandate_ref) = mandate_reference {
+ mandate_ref.connector_mandate_id.clone()
+ } else {
+ None
+ }
+ }
+ _ => None,
+ };
+
+ if let Some(mca_id) = connector.merchant_connector_id.clone() {
+ mandate_details.insert(mca_id, connector_mandate_id);
+ Some(storage::PaymentsMandateReference(mandate_details))
+ } else {
+ None
+ }
+}
diff --git a/crates/router/src/core/payouts/helpers.rs b/crates/router/src/core/payouts/helpers.rs
index 7005e955df..9fd7458155 100644
--- a/crates/router/src/core/payouts/helpers.rs
+++ b/crates/router/src/core/payouts/helpers.rs
@@ -385,6 +385,7 @@ pub async fn save_payout_data_to_locker(
None,
card_details_encrypted,
key_store,
+ None,
)
.await?;
diff --git a/crates/router/src/types/storage/payment_method.rs b/crates/router/src/types/storage/payment_method.rs
index a787a4d932..dee7a8d452 100644
--- a/crates/router/src/types/storage/payment_method.rs
+++ b/crates/router/src/types/storage/payment_method.rs
@@ -1,9 +1,10 @@
+use std::collections::HashMap;
+
use api_models::payment_methods;
pub use diesel_models::payment_method::{
PaymentMethod, PaymentMethodNew, PaymentMethodUpdate, PaymentMethodUpdateInternal,
TokenizeCoreWorkflow,
};
-
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PaymentTokenKind {
@@ -52,3 +53,6 @@ impl PaymentTokenData {
Self::TemporaryGeneric(GenericTokenData { token })
}
}
+
+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
+pub struct PaymentsMandateReference(pub HashMap<String, Option<String>>);