Skip to content

Commit

Permalink
clippy fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sumanmaji4 committed Dec 20, 2024
1 parent bfa13fc commit 2535ab6
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 195 deletions.
111 changes: 106 additions & 5 deletions crates/api_models/src/payment_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl From<PaymentMethodIntentConfirmInternal> for PaymentMethodIntentConfirm {
}
}
}
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
#[derive(Debug, serde::Serialize, Clone)]
/// This struct is only used by and internal api to migrate payment method
pub struct PaymentMethodMigrate {
/// Merchant id
Expand Down Expand Up @@ -283,7 +283,7 @@ pub struct PaymentMethodMigrate {
pub billing: Option<payments::Address>,

/// The connector mandate details of the payment method
pub connector_mandate_details: Option<PaymentsMandateReference>,
pub connector_mandate_details: Option<CommonMandateReference>,

// The CIT (customer initiated transaction) transaction id associated with the payment method
pub network_transaction_id: Option<String>,
Expand All @@ -307,11 +307,21 @@ pub struct PaymentMethodMigrateResponse {
pub network_transaction_id_migrated: Option<bool>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
pub struct PaymentsMandateReference(
pub HashMap<id_type::MerchantConnectorAccountId, PaymentsMandateReferenceRecord>,
);

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PayoutsMandateReference(
pub HashMap<id_type::MerchantConnectorAccountId, PayoutsMandateReferenceRecord>,
);

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PayoutsMandateReferenceRecord {
pub transfer_method_id: Option<String>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PaymentsMandateReferenceRecord {
pub connector_mandate_id: String,
Expand All @@ -320,6 +330,95 @@ pub struct PaymentsMandateReferenceRecord {
pub original_payment_authorized_currency: Option<common_enums::Currency>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CommonMandateReference {
pub payments: Option<PaymentsMandateReference>,
pub payouts: Option<PayoutsMandateReference>,
}

impl<'de> serde::Deserialize<'de> for PaymentMethodMigrate {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Debug, serde::Deserialize)]
struct InnerPaymentMethodMigrate {
pub merchant_id: id_type::MerchantId,
pub payment_method: Option<api_enums::PaymentMethod>,
pub payment_method_type: Option<api_enums::PaymentMethodType>,
pub payment_method_issuer: Option<String>,
pub payment_method_issuer_code: Option<api_enums::PaymentMethodIssuerCode>,
pub card: Option<MigrateCardDetail>,
pub network_token: Option<MigrateNetworkTokenDetail>,
pub metadata: Option<pii::SecretSerdeValue>,
pub customer_id: Option<id_type::CustomerId>,
pub card_network: Option<String>,
pub bank_transfer: Option<payouts::Bank>,
pub wallet: Option<payouts::Wallet>,
pub payment_method_data: Option<PaymentMethodCreateData>,
pub billing: Option<payments::Address>,
pub connector_mandate_details: Option<serde_json::Value>,
pub network_transaction_id: Option<String>,
}

let inner = InnerPaymentMethodMigrate::deserialize(deserializer)?;

let connector_mandate_details =
if let Some(connector_mandate_value) = inner.connector_mandate_details {
if let Ok(common_mandate) = serde_json::from_value::<CommonMandateReference>(
connector_mandate_value.clone(),
) {
Some(common_mandate)
} else if let Ok(payment_mandate_record) =
serde_json::from_value::<PaymentsMandateReference>(connector_mandate_value)
{
Some(CommonMandateReference {
payments: Some(payment_mandate_record),
payouts: None,
})
} else {
return Err(de::Error::custom("Faild to deserialize PaymentMethod_V2"));

Check warning on line 380 in crates/api_models/src/payment_methods.rs

View workflow job for this annotation

GitHub Actions / Spell check

"Faild" should be "Failed" or "Fail" or "Fails".
}
} else {
None
};

Ok(Self {
merchant_id: inner.merchant_id,
payment_method: inner.payment_method,
payment_method_type: inner.payment_method_type,
payment_method_issuer: inner.payment_method_issuer,
payment_method_issuer_code: inner.payment_method_issuer_code,
card: inner.card,
network_token: inner.network_token,
metadata: inner.metadata,
customer_id: inner.customer_id,
card_network: inner.card_network,
bank_transfer: inner.bank_transfer,
wallet: inner.wallet,
payment_method_data: inner.payment_method_data,
billing: inner.billing,
connector_mandate_details,
network_transaction_id: inner.network_transaction_id,
})
}
}

pub fn convert_to_payments_reference(
common_mandate: Option<CommonMandateReference>,
) -> Option<PaymentsMandateReference> {
common_mandate.and_then(|cm| cm.payments)
}

pub fn convert_to_common_reference(
payments_reference: Option<PaymentsMandateReference>,
) -> Option<CommonMandateReference> {
payments_reference.map(|payments| CommonMandateReference {
payments: Some(payments),
payouts: None,
})
}

#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_methods_v2")
Expand Down Expand Up @@ -353,7 +452,9 @@ impl PaymentMethodCreate {
payment_method_issuer_code: payment_method_migrate.payment_method_issuer_code,
metadata: payment_method_migrate.metadata.clone(),
payment_method_data: payment_method_migrate.payment_method_data.clone(),
connector_mandate_details: payment_method_migrate.connector_mandate_details.clone(),
connector_mandate_details: convert_to_payments_reference(
payment_method_migrate.connector_mandate_details.clone(),
),
client_secret: None,
billing: payment_method_migrate.billing.clone(),
card: card_details,
Expand Down Expand Up @@ -2359,7 +2460,7 @@ impl
}),
email: record.email,
}),
connector_mandate_details,
connector_mandate_details: convert_to_common_reference(connector_mandate_details),
metadata: None,
payment_method_issuer_code: None,
card_network: None,
Expand Down
4 changes: 0 additions & 4 deletions crates/common_utils/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ macro_rules! impl_to_sql_from_sql_json {
};
}





mod id_type {
/// Defines an ID type.
#[macro_export]
Expand Down
158 changes: 16 additions & 142 deletions crates/diesel_models/src/payment_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ use std::collections::HashMap;
use common_enums::MerchantStorageScheme;
use common_utils::{encryption::Encryption, errors::ParsingError, pii};
use diesel::{AsChangeset, Identifiable, Insertable, Queryable, Selectable};
use error_stack::report;
#[cfg(all(
any(feature = "v1", feature = "v2"),
not(feature = "payment_methods_v2")
))]
use masking::{ExposeInterface, Secret};
use serde::{de, Deserialize, Deserializer, Serialize};
use serde::{Deserialize, Serialize};
use time::PrimitiveDateTime;
use error_stack::report;
// use diesel::{
// backend::Backend,
// deserialize,
// deserialize::FromSql,
// // serialize::{Output, ToSql},
// };

#[cfg(all(
any(feature = "v1", feature = "v2"),
Expand Down Expand Up @@ -135,129 +129,6 @@ impl PaymentMethod {
}
}


#[cfg(all(feature = "v2", feature = "payment_methods_v2"))]
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Self>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(serde::Deserialize, Debug)]
pub struct __InnerPaymentMethodData {
pub customer_id: common_utils::id_type::GlobalCustomerId,
pub merchant_id: common_utils::id_type::MerchantId,
pub created_at: PrimitiveDateTime,
pub last_modified: PrimitiveDateTime,
pub payment_method_data: Option<Encryption>,
pub locker_id: Option<String>,
pub last_used_at: PrimitiveDateTime,
pub connector_mandate_details: Option<serde_json::Value>,
pub customer_acceptance: Option<pii::SecretSerdeValue>,
pub status: storage_enums::PaymentMethodStatus,
pub network_transaction_id: Option<String>,
pub client_secret: Option<String>,
pub payment_method_billing_address: Option<Encryption>,
pub updated_by: Option<String>,
pub locker_fingerprint_id: Option<String>,
pub payment_method_type_v2: Option<storage_enums::PaymentMethod>,
pub payment_method_subtype: Option<storage_enums::PaymentMethodType>,
pub id: common_utils::id_type::GlobalPaymentMethodId,
pub version: common_enums::ApiVersion,
pub network_token_requestor_reference_id: Option<String>,
pub network_token_locker_id: Option<String>,
pub network_token_payment_method_data: Option<Encryption>,
pub transaction_flow: Option<storage_enums::PaymentDirection>,
}

let mut deserialize_to_inner = __InnerPaymentMethodData::deserialize(deserializer)?;

if let Some(connector_mandate_details) = deserialize_to_inner.connector_mandate_details {
match serde_json::from_value::<CommonMandateReference>(
connector_mandate_details.clone(),
) {
Ok(common_mandate_reference) => {
// let common_mandate_reference_value = serde_json::to_value(
// common_mandate_reference,
// )
// .map_err(|serde_json_error| de::Error::custom(serde_json_error.to_string()))?;

// deserialize_to_inner.connector_mandate_details =
// Some(common_mandate_reference);

Ok(Some(Self {
customer_id: deserialize_to_inner.customer_id,
merchant_id: deserialize_to_inner.merchant_id,
created_at: deserialize_to_inner.created_at,
last_modified: deserialize_to_inner.last_modified,
payment_method_data: deserialize_to_inner.payment_method_data,
locker_id: deserialize_to_inner.locker_id,
last_used_at: deserialize_to_inner.last_used_at,
connector_mandate_details: Some(common_mandate_reference),
customer_acceptance: deserialize_to_inner.customer_acceptance,
status: deserialize_to_inner.status,
network_transaction_id: deserialize_to_inner.network_transaction_id,
client_secret: deserialize_to_inner.client_secret,
payment_method_billing_address: deserialize_to_inner.payment_method_billing_address,
updated_by: deserialize_to_inner.updated_by,
locker_fingerprint_id: deserialize_to_inner.locker_fingerprint_id,
payment_method_type_v2: deserialize_to_inner.payment_method_type_v2,
payment_method_subtype: deserialize_to_inner.payment_method_subtype,
id: deserialize_to_inner.id,
version: deserialize_to_inner.version,
network_token_requestor_reference_id: deserialize_to_inner.network_token_requestor_reference_id,
network_token_locker_id: deserialize_to_inner.network_token_locker_id,
network_token_payment_method_data: deserialize_to_inner.network_token_payment_method_data,
transaction_flow: deserialize_to_inner.transaction_flow,
}))
}
Err(_) => {
match serde_json::from_value::<PaymentsMandateReference>(
connector_mandate_details,
) {
Ok(payment_mandate_reference) => {
let common_mandate_reference = CommonMandateReference {
payments: Some(payment_mandate_reference),
payouts: None,
};

// deserialize_to_inner.connector_mandate_details =
// Some(common_mandate_reference);

Ok(Some(Self {
customer_id: deserialize_to_inner.customer_id,
merchant_id: deserialize_to_inner.merchant_id,
created_at: deserialize_to_inner.created_at,
last_modified: deserialize_to_inner.last_modified,
payment_method_data: deserialize_to_inner.payment_method_data,
locker_id: deserialize_to_inner.locker_id,
last_used_at: deserialize_to_inner.last_used_at,
connector_mandate_details: Some(common_mandate_reference),
customer_acceptance: deserialize_to_inner.customer_acceptance,
status: deserialize_to_inner.status,
network_transaction_id: deserialize_to_inner.network_transaction_id,
client_secret: deserialize_to_inner.client_secret,
payment_method_billing_address: deserialize_to_inner.payment_method_billing_address,
updated_by: deserialize_to_inner.updated_by,
locker_fingerprint_id: deserialize_to_inner.locker_fingerprint_id,
payment_method_type_v2: deserialize_to_inner.payment_method_type_v2,
payment_method_subtype: deserialize_to_inner.payment_method_subtype,
id: deserialize_to_inner.id,
version: deserialize_to_inner.version,
network_token_requestor_reference_id: deserialize_to_inner.network_token_requestor_reference_id,
network_token_locker_id: deserialize_to_inner.network_token_locker_id,
network_token_payment_method_data: deserialize_to_inner.network_token_payment_method_data,
transaction_flow: deserialize_to_inner.transaction_flow,
}))
}
Err(_) => Err(de::Error::custom("Faild to deserialize PaymentMethod_V2"))?,
}
}
}
} else {
Err(de::Error::custom("Faild to deserialize PaymentMethod_V2"))?
}
}
//*/

#[cfg(all(feature = "v2", feature = "payment_methods_v2"))]
pub fn get_id(&self) -> &common_utils::id_type::GlobalPaymentMethodId {
&self.id
Expand Down Expand Up @@ -1168,17 +1039,13 @@ impl std::ops::DerefMut for PayoutsMandateReference {
}
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, diesel::AsExpression)]
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize, diesel::AsExpression)]
#[diesel(sql_type = diesel::sql_types::Jsonb)]
// #[derive(Debug, Clone, Serialize, Deserialize, diesel::AsExpression, diesel::FromSqlRow)]
// #[diesel(sql_type = Nullable<diesel::sql_types::Jsonb>)]
pub struct CommonMandateReference {
pub payments: Option<PaymentsMandateReference>,
pub payouts: Option<PayoutsMandateReference>,
}

// common_utils::impl_to_sql_from_sql_json!(CommonMandateReference);

impl diesel::serialize::ToSql<diesel::sql_types::Jsonb, diesel::pg::Pg> for CommonMandateReference {
fn to_sql<'b>(
&'b self,
Expand All @@ -1192,13 +1059,17 @@ impl diesel::serialize::ToSql<diesel::sql_types::Jsonb, diesel::pg::Pg> for Comm
}
}

impl<DB: diesel::backend::Backend> diesel::deserialize::FromSql<diesel::sql_types::Jsonb, DB> for CommonMandateReference
impl<DB: diesel::backend::Backend> diesel::deserialize::FromSql<diesel::sql_types::Jsonb, DB>
for CommonMandateReference
where
serde_json::Value: diesel::deserialize::FromSql<diesel::sql_types::Jsonb, DB>,
{
fn from_sql(bytes: DB::RawValue<'_>) -> diesel::deserialize::Result<Self> {
// Deserialize into a generic serde_json::Value first
let value = <serde_json::Value as diesel::deserialize::FromSql<diesel::sql_types::Jsonb, DB>>::from_sql(bytes)?;
let value = <serde_json::Value as diesel::deserialize::FromSql<
diesel::sql_types::Jsonb,
DB,
>>::from_sql(bytes)?;

// Try to directly deserialize into CommonMandateReference
if let Ok(common_reference) = serde_json::from_value::<Self>(value.clone()) {
Expand All @@ -1212,16 +1083,19 @@ where
}

// If neither succeeds, return an error
Err(report!(ParsingError::StructParseFailure("CommonMandateReference"))
.attach_printable("Failed to parse JSON into CommonMandateReference or PaymentsMandateReference"))?
Err(
report!(ParsingError::StructParseFailure("CommonMandateReference")).attach_printable(
"Failed to parse JSON into CommonMandateReference or PaymentsMandateReference",
),
)?
}
}

impl From<PaymentsMandateReference> for CommonMandateReference {
fn from(payment_reference: PaymentsMandateReference) -> Self {
Self{
Self {
payments: Some(payment_reference),
payouts: None,
}
}
}
}
6 changes: 3 additions & 3 deletions crates/router/src/core/payment_methods/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,9 +850,9 @@ pub async fn skip_locker_call_and_migrate_payment_method(
.clone()
.and_then(|val| if val == json!({}) { None } else { Some(true) })
.or_else(|| {
req.connector_mandate_details
.clone()
.and_then(|val| (!val.0.is_empty()).then_some(false))
req.connector_mandate_details.clone().and_then(|val| {
(!val.payments.unwrap_or_default().0.is_empty()).then_some(false)
})
}),
);

Expand Down
Loading

0 comments on commit 2535ab6

Please sign in to comment.