mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-02 04:04:43 +08:00
refactor(connector): add amount conversion framework to Forte (#5461)
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
@ -1,14 +1,16 @@
|
||||
pub mod transformers;
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
use base64::Engine;
|
||||
use common_utils::request::RequestContent;
|
||||
use common_utils::{
|
||||
request::RequestContent,
|
||||
types::{AmountConvertor, FloatMajorUnit, FloatMajorUnitForConnector},
|
||||
};
|
||||
use diesel_models::enums;
|
||||
use error_stack::{report, ResultExt};
|
||||
use masking::PeekInterface;
|
||||
use transformers as forte;
|
||||
|
||||
use super::utils::convert_amount;
|
||||
use crate::{
|
||||
configs::settings,
|
||||
connector::{
|
||||
@ -31,9 +33,18 @@ use crate::{
|
||||
},
|
||||
utils::BytesExt,
|
||||
};
|
||||
#[derive(Clone)]
|
||||
pub struct Forte {
|
||||
amount_converter: &'static (dyn AmountConvertor<Output = FloatMajorUnit> + Sync),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Forte;
|
||||
impl Forte {
|
||||
pub fn new() -> &'static Self {
|
||||
&Self {
|
||||
amount_converter: &FloatMajorUnitForConnector,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl api::Payment for Forte {}
|
||||
impl api::PaymentSession for Forte {}
|
||||
@ -225,7 +236,14 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
|
||||
req: &types::PaymentsAuthorizeRouterData,
|
||||
_connectors: &settings::Connectors,
|
||||
) -> CustomResult<RequestContent, errors::ConnectorError> {
|
||||
let connector_req = forte::FortePaymentsRequest::try_from(req)?;
|
||||
let amount = convert_amount(
|
||||
self.amount_converter,
|
||||
req.request.minor_amount,
|
||||
req.request.currency,
|
||||
)?;
|
||||
|
||||
let connector_router_data = forte::ForteRouterData::from((amount, req));
|
||||
let connector_req = forte::FortePaymentsRequest::try_from(&connector_router_data)?;
|
||||
Ok(RequestContent::Json(Box::new(connector_req)))
|
||||
}
|
||||
|
||||
@ -562,7 +580,14 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
|
||||
req: &types::RefundsRouterData<api::Execute>,
|
||||
_connectors: &settings::Connectors,
|
||||
) -> CustomResult<RequestContent, errors::ConnectorError> {
|
||||
let connector_req = forte::ForteRefundRequest::try_from(req)?;
|
||||
let refund_amount = convert_amount(
|
||||
self.amount_converter,
|
||||
req.request.minor_refund_amount,
|
||||
req.request.currency,
|
||||
)?;
|
||||
|
||||
let connector_router_data = forte::ForteRouterData::from((refund_amount, req));
|
||||
let connector_req = forte::ForteRefundRequest::try_from(&connector_router_data)?;
|
||||
Ok(RequestContent::Json(Box::new(connector_req)))
|
||||
}
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
use cards::CardNumber;
|
||||
use common_utils::types::FloatMajorUnit;
|
||||
use masking::{PeekInterface, Secret};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@ -10,10 +11,25 @@ use crate::{
|
||||
types::{self, api, domain, storage::enums, transformers::ForeignFrom},
|
||||
};
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct ForteRouterData<T> {
|
||||
pub amount: FloatMajorUnit,
|
||||
pub router_data: T,
|
||||
}
|
||||
|
||||
impl<T> From<(FloatMajorUnit, T)> for ForteRouterData<T> {
|
||||
fn from((amount, router_data): (FloatMajorUnit, T)) -> Self {
|
||||
Self {
|
||||
amount,
|
||||
router_data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct FortePaymentsRequest {
|
||||
action: ForteAction,
|
||||
authorization_amount: f64,
|
||||
authorization_amount: FloatMajorUnit,
|
||||
billing_address: BillingAddress,
|
||||
card: Card,
|
||||
}
|
||||
@ -62,9 +78,12 @@ impl TryFrom<utils::CardIssuer> for ForteCardType {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&types::PaymentsAuthorizeRouterData> for FortePaymentsRequest {
|
||||
impl TryFrom<&ForteRouterData<&types::PaymentsAuthorizeRouterData>> for FortePaymentsRequest {
|
||||
type Error = error_stack::Report<errors::ConnectorError>;
|
||||
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
|
||||
fn try_from(
|
||||
item_data: &ForteRouterData<&types::PaymentsAuthorizeRouterData>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let item = item_data.router_data;
|
||||
if item.request.currency != enums::Currency::USD {
|
||||
Err(errors::ConnectorError::NotImplemented(
|
||||
utils::get_unimplemented_payment_method_error_message("Forte"),
|
||||
@ -93,8 +112,7 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for FortePaymentsRequest {
|
||||
first_name: first_name.clone(),
|
||||
last_name: address.get_last_name().unwrap_or(first_name).clone(),
|
||||
};
|
||||
let authorization_amount =
|
||||
utils::to_currency_base_unit_asf64(item.request.amount, item.request.currency)?;
|
||||
let authorization_amount = item_data.amount;
|
||||
Ok(Self {
|
||||
action,
|
||||
authorization_amount,
|
||||
@ -245,7 +263,7 @@ pub struct FortePaymentsResponse {
|
||||
pub transaction_id: String,
|
||||
pub location_id: Secret<String>,
|
||||
pub action: ForteAction,
|
||||
pub authorization_amount: Option<f64>,
|
||||
pub authorization_amount: Option<FloatMajorUnit>,
|
||||
pub authorization_code: String,
|
||||
pub entered_by: String,
|
||||
pub billing_address: Option<BillingAddress>,
|
||||
@ -296,7 +314,7 @@ pub struct FortePaymentsSyncResponse {
|
||||
pub location_id: Secret<String>,
|
||||
pub status: FortePaymentStatus,
|
||||
pub action: ForteAction,
|
||||
pub authorization_amount: Option<f64>,
|
||||
pub authorization_amount: Option<FloatMajorUnit>,
|
||||
pub authorization_code: String,
|
||||
pub entered_by: String,
|
||||
pub billing_address: Option<BillingAddress>,
|
||||
@ -479,20 +497,22 @@ impl<F, T>
|
||||
#[derive(Default, Debug, Serialize)]
|
||||
pub struct ForteRefundRequest {
|
||||
action: String,
|
||||
authorization_amount: f64,
|
||||
authorization_amount: FloatMajorUnit,
|
||||
original_transaction_id: String,
|
||||
authorization_code: String,
|
||||
}
|
||||
|
||||
impl<F> TryFrom<&types::RefundsRouterData<F>> for ForteRefundRequest {
|
||||
impl<F> TryFrom<&ForteRouterData<&types::RefundsRouterData<F>>> for ForteRefundRequest {
|
||||
type Error = error_stack::Report<errors::ConnectorError>;
|
||||
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
|
||||
fn try_from(
|
||||
item_data: &ForteRouterData<&types::RefundsRouterData<F>>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let item = item_data.router_data;
|
||||
let trn_id = item.request.connector_transaction_id.clone();
|
||||
let connector_auth_id: ForteMeta =
|
||||
utils::to_connector_meta(item.request.connector_metadata.clone())?;
|
||||
let auth_code = connector_auth_id.auth_id;
|
||||
let authorization_amount =
|
||||
utils::to_currency_base_unit_asf64(item.request.refund_amount, item.request.currency)?;
|
||||
let authorization_amount = item_data.amount;
|
||||
Ok(Self {
|
||||
action: "reverse".to_string(),
|
||||
authorization_amount,
|
||||
@ -535,7 +555,7 @@ pub struct RefundResponse {
|
||||
pub transaction_id: String,
|
||||
pub original_transaction_id: String,
|
||||
pub action: String,
|
||||
pub authorization_amount: Option<f64>,
|
||||
pub authorization_amount: Option<FloatMajorUnit>,
|
||||
pub authorization_code: String,
|
||||
pub response: ResponseStatus,
|
||||
}
|
||||
|
||||
@ -393,7 +393,9 @@ impl ConnectorData {
|
||||
// enums::Connector::Fiservemea => {
|
||||
// Ok(ConnectorEnum::Old(Box::new(connector::Fiservemea)))
|
||||
// }
|
||||
enums::Connector::Forte => Ok(ConnectorEnum::Old(Box::new(&connector::Forte))),
|
||||
enums::Connector::Forte => {
|
||||
Ok(ConnectorEnum::Old(Box::new(connector::Forte::new())))
|
||||
}
|
||||
enums::Connector::Globalpay => {
|
||||
Ok(ConnectorEnum::Old(Box::new(connector::Globalpay::new())))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user