Skip to content

Commit

Permalink
refactor: add surcharge_applicable to payment_intent and remove surch…
Browse files Browse the repository at this point in the history
…arge_metadata from payment_attempt (#2642)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
hrithikesh026 and github-actions[bot] authored Oct 19, 2023
1 parent 41b7742 commit e5fbaae
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 65 deletions.
17 changes: 17 additions & 0 deletions crates/api_models/src/payment_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,23 @@ pub struct SurchargeMetadata {
pub surcharge_results: HashMap<String, SurchargeDetailsResponse>,
}

impl SurchargeMetadata {
pub fn get_key_for_surcharge_details_hash_map(
payment_method: &common_enums::PaymentMethod,
payment_method_type: &common_enums::PaymentMethodType,
card_network: Option<&common_enums::CardNetwork>,
) -> String {
if let Some(card_network) = card_network {
format!(
"{}_{}_{}",
payment_method, payment_method_type, card_network
)
} else {
format!("{}_{}", payment_method, payment_method_type)
}
}
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize, ToSchema)]
#[serde(rename_all = "snake_case", tag = "type", content = "value")]
pub enum Surcharge {
Expand Down
2 changes: 2 additions & 0 deletions crates/data_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ pub struct PaymentIntent {
// Manual review can occur when the transaction is marked as risky by the frm_processor, payment processor or when there is underpayment/over payment incase of crypto payment
pub merchant_decision: Option<String>,
pub payment_confirm_source: Option<storage_enums::PaymentSource>,

pub updated_by: String,
pub surcharge_applicable: Option<bool>,
}
6 changes: 0 additions & 6 deletions crates/data_models/src/payments/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ pub struct PaymentAttempt {
// reference to the payment at connector side
pub connector_response_reference_id: Option<String>,
pub amount_capturable: i64,
pub surcharge_metadata: Option<serde_json::Value>,
pub updated_by: String,
}

Expand Down Expand Up @@ -201,7 +200,6 @@ pub struct PaymentAttemptNew {
pub connector_response_reference_id: Option<String>,
pub multiple_capture_count: Option<i16>,
pub amount_capturable: i64,
pub surcharge_metadata: Option<serde_json::Value>,
pub updated_by: String,
}

Expand Down Expand Up @@ -323,10 +321,6 @@ pub enum PaymentAttemptUpdate {
connector_response_reference_id: Option<String>,
updated_by: String,
},
SurchargeMetadataUpdate {
surcharge_metadata: Option<serde_json::Value>,
updated_by: String,
},
}

impl ForeignIDRef for PaymentAttempt {
Expand Down
4 changes: 4 additions & 0 deletions crates/data_models/src/payments/payment_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ pub struct PaymentIntentNew {
pub merchant_decision: Option<String>,
pub payment_link_id: Option<String>,
pub payment_confirm_source: Option<storage_enums::PaymentSource>,

pub updated_by: String,
pub surcharge_applicable: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -204,7 +206,9 @@ pub struct PaymentIntentUpdateInternal {
// Manual review can occur when the transaction is marked as risky by the frm_processor, payment processor or when there is underpayment/over payment incase of crypto payment
pub merchant_decision: Option<String>,
pub payment_confirm_source: Option<storage_enums::PaymentSource>,

pub updated_by: String,
pub surcharge_applicable: Option<bool>,
}

impl PaymentIntentUpdate {
Expand Down
16 changes: 0 additions & 16 deletions crates/diesel_models/src/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ pub struct PaymentAttempt {
// reference to the payment at connector side
pub connector_response_reference_id: Option<String>,
pub amount_capturable: i64,
pub surcharge_metadata: Option<serde_json::Value>,
pub updated_by: String,
}

Expand Down Expand Up @@ -117,7 +116,6 @@ pub struct PaymentAttemptNew {
pub connector_response_reference_id: Option<String>,
pub multiple_capture_count: Option<i16>,
pub amount_capturable: i64,
pub surcharge_metadata: Option<serde_json::Value>,
pub updated_by: String,
}

Expand Down Expand Up @@ -239,10 +237,6 @@ pub enum PaymentAttemptUpdate {
connector_response_reference_id: Option<String>,
updated_by: String,
},
SurchargeMetadataUpdate {
surcharge_metadata: Option<serde_json::Value>,
updated_by: String,
},
}

#[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)]
Expand Down Expand Up @@ -278,7 +272,6 @@ pub struct PaymentAttemptUpdateInternal {
surcharge_amount: Option<i64>,
tax_amount: Option<i64>,
amount_capturable: Option<i64>,
surcharge_metadata: Option<serde_json::Value>,
updated_by: String,
}

Expand Down Expand Up @@ -330,7 +323,6 @@ impl PaymentAttemptUpdate {
amount_capturable: pa_update
.amount_capturable
.unwrap_or(source.amount_capturable),
surcharge_metadata: pa_update.surcharge_metadata.or(source.surcharge_metadata),
updated_by: pa_update.updated_by,
..source
}
Expand Down Expand Up @@ -578,14 +570,6 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
updated_by,
..Default::default()
},
PaymentAttemptUpdate::SurchargeMetadataUpdate {
surcharge_metadata,
updated_by,
} => Self {
surcharge_metadata,
updated_by,
..Default::default()
},
}
}
}
6 changes: 6 additions & 0 deletions crates/diesel_models/src/payment_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ pub struct PaymentIntent {
pub merchant_decision: Option<String>,
pub payment_link_id: Option<String>,
pub payment_confirm_source: Option<storage_enums::PaymentSource>,

pub updated_by: String,
pub surcharge_applicable: Option<bool>,
}

#[derive(
Expand Down Expand Up @@ -101,7 +103,9 @@ pub struct PaymentIntentNew {
pub merchant_decision: Option<String>,
pub payment_link_id: Option<String>,
pub payment_confirm_source: Option<storage_enums::PaymentSource>,

pub updated_by: String,
pub surcharge_applicable: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -202,7 +206,9 @@ pub struct PaymentIntentUpdateInternal {
pub profile_id: Option<String>,
merchant_decision: Option<String>,
payment_confirm_source: Option<storage_enums::PaymentSource>,

pub updated_by: String,
pub surcharge_applicable: Option<bool>,
}

impl PaymentIntentUpdate {
Expand Down
2 changes: 1 addition & 1 deletion crates/diesel_models/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,6 @@ diesel::table! {
#[max_length = 128]
connector_response_reference_id -> Nullable<Varchar>,
amount_capturable -> Int8,
surcharge_metadata -> Nullable<Jsonb>,
#[max_length = 32]
updated_by -> Varchar,
}
Expand Down Expand Up @@ -622,6 +621,7 @@ diesel::table! {
payment_confirm_source -> Nullable<PaymentSource>,
#[max_length = 32]
updated_by -> Varchar,
surcharge_applicable -> Nullable<Bool>,
}
}

Expand Down
36 changes: 15 additions & 21 deletions crates/router/src/core/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ where
&operation,
payment_data,
&customer,
None,
)
.await?
}
Expand Down Expand Up @@ -791,6 +792,7 @@ where
router_data_res
}

#[allow(clippy::too_many_arguments)]
pub async fn call_multiple_connectors_service<F, Op, Req, Ctx>(
state: &AppState,
merchant_account: &domain::MerchantAccount,
Expand All @@ -799,6 +801,7 @@ pub async fn call_multiple_connectors_service<F, Op, Req, Ctx>(
_operation: &Op,
mut payment_data: PaymentData<F>,
customer: &Option<domain::Customer>,
session_surcharge_metadata: Option<SurchargeMetadata>,
) -> RouterResult<PaymentData<F>>
where
Op: Debug,
Expand All @@ -818,19 +821,6 @@ where
{
let call_connectors_start_time = Instant::now();
let mut join_handlers = Vec::with_capacity(connectors.len());
let surcharge_metadata = payment_data
.payment_attempt
.surcharge_metadata
.as_ref()
.map(|surcharge_metadata_value| {
surcharge_metadata_value
.clone()
.parse_value::<SurchargeMetadata>("SurchargeMetadata")
})
.transpose()
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to Deserialize SurchargeMetadata")?;

for session_connector_data in connectors.iter() {
let connector_id = session_connector_data.connector.connector.id();

Expand All @@ -843,14 +833,18 @@ where
false,
)
.await?;
payment_data.surcharge_details =
surcharge_metadata.as_ref().and_then(|surcharge_metadata| {
let payment_method_type = session_connector_data.payment_method_type;
surcharge_metadata
.surcharge_results
.get(&payment_method_type.to_string())
.cloned()
});
payment_data.surcharge_details = session_surcharge_metadata
.as_ref()
.and_then(|surcharge_metadata| {
surcharge_metadata.surcharge_results.get(
&SurchargeMetadata::get_key_for_surcharge_details_hash_map(
&session_connector_data.payment_method_type.into(),
&session_connector_data.payment_method_type,
None,
),
)
})
.cloned();

let router_data = payment_data
.construct_router_data(
Expand Down
4 changes: 3 additions & 1 deletion crates/router/src/core/payments/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2409,6 +2409,7 @@ mod tests {
profile_id: None,
merchant_decision: None,
payment_confirm_source: None,
surcharge_applicable: None,
updated_by: storage_enums::MerchantStorageScheme::PostgresOnly.to_string(),
};
let req_cs = Some("1".to_string());
Expand Down Expand Up @@ -2458,6 +2459,7 @@ mod tests {
profile_id: None,
merchant_decision: None,
payment_confirm_source: None,
surcharge_applicable: None,
updated_by: storage_enums::MerchantStorageScheme::PostgresOnly.to_string(),
};
let req_cs = Some("1".to_string());
Expand Down Expand Up @@ -2507,6 +2509,7 @@ mod tests {
profile_id: None,
merchant_decision: None,
payment_confirm_source: None,
surcharge_applicable: None,
updated_by: storage_enums::MerchantStorageScheme::PostgresOnly.to_string(),
};
let req_cs = Some("1".to_string());
Expand Down Expand Up @@ -2900,7 +2903,6 @@ impl AttemptType {
multiple_capture_count: None,
connector_response_reference_id: None,
amount_capturable: old_payment_attempt.amount,
surcharge_metadata: old_payment_attempt.surcharge_metadata,
updated_by: storage_scheme.to_string(),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ impl PaymentCreate {
merchant_decision: None,
payment_link_id,
payment_confirm_source: None,
surcharge_applicable: None,
updated_by: merchant_account.storage_scheme.to_string(),
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ impl PaymentMethodValidate {
profile_id: Default::default(),
merchant_decision: Default::default(),
payment_confirm_source: Default::default(),
surcharge_applicable: Default::default(),
payment_link_id: Default::default(),
updated_by: storage_scheme.to_string(),
}
Expand Down
1 change: 0 additions & 1 deletion crates/storage_impl/src/mock_db/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ impl PaymentAttemptInterface for MockDb {
multiple_capture_count: payment_attempt.multiple_capture_count,
connector_response_reference_id: None,
amount_capturable: payment_attempt.amount_capturable,
surcharge_metadata: payment_attempt.surcharge_metadata,
updated_by: storage_scheme.to_string(),
};
payment_attempts.push(payment_attempt.clone());
Expand Down
1 change: 1 addition & 0 deletions crates/storage_impl/src/mock_db/payment_intent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ impl PaymentIntentInterface for MockDb {
payment_link_id: new.payment_link_id,
payment_confirm_source: new.payment_confirm_source,
updated_by: storage_scheme.to_string(),
surcharge_applicable: new.surcharge_applicable,
};
payment_intents.push(payment_intent.clone());
Ok(payment_intent)
Expand Down
24 changes: 5 additions & 19 deletions crates/storage_impl/src/payments/payment_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ impl<T: DatabaseStore> PaymentAttemptInterface for KVRouterStore<T> {
multiple_capture_count: payment_attempt.multiple_capture_count,
connector_response_reference_id: None,
amount_capturable: payment_attempt.amount_capturable,
surcharge_metadata: payment_attempt.surcharge_metadata.clone(),

updated_by: storage_scheme.to_string(),
};

Expand Down Expand Up @@ -956,7 +956,7 @@ impl DataModelExt for PaymentAttempt {
multiple_capture_count: self.multiple_capture_count,
connector_response_reference_id: self.connector_response_reference_id,
amount_capturable: self.amount_capturable,
surcharge_metadata: self.surcharge_metadata,

updated_by: self.updated_by,
}
}
Expand Down Expand Up @@ -1006,7 +1006,7 @@ impl DataModelExt for PaymentAttempt {
multiple_capture_count: storage_model.multiple_capture_count,
connector_response_reference_id: storage_model.connector_response_reference_id,
amount_capturable: storage_model.amount_capturable,
surcharge_metadata: storage_model.surcharge_metadata,

updated_by: storage_model.updated_by,
}
}
Expand Down Expand Up @@ -1056,7 +1056,7 @@ impl DataModelExt for PaymentAttemptNew {
connector_response_reference_id: self.connector_response_reference_id,
multiple_capture_count: self.multiple_capture_count,
amount_capturable: self.amount_capturable,
surcharge_metadata: self.surcharge_metadata,

updated_by: self.updated_by,
}
}
Expand Down Expand Up @@ -1104,7 +1104,7 @@ impl DataModelExt for PaymentAttemptNew {
connector_response_reference_id: storage_model.connector_response_reference_id,
multiple_capture_count: storage_model.multiple_capture_count,
amount_capturable: storage_model.amount_capturable,
surcharge_metadata: storage_model.surcharge_metadata,

updated_by: storage_model.updated_by,
}
}
Expand Down Expand Up @@ -1330,13 +1330,6 @@ impl DataModelExt for PaymentAttemptUpdate {
amount_capturable,
updated_by,
},
Self::SurchargeMetadataUpdate {
surcharge_metadata,
updated_by,
} => DieselPaymentAttemptUpdate::SurchargeMetadataUpdate {
surcharge_metadata,
updated_by,
},
}
}

Expand Down Expand Up @@ -1557,13 +1550,6 @@ impl DataModelExt for PaymentAttemptUpdate {
amount_capturable,
updated_by,
},
DieselPaymentAttemptUpdate::SurchargeMetadataUpdate {
surcharge_metadata,
updated_by,
} => Self::SurchargeMetadataUpdate {
surcharge_metadata,
updated_by,
},
}
}
}
Expand Down
Loading

0 comments on commit e5fbaae

Please sign in to comment.