refactor(router): modify net_amount to be a struct in the domain model of payment_attempt and handle amount changes across all flows (#6252)

Co-authored-by: swangi-kumari <swangi.12015941@lpu.in>
Co-authored-by: Swangi Kumari <85639103+swangi-kumari@users.noreply.github.com>
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
Sai Harsha Vardhan
2024-10-10 18:27:14 +05:30
committed by GitHub
parent e911ea4615
commit 5930089682
36 changed files with 830 additions and 1134 deletions

View File

@ -9,7 +9,6 @@ use diesel_models::{
kv,
payment_attempt::{
PaymentAttempt as DieselPaymentAttempt, PaymentAttemptNew as DieselPaymentAttemptNew,
PaymentAttemptUpdate as DieselPaymentAttemptUpdate,
},
reverse_lookup::{ReverseLookup, ReverseLookupNew},
};
@ -459,7 +458,6 @@ impl<T: DatabaseStore> PaymentAttemptInterface for KVRouterStore<T> {
.await
}
MerchantStorageScheme::RedisKv => {
let payment_attempt = payment_attempt.populate_derived_fields();
let merchant_id = payment_attempt.merchant_id.clone();
let payment_id = payment_attempt.payment_id.clone();
let key = PartitionKey::MerchantIdPaymentId {
@ -472,15 +470,12 @@ impl<T: DatabaseStore> PaymentAttemptInterface for KVRouterStore<T> {
merchant_id: payment_attempt.merchant_id.clone(),
attempt_id: payment_attempt.attempt_id.clone(),
status: payment_attempt.status,
amount: payment_attempt.amount,
net_amount: payment_attempt.net_amount,
net_amount: payment_attempt.net_amount.clone(),
currency: payment_attempt.currency,
save_to_locker: payment_attempt.save_to_locker,
connector: payment_attempt.connector.clone(),
error_message: payment_attempt.error_message.clone(),
offer_amount: payment_attempt.offer_amount,
surcharge_amount: payment_attempt.surcharge_amount,
tax_amount: payment_attempt.tax_amount,
payment_method_id: payment_attempt.payment_method_id.clone(),
payment_method: payment_attempt.payment_method,
connector_transaction_id: None,
@ -534,8 +529,6 @@ impl<T: DatabaseStore> PaymentAttemptInterface for KVRouterStore<T> {
customer_acceptance: payment_attempt.customer_acceptance.clone(),
organization_id: payment_attempt.organization_id.clone(),
profile_id: payment_attempt.profile_id.clone(),
shipping_cost: payment_attempt.shipping_cost,
order_tax_amount: payment_attempt.order_tax_amount,
};
let field = format!("pa_{}", created_attempt.attempt_id);
@ -1379,15 +1372,15 @@ impl DataModelExt for PaymentAttempt {
merchant_id: self.merchant_id,
attempt_id: self.attempt_id,
status: self.status,
amount: self.amount,
net_amount: Some(self.net_amount),
amount: self.net_amount.get_order_amount(),
net_amount: Some(self.net_amount.get_total_amount()),
currency: self.currency,
save_to_locker: self.save_to_locker,
connector: self.connector,
error_message: self.error_message,
offer_amount: self.offer_amount,
surcharge_amount: self.surcharge_amount,
tax_amount: self.tax_amount,
surcharge_amount: self.net_amount.get_surcharge_amount(),
tax_amount: self.net_amount.get_tax_on_surcharge(),
payment_method_id: self.payment_method_id,
payment_method: self.payment_method,
connector_transaction_id: self.connector_transaction_id,
@ -1444,26 +1437,29 @@ impl DataModelExt for PaymentAttempt {
customer_acceptance: self.customer_acceptance,
organization_id: self.organization_id,
profile_id: self.profile_id,
shipping_cost: self.shipping_cost,
order_tax_amount: self.order_tax_amount,
shipping_cost: self.net_amount.get_shipping_cost(),
order_tax_amount: self.net_amount.get_order_tax_amount(),
}
}
fn from_storage_model(storage_model: Self::StorageModel) -> Self {
Self {
net_amount: storage_model.get_or_calculate_net_amount(),
net_amount: hyperswitch_domain_models::payments::payment_attempt::NetAmount::new(
storage_model.amount,
storage_model.shipping_cost,
storage_model.order_tax_amount,
storage_model.surcharge_amount,
storage_model.tax_amount,
),
payment_id: storage_model.payment_id,
merchant_id: storage_model.merchant_id,
attempt_id: storage_model.attempt_id,
status: storage_model.status,
amount: storage_model.amount,
currency: storage_model.currency,
save_to_locker: storage_model.save_to_locker,
connector: storage_model.connector,
error_message: storage_model.error_message,
offer_amount: storage_model.offer_amount,
surcharge_amount: storage_model.surcharge_amount,
tax_amount: storage_model.tax_amount,
payment_method_id: storage_model.payment_method_id,
payment_method: storage_model.payment_method,
connector_transaction_id: storage_model.connector_transaction_id,
@ -1515,8 +1511,6 @@ impl DataModelExt for PaymentAttempt {
customer_acceptance: storage_model.customer_acceptance,
organization_id: storage_model.organization_id,
profile_id: storage_model.profile_id,
shipping_cost: storage_model.shipping_cost,
order_tax_amount: storage_model.order_tax_amount,
}
}
}
@ -1527,19 +1521,19 @@ impl DataModelExt for PaymentAttemptNew {
fn to_storage_model(self) -> Self::StorageModel {
DieselPaymentAttemptNew {
net_amount: Some(self.net_amount),
net_amount: Some(self.net_amount.get_total_amount()),
payment_id: self.payment_id,
merchant_id: self.merchant_id,
attempt_id: self.attempt_id,
status: self.status,
amount: self.amount,
amount: self.net_amount.get_order_amount(),
currency: self.currency,
save_to_locker: self.save_to_locker,
connector: self.connector,
error_message: self.error_message,
offer_amount: self.offer_amount,
surcharge_amount: self.surcharge_amount,
tax_amount: self.tax_amount,
surcharge_amount: self.net_amount.get_surcharge_amount(),
tax_amount: self.net_amount.get_tax_on_surcharge(),
payment_method_id: self.payment_method_id,
payment_method: self.payment_method,
capture_method: self.capture_method,
@ -1597,26 +1591,29 @@ impl DataModelExt for PaymentAttemptNew {
customer_acceptance: self.customer_acceptance,
organization_id: self.organization_id,
profile_id: self.profile_id,
shipping_cost: self.shipping_cost,
order_tax_amount: self.order_tax_amount,
shipping_cost: self.net_amount.get_shipping_cost(),
order_tax_amount: self.net_amount.get_order_tax_amount(),
}
}
fn from_storage_model(storage_model: Self::StorageModel) -> Self {
Self {
net_amount: storage_model.get_or_calculate_net_amount(),
net_amount: hyperswitch_domain_models::payments::payment_attempt::NetAmount::new(
storage_model.amount,
storage_model.shipping_cost,
storage_model.order_tax_amount,
storage_model.surcharge_amount,
storage_model.tax_amount,
),
payment_id: storage_model.payment_id,
merchant_id: storage_model.merchant_id,
attempt_id: storage_model.attempt_id,
status: storage_model.status,
amount: storage_model.amount,
currency: storage_model.currency,
save_to_locker: storage_model.save_to_locker,
connector: storage_model.connector,
error_message: storage_model.error_message,
offer_amount: storage_model.offer_amount,
surcharge_amount: storage_model.surcharge_amount,
tax_amount: storage_model.tax_amount,
payment_method_id: storage_model.payment_method_id,
payment_method: storage_model.payment_method,
capture_method: storage_model.capture_method,
@ -1667,725 +1664,6 @@ impl DataModelExt for PaymentAttemptNew {
customer_acceptance: storage_model.customer_acceptance,
organization_id: storage_model.organization_id,
profile_id: storage_model.profile_id,
shipping_cost: storage_model.shipping_cost,
order_tax_amount: storage_model.order_tax_amount,
}
}
}
#[cfg(feature = "v1")]
impl DataModelExt for PaymentAttemptUpdate {
type StorageModel = DieselPaymentAttemptUpdate;
fn to_storage_model(self) -> Self::StorageModel {
match self {
Self::Update {
amount,
currency,
status,
authentication_type,
payment_method,
payment_token,
payment_method_data,
payment_method_type,
payment_experience,
business_sub_label,
amount_to_capture,
capture_method,
surcharge_amount,
tax_amount,
fingerprint_id,
payment_method_billing_address_id,
updated_by,
} => DieselPaymentAttemptUpdate::Update {
amount,
currency,
status,
authentication_type,
payment_method,
payment_token,
payment_method_data,
payment_method_type,
payment_experience,
business_sub_label,
amount_to_capture,
capture_method,
surcharge_amount,
tax_amount,
fingerprint_id,
payment_method_billing_address_id,
updated_by,
},
Self::UpdateTrackers {
payment_token,
connector,
straight_through_algorithm,
amount_capturable,
updated_by,
surcharge_amount,
tax_amount,
merchant_connector_id,
} => DieselPaymentAttemptUpdate::UpdateTrackers {
payment_token,
connector,
straight_through_algorithm,
amount_capturable,
surcharge_amount,
tax_amount,
updated_by,
merchant_connector_id,
},
Self::AuthenticationTypeUpdate {
authentication_type,
updated_by,
} => DieselPaymentAttemptUpdate::AuthenticationTypeUpdate {
authentication_type,
updated_by,
},
Self::BlocklistUpdate {
status,
error_code,
error_message,
updated_by,
} => DieselPaymentAttemptUpdate::BlocklistUpdate {
status,
error_code,
error_message,
updated_by,
},
Self::PaymentMethodDetailsUpdate {
payment_method_id,
updated_by,
} => DieselPaymentAttemptUpdate::PaymentMethodDetailsUpdate {
payment_method_id,
updated_by,
},
Self::ConfirmUpdate {
amount,
currency,
status,
authentication_type,
capture_method,
payment_method,
browser_info,
connector,
payment_token,
payment_method_data,
payment_method_type,
payment_experience,
business_sub_label,
straight_through_algorithm,
error_code,
error_message,
amount_capturable,
surcharge_amount,
tax_amount,
fingerprint_id,
updated_by,
merchant_connector_id: connector_id,
payment_method_id,
external_three_ds_authentication_attempted,
authentication_connector,
authentication_id,
payment_method_billing_address_id,
client_source,
client_version,
customer_acceptance,
shipping_cost,
order_tax_amount,
} => DieselPaymentAttemptUpdate::ConfirmUpdate {
amount,
currency,
status,
authentication_type,
capture_method,
payment_method,
browser_info,
connector,
payment_token,
payment_method_data,
payment_method_type,
payment_experience,
business_sub_label,
straight_through_algorithm,
error_code,
error_message,
amount_capturable,
surcharge_amount,
tax_amount,
fingerprint_id,
updated_by,
merchant_connector_id: connector_id,
payment_method_id,
external_three_ds_authentication_attempted,
authentication_connector,
authentication_id,
payment_method_billing_address_id,
client_source,
client_version,
customer_acceptance,
shipping_cost,
order_tax_amount,
},
Self::VoidUpdate {
status,
cancellation_reason,
updated_by,
} => DieselPaymentAttemptUpdate::VoidUpdate {
status,
cancellation_reason,
updated_by,
},
Self::ResponseUpdate {
status,
connector,
connector_transaction_id,
authentication_type,
payment_method_id,
mandate_id,
connector_metadata,
payment_token,
error_code,
error_message,
error_reason,
connector_response_reference_id,
amount_capturable,
updated_by,
authentication_data,
encoded_data,
unified_code,
unified_message,
payment_method_data,
charge_id,
} => DieselPaymentAttemptUpdate::ResponseUpdate {
status,
connector,
connector_transaction_id,
authentication_type,
payment_method_id,
mandate_id,
connector_metadata,
payment_token,
error_code,
error_message,
error_reason,
connector_response_reference_id,
amount_capturable,
updated_by,
authentication_data,
encoded_data,
unified_code,
unified_message,
payment_method_data,
charge_id,
},
Self::UnresolvedResponseUpdate {
status,
connector,
connector_transaction_id,
payment_method_id,
error_code,
error_message,
error_reason,
connector_response_reference_id,
updated_by,
} => DieselPaymentAttemptUpdate::UnresolvedResponseUpdate {
status,
connector,
connector_transaction_id,
payment_method_id,
error_code,
error_message,
error_reason,
connector_response_reference_id,
updated_by,
},
Self::StatusUpdate { status, updated_by } => {
DieselPaymentAttemptUpdate::StatusUpdate { status, updated_by }
}
Self::ErrorUpdate {
connector,
status,
error_code,
error_message,
error_reason,
amount_capturable,
updated_by,
unified_code,
unified_message,
connector_transaction_id,
payment_method_data,
authentication_type,
} => DieselPaymentAttemptUpdate::ErrorUpdate {
connector,
status,
error_code,
error_message,
error_reason,
amount_capturable,
updated_by,
unified_code,
unified_message,
connector_transaction_id,
payment_method_data,
authentication_type,
},
Self::CaptureUpdate {
multiple_capture_count,
updated_by,
amount_to_capture,
} => DieselPaymentAttemptUpdate::CaptureUpdate {
multiple_capture_count,
updated_by,
amount_to_capture,
},
Self::PreprocessingUpdate {
status,
payment_method_id,
connector_metadata,
preprocessing_step_id,
connector_transaction_id,
connector_response_reference_id,
updated_by,
} => DieselPaymentAttemptUpdate::PreprocessingUpdate {
status,
payment_method_id,
connector_metadata,
preprocessing_step_id,
connector_transaction_id,
connector_response_reference_id,
updated_by,
},
Self::RejectUpdate {
status,
error_code,
error_message,
updated_by,
} => DieselPaymentAttemptUpdate::RejectUpdate {
status,
error_code,
error_message,
updated_by,
},
Self::AmountToCaptureUpdate {
status,
amount_capturable,
updated_by,
} => DieselPaymentAttemptUpdate::AmountToCaptureUpdate {
status,
amount_capturable,
updated_by,
},
Self::ConnectorResponse {
authentication_data,
encoded_data,
connector_transaction_id,
connector,
charge_id,
updated_by,
} => DieselPaymentAttemptUpdate::ConnectorResponse {
authentication_data,
encoded_data,
connector_transaction_id,
connector,
charge_id,
updated_by,
},
Self::IncrementalAuthorizationAmountUpdate {
amount,
amount_capturable,
} => DieselPaymentAttemptUpdate::IncrementalAuthorizationAmountUpdate {
amount,
amount_capturable,
},
Self::AuthenticationUpdate {
status,
external_three_ds_authentication_attempted,
authentication_connector,
authentication_id,
updated_by,
} => DieselPaymentAttemptUpdate::AuthenticationUpdate {
status,
external_three_ds_authentication_attempted,
authentication_connector,
authentication_id,
updated_by,
},
Self::ManualUpdate {
status,
error_code,
error_message,
error_reason,
updated_by,
unified_code,
unified_message,
connector_transaction_id,
} => DieselPaymentAttemptUpdate::ManualUpdate {
status,
error_code,
error_message,
error_reason,
updated_by,
unified_code,
unified_message,
connector_transaction_id,
},
}
}
fn from_storage_model(storage_model: Self::StorageModel) -> Self {
match storage_model {
DieselPaymentAttemptUpdate::Update {
amount,
currency,
status,
authentication_type,
payment_method,
payment_token,
payment_method_data,
payment_method_type,
payment_experience,
business_sub_label,
amount_to_capture,
capture_method,
surcharge_amount,
tax_amount,
fingerprint_id,
updated_by,
payment_method_billing_address_id,
} => Self::Update {
amount,
currency,
status,
authentication_type,
payment_method,
payment_token,
payment_method_data,
payment_method_type,
payment_experience,
business_sub_label,
amount_to_capture,
capture_method,
surcharge_amount,
tax_amount,
fingerprint_id,
payment_method_billing_address_id,
updated_by,
},
DieselPaymentAttemptUpdate::UpdateTrackers {
payment_token,
connector,
straight_through_algorithm,
amount_capturable,
updated_by,
surcharge_amount,
tax_amount,
merchant_connector_id: connector_id,
} => Self::UpdateTrackers {
payment_token,
connector,
straight_through_algorithm,
amount_capturable,
surcharge_amount,
tax_amount,
updated_by,
merchant_connector_id: connector_id,
},
DieselPaymentAttemptUpdate::AuthenticationTypeUpdate {
authentication_type,
updated_by,
} => Self::AuthenticationTypeUpdate {
authentication_type,
updated_by,
},
DieselPaymentAttemptUpdate::ConfirmUpdate {
amount,
currency,
status,
authentication_type,
capture_method,
payment_method,
browser_info,
connector,
payment_token,
payment_method_data,
payment_method_type,
payment_experience,
business_sub_label,
straight_through_algorithm,
error_code,
error_message,
amount_capturable,
surcharge_amount,
tax_amount,
fingerprint_id,
updated_by,
merchant_connector_id: connector_id,
payment_method_id,
external_three_ds_authentication_attempted,
authentication_connector,
authentication_id,
payment_method_billing_address_id,
client_source,
client_version,
customer_acceptance,
shipping_cost,
order_tax_amount,
} => Self::ConfirmUpdate {
amount,
currency,
status,
authentication_type,
capture_method,
payment_method,
browser_info,
connector,
payment_token,
payment_method_data,
payment_method_type,
payment_experience,
business_sub_label,
straight_through_algorithm,
error_code,
error_message,
amount_capturable,
surcharge_amount,
tax_amount,
fingerprint_id,
updated_by,
merchant_connector_id: connector_id,
payment_method_id,
external_three_ds_authentication_attempted,
authentication_connector,
authentication_id,
payment_method_billing_address_id,
client_source,
client_version,
customer_acceptance,
shipping_cost,
order_tax_amount,
},
DieselPaymentAttemptUpdate::VoidUpdate {
status,
cancellation_reason,
updated_by,
} => Self::VoidUpdate {
status,
cancellation_reason,
updated_by,
},
DieselPaymentAttemptUpdate::BlocklistUpdate {
status,
error_code,
error_message,
updated_by,
} => Self::BlocklistUpdate {
status,
error_code,
error_message,
updated_by,
},
DieselPaymentAttemptUpdate::PaymentMethodDetailsUpdate {
payment_method_id,
updated_by,
} => Self::PaymentMethodDetailsUpdate {
payment_method_id,
updated_by,
},
DieselPaymentAttemptUpdate::ResponseUpdate {
status,
connector,
connector_transaction_id,
authentication_type,
payment_method_id,
mandate_id,
connector_metadata,
payment_token,
error_code,
error_message,
error_reason,
connector_response_reference_id,
amount_capturable,
updated_by,
authentication_data,
encoded_data,
unified_code,
unified_message,
payment_method_data,
charge_id,
} => Self::ResponseUpdate {
status,
connector,
connector_transaction_id,
authentication_type,
payment_method_id,
mandate_id,
connector_metadata,
payment_token,
error_code,
error_message,
error_reason,
connector_response_reference_id,
amount_capturable,
updated_by,
authentication_data,
encoded_data,
unified_code,
unified_message,
payment_method_data,
charge_id,
},
DieselPaymentAttemptUpdate::UnresolvedResponseUpdate {
status,
connector,
connector_transaction_id,
payment_method_id,
error_code,
error_message,
error_reason,
connector_response_reference_id,
updated_by,
} => Self::UnresolvedResponseUpdate {
status,
connector,
connector_transaction_id,
payment_method_id,
error_code,
error_message,
error_reason,
connector_response_reference_id,
updated_by,
},
DieselPaymentAttemptUpdate::StatusUpdate { status, updated_by } => {
Self::StatusUpdate { status, updated_by }
}
DieselPaymentAttemptUpdate::ErrorUpdate {
connector,
status,
error_code,
error_message,
error_reason,
amount_capturable,
updated_by,
unified_code,
unified_message,
connector_transaction_id,
payment_method_data,
authentication_type,
} => Self::ErrorUpdate {
connector,
status,
error_code,
error_message,
error_reason,
amount_capturable,
updated_by,
unified_code,
unified_message,
connector_transaction_id,
payment_method_data,
authentication_type,
},
DieselPaymentAttemptUpdate::CaptureUpdate {
amount_to_capture,
multiple_capture_count,
updated_by,
} => Self::CaptureUpdate {
amount_to_capture,
multiple_capture_count,
updated_by,
},
DieselPaymentAttemptUpdate::PreprocessingUpdate {
status,
payment_method_id,
connector_metadata,
preprocessing_step_id,
connector_transaction_id,
connector_response_reference_id,
updated_by,
} => Self::PreprocessingUpdate {
status,
payment_method_id,
connector_metadata,
preprocessing_step_id,
connector_transaction_id,
connector_response_reference_id,
updated_by,
},
DieselPaymentAttemptUpdate::RejectUpdate {
status,
error_code,
error_message,
updated_by,
} => Self::RejectUpdate {
status,
error_code,
error_message,
updated_by,
},
DieselPaymentAttemptUpdate::AmountToCaptureUpdate {
status,
amount_capturable,
updated_by,
} => Self::AmountToCaptureUpdate {
status,
amount_capturable,
updated_by,
},
DieselPaymentAttemptUpdate::ConnectorResponse {
authentication_data,
encoded_data,
connector_transaction_id,
connector,
charge_id,
updated_by,
} => Self::ConnectorResponse {
authentication_data,
encoded_data,
connector_transaction_id,
connector,
charge_id,
updated_by,
},
DieselPaymentAttemptUpdate::IncrementalAuthorizationAmountUpdate {
amount,
amount_capturable,
} => Self::IncrementalAuthorizationAmountUpdate {
amount,
amount_capturable,
},
DieselPaymentAttemptUpdate::AuthenticationUpdate {
status,
external_three_ds_authentication_attempted,
authentication_connector,
authentication_id,
updated_by,
} => Self::AuthenticationUpdate {
status,
external_three_ds_authentication_attempted,
authentication_connector,
authentication_id,
updated_by,
},
DieselPaymentAttemptUpdate::ManualUpdate {
status,
error_code,
error_message,
error_reason,
updated_by,
unified_code,
unified_message,
connector_transaction_id,
} => Self::ManualUpdate {
status,
error_code,
error_message,
error_reason,
updated_by,
unified_code,
unified_message,
connector_transaction_id,
},
}
}
}