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

refactor: add surcharge_applicable to payment_intent and remove surcharge_metadata from payment_attempt #2642

Merged
merged 6 commits into from
Oct 19, 2023
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 @@ -306,7 +299,6 @@ impl PaymentAttemptUpdate {
preprocessing_step_id: pa_update
.preprocessing_step_id
.or(source.preprocessing_step_id),
surcharge_metadata: pa_update.surcharge_metadata.or(source.surcharge_metadata),
updated_by: pa_update.updated_by,
..source
}
Expand Down Expand Up @@ -554,14 +546,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
35 changes: 14 additions & 21 deletions crates/router/src/core/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,19 +818,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 +830,19 @@ 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 = payment_data
.session_surcharge_details
.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 Expand Up @@ -1485,6 +1477,7 @@ where
pub ephemeral_key: Option<ephemeral_key::EphemeralKey>,
pub redirect_response: Option<api_models::payments::RedirectResponse>,
pub surcharge_details: Option<SurchargeDetailsResponse>,
pub session_surcharge_details: Option<SurchargeMetadata>,
pub frm_message: Option<FraudCheck>,
pub payment_link_data: Option<api_models::payments::PaymentLinkResponse>,
}
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 @@ -252,6 +252,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
multiple_capture_data: None,
redirect_response,
surcharge_details: None,
session_surcharge_details: None,
frm_message: frm_response.ok(),
payment_link_data: None,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
multiple_capture_data: None,
redirect_response: None,
surcharge_details: None,
session_surcharge_details: None,
frm_message: None,
payment_link_data: None,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
multiple_capture_data,
redirect_response: None,
surcharge_details: None,
session_surcharge_details: None,
frm_message: None,
payment_link_data: None,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
multiple_capture_data: None,
redirect_response,
surcharge_details: None,
session_surcharge_details: None,
frm_message: None,
payment_link_data: None,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
multiple_capture_data: None,
redirect_response: None,
surcharge_details,
session_surcharge_details: None,
frm_message: None,
payment_link_data: None,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
multiple_capture_data: None,
redirect_response: None,
surcharge_details: None,
session_surcharge_details: None,
frm_message: None,
payment_link_data,
},
Expand Down Expand Up @@ -720,6 +721,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 @@ -198,6 +198,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
multiple_capture_data: None,
redirect_response: None,
surcharge_details: None,
session_surcharge_details: None,
frm_message: None,
payment_link_data: None,
},
Expand Down Expand Up @@ -401,6 +402,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
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
multiple_capture_data: None,
redirect_response: None,
surcharge_details: None,
session_surcharge_details: None,
frm_message: frm_response.ok(),
payment_link_data: None,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
multiple_capture_data: None,
redirect_response: None,
surcharge_details: None,
session_surcharge_details: None,
frm_message: None,
payment_link_data: None,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
multiple_capture_data: None,
redirect_response: None,
surcharge_details: None,
session_surcharge_details: None,
frm_message: None,
payment_link_data: None,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ async fn get_tracker_for_sync<
redirect_response: None,
payment_link_data: None,
surcharge_details: None,
session_surcharge_details: None,
frm_message: frm_response.ok(),
},
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
multiple_capture_data: None,
redirect_response: None,
surcharge_details: None,
session_surcharge_details: None,
frm_message: None,
payment_link_data: None,
},
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
Loading
Loading