mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-11-01 02:57:02 +08:00 
			
		
		
		
	refactor(connector): add amount conversion framework to volt (#4985)
This commit is contained in:
		| @ -1,14 +1,20 @@ | ||||
| pub mod transformers; | ||||
|  | ||||
| use std::fmt::Debug; | ||||
|  | ||||
| use common_utils::{crypto, ext_traits::ByteSliceExt, request::RequestContent}; | ||||
| use common_utils::{ | ||||
|     crypto, | ||||
|     ext_traits::ByteSliceExt, | ||||
|     request::RequestContent, | ||||
|     types::{AmountConvertor, MinorUnit, MinorUnitForConnector}, | ||||
| }; | ||||
| use error_stack::ResultExt; | ||||
| use masking::{ExposeInterface, PeekInterface}; | ||||
| use transformers as volt; | ||||
|  | ||||
| use self::transformers::webhook_headers; | ||||
| use super::utils; | ||||
| use super::{ | ||||
|     utils, | ||||
|     utils::{self as connector_utils}, | ||||
| }; | ||||
| use crate::{ | ||||
|     configs::settings, | ||||
|     core::errors::{self, CustomResult}, | ||||
| @ -27,8 +33,18 @@ use crate::{ | ||||
|     utils::BytesExt, | ||||
| }; | ||||
|  | ||||
| #[derive(Debug, Clone)] | ||||
| pub struct Volt; | ||||
| #[derive(Clone)] | ||||
| pub struct Volt { | ||||
|     amount_converter: &'static (dyn AmountConvertor<Output = MinorUnit> + Sync), | ||||
| } | ||||
|  | ||||
| impl Volt { | ||||
|     pub fn new() -> &'static Self { | ||||
|         &Self { | ||||
|             amount_converter: &MinorUnitForConnector, | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl api::Payment for Volt {} | ||||
| impl api::PaymentSession for Volt {} | ||||
| @ -306,12 +322,12 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P | ||||
|         req: &types::PaymentsAuthorizeRouterData, | ||||
|         _connectors: &settings::Connectors, | ||||
|     ) -> CustomResult<RequestContent, errors::ConnectorError> { | ||||
|         let connector_router_data = volt::VoltRouterData::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 = volt::VoltRouterData::from((amount, req)); | ||||
|         let connector_req = volt::VoltPaymentsRequest::try_from(&connector_router_data)?; | ||||
|         Ok(RequestContent::Json(Box::new(connector_req))) | ||||
|     } | ||||
| @ -560,12 +576,12 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon | ||||
|         req: &types::RefundsRouterData<api::Execute>, | ||||
|         _connectors: &settings::Connectors, | ||||
|     ) -> CustomResult<RequestContent, errors::ConnectorError> { | ||||
|         let connector_router_data = volt::VoltRouterData::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_router_data = volt::VoltRouterData::from((amount, req)); | ||||
|         let connector_req = volt::VoltRefundRequest::try_from(&connector_router_data)?; | ||||
|         Ok(RequestContent::Json(Box::new(connector_req))) | ||||
|     } | ||||
|  | ||||
| @ -1,4 +1,4 @@ | ||||
| use common_utils::{id_type, pii::Email}; | ||||
| use common_utils::{id_type, pii::Email, types::MinorUnit}; | ||||
| use diesel_models::enums; | ||||
| use masking::Secret; | ||||
| use serde::{Deserialize, Serialize}; | ||||
| @ -14,26 +14,16 @@ use crate::{ | ||||
| const PASSWORD: &str = "password"; | ||||
|  | ||||
| pub struct VoltRouterData<T> { | ||||
|     pub amount: i64, // The type of amount that a connector accepts, for example, String, i64, f64, etc. | ||||
|     pub amount: MinorUnit, // The type of amount that a connector accepts, for example, String, i64, f64, etc. | ||||
|     pub router_data: T, | ||||
| } | ||||
|  | ||||
| impl<T> TryFrom<(&api::CurrencyUnit, types::storage::enums::Currency, i64, T)> | ||||
|     for VoltRouterData<T> | ||||
| { | ||||
|     type Error = error_stack::Report<errors::ConnectorError>; | ||||
|     fn try_from( | ||||
|         (_currency_unit, _currency, amount, item): ( | ||||
|             &api::CurrencyUnit, | ||||
|             types::storage::enums::Currency, | ||||
|             i64, | ||||
|             T, | ||||
|         ), | ||||
|     ) -> Result<Self, Self::Error> { | ||||
|         Ok(Self { | ||||
| impl<T> From<(MinorUnit, T)> for VoltRouterData<T> { | ||||
|     fn from((amount, item): (MinorUnit, T)) -> Self { | ||||
|         Self { | ||||
|             amount, | ||||
|             router_data: item, | ||||
|         }) | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -46,7 +36,7 @@ pub mod webhook_headers { | ||||
| #[derive(Debug, Serialize)] | ||||
| #[serde(rename_all = "camelCase")] | ||||
| pub struct VoltPaymentsRequest { | ||||
|     amount: i64, | ||||
|     amount: MinorUnit, | ||||
|     currency_code: storage_enums::Currency, | ||||
|     #[serde(rename = "type")] | ||||
|     transaction_type: TransactionType, | ||||
| @ -437,7 +427,7 @@ impl From<VoltWebhookPaymentStatus> for enums::AttemptStatus { | ||||
| #[derive(Default, Debug, Serialize)] | ||||
| #[serde(rename_all = "camelCase")] | ||||
| pub struct VoltRefundRequest { | ||||
|     pub amount: i64, | ||||
|     pub amount: MinorUnit, | ||||
|     pub external_reference: String, | ||||
| } | ||||
|  | ||||
| @ -445,7 +435,7 @@ impl<F> TryFrom<&VoltRouterData<&types::RefundsRouterData<F>>> for VoltRefundReq | ||||
|     type Error = error_stack::Report<errors::ConnectorError>; | ||||
|     fn try_from(item: &VoltRouterData<&types::RefundsRouterData<F>>) -> Result<Self, Self::Error> { | ||||
|         Ok(Self { | ||||
|             amount: item.router_data.request.refund_amount, | ||||
|             amount: item.amount, | ||||
|             external_reference: item.router_data.request.refund_id.clone(), | ||||
|         }) | ||||
|     } | ||||
|  | ||||
| @ -500,7 +500,7 @@ impl ConnectorData { | ||||
|                     Ok(ConnectorEnum::Old(Box::new(connector::Trustpay::new()))) | ||||
|                 } | ||||
|                 enums::Connector::Tsys => Ok(ConnectorEnum::Old(Box::new(&connector::Tsys))), | ||||
|                 enums::Connector::Volt => Ok(ConnectorEnum::Old(Box::new(&connector::Volt))), | ||||
|                 enums::Connector::Volt => Ok(ConnectorEnum::Old(Box::new(connector::Volt::new()))), | ||||
|                 enums::Connector::Zen => Ok(ConnectorEnum::Old(Box::new(&connector::Zen))), | ||||
|                 enums::Connector::Zsl => Ok(ConnectorEnum::Old(Box::new(&connector::Zsl))), | ||||
|                 enums::Connector::Plaid => { | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Kiran Kumar
					Kiran Kumar