Skip to content

Commit

Permalink
feat(connector): [Airwallex] Currency Unit Conversion (#2571)
Browse files Browse the repository at this point in the history
  • Loading branch information
SagarDevAchar authored Oct 15, 2023
1 parent 1f48860 commit 8971b17
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
20 changes: 18 additions & 2 deletions crates/router/src/connector/airwallex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl ConnectorCommon for Airwallex {
"airwallex"
}

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

fn common_get_content_type(&self) -> &'static str {
"application/json"
}
Expand Down Expand Up @@ -369,7 +373,13 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
&self,
req: &types::PaymentsAuthorizeRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let connector_req = airwallex::AirwallexPaymentsRequest::try_from(req)?;
let connector_router_data = airwallex::AirwallexRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.amount,
req,
))?;
let connector_req = airwallex::AirwallexPaymentsRequest::try_from(&connector_router_data)?;
let airwallex_req = types::RequestBody::log_and_get_request_body(
&connector_req,
utils::Encode::<airwallex::AirwallexPaymentsRequest>::encode_to_string_of_json,
Expand Down Expand Up @@ -810,7 +820,13 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
&self,
req: &types::RefundsRouterData<api::Execute>,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let connector_req = airwallex::AirwallexRefundRequest::try_from(req)?;
let connector_router_data = airwallex::AirwallexRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.refund_amount,
req,
))?;
let connector_req = airwallex::AirwallexRefundRequest::try_from(&connector_router_data)?;
let airwallex_req = types::RequestBody::log_and_get_request_body(
&connector_req,
utils::Encode::<airwallex::AirwallexRefundRequest>::encode_to_string_of_json,
Expand Down
62 changes: 49 additions & 13 deletions crates/router/src/connector/airwallex/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,38 @@ impl TryFrom<&types::PaymentsInitRouterData> for AirwallexIntentRequest {
}
}

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

impl<T>
TryFrom<(
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
)> for AirwallexRouterData<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> {
let amount = utils::get_amount_as_string(currency_unit, amount, currency)?;
Ok(Self {
amount,
router_data,
})
}
}

#[derive(Debug, Serialize)]
pub struct AirwallexPaymentsRequest {
// Unique ID to be sent for each transaction/operation request to the connector
Expand Down Expand Up @@ -125,16 +157,21 @@ pub struct AirwallexCardPaymentOptions {
auto_capture: bool,
}

impl TryFrom<&types::PaymentsAuthorizeRouterData> for AirwallexPaymentsRequest {
impl TryFrom<&AirwallexRouterData<&types::PaymentsAuthorizeRouterData>>
for AirwallexPaymentsRequest
{
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
fn try_from(
item: &AirwallexRouterData<&types::PaymentsAuthorizeRouterData>,
) -> Result<Self, Self::Error> {
let mut payment_method_options = None;
let payment_method = match item.request.payment_method_data.clone() {
let request = &item.router_data.request;
let payment_method = match request.payment_method_data.clone() {
api::PaymentMethodData::Card(ccard) => {
payment_method_options =
Some(AirwallexPaymentOptions::Card(AirwallexCardPaymentOptions {
auto_capture: matches!(
item.request.capture_method,
request.capture_method,
Some(enums::CaptureMethod::Automatic) | None
),
}));
Expand All @@ -158,7 +195,7 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for AirwallexPaymentsRequest {
request_id: Uuid::new_v4().to_string(),
payment_method,
payment_method_options,
return_url: item.request.complete_authorize_url.clone(),
return_url: request.complete_authorize_url.clone(),
})
}
}
Expand Down Expand Up @@ -538,17 +575,16 @@ pub struct AirwallexRefundRequest {
payment_intent_id: String,
}

impl<F> TryFrom<&types::RefundsRouterData<F>> for AirwallexRefundRequest {
impl<F> TryFrom<&AirwallexRouterData<&types::RefundsRouterData<F>>> for AirwallexRefundRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
fn try_from(
item: &AirwallexRouterData<&types::RefundsRouterData<F>>,
) -> Result<Self, Self::Error> {
Ok(Self {
request_id: Uuid::new_v4().to_string(),
amount: Some(utils::to_currency_base_unit(
item.request.refund_amount,
item.request.currency,
)?),
reason: item.request.reason.clone(),
payment_intent_id: item.request.connector_transaction_id.clone(),
amount: Some(item.amount.to_owned()),
reason: item.router_data.request.reason.clone(),
payment_intent_id: item.router_data.request.connector_transaction_id.clone(),
})
}
}
Expand Down

0 comments on commit 8971b17

Please sign in to comment.