Skip to content

Commit b78109b

Browse files
authored
refactor(connector): [Worldpay] Currency Unit Conversion (#2436)
Co-authored-by: Suraj <surajshinde.com>
1 parent 550377a commit b78109b

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed

crates/router/src/connector/worldpay.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ impl ConnectorCommon for Worldpay {
5656
"worldpay"
5757
}
5858

59+
fn get_currency_unit(&self) -> api::CurrencyUnit {
60+
api::CurrencyUnit::Minor
61+
}
62+
5963
fn common_get_content_type(&self) -> &'static str {
6064
"application/vnd.worldpay.payments-v6+json"
6165
}
@@ -428,7 +432,13 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
428432
&self,
429433
req: &types::PaymentsAuthorizeRouterData,
430434
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
431-
let connector_request = WorldpayPaymentsRequest::try_from(req)?;
435+
let connector_router_data = worldpay::WorldpayRouterData::try_from((
436+
&self.get_currency_unit(),
437+
req.request.currency,
438+
req.request.amount,
439+
req,
440+
))?;
441+
let connector_request = WorldpayPaymentsRequest::try_from(&connector_router_data)?;
432442
let worldpay_payment_request = types::RequestBody::log_and_get_request_body(
433443
&connector_request,
434444
ext_traits::Encode::<WorldpayPaymentsRequest>::encode_to_string_of_json,

crates/router/src/connector/worldpay/transformers.rs

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,44 @@ use common_utils::errors::CustomResult;
33
use diesel_models::enums;
44
use error_stack::{IntoReport, ResultExt};
55
use masking::{PeekInterface, Secret};
6+
use serde::Serialize;
67

78
use super::{requests::*, response::*};
89
use crate::{
910
connector::utils,
1011
consts,
1112
core::errors,
12-
types::{self, api},
13+
types::{self, api, PaymentsAuthorizeData, PaymentsResponseData},
1314
};
1415

16+
#[derive(Debug, Serialize)]
17+
pub struct WorldpayRouterData<T> {
18+
amount: i64,
19+
router_data: T,
20+
}
21+
impl<T>
22+
TryFrom<(
23+
&types::api::CurrencyUnit,
24+
types::storage::enums::Currency,
25+
i64,
26+
T,
27+
)> for WorldpayRouterData<T>
28+
{
29+
type Error = error_stack::Report<errors::ConnectorError>;
30+
fn try_from(
31+
(_currency_unit, _currency, amount, item): (
32+
&types::api::CurrencyUnit,
33+
types::storage::enums::Currency,
34+
i64,
35+
T,
36+
),
37+
) -> Result<Self, Self::Error> {
38+
Ok(Self {
39+
amount,
40+
router_data: item,
41+
})
42+
}
43+
}
1544
fn fetch_payment_instrument(
1645
payment_method: api::PaymentMethodData,
1746
) -> CustomResult<PaymentInstrument, errors::ConnectorError> {
@@ -100,29 +129,48 @@ fn fetch_payment_instrument(
100129
}
101130
}
102131

103-
impl TryFrom<&types::PaymentsAuthorizeRouterData> for WorldpayPaymentsRequest {
132+
impl
133+
TryFrom<
134+
&WorldpayRouterData<
135+
&types::RouterData<
136+
types::api::payments::Authorize,
137+
PaymentsAuthorizeData,
138+
PaymentsResponseData,
139+
>,
140+
>,
141+
> for WorldpayPaymentsRequest
142+
{
104143
type Error = error_stack::Report<errors::ConnectorError>;
105-
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
144+
145+
fn try_from(
146+
item: &WorldpayRouterData<
147+
&types::RouterData<
148+
types::api::payments::Authorize,
149+
PaymentsAuthorizeData,
150+
PaymentsResponseData,
151+
>,
152+
>,
153+
) -> Result<Self, Self::Error> {
106154
Ok(Self {
107155
instruction: Instruction {
108156
value: PaymentValue {
109-
amount: item.request.amount,
110-
currency: item.request.currency.to_string(),
157+
amount: item.amount,
158+
currency: item.router_data.request.currency.to_string(),
111159
},
112160
narrative: InstructionNarrative {
113-
line1: item.merchant_id.clone().replace('_', "-"),
161+
line1: item.router_data.merchant_id.clone().replace('_', "-"),
114162
..Default::default()
115163
},
116164
payment_instrument: fetch_payment_instrument(
117-
item.request.payment_method_data.clone(),
165+
item.router_data.request.payment_method_data.clone(),
118166
)?,
119167
debt_repayment: None,
120168
},
121169
merchant: Merchant {
122-
entity: item.attempt_id.clone().replace('_', "-"),
170+
entity: item.router_data.attempt_id.clone().replace('_', "-"),
123171
..Default::default()
124172
},
125-
transaction_reference: item.attempt_id.clone(),
173+
transaction_reference: item.router_data.attempt_id.clone(),
126174
channel: None,
127175
customer: None,
128176
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
"devDependencies": {
77
"newman": "git+ssh://[email protected]:knutties/newman.git#7106e194c15d49d066fa09d9a2f18b2238f3dba8"
88
}
9-
}
9+
}

0 commit comments

Comments
 (0)