refactor(connector): [CyberSource] Enhance currency Mapping with ConnectorCurrencyCommon Trait (#2626)

This commit is contained in:
mdrokz
2023-10-25 14:59:50 +05:30
committed by GitHub
parent bb86cc2d04
commit f2f8170ae1
3 changed files with 59 additions and 11 deletions

View File

@ -93,6 +93,10 @@ impl ConnectorCommon for Cybersource {
connectors.cybersource.base_url.as_ref()
}
fn get_currency_unit(&self) -> api::CurrencyUnit {
api::CurrencyUnit::Minor
}
fn build_error_response(
&self,
res: types::Response,
@ -468,7 +472,14 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
&self,
req: &types::PaymentsAuthorizeRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let connector_request = cybersource::CybersourcePaymentsRequest::try_from(req)?;
let connector_router_data = cybersource::CybersourceRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.amount,
req,
))?;
let connector_request =
cybersource::CybersourcePaymentsRequest::try_from(&connector_router_data)?;
let cybersource_payments_request = types::RequestBody::log_and_get_request_body(
&connector_request,
utils::Encode::<cybersource::CybersourcePaymentsRequest>::encode_to_string_of_json,

View File

@ -15,6 +15,37 @@ use crate::{
},
};
#[derive(Debug, Serialize)]
pub struct CybersourceRouterData<T> {
pub amount: String,
pub router_data: T,
}
impl<T>
TryFrom<(
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
)> for CybersourceRouterData<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> {
let amount = utils::get_amount_as_string(currency_unit, amount, currency)?;
Ok(Self {
amount,
router_data: item,
})
}
}
#[derive(Default, Debug, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct CybersourcePaymentsRequest {
@ -109,27 +140,33 @@ fn build_bill_to(
})
}
impl TryFrom<&types::PaymentsAuthorizeRouterData> for CybersourcePaymentsRequest {
impl TryFrom<&CybersourceRouterData<&types::PaymentsAuthorizeRouterData>>
for CybersourcePaymentsRequest
{
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
match item.request.payment_method_data.clone() {
fn try_from(
item: &CybersourceRouterData<&types::PaymentsAuthorizeRouterData>,
) -> Result<Self, Self::Error> {
match item.router_data.request.payment_method_data.clone() {
api::PaymentMethodData::Card(ccard) => {
let phone = item.get_billing_phone()?;
let phone = item.router_data.get_billing_phone()?;
let phone_number = phone.get_number()?;
let country_code = phone.get_country_code()?;
let number_with_code =
Secret::new(format!("{}{}", country_code, phone_number.peek()));
let email = item
.router_data
.request
.email
.clone()
.ok_or_else(utils::missing_field_err("email"))?;
let bill_to = build_bill_to(item.get_billing()?, email, number_with_code)?;
let bill_to =
build_bill_to(item.router_data.get_billing()?, email, number_with_code)?;
let order_information = OrderInformationWithBill {
amount_details: Amount {
total_amount: item.request.amount.to_string(),
currency: item.request.currency.to_string().to_uppercase(),
total_amount: item.amount.to_owned(),
currency: item.router_data.request.currency.to_string().to_uppercase(),
},
bill_to,
};
@ -145,14 +182,14 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for CybersourcePaymentsRequest
let processing_information = ProcessingInformation {
capture: matches!(
item.request.capture_method,
item.router_data.request.capture_method,
Some(enums::CaptureMethod::Automatic) | None
),
capture_options: None,
};
let client_reference_information = ClientReferenceInformation {
code: Some(item.connector_request_reference_id.clone()),
code: Some(item.router_data.connector_request_reference_id.clone()),
};
Ok(Self {

View File

@ -1077,7 +1077,7 @@ pub fn get_amount_as_string(
currency: diesel_models::enums::Currency,
) -> Result<String, error_stack::Report<errors::ConnectorError>> {
let amount = match currency_unit {
types::api::CurrencyUnit::Minor => amount.to_string(),
types::api::CurrencyUnit::Minor => to_currency_lower_unit(amount.to_string(), currency)?,
types::api::CurrencyUnit::Base => to_currency_base_unit(amount, currency)?,
};
Ok(amount)