Skip to content

Commit

Permalink
PaymentsMandate to CommonMandate Reference
Browse files Browse the repository at this point in the history
  • Loading branch information
sumanmaji4 committed Dec 19, 2024
1 parent cd80609 commit 4d3f67e
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 51 deletions.
10 changes: 6 additions & 4 deletions crates/diesel_models/src/payment_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub struct PaymentMethod {
pub payment_method_data: Option<Encryption>,
pub locker_id: Option<String>,
pub last_used_at: PrimitiveDateTime,
pub connector_mandate_details: Option<PaymentsMandateReference>,
pub connector_mandate_details: Option<CommonMandateReference>,
pub customer_acceptance: Option<pii::SecretSerdeValue>,
pub status: storage_enums::PaymentMethodStatus,
pub network_transaction_id: Option<String>,
Expand Down Expand Up @@ -128,6 +128,7 @@ impl PaymentMethod {
}
}

/*
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<PaymentMethod>, D::Error>
where
D: Deserializer<'de>,
Expand Down Expand Up @@ -296,6 +297,7 @@ impl PaymentMethod {
Err(de::Error::custom("Faild to deserialize PaymentMethod"))?
}
}
*/

#[cfg(all(feature = "v2", feature = "payment_methods_v2"))]
pub fn get_id(&self) -> &common_utils::id_type::GlobalPaymentMethodId {
Expand Down Expand Up @@ -360,7 +362,7 @@ pub struct PaymentMethodNew {
pub payment_method_data: Option<Encryption>,
pub locker_id: Option<String>,
pub last_used_at: PrimitiveDateTime,
pub connector_mandate_details: Option<PaymentsMandateReference>,
pub connector_mandate_details: Option<CommonMandateReference>,
pub customer_acceptance: Option<pii::SecretSerdeValue>,
pub status: storage_enums::PaymentMethodStatus,
pub network_transaction_id: Option<String>,
Expand Down Expand Up @@ -488,7 +490,7 @@ pub enum PaymentMethodUpdate {
network_token_payment_method_data: Option<Encryption>,
},
ConnectorMandateDetailsUpdate {
connector_mandate_details: Option<PaymentsMandateReference>,
connector_mandate_details: Option<CommonMandateReference>,
},
}

Expand All @@ -513,7 +515,7 @@ pub struct PaymentMethodUpdateInternal {
status: Option<storage_enums::PaymentMethodStatus>,
locker_id: Option<String>,
payment_method_type_v2: Option<storage_enums::PaymentMethod>,
connector_mandate_details: Option<PaymentsMandateReference>,
connector_mandate_details: Option<CommonMandateReference>,
updated_by: Option<String>,
payment_method_subtype: Option<storage_enums::PaymentMethodType>,
last_modified: PrimitiveDateTime,
Expand Down
16 changes: 13 additions & 3 deletions crates/hyperswitch_domain_models/src/merchant_connector_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ common_utils::create_list_wrapper!(
pub fn is_merchant_connector_account_id_in_connector_mandate_details(
&self,
profile_id: Option<&id_type::ProfileId>,
connector_mandate_details: &diesel_models::PaymentsMandateReference,
connector_mandate_details: &diesel_models::CommonMandateReference,
) -> bool {
let mca_ids = self
.iter()
Expand All @@ -624,8 +624,18 @@ common_utils::create_list_wrapper!(
.collect::<std::collections::HashSet<_>>();

connector_mandate_details
.keys()
.any(|mca_id| mca_ids.contains(mca_id))
.payments
.as_ref()
.map_or(false, |payments| {
payments.0.keys().any(|mca_id| mca_ids.contains(mca_id))
}) ||
connector_mandate_details
.payouts
.as_ref()
.map_or(false, |payouts| {
payouts.0.keys().any(|mca_id| mca_ids.contains(mca_id))
})

}
}
);
2 changes: 1 addition & 1 deletion crates/hyperswitch_domain_models/src/payment_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub struct PaymentMethod {
OptionalEncryptableJsonType<api_models::payment_methods::PaymentMethodsData>,
pub locker_id: Option<VaultId>,
pub last_used_at: PrimitiveDateTime,
pub connector_mandate_details: Option<diesel_models::PaymentsMandateReference>,
pub connector_mandate_details: Option<diesel_models::CommonMandateReference>,
pub customer_acceptance: Option<pii::SecretSerdeValue>,
pub status: storage_enums::PaymentMethodStatus,
pub network_transaction_id: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/core/payment_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ pub async fn create_payment_method_in_db(
api::payment_methods::PaymentMethodsData,
>,
key_store: &domain::MerchantKeyStore,
connector_mandate_details: Option<diesel_models::PaymentsMandateReference>,
connector_mandate_details: Option<diesel_models::CommonMandateReference>,
status: Option<enums::PaymentMethodStatus>,
network_transaction_id: Option<String>,
storage_scheme: enums::MerchantStorageScheme,
Expand Down
10 changes: 4 additions & 6 deletions crates/router/src/core/payment_methods/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2829,7 +2829,7 @@ pub async fn update_payment_method_connector_mandate_details(
key_store: &domain::MerchantKeyStore,
db: &dyn db::StorageInterface,
pm: domain::PaymentMethod,
connector_mandate_details: Option<diesel_models::PaymentsMandateReference>,
connector_mandate_details: Option<diesel_models::CommonMandateReference>,
storage_scheme: MerchantStorageScheme,
) -> errors::CustomResult<(), errors::VaultError> {
let pm_update = payment_method::PaymentMethodUpdate::ConnectorMandateDetailsUpdate {
Expand Down Expand Up @@ -4962,9 +4962,7 @@ pub async fn list_customer_payment_method(
.connector_mandate_details
.clone()
.map(|val| {
val.parse_value::<diesel_models::PaymentsMandateReference>(
"PaymentsMandateReference",
)
val.parse_value::<diesel_models::CommonMandateReference>("CommonMandateReference")
})
.transpose()
.change_context(errors::ApiErrorResponse::InternalServerError)
Expand Down Expand Up @@ -5241,7 +5239,7 @@ pub async fn get_mca_status(
profile_id: Option<id_type::ProfileId>,
merchant_id: &id_type::MerchantId,
is_connector_agnostic_mit_enabled: bool,
connector_mandate_details: Option<payment_method::PaymentsMandateReference>,
connector_mandate_details: Option<payment_method::CommonMandateReference>,
network_transaction_id: Option<&String>,
) -> errors::RouterResult<bool> {
if is_connector_agnostic_mit_enabled && network_transaction_id.is_some() {
Expand Down Expand Up @@ -5279,7 +5277,7 @@ pub async fn get_mca_status(
profile_id: Option<id_type::ProfileId>,
merchant_id: &id_type::MerchantId,
is_connector_agnostic_mit_enabled: bool,
connector_mandate_details: Option<&payment_method::PaymentsMandateReference>,
connector_mandate_details: Option<&payment_method::CommonMandateReference>,
network_transaction_id: Option<&String>,
merchant_connector_accounts: &domain::MerchantConnectorAccounts,
) -> bool {
Expand Down
28 changes: 18 additions & 10 deletions crates/router/src/core/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5700,16 +5700,24 @@ pub async fn decide_connector_for_normal_or_recurring_payment<F: Clone, D>(
where
D: OperationSessionGetters<F> + OperationSessionSetters<F> + Send + Sync + Clone,
{
let connector_mandate_details = &payment_method_info
.connector_mandate_details
.clone()
.map(|details| {
details
.parse_value::<diesel_models::PaymentsMandateReference>("connector_mandate_details")
})
.transpose()
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("unable to deserialize connector mandate details")?;
// let connector_mandate_details = &payment_method_info
// .connector_mandate_details
// .clone()
// .map(|details| {
// details
// .parse_value::<diesel_models::PaymentsMandateReference>("connector_mandate_details")
// })
// .transpose()
// .change_context(errors::ApiErrorResponse::InternalServerError)
// .attach_printable("unable to deserialize connector mandate details")?;

let connector_common_mandate_details = storage::PaymentMethod::get_common_mandate_reference(
payment_method_info.connector_mandate_details.clone(),
)
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to get the common mandate reference")?;

let connector_mandate_details = connector_common_mandate_details.payments;

let mut connector_choice = None;

Expand Down
10 changes: 6 additions & 4 deletions crates/router/src/core/payments/operations/payment_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1591,8 +1591,8 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
.connector_mandate_details
.clone()
.map(|val| {
val.parse_value::<diesel_models::PaymentsMandateReference>(
"PaymentsMandateReference",
val.parse_value::<diesel_models::CommonMandateReference>(
"CommonMandateReference",
)
})
.transpose()
Expand All @@ -1609,9 +1609,11 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
// check if the mandate has not already been set to active
if !mandate_details
.as_ref()
.map(|payment_mandate_reference| {
.map(|common_mandate_reference| {

payment_mandate_reference.0.get(&mca_id)
common_mandate_reference.payments
.as_ref()
.and_then(|payments| payments.0.get(&mca_id))
.map(|payment_mandate_reference_record| payment_mandate_reference_record.connector_mandate_status == Some(common_enums::ConnectorMandateStatus::Active))
.unwrap_or(false)
})
Expand Down
53 changes: 36 additions & 17 deletions crates/router/src/core/payments/tokenization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ pub fn add_connector_mandate_details_in_payment_method(
connector_mandate_id: Option<String>,
mandate_metadata: Option<Secret<serde_json::Value>>,
connector_mandate_request_reference_id: Option<String>,
) -> Option<diesel_models::PaymentsMandateReference> {
) -> Option<diesel_models::CommonMandateReference> {
let mut mandate_details = HashMap::new();

if let Some((mca_id, connector_mandate_id)) =
Expand All @@ -1194,7 +1194,11 @@ pub fn add_connector_mandate_details_in_payment_method(
connector_mandate_request_reference_id,
},
);
Some(diesel_models::PaymentsMandateReference(mandate_details))
// Some(diesel_models::PaymentsMandateReference(mandate_details))
Some(diesel_models::CommonMandateReference {
payments: Some(diesel_models::PaymentsMandateReference(mandate_details)),
payouts: None,
})
} else {
None
}
Expand All @@ -1203,7 +1207,7 @@ pub fn add_connector_mandate_details_in_payment_method(
#[allow(clippy::too_many_arguments)]
#[cfg(feature = "v1")]
pub fn update_connector_mandate_details(
mandate_details: Option<diesel_models::PaymentsMandateReference>,
mandate_details: Option<diesel_models::CommonMandateReference>,
payment_method_type: Option<storage_enums::PaymentMethodType>,
authorized_amount: Option<i64>,
authorized_currency: Option<storage_enums::Currency>,
Expand All @@ -1213,7 +1217,7 @@ pub fn update_connector_mandate_details(
connector_mandate_request_reference_id: Option<String>,
) -> RouterResult<Option<serde_json::Value>> {
let mandate_reference = match mandate_details {
Some(mut payment_mandate_reference) => {
Some(mut common_mandate_reference) => {
if let Some((mca_id, connector_mandate_id)) =
merchant_connector_id.clone().zip(connector_mandate_id)
{
Expand All @@ -1228,19 +1232,34 @@ pub fn update_connector_mandate_details(
.clone(),
};

payment_mandate_reference
.entry(mca_id)
.and_modify(|pm| *pm = updated_record)
.or_insert(diesel_models::PaymentsMandateReferenceRecord {
connector_mandate_id,
payment_method_type,
original_payment_authorized_amount: authorized_amount,
original_payment_authorized_currency: authorized_currency,
mandate_metadata: mandate_metadata.clone(),
connector_mandate_status: Some(ConnectorMandateStatus::Active),
connector_mandate_request_reference_id,
});
Some(payment_mandate_reference)
if let Some(payments_mandate_reference) = common_mandate_reference.payments.as_mut()
{
payments_mandate_reference
.entry(mca_id)
.and_modify(|pm| *pm = updated_record)
.or_insert(diesel_models::PaymentsMandateReferenceRecord {
connector_mandate_id,
payment_method_type,
original_payment_authorized_amount: authorized_amount,
original_payment_authorized_currency: authorized_currency,
mandate_metadata: mandate_metadata.clone(),
connector_mandate_status: Some(ConnectorMandateStatus::Active),
connector_mandate_request_reference_id,
});
}
// common_mandate_reference
// .entry(mca_id)
// .and_modify(|pm| *pm = updated_record)
// .or_insert(diesel_models::PaymentsMandateReferenceRecord {
// connector_mandate_id,
// payment_method_type,
// original_payment_authorized_amount: authorized_amount,
// original_payment_authorized_currency: authorized_currency,
// mandate_metadata: mandate_metadata.clone(),
// connector_mandate_status: Some(ConnectorMandateStatus::Active),
// connector_mandate_request_reference_id,
// });
Some(common_mandate_reference)
} else {
None
}
Expand Down
12 changes: 7 additions & 5 deletions crates/router/src/core/webhooks/incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1841,8 +1841,8 @@ async fn update_connector_mandate_details(
.connector_mandate_details
.clone()
.map(|val| {
val.parse_value::<diesel_models::PaymentsMandateReference>(
"PaymentsMandateReference",
val.parse_value::<diesel_models::CommonMandateReference>(
"CommonMandateReference",
)
})
.transpose()
Expand All @@ -1856,8 +1856,10 @@ async fn update_connector_mandate_details(

if mandate_details
.as_ref()
.map(|details: &diesel_models::PaymentsMandateReference| {
!details.0.contains_key(&merchant_connector_account_id)
.map(|details: &diesel_models::CommonMandateReference| {
!details.payments.as_ref().map_or(false, |payments| {
payments.0.contains_key(&merchant_connector_account_id)
})
})
.unwrap_or(true)
{
Expand Down Expand Up @@ -1943,7 +1945,7 @@ async fn update_connector_mandate_details(
fn insert_mandate_details(
payment_attempt: &PaymentAttempt,
webhook_mandate_details: &hyperswitch_domain_models::router_flow_types::ConnectorMandateDetails,
payment_method_mandate_details: Option<diesel_models::PaymentsMandateReference>,
payment_method_mandate_details: Option<diesel_models::CommonMandateReference>,
) -> CustomResult<Option<serde_json::Value>, errors::ApiErrorResponse> {
let (mandate_metadata, connector_mandate_request_reference_id) = payment_attempt
.connector_mandate_detail
Expand Down

0 comments on commit 4d3f67e

Please sign in to comment.