Skip to content

Commit

Permalink
fix(mandates): handle the connector_mandate creation once and only if…
Browse files Browse the repository at this point in the history
… the payment is charged (#6327)
  • Loading branch information
Aprabhat19 authored Oct 17, 2024
1 parent 1a3d0a6 commit e14a0fe
Show file tree
Hide file tree
Showing 21 changed files with 366 additions and 208 deletions.
13 changes: 13 additions & 0 deletions crates/common_enums/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3356,3 +3356,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,
}
41 changes: 40 additions & 1 deletion crates/diesel_models/src/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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 @@ -76,6 +86,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 @@ -153,6 +164,7 @@ pub struct PaymentAttempt {
pub shipping_cost: Option<MinorUnit>,
pub order_tax_amount: Option<MinorUnit>,
pub connector_transaction_data: Option<String>,
pub connector_mandate_detail: Option<ConnectorMandateReferenceId>,
}

#[cfg(feature = "v1")]
Expand Down Expand Up @@ -256,6 +268,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 @@ -328,6 +341,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 @@ -442,6 +456,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 @@ -757,6 +772,7 @@ pub struct PaymentAttemptUpdateInternal {
customer_acceptance: Option<pii::SecretSerdeValue>,
card_network: Option<String>,
connector_payment_data: Option<String>,
connector_mandate_detail: Option<ConnectorMandateReferenceId>,
}

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

#[cfg(feature = "v2")]
Expand Down Expand Up @@ -1002,6 +1019,7 @@ impl PaymentAttemptUpdate {
shipping_cost,
order_tax_amount,
connector_transaction_data,
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 {
order_tax_amount: order_tax_amount.or(source.order_tax_amount),
connector_transaction_data: connector_transaction_data
.or(source.connector_transaction_data),
connector_mandate_detail: connector_mandate_detail.or(source.connector_mandate_detail),
..source
}
}
Expand Down Expand Up @@ -2054,6 +2073,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
shipping_cost: None,
order_tax_amount: None,
connector_transaction_data: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::AuthenticationTypeUpdate {
authentication_type,
Expand Down Expand Up @@ -2109,6 +2129,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
shipping_cost: None,
order_tax_amount: None,
connector_transaction_data: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::ConfirmUpdate {
amount,
Expand Down Expand Up @@ -2194,6 +2215,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
shipping_cost,
order_tax_amount,
connector_transaction_data: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::VoidUpdate {
status,
Expand Down Expand Up @@ -2250,6 +2272,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
shipping_cost: None,
order_tax_amount: None,
connector_transaction_data: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::RejectUpdate {
status,
Expand Down Expand Up @@ -2307,6 +2330,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
shipping_cost: None,
order_tax_amount: None,
connector_transaction_data: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::BlocklistUpdate {
status,
Expand Down Expand Up @@ -2364,6 +2388,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
shipping_cost: None,
order_tax_amount: None,
connector_transaction_data: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::PaymentMethodDetailsUpdate {
payment_method_id,
Expand Down Expand Up @@ -2419,6 +2444,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
shipping_cost: None,
order_tax_amount: None,
connector_transaction_data: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::ResponseUpdate {
status,
Expand All @@ -2441,6 +2467,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
unified_message,
payment_method_data,
charge_id,
connector_mandate_detail,
} => {
let (connector_transaction_id, connector_transaction_data) =
connector_transaction_id
Expand Down Expand Up @@ -2498,6 +2525,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail,
}
}
PaymentAttemptUpdate::ErrorUpdate {
Expand Down Expand Up @@ -2570,6 +2598,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 {
Expand Down Expand Up @@ -2623,6 +2652,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
shipping_cost: None,
order_tax_amount: None,
connector_transaction_data: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::UpdateTrackers {
payment_token,
Expand Down Expand Up @@ -2684,6 +2714,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
shipping_cost: None,
order_tax_amount: None,
connector_transaction_data: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::UnresolvedResponseUpdate {
status,
Expand Down Expand Up @@ -2752,6 +2783,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
}
}
PaymentAttemptUpdate::PreprocessingUpdate {
Expand Down Expand Up @@ -2819,6 +2851,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
}
}
PaymentAttemptUpdate::CaptureUpdate {
Expand Down Expand Up @@ -2876,6 +2909,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
shipping_cost: None,
order_tax_amount: None,
connector_transaction_data: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::AmountToCaptureUpdate {
status,
Expand Down Expand Up @@ -2932,6 +2966,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
shipping_cost: None,
order_tax_amount: None,
connector_transaction_data: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::ConnectorResponse {
authentication_data,
Expand Down Expand Up @@ -2997,6 +3032,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
}
}
PaymentAttemptUpdate::IncrementalAuthorizationAmountUpdate {
Expand Down Expand Up @@ -3053,6 +3089,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
shipping_cost: None,
order_tax_amount: None,
connector_transaction_data: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::AuthenticationUpdate {
status,
Expand Down Expand Up @@ -3111,6 +3148,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
shipping_cost: None,
order_tax_amount: None,
connector_transaction_data: None,
connector_mandate_detail: None,
},
PaymentAttemptUpdate::ManualUpdate {
status,
Expand Down Expand Up @@ -3178,6 +3216,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
card_network: None,
shipping_cost: None,
order_tax_amount: None,
connector_mandate_detail: None,
}
}
PaymentAttemptUpdate::PostSessionTokensUpdate {
Expand Down Expand Up @@ -3234,6 +3273,7 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
shipping_cost: None,
order_tax_amount: None,
connector_transaction_data: None,
connector_mandate_detail: None,
},
}
}
Expand Down Expand Up @@ -3319,7 +3359,6 @@ mod tests {
"user_agent": "amet irure esse"
}
},
"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 @@ -850,6 +850,7 @@ diesel::table! {
order_tax_amount -> Nullable<Int8>,
#[max_length = 512]
connector_transaction_data -> Nullable<Varchar>,
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 @@ -821,6 +821,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 @@ -204,6 +204,7 @@ pub struct PaymentAttemptBatchNew {
pub shipping_cost: Option<MinorUnit>,
pub order_tax_amount: Option<MinorUnit>,
pub connector_transaction_data: Option<String>,
pub connector_mandate_detail: Option<ConnectorMandateReferenceId>,
}

#[cfg(feature = "v1")]
Expand Down Expand Up @@ -282,6 +283,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

0 comments on commit e14a0fe

Please sign in to comment.