mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-10-31 10:06:32 +08:00 
			
		
		
		
	refactor(connector): added amount conversion framework for multisafepay (#4982)
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
		| @ -1,13 +1,15 @@ | ||||
| pub mod transformers; | ||||
|  | ||||
| use std::fmt::Debug; | ||||
|  | ||||
| use common_enums::AttemptStatus; | ||||
| use common_utils::request::RequestContent; | ||||
| use common_utils::{ | ||||
|     request::RequestContent, | ||||
|     types::{AmountConvertor, MinorUnit, MinorUnitForConnector}, | ||||
| }; | ||||
| use error_stack::{report, ResultExt}; | ||||
| use masking::ExposeInterface; | ||||
| use transformers as multisafepay; | ||||
|  | ||||
| use super::utils::{self as connector_utils}; | ||||
| use crate::{ | ||||
|     configs::settings, | ||||
|     connector::utils::{is_mandate_supported, PaymentMethodDataType}, | ||||
| @ -29,8 +31,18 @@ use crate::{ | ||||
|     utils::BytesExt, | ||||
| }; | ||||
|  | ||||
| #[derive(Debug, Clone)] | ||||
| pub struct Multisafepay; | ||||
| #[derive(Clone)] | ||||
| pub struct Multisafepay { | ||||
|     amount_converter: &'static (dyn AmountConvertor<Output = MinorUnit> + Sync), | ||||
| } | ||||
|  | ||||
| impl Multisafepay { | ||||
|     pub fn new() -> &'static Self { | ||||
|         &Self { | ||||
|             amount_converter: &MinorUnitForConnector, | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl<Flow, Request, Response> ConnectorCommonExt<Flow, Request, Response> for Multisafepay | ||||
| where | ||||
| @ -308,12 +320,12 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P | ||||
|         req: &types::PaymentsAuthorizeRouterData, | ||||
|         _connectors: &settings::Connectors, | ||||
|     ) -> CustomResult<RequestContent, errors::ConnectorError> { | ||||
|         let connector_router_data = multisafepay::MultisafepayRouterData::try_from(( | ||||
|             &self.get_currency_unit(), | ||||
|         let amount = connector_utils::convert_amount( | ||||
|             self.amount_converter, | ||||
|             req.request.minor_amount, | ||||
|             req.request.currency, | ||||
|             req.request.amount, | ||||
|             req, | ||||
|         ))?; | ||||
|         )?; | ||||
|         let connector_router_data = multisafepay::MultisafepayRouterData::from((amount, req)); | ||||
|         let connector_req = | ||||
|             multisafepay::MultisafepayPaymentsRequest::try_from(&connector_router_data)?; | ||||
|         Ok(RequestContent::Json(Box::new(connector_req))) | ||||
| @ -412,13 +424,14 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon | ||||
|         req: &types::RefundsRouterData<api::Execute>, | ||||
|         _connectors: &settings::Connectors, | ||||
|     ) -> CustomResult<RequestContent, errors::ConnectorError> { | ||||
|         let connector_req = multisafepay::MultisafepayRouterData::try_from(( | ||||
|             &self.get_currency_unit(), | ||||
|         let amount = connector_utils::convert_amount( | ||||
|             self.amount_converter, | ||||
|             req.request.minor_refund_amount, | ||||
|             req.request.currency, | ||||
|             req.request.refund_amount, | ||||
|             req, | ||||
|         ))?; | ||||
|         let connector_req = multisafepay::MultisafepayRefundRequest::try_from(&connector_req)?; | ||||
|         )?; | ||||
|         let connector_router_data = multisafepay::MultisafepayRouterData::from((amount, req)); | ||||
|         let connector_req = | ||||
|             multisafepay::MultisafepayRefundRequest::try_from(&connector_router_data)?; | ||||
|  | ||||
|         Ok(RequestContent::Json(Box::new(connector_req))) | ||||
|     } | ||||
|  | ||||
| @ -1,6 +1,9 @@ | ||||
| use api_models::enums::BankNames; | ||||
| use common_enums::AttemptStatus; | ||||
| use common_utils::pii::{Email, IpAddress}; | ||||
| use common_utils::{ | ||||
|     pii::{Email, IpAddress}, | ||||
|     types::{FloatMajorUnit, MinorUnit}, | ||||
| }; | ||||
| use masking::ExposeInterface; | ||||
| use serde::{Deserialize, Serialize}; | ||||
| use url::Url; | ||||
| @ -17,20 +20,16 @@ use crate::{ | ||||
|  | ||||
| #[derive(Debug, Serialize)] | ||||
| pub struct MultisafepayRouterData<T> { | ||||
|     amount: i64, | ||||
|     amount: MinorUnit, | ||||
|     router_data: T, | ||||
| } | ||||
|  | ||||
| impl<T> TryFrom<(&api::CurrencyUnit, enums::Currency, i64, T)> for MultisafepayRouterData<T> { | ||||
|     type Error = error_stack::Report<errors::ConnectorError>; | ||||
|  | ||||
|     fn try_from( | ||||
|         (_currency_unit, _currency, amount, item): (&api::CurrencyUnit, enums::Currency, i64, T), | ||||
|     ) -> Result<Self, Self::Error> { | ||||
|         Ok(Self { | ||||
| impl<T> From<(MinorUnit, T)> for MultisafepayRouterData<T> { | ||||
|     fn from((amount, item): (MinorUnit, T)) -> Self { | ||||
|         Self { | ||||
|             amount, | ||||
|             router_data: item, | ||||
|         }) | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -408,7 +407,7 @@ pub struct CheckoutOptions { | ||||
| #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)] | ||||
| pub struct Item { | ||||
|     pub name: String, | ||||
|     pub unit_price: f64, | ||||
|     pub unit_price: FloatMajorUnit, | ||||
|     pub description: Option<String>, | ||||
|     pub quantity: i64, | ||||
| } | ||||
| @ -426,7 +425,7 @@ pub struct MultisafepayPaymentsRequest { | ||||
|     pub gateway: Option<Gateway>, | ||||
|     pub order_id: String, | ||||
|     pub currency: String, | ||||
|     pub amount: i64, | ||||
|     pub amount: MinorUnit, | ||||
|     pub description: String, | ||||
|     pub payment_options: Option<PaymentOptions>, | ||||
|     pub customer: Option<Customer>, | ||||
| @ -890,7 +889,7 @@ pub struct Data { | ||||
|     pub payment_type: Option<String>, | ||||
|     pub order_id: String, | ||||
|     pub currency: Option<String>, | ||||
|     pub amount: Option<i64>, | ||||
|     pub amount: Option<MinorUnit>, | ||||
|     pub description: Option<String>, | ||||
|     pub capture: Option<String>, | ||||
|     pub payment_url: Option<Url>, | ||||
| @ -1018,7 +1017,7 @@ impl<F, T> | ||||
| #[derive(Debug, Serialize)] | ||||
| pub struct MultisafepayRefundRequest { | ||||
|     pub currency: diesel_models::enums::Currency, | ||||
|     pub amount: i64, | ||||
|     pub amount: MinorUnit, | ||||
|     pub description: Option<String>, | ||||
|     pub refund_order_id: Option<String>, | ||||
|     pub checkout_data: Option<ShoppingCart>, | ||||
|  | ||||
| @ -458,7 +458,7 @@ impl ConnectorData { | ||||
|                     Ok(ConnectorEnum::Old(Box::new(connector::Mifinity::new()))) | ||||
|                 } | ||||
|                 enums::Connector::Multisafepay => { | ||||
|                     Ok(ConnectorEnum::Old(Box::new(&connector::Multisafepay))) | ||||
|                     Ok(ConnectorEnum::Old(Box::new(connector::Multisafepay::new()))) | ||||
|                 } | ||||
|                 enums::Connector::Netcetera => { | ||||
|                     Ok(ConnectorEnum::Old(Box::new(&connector::Netcetera))) | ||||
|  | ||||
| @ -14,7 +14,7 @@ impl utils::Connector for MultisafepayTest { | ||||
|     fn get_data(&self) -> types::api::ConnectorData { | ||||
|         use router::connector::Multisafepay; | ||||
|         utils::construct_connector_data_old( | ||||
|             Box::new(&Multisafepay), | ||||
|             Box::new(Multisafepay::new()), | ||||
|             types::Connector::Multisafepay, | ||||
|             types::api::GetToken::Connector, | ||||
|             None, | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Kiran Kumar
					Kiran Kumar