Skip to content

Commit

Permalink
feat(connector): [OpenNode] Currency Unit Conversion (#2645)
Browse files Browse the repository at this point in the history
  • Loading branch information
SagarDevAchar authored Oct 27, 2023
1 parent efed596 commit 88e1f29
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
12 changes: 11 additions & 1 deletion crates/router/src/connector/opennode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ impl ConnectorCommon for Opennode {
"opennode"
}

fn get_currency_unit(&self) -> api::CurrencyUnit {
api::CurrencyUnit::Minor
}

fn common_get_content_type(&self) -> &'static str {
"application/json"
}
Expand Down Expand Up @@ -169,7 +173,13 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
&self,
req: &types::PaymentsAuthorizeRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let req_obj = opennode::OpennodePaymentsRequest::try_from(req)?;
let connector_router_data = opennode::OpennodeRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.amount,
req,
))?;
let req_obj = opennode::OpennodePaymentsRequest::try_from(&connector_router_data)?;
let opennode_req = types::RequestBody::log_and_get_request_body(
&req_obj,
Encode::<opennode::OpennodePaymentsRequest>::encode_to_string_of_json,
Expand Down
60 changes: 48 additions & 12 deletions crates/router/src/connector/opennode/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,37 @@ use crate::{
types::{self, api, storage::enums},
};

#[derive(Debug, Serialize)]
pub struct OpennodeRouterData<T> {
pub amount: i64,
pub router_data: T,
}

impl<T>
TryFrom<(
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
)> for OpennodeRouterData<T>
{
type Error = error_stack::Report<errors::ConnectorError>;

fn try_from(
(_currency_unit, _currency, amount, router_data): (
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
),
) -> Result<Self, Self::Error> {
Ok(Self {
amount,
router_data,
})
}
}

//TODO: Fill the struct with respective fields
#[derive(Default, Debug, Serialize, Eq, PartialEq)]
pub struct OpennodePaymentsRequest {
Expand All @@ -22,9 +53,11 @@ pub struct OpennodePaymentsRequest {
order_id: String,
}

impl TryFrom<&types::PaymentsAuthorizeRouterData> for OpennodePaymentsRequest {
impl TryFrom<&OpennodeRouterData<&types::PaymentsAuthorizeRouterData>> for OpennodePaymentsRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
fn try_from(
item: &OpennodeRouterData<&types::PaymentsAuthorizeRouterData>,
) -> Result<Self, Self::Error> {
get_crypto_specific_payment_data(item)
}
}
Expand Down Expand Up @@ -146,11 +179,13 @@ pub struct OpennodeRefundRequest {
pub amount: i64,
}

impl<F> TryFrom<&types::RefundsRouterData<F>> for OpennodeRefundRequest {
impl<F> TryFrom<&OpennodeRouterData<&types::RefundsRouterData<F>>> for OpennodeRefundRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
fn try_from(
item: &OpennodeRouterData<&types::RefundsRouterData<F>>,
) -> Result<Self, Self::Error> {
Ok(Self {
amount: item.request.refund_amount,
amount: item.router_data.request.refund_amount,
})
}
}
Expand Down Expand Up @@ -222,14 +257,15 @@ pub struct OpennodeErrorResponse {
}

fn get_crypto_specific_payment_data(
item: &types::PaymentsAuthorizeRouterData,
item: &OpennodeRouterData<&types::PaymentsAuthorizeRouterData>,
) -> Result<OpennodePaymentsRequest, error_stack::Report<errors::ConnectorError>> {
let amount = item.request.amount;
let currency = item.request.currency.to_string();
let description = item.get_description()?;
let amount = item.amount;
let currency = item.router_data.request.currency.to_string();
let description = item.router_data.get_description()?;
let auto_settle = true;
let success_url = item.get_return_url()?;
let callback_url = item.request.get_webhook_url()?;
let success_url = item.router_data.get_return_url()?;
let callback_url = item.router_data.request.get_webhook_url()?;
let order_id = item.router_data.connector_request_reference_id.clone();

Ok(OpennodePaymentsRequest {
amount,
Expand All @@ -238,7 +274,7 @@ fn get_crypto_specific_payment_data(
auto_settle,
success_url,
callback_url,
order_id: item.connector_request_reference_id.clone(),
order_id,
})
}

Expand Down

0 comments on commit 88e1f29

Please sign in to comment.