Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mandates): handle the connector_mandate creation once and only if the payment is charged #6358

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crates/common_enums/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3297,3 +3297,16 @@ pub enum SurchargeCalculationOverride {
/// Calculate surcharge
Calculate,
}

/// Connector Mandate Status
#[derive(
Clone, Copy, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize, strum::Display,
)]
#[strum(serialize_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum ConnectorMandateStatus {
/// Indicates that the connector mandate is active and can be used for payments.
Active,
/// Indicates that the connector mandate is not active and hence cannot be used for payments.
Inactive,
}
42 changes: 40 additions & 2 deletions crates/diesel_models/src/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ use crate::schema::payment_attempt;
#[cfg(feature = "v2")]
use crate::schema_v2::payment_attempt;

common_utils::impl_to_sql_from_sql_json!(ConnectorMandateReferenceId);
#[derive(
Clone, Debug, serde::Deserialize, serde::Serialize, Eq, PartialEq, diesel::AsExpression,
)]
#[diesel(sql_type = diesel::sql_types::Jsonb)]
pub struct ConnectorMandateReferenceId {
pub connector_mandate_id: Option<String>,
pub payment_method_id: Option<String>,
pub mandate_metadata: Option<serde_json::Value>,
}
#[cfg(feature = "v2")]
#[derive(
Clone, Debug, Eq, PartialEq, Identifiable, Queryable, Serialize, Deserialize, Selectable,
Expand Down Expand Up @@ -72,6 +82,7 @@ pub struct PaymentAttempt {
pub id: String,
pub shipping_cost: Option<MinorUnit>,
pub order_tax_amount: Option<MinorUnit>,
pub connector_mandate_detail: Option<ConnectorMandateReferenceId>,
}

#[cfg(feature = "v1")]
Expand Down Expand Up @@ -148,6 +159,7 @@ pub struct PaymentAttempt {
pub card_network: Option<String>,
pub shipping_cost: Option<MinorUnit>,
pub order_tax_amount: Option<MinorUnit>,
pub connector_mandate_detail: Option<ConnectorMandateReferenceId>,
}

#[cfg(feature = "v1")]
Expand Down Expand Up @@ -224,6 +236,7 @@ pub struct PaymentAttemptNew {
pub card_network: Option<String>,
pub shipping_cost: Option<MinorUnit>,
pub order_tax_amount: Option<MinorUnit>,
pub connector_mandate_detail: Option<ConnectorMandateReferenceId>,
}

#[cfg(feature = "v1")]
Expand Down Expand Up @@ -296,6 +309,7 @@ pub struct PaymentAttemptNew {
pub card_network: Option<String>,
pub shipping_cost: Option<MinorUnit>,
pub order_tax_amount: Option<MinorUnit>,
pub connector_mandate_detail: Option<ConnectorMandateReferenceId>,
}

#[cfg(feature = "v1")]
Expand Down Expand Up @@ -451,6 +465,7 @@ pub enum PaymentAttemptUpdate {
unified_message: Option<Option<String>>,
payment_method_data: Option<serde_json::Value>,
charge_id: Option<String>,
connector_mandate_detail: Option<ConnectorMandateReferenceId>,
},
UnresolvedResponseUpdate {
status: storage_enums::AttemptStatus,
Expand Down Expand Up @@ -761,6 +776,7 @@ pub struct PaymentAttemptUpdateInternal {
client_version: Option<String>,
customer_acceptance: Option<pii::SecretSerdeValue>,
card_network: Option<String>,
connector_mandate_detail: Option<ConnectorMandateReferenceId>,
}

#[cfg(feature = "v1")]
Expand Down Expand Up @@ -816,6 +832,7 @@ pub struct PaymentAttemptUpdateInternal {
pub card_network: Option<String>,
pub shipping_cost: Option<MinorUnit>,
pub order_tax_amount: Option<MinorUnit>,
pub connector_mandate_detail: Option<ConnectorMandateReferenceId>,
}

#[cfg(feature = "v2")]
Expand Down Expand Up @@ -1004,6 +1021,7 @@ impl PaymentAttemptUpdate {
card_network,
shipping_cost,
order_tax_amount,
connector_mandate_detail,
} = PaymentAttemptUpdateInternal::from(self).populate_derived_fields(&source);
PaymentAttempt {
amount: amount.unwrap_or(source.amount),
Expand Down Expand Up @@ -1059,6 +1077,7 @@ impl PaymentAttemptUpdate {
card_network: card_network.or(source.card_network),
shipping_cost: shipping_cost.or(source.shipping_cost),
order_tax_amount: order_tax_amount.or(source.order_tax_amount),
connector_mandate_detail: connector_mandate_detail.or(source.connector_mandate_detail),
..source
}
}
Expand Down Expand Up @@ -2053,6 +2072,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::AuthenticationTypeUpdate {
authentication_type,
Expand Down Expand Up @@ -2107,6 +2127,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::ConfirmUpdate {
amount,
Expand Down Expand Up @@ -2191,6 +2212,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost,
order_tax_amount,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::VoidUpdate {
status,
Expand Down Expand Up @@ -2246,6 +2268,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::RejectUpdate {
status,
Expand Down Expand Up @@ -2302,6 +2325,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::BlocklistUpdate {
status,
Expand Down Expand Up @@ -2358,6 +2382,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::PaymentMethodDetailsUpdate {
payment_method_id,
Expand Down Expand Up @@ -2412,6 +2437,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::ResponseUpdate {
status,
Expand All @@ -2434,6 +2460,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
unified_message,
payment_method_data,
charge_id,
connector_mandate_detail,
} => Self {
status: Some(status),
connector: connector.map(Some),
Expand Down Expand Up @@ -2484,6 +2511,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail,
},
PaymentAttemptUpdate::ErrorUpdate {
connector,
Expand Down Expand Up @@ -2548,6 +2576,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::StatusUpdate { status, updated_by } => Self {
status: Some(status),
Expand Down Expand Up @@ -2599,6 +2628,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::UpdateTrackers {
payment_token,
Expand Down Expand Up @@ -2659,6 +2689,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::UnresolvedResponseUpdate {
status,
Expand Down Expand Up @@ -2720,6 +2751,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::PreprocessingUpdate {
status,
Expand Down Expand Up @@ -2779,6 +2811,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::CaptureUpdate {
multiple_capture_count,
Expand Down Expand Up @@ -2834,6 +2867,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::AmountToCaptureUpdate {
status,
Expand Down Expand Up @@ -2889,6 +2923,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::ConnectorResponse {
authentication_data,
Expand Down Expand Up @@ -2947,6 +2982,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::IncrementalAuthorizationAmountUpdate {
amount,
Expand Down Expand Up @@ -3001,6 +3037,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::AuthenticationUpdate {
status,
Expand Down Expand Up @@ -3058,6 +3095,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::ManualUpdate {
status,
Expand Down Expand Up @@ -3118,6 +3156,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
},
}
}
Expand Down Expand Up @@ -3203,8 +3242,7 @@ mod tests {
"user_agent": "amet irure esse"
}
},
"mandate_type": {
Aprabhat19 marked this conversation as resolved.
Show resolved Hide resolved
"single_use": {
"mandate_type": {"single_use": {
"amount": 6540,
"currency": "USD"
}
Expand Down
1 change: 1 addition & 0 deletions crates/diesel_models/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ diesel::table! {
card_network -> Nullable<Varchar>,
shipping_cost -> Nullable<Int8>,
order_tax_amount -> Nullable<Int8>,
connector_mandate_detail -> Nullable<Jsonb>,
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/diesel_models/src/schema_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ diesel::table! {
id -> Varchar,
shipping_cost -> Nullable<Int8>,
order_tax_amount -> Nullable<Int8>,
connector_mandate_detail -> Nullable<Jsonb>,
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/diesel_models/src/user/sample_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::schema::payment_attempt;
use crate::schema_v2::payment_attempt;
use crate::{
enums::{MandateDataType, MandateDetails},
PaymentAttemptNew,
ConnectorMandateReferenceId, PaymentAttemptNew,
};

#[cfg(feature = "v2")]
Expand Down Expand Up @@ -203,6 +203,7 @@ pub struct PaymentAttemptBatchNew {
pub organization_id: common_utils::id_type::OrganizationId,
pub shipping_cost: Option<MinorUnit>,
pub order_tax_amount: Option<MinorUnit>,
pub connector_mandate_detail: Option<ConnectorMandateReferenceId>,
}

#[cfg(feature = "v1")]
Expand Down Expand Up @@ -281,6 +282,7 @@ impl PaymentAttemptBatchNew {
organization_id: self.organization_id,
shipping_cost: self.shipping_cost,
order_tax_amount: self.order_tax_amount,
connector_mandate_detail: self.connector_mandate_detail,
}
}
}
Loading
Loading