Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

refactor(core): Modularize and encapsulate L2/L3 data access with structured getters (#9964)

awasthi215d6b85aee6f6
diff --git a/crates/hyperswitch_connectors/src/connectors/checkout/transformers.rs b/crates/hyperswitch_connectors/src/connectors/checkout/transformers.rs
index d48184f7d8..6570990fda 100644
--- a/crates/hyperswitch_connectors/src/connectors/checkout/transformers.rs
+++ b/crates/hyperswitch_connectors/src/connectors/checkout/transformers.rs
@@ -748,22 +748,22 @@ impl TryFrom<&CheckoutRouterData<&PaymentsAuthorizeRouterData>> for PaymentsRequ
&item.router_data.l2_l3_data
{
(
- Some(CheckoutCustomer {
- name: l2l3_data.customer_name.clone(),
- email: l2l3_data.customer_email.clone(),
+ l2l3_data.customer_info.as_ref().map(|_| CheckoutCustomer {
+ name: l2l3_data.get_customer_name(),
+ email: l2l3_data.get_customer_email(),
phone: Some(CheckoutPhoneDetails {
- country_code: l2l3_data.customer_phone_country_code.clone(),
- number: l2l3_data.customer_phone_number.clone(),
+ country_code: l2l3_data.get_customer_phone_country_code(),
+ number: l2l3_data.get_customer_phone_number(),
}),
- tax_number: l2l3_data.customer_tax_registration_id.clone(),
+ tax_number: l2l3_data.get_customer_tax_registration_id(),
}),
- Some(CheckoutProcessing {
- order_id: l2l3_data.merchant_order_reference_id.clone(),
- tax_amount: l2l3_data.order_tax_amount,
- discount_amount: l2l3_data.discount_amount,
- duty_amount: l2l3_data.duty_amount,
- shipping_amount: l2l3_data.shipping_cost,
- shipping_tax_amount: l2l3_data.shipping_amount_tax,
+ l2l3_data.order_info.as_ref().map(|_| CheckoutProcessing {
+ order_id: l2l3_data.get_merchant_order_reference_id(),
+ tax_amount: l2l3_data.get_order_tax_amount(),
+ discount_amount: l2l3_data.get_discount_amount(),
+ duty_amount: l2l3_data.get_duty_amount(),
+ shipping_amount: l2l3_data.get_shipping_cost(),
+ shipping_tax_amount: l2l3_data.get_shipping_amount_tax(),
}),
Some(CheckoutShipping {
address: Some(CheckoutAddress {
@@ -776,7 +776,7 @@ impl TryFrom<&CheckoutRouterData<&PaymentsAuthorizeRouterData>> for PaymentsRequ
}),
from_address_zip: l2l3_data.get_shipping_origin_zip().map(|zip| zip.expose()),
}),
- l2l3_data.order_details.as_ref().map(|details| {
+ l2l3_data.get_order_details().map(|details| {
details
.iter()
.map(|item| CheckoutLineItem {
diff --git a/crates/hyperswitch_connectors/src/connectors/nuvei/transformers.rs b/crates/hyperswitch_connectors/src/connectors/nuvei/transformers.rs
index 724d6f59bf..ed76a2ef49 100644
--- a/crates/hyperswitch_connectors/src/connectors/nuvei/transformers.rs
+++ b/crates/hyperswitch_connectors/src/connectors/nuvei/transformers.rs
@@ -1661,8 +1661,7 @@ fn get_l2_l3_items(
currency: enums::Currency,
) -> Result<Option<Vec<NuveiItem>>, error_stack::Report<errors::ConnectorError>> {
l2_l3_data.as_ref().map_or(Ok(None), |data| {
- data.order_details
- .as_ref()
+ data.get_order_details()
.map_or(Ok(None), |order_details_list| {
// Map each order to a Result<NuveiItem>
let results: Vec<Result<NuveiItem, error_stack::Report<errors::ConnectorError>>> =
@@ -1721,19 +1720,19 @@ fn get_amount_details(
) -> Result<Option<NuveiAmountDetails>, error_stack::Report<errors::ConnectorError>> {
l2_l3_data.as_ref().map_or(Ok(None), |data| {
let total_tax = data
- .order_tax_amount
+ .get_order_tax_amount()
.map(|amount| convert_amount(NUVEI_AMOUNT_CONVERTOR, amount, currency))
.transpose()?;
let total_shipping = data
- .shipping_cost
+ .get_shipping_cost()
.map(|amount| convert_amount(NUVEI_AMOUNT_CONVERTOR, amount, currency))
.transpose()?;
let total_discount = data
- .discount_amount
+ .get_discount_amount()
.map(|amount| convert_amount(NUVEI_AMOUNT_CONVERTOR, amount, currency))
.transpose()?;
let total_handling = data
- .duty_amount
+ .get_duty_amount()
.map(|amount| convert_amount(NUVEI_AMOUNT_CONVERTOR, amount, currency))
.transpose()?;
Ok(Some(NuveiAmountDetails {
@@ -1744,7 +1743,6 @@ fn get_amount_details(
}))
})
}
-
impl<F, Req> TryFrom<(&RouterData<F, Req, PaymentsResponseData>, String)> for NuveiPaymentsRequest
where
Req: NuveiAuthorizePreprocessingCommon + std::fmt::Debug,
diff --git a/crates/hyperswitch_connectors/src/connectors/worldpayvantiv/transformers.rs b/crates/hyperswitch_connectors/src/connectors/worldpayvantiv/transformers.rs
index fbc20dd92b..8fca268c2f 100644
--- a/crates/hyperswitch_connectors/src/connectors/worldpayvantiv/transformers.rs
+++ b/crates/hyperswitch_connectors/src/connectors/worldpayvantiv/transformers.rs
@@ -954,7 +954,7 @@ where
{
let l2_l3_data = item.get_optional_l2_l3_data();
if let Some(l2_l3_data) = l2_l3_data {
- let line_item_data = l2_l3_data.order_details.as_ref().map(|order_details| {
+ let line_item_data = l2_l3_data.get_order_details().map(|order_details| {
order_details
.iter()
.enumerate()
@@ -977,40 +977,41 @@ where
.collect()
});
- let tax_exempt = match l2_l3_data.tax_status {
+ let tax_exempt = match l2_l3_data.get_tax_status() {
Some(common_enums::TaxStatus::Exempt) => Some(true),
Some(common_enums::TaxStatus::Taxable) => Some(false),
None => None,
};
let customer_reference =
- get_vantiv_customer_reference(&l2_l3_data.merchant_order_reference_id);
-
- let detail_tax: Option<DetailTax> = if l2_l3_data.merchant_tax_registration_id.is_some()
- && l2_l3_data.order_details.is_some()
- {
- Some(DetailTax {
- tax_included_in_total: match tax_exempt {
- Some(false) => Some(true),
- Some(true) | None => Some(false),
- },
- card_acceptor_tax_id: l2_l3_data.merchant_tax_registration_id.clone(),
- tax_amount: l2_l3_data.order_details.as_ref().map(|orders| {
- orders
- .iter()
- .filter_map(|order| order.total_tax_amount)
- .fold(MinorUnit::zero(), |acc, tax| acc + tax)
- }),
- })
- } else {
- None
- };
+ get_vantiv_customer_reference(&l2_l3_data.get_merchant_order_reference_id());
+
+ let detail_tax: Option<DetailTax> =
+ if l2_l3_data.get_merchant_tax_registration_id().is_some()
+ && l2_l3_data.get_order_details().is_some()
+ {
+ Some(DetailTax {
+ tax_included_in_total: match tax_exempt {
+ Some(false) => Some(true),
+ Some(true) | None => Some(false),
+ },
+ card_acceptor_tax_id: l2_l3_data.get_merchant_tax_registration_id(),
+ tax_amount: l2_l3_data.get_order_details().map(|orders| {
+ orders
+ .iter()
+ .filter_map(|order| order.total_tax_amount)
+ .fold(MinorUnit::zero(), |acc, tax| acc + tax)
+ }),
+ })
+ } else {
+ None
+ };
let enhanced_data = EnhancedData {
customer_reference,
- sales_tax: l2_l3_data.order_tax_amount,
+ sales_tax: l2_l3_data.get_order_tax_amount(),
tax_exempt,
- discount_amount: l2_l3_data.discount_amount,
- shipping_amount: l2_l3_data.shipping_cost,
- duty_amount: l2_l3_data.duty_amount,
+ discount_amount: l2_l3_data.get_discount_amount(),
+ shipping_amount: l2_l3_data.get_shipping_cost(),
+ duty_amount: l2_l3_data.get_duty_amount(),
ship_from_postal_code: l2_l3_data.get_shipping_origin_zip(),
destination_postal_code: l2_l3_data.get_shipping_zip(),
destination_country_code: l2_l3_data.get_shipping_country(),
diff --git a/crates/hyperswitch_domain_models/src/router_data.rs b/crates/hyperswitch_domain_models/src/router_data.rs
index 62271d077b..945f8e27b2 100644
--- a/crates/hyperswitch_domain_models/src/router_data.rs
+++ b/crates/hyperswitch_domain_models/src/router_data.rs
@@ -121,26 +121,43 @@ pub struct RouterData<Flow, Request, Response> {
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct L2L3Data {
+ pub order_info: Option<OrderInfo>,
+ pub tax_info: Option<TaxInfo>,
+ pub customer_info: Option<CustomerInfo>,
+ pub shipping_details: Option<AddressDetails>,
+ pub billing_details: Option<BillingDetails>,
+}
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct OrderInfo {
pub order_date: Option<time::PrimitiveDateTime>,
- pub tax_status: Option<common_enums::TaxStatus>,
- pub customer_tax_registration_id: Option<Secret<String>>,
pub order_details: Option<Vec<api_models::payments::OrderDetailsWithAmount>>,
+ pub merchant_order_reference_id: Option<String>,
pub discount_amount: Option<MinorUnit>,
pub shipping_cost: Option<MinorUnit>,
- pub shipping_amount_tax: Option<MinorUnit>,
pub duty_amount: Option<MinorUnit>,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct TaxInfo {
+ pub tax_status: Option<common_enums::TaxStatus>,
+ pub customer_tax_registration_id: Option<Secret<String>>,
+ pub merchant_tax_registration_id: Option<Secret<String>>,
+ pub shipping_amount_tax: Option<MinorUnit>,
pub order_tax_amount: Option<MinorUnit>,
- pub merchant_order_reference_id: Option<String>,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct CustomerInfo {
pub customer_id: Option<id_type::CustomerId>,
- pub billing_address_city: Option<String>,
- pub merchant_tax_registration_id: Option<Secret<String>>,
pub customer_email: Option<common_utils::pii::Email>,
pub customer_name: Option<Secret<String>>,
pub customer_phone_number: Option<Secret<String>>,
pub customer_phone_country_code: Option<String>,
- pub shipping_details: Option<AddressDetails>,
}
-
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct BillingDetails {
+ pub address_city: Option<String>,
+}
impl L2L3Data {
pub fn get_shipping_country(&self) -> Option<common_enums::enums::CountryAlpha2> {
self.shipping_details
@@ -183,8 +200,100 @@ impl L2L3Data {
.as_ref()
.and_then(|address| address.line2.clone())
}
-}
+ pub fn get_order_date(&self) -> Option<time::PrimitiveDateTime> {
+ self.order_info.as_ref().and_then(|order| order.order_date)
+ }
+
+ pub fn get_order_details(&self) -> Option<Vec<api_models::payments::OrderDetailsWithAmount>> {
+ self.order_info
+ .as_ref()
+ .and_then(|order| order.order_details.clone())
+ }
+
+ pub fn get_merchant_order_reference_id(&self) -> Option<String> {
+ self.order_info
+ .as_ref()
+ .and_then(|order| order.merchant_order_reference_id.clone())
+ }
+
+ pub fn get_discount_amount(&self) -> Option<MinorUnit> {
+ self.order_info
+ .as_ref()
+ .and_then(|order| order.discount_amount)
+ }
+
+ pub fn get_shipping_cost(&self) -> Option<MinorUnit> {
+ self.order_info
+ .as_ref()
+ .and_then(|order| order.shipping_cost)
+ }
+
+ pub fn get_duty_amount(&self) -> Option<MinorUnit> {
+ self.order_info.as_ref().and_then(|order| order.duty_amount)
+ }
+
+ pub fn get_tax_status(&self) -> Option<common_enums::TaxStatus> {
+ self.tax_info.as_ref().and_then(|tax| tax.tax_status)
+ }
+
+ pub fn get_customer_tax_registration_id(&self) -> Option<Secret<String>> {
+ self.tax_info
+ .as_ref()
+ .and_then(|tax| tax.customer_tax_registration_id.clone())
+ }
+
+ pub fn get_merchant_tax_registration_id(&self) -> Option<Secret<String>> {
+ self.tax_info
+ .as_ref()
+ .and_then(|tax| tax.merchant_tax_registration_id.clone())
+ }
+
+ pub fn get_shipping_amount_tax(&self) -> Option<MinorUnit> {
+ self.tax_info
+ .as_ref()
+ .and_then(|tax| tax.shipping_amount_tax)
+ }
+
+ pub fn get_order_tax_amount(&self) -> Option<MinorUnit> {
+ self.tax_info.as_ref().and_then(|tax| tax.order_tax_amount)
+ }
+
+ pub fn get_customer_id(&self) -> Option<id_type::CustomerId> {
+ self.customer_info
+ .as_ref()
+ .and_then(|customer| customer.customer_id.clone())
+ }
+
+ pub fn get_customer_email(&self) -> Option<common_utils::pii::Email> {
+ self.customer_info
+ .as_ref()
+ .and_then(|customer| customer.customer_email.clone())
+ }
+
+ pub fn get_customer_name(&self) -> Option<Secret<String>> {
+ self.customer_info
+ .as_ref()
+ .and_then(|customer| customer.customer_name.clone())
+ }
+
+ pub fn get_customer_phone_number(&self) -> Option<Secret<String>> {
+ self.customer_info
+ .as_ref()
+ .and_then(|customer| customer.customer_phone_number.clone())
+ }
+
+ pub fn get_customer_phone_country_code(&self) -> Option<String> {
+ self.customer_info
+ .as_ref()
+ .and_then(|customer| customer.customer_phone_country_code.clone())
+ }
+ pub fn get_billing_city(&self) -> Option<String> {
+ self.billing_details
+ .as_ref()
+ .and_then(|billing| billing.address_city.clone())
+ }
+}
// Different patterns of authentication.
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
#[serde(tag = "auth_type")]
diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs
index a237519112..abe86f6160 100644
--- a/crates/router/src/core/payments/transformers.rs
+++ b/crates/router/src/core/payments/transformers.rs
@@ -1774,42 +1774,58 @@ where
.get_merchant_tax_registration_id();
Box::new(types::L2L3Data {
- order_date: payment_data.payment_intent.order_date,
- tax_status: payment_data.payment_intent.tax_status,
- customer_tax_registration_id: customer.as_ref().and_then(|c| {
- c.tax_registration_id
+ order_info: Some(types::OrderInfo {
+ order_date: payment_data.payment_intent.order_date,
+ order_details: order_details.clone(),
+ merchant_order_reference_id: payment_data
+ .payment_intent
+ .merchant_order_reference_id
+ .clone(),
+ discount_amount: payment_data.payment_intent.discount_amount,
+ shipping_cost: payment_data.payment_intent.shipping_cost,
+ duty_amount: payment_data.payment_intent.duty_amount,
+ }),
+ tax_info: Some(types::TaxInfo {
+ tax_status: payment_data.payment_intent.tax_status,
+ customer_tax_registration_id: customer.as_ref().and_then(|customer| {
+ customer
+ .tax_registration_id
+ .as_ref()
+ .map(|tax_registration_id| tax_registration_id.clone().into_inner())
+ }),
+ merchant_tax_registration_id,
+ shipping_amount_tax: payment_data.payment_intent.shipping_amount_tax,
+ order_tax_amount: payment_data
+ .payment_attempt
+ .net_amount
+ .get_order_tax_amount(),
+ }),
+ customer_info: Some(types::CustomerInfo {
+ customer_id: payment_data.payment_intent.customer_id.clone(),
+ customer_email: payment_data.email,
+ customer_name: customer.as_ref().and_then(|customer_data| {
+ customer_data
+ .name
+ .as_ref()
+ .map(|name| name.clone().into_inner())
+ }),
+ customer_phone_number: customer.as_ref().and_then(|customer_data| {
+ customer_data
+ .phone
+ .as_ref()
+ .map(|phone| phone.clone().into_inner())
+ }),
+ customer_phone_country_code: customer
.as_ref()
- .map(|e| e.clone().into_inner())
+ .and_then(|customer_data| customer_data.phone_country_code.clone()),
}),
- order_details: order_details.clone(),
- discount_amount: payment_data.payment_intent.discount_amount,
- shipping_cost: payment_data.payment_intent.shipping_cost,
- shipping_amount_tax: payment_data.payment_intent.shipping_amount_tax,
- duty_amount: payment_data.payment_intent.duty_amount,
- order_tax_amount: payment_data
- .payment_attempt
- .net_amount
- .get_order_tax_amount(),
- merchant_order_reference_id: payment_data
- .payment_intent
- .merchant_order_reference_id
- .clone(),
- customer_id: payment_data.payment_intent.customer_id.clone(),
- billing_address_city: billing_address
+ billing_details: billing_address
.as_ref()
.and_then(|addr| addr.address.as_ref())
- .and_then(|details| details.city.clone()),
- merchant_tax_registration_id,
- customer_name: customer
- .as_ref()
- .and_then(|c| c.name.as_ref().map(|e| e.clone().into_inner())),
- customer_email: payment_data.email,
- customer_phone_number: customer
- .as_ref()
- .and_then(|c| c.phone.as_ref().map(|e| e.clone().into_inner())),
- customer_phone_country_code: customer
- .as_ref()
- .and_then(|c| c.phone_country_code.clone()),
+ .and_then(|details| details.city.clone())
+ .map(|city| types::BillingDetails {
+ address_city: Some(city),
+ }),
shipping_details: shipping_address
.and_then(|address| address.address.as_ref())
.cloned(),
diff --git a/crates/router/src/types.rs b/crates/router/src/types.rs
index adc21e2ea3..3269a75a89 100644
--- a/crates/router/src/types.rs
+++ b/crates/router/src/types.rs
@@ -49,9 +49,9 @@ pub use hyperswitch_domain_models::{
payment_address::PaymentAddress,
router_data::{
AccessToken, AccessTokenAuthenticationResponse, AdditionalPaymentMethodConnectorResponse,
- ConnectorAuthType, ConnectorResponseData, ErrorResponse, GooglePayPaymentMethodDetails,
- GooglePayPredecryptDataInternal, L2L3Data, PaymentMethodBalance, PaymentMethodToken,
- RecurringMandatePaymentData, RouterData,
+ BillingDetails, ConnectorAuthType, ConnectorResponseData, CustomerInfo, ErrorResponse,
+ GooglePayPaymentMethodDetails, GooglePayPredecryptDataInternal, L2L3Data, OrderInfo,
+ PaymentMethodBalance, PaymentMethodToken, RecurringMandatePaymentData, RouterData, TaxInfo,
},
router_data_v2::{
AccessTokenFlowData, AuthenticationTokenFlowData, DisputesFlowData,