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(connector): [Worldpay] Currency Unit Conversion (#2436)

Surajb78109bc9343
diff --git a/crates/router/src/connector/worldpay.rs b/crates/router/src/connector/worldpay.rs
index 0c69cd981b..60a5fc8304 100644
--- a/crates/router/src/connector/worldpay.rs
+++ b/crates/router/src/connector/worldpay.rs
@@ -56,6 +56,10 @@ impl ConnectorCommon for Worldpay {
"worldpay"
}
+ fn get_currency_unit(&self) -> api::CurrencyUnit {
+ api::CurrencyUnit::Minor
+ }
+
fn common_get_content_type(&self) -> &'static str {
"application/vnd.worldpay.payments-v6+json"
}
@@ -428,7 +432,13 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
&self,
req: &types::PaymentsAuthorizeRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
- let connector_request = WorldpayPaymentsRequest::try_from(req)?;
+ let connector_router_data = worldpay::WorldpayRouterData::try_from((
+ &self.get_currency_unit(),
+ req.request.currency,
+ req.request.amount,
+ req,
+ ))?;
+ let connector_request = WorldpayPaymentsRequest::try_from(&connector_router_data)?;
let worldpay_payment_request = types::RequestBody::log_and_get_request_body(
&connector_request,
ext_traits::Encode::<WorldpayPaymentsRequest>::encode_to_string_of_json,
diff --git a/crates/router/src/connector/worldpay/transformers.rs b/crates/router/src/connector/worldpay/transformers.rs
index 3d467f4198..aabe27fc4e 100644
--- a/crates/router/src/connector/worldpay/transformers.rs
+++ b/crates/router/src/connector/worldpay/transformers.rs
@@ -3,15 +3,44 @@ use common_utils::errors::CustomResult;
use diesel_models::enums;
use error_stack::{IntoReport, ResultExt};
use masking::{PeekInterface, Secret};
+use serde::Serialize;
use super::{requests::*, response::*};
use crate::{
connector::utils,
consts,
core::errors,
- types::{self, api},
+ types::{self, api, PaymentsAuthorizeData, PaymentsResponseData},
};
+#[derive(Debug, Serialize)]
+pub struct WorldpayRouterData<T> {
+ amount: i64,
+ router_data: T,
+}
+impl<T>
+ TryFrom<(
+ &types::api::CurrencyUnit,
+ types::storage::enums::Currency,
+ i64,
+ T,
+ )> for WorldpayRouterData<T>
+{
+ type Error = error_stack::Report<errors::ConnectorError>;
+ fn try_from(
+ (_currency_unit, _currency, amount, item): (
+ &types::api::CurrencyUnit,
+ types::storage::enums::Currency,
+ i64,
+ T,
+ ),
+ ) -> Result<Self, Self::Error> {
+ Ok(Self {
+ amount,
+ router_data: item,
+ })
+ }
+}
fn fetch_payment_instrument(
payment_method: api::PaymentMethodData,
) -> CustomResult<PaymentInstrument, errors::ConnectorError> {
@@ -100,29 +129,48 @@ fn fetch_payment_instrument(
}
}
-impl TryFrom<&types::PaymentsAuthorizeRouterData> for WorldpayPaymentsRequest {
+impl
+ TryFrom<
+ &WorldpayRouterData<
+ &types::RouterData<
+ types::api::payments::Authorize,
+ PaymentsAuthorizeData,
+ PaymentsResponseData,
+ >,
+ >,
+ > for WorldpayPaymentsRequest
+{
type Error = error_stack::Report<errors::ConnectorError>;
- fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
+
+ fn try_from(
+ item: &WorldpayRouterData<
+ &types::RouterData<
+ types::api::payments::Authorize,
+ PaymentsAuthorizeData,
+ PaymentsResponseData,
+ >,
+ >,
+ ) -> Result<Self, Self::Error> {
Ok(Self {
instruction: Instruction {
value: PaymentValue {
- amount: item.request.amount,
- currency: item.request.currency.to_string(),
+ amount: item.amount,
+ currency: item.router_data.request.currency.to_string(),
},
narrative: InstructionNarrative {
- line1: item.merchant_id.clone().replace('_', "-"),
+ line1: item.router_data.merchant_id.clone().replace('_', "-"),
..Default::default()
},
payment_instrument: fetch_payment_instrument(
- item.request.payment_method_data.clone(),
+ item.router_data.request.payment_method_data.clone(),
)?,
debt_repayment: None,
},
merchant: Merchant {
- entity: item.attempt_id.clone().replace('_', "-"),
+ entity: item.router_data.attempt_id.clone().replace('_', "-"),
..Default::default()
},
- transaction_reference: item.attempt_id.clone(),
+ transaction_reference: item.router_data.attempt_id.clone(),
channel: None,
customer: None,
})
diff --git a/package.json b/package.json
index 0766efdcc1..45d2be3153 100644
--- a/package.json
+++ b/package.json
@@ -6,4 +6,4 @@
"devDependencies": {
"newman": "git+ssh://[email protected]:knutties/newman.git#7106e194c15d49d066fa09d9a2f18b2238f3dba8"
}
-}
+}
\ No newline at end of file