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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion crates/router/src/connector/worldpay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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,
Expand Down
66 changes: 57 additions & 9 deletions crates/router/src/connector/worldpay/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The amount field needs to be retrieved from the WorldpayRouterData

Suggested change
value: PaymentValue {
value: PaymentValue {
amount : item.amount ,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

I think this will solve the issue.
I will commit the changes said.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The amount field needs to be retrieved from the WorldpayRouterData

@srujanchikke please check and inform if any further changes to be made.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Suraj3240 Why do we need to match with order_details ,As We are not using order details anywhere in payment request ?

Please refer to this pr for more info #2192

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Suraj3240 Why do we need to match with order_details ,As We are not using order details anywhere in payment request ?

Please refer to this pr for more info #2192

image

So I will have to code for all the payment methods like wallet,cards,etc. as given in this file, right?

image
And the amount will be like this, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to worry about the other payment methods mapping . But yes , as you mentioned amount should be mapped that way .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Suraj3240 Why do we need to match with order_details ,As We are not using order details anywhere in payment request ?

Please refer to this pr for more info #2192

In this PR link the fields are derived from the Order_details. And hence I used that method.
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Suraj3240 Why do we need to match with order_details ,As We are not using order details anywhere in payment request ?

Please refer to this pr for more info #2192

And following the method used in this PR, it is giving out errors.
image

So pls someone kindly help me.

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,
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"devDependencies": {
"newman": "git+ssh://[email protected]:knutties/newman.git#7106e194c15d49d066fa09d9a2f18b2238f3dba8"
}
}
}
Loading