mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-02 04:04:43 +08:00
refactor(connector): add amount conversion framework to bluesnap (#4825)
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com> Co-authored-by: Hrithikesh <61539176+hrithikesh026@users.noreply.github.com> Co-authored-by: Narayan Bhat <narayan.bhat@juspay.in>
This commit is contained in:
@ -1,12 +1,11 @@
|
||||
pub mod transformers;
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
use base64::Engine;
|
||||
use common_utils::{
|
||||
crypto,
|
||||
ext_traits::{StringExt, ValueExt},
|
||||
request::RequestContent,
|
||||
types::{AmountConvertor, StringMajorUnit, StringMajorUnitForConnector},
|
||||
};
|
||||
use diesel_models::enums;
|
||||
use error_stack::{report, ResultExt};
|
||||
@ -41,8 +40,18 @@ use crate::{
|
||||
|
||||
pub const BLUESNAP_TRANSACTION_NOT_FOUND: &str = "is not authorized to view merchant-transaction:";
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Bluesnap;
|
||||
#[derive(Clone)]
|
||||
pub struct Bluesnap {
|
||||
amount_converter: &'static (dyn AmountConvertor<Output = StringMajorUnit> + Sync),
|
||||
}
|
||||
|
||||
impl Bluesnap {
|
||||
pub fn new() -> &'static Self {
|
||||
&Self {
|
||||
amount_converter: &StringMajorUnitForConnector,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Flow, Request, Response> ConnectorCommonExt<Flow, Request, Response> for Bluesnap
|
||||
where
|
||||
@ -110,7 +119,7 @@ impl ConnectorCommon for Bluesnap {
|
||||
bluesnap::BluesnapErrors::Payment(error_response) => {
|
||||
let error_list = error_response.message.clone();
|
||||
let option_error_code_message = get_error_code_error_message_based_on_priority(
|
||||
Self.clone(),
|
||||
self.clone(),
|
||||
error_list.into_iter().map(|errors| errors.into()).collect(),
|
||||
);
|
||||
let reason = error_response
|
||||
@ -464,12 +473,13 @@ impl ConnectorIntegration<api::Capture, types::PaymentsCaptureData, types::Payme
|
||||
req: &types::PaymentsCaptureRouterData,
|
||||
_connectors: &settings::Connectors,
|
||||
) -> CustomResult<RequestContent, errors::ConnectorError> {
|
||||
let connector_router_data = bluesnap::BluesnapRouterData::try_from((
|
||||
&self.get_currency_unit(),
|
||||
let amount_to_capture = connector_utils::convert_amount(
|
||||
self.amount_converter,
|
||||
req.request.minor_amount_to_capture,
|
||||
req.request.currency,
|
||||
req.request.amount_to_capture,
|
||||
req,
|
||||
))?;
|
||||
)?;
|
||||
let connector_router_data =
|
||||
bluesnap::BluesnapRouterData::try_from((amount_to_capture, req))?;
|
||||
let connector_req = bluesnap::BluesnapCaptureRequest::try_from(&connector_router_data)?;
|
||||
Ok(RequestContent::Json(Box::new(connector_req)))
|
||||
}
|
||||
@ -651,12 +661,12 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
|
||||
req: &types::PaymentsAuthorizeRouterData,
|
||||
_connectors: &settings::Connectors,
|
||||
) -> CustomResult<RequestContent, errors::ConnectorError> {
|
||||
let connector_router_data = bluesnap::BluesnapRouterData::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 = bluesnap::BluesnapRouterData::try_from((amount, req))?;
|
||||
match req.is_three_ds() && req.request.is_card() {
|
||||
true => {
|
||||
let connector_req =
|
||||
@ -791,12 +801,12 @@ impl
|
||||
req: &types::PaymentsCompleteAuthorizeRouterData,
|
||||
_connectors: &settings::Connectors,
|
||||
) -> CustomResult<RequestContent, errors::ConnectorError> {
|
||||
let connector_router_data = bluesnap::BluesnapRouterData::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 = bluesnap::BluesnapRouterData::try_from((amount, req))?;
|
||||
let connector_req =
|
||||
bluesnap::BluesnapCompletePaymentsRequest::try_from(&connector_router_data)?;
|
||||
Ok(RequestContent::Json(Box::new(connector_req)))
|
||||
@ -888,12 +898,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 = bluesnap::BluesnapRouterData::try_from((
|
||||
&self.get_currency_unit(),
|
||||
let refund_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 = bluesnap::BluesnapRouterData::try_from((refund_amount, req))?;
|
||||
let connector_req = bluesnap::BluesnapRefundRequest::try_from(&connector_router_data)?;
|
||||
Ok(RequestContent::Json(Box::new(connector_req)))
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ use common_utils::{
|
||||
errors::CustomResult,
|
||||
ext_traits::{ByteSliceExt, StringExt, ValueExt},
|
||||
pii::Email,
|
||||
types::StringMajorUnit,
|
||||
};
|
||||
use error_stack::ResultExt;
|
||||
use masking::{ExposeInterface, PeekInterface};
|
||||
@ -32,16 +33,13 @@ const DISPLAY_METADATA: &str = "Y";
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct BluesnapRouterData<T> {
|
||||
pub amount: String,
|
||||
pub amount: StringMajorUnit,
|
||||
pub router_data: T,
|
||||
}
|
||||
|
||||
impl<T> TryFrom<(&api::CurrencyUnit, enums::Currency, i64, T)> for BluesnapRouterData<T> {
|
||||
impl<T> TryFrom<(StringMajorUnit, T)> for BluesnapRouterData<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> {
|
||||
let amount = utils::get_amount_as_string(currency_unit, amount, currency)?;
|
||||
fn try_from((amount, item): (StringMajorUnit, T)) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
amount,
|
||||
router_data: item,
|
||||
@ -52,7 +50,7 @@ impl<T> TryFrom<(&api::CurrencyUnit, enums::Currency, i64, T)> for BluesnapRoute
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct BluesnapPaymentsRequest {
|
||||
amount: String,
|
||||
amount: StringMajorUnit,
|
||||
#[serde(flatten)]
|
||||
payment_method: PaymentMethodDetails,
|
||||
currency: enums::Currency,
|
||||
@ -561,7 +559,7 @@ impl TryFrom<types::PaymentsSessionResponseRouterData<BluesnapWalletTokenRespons
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct BluesnapCompletePaymentsRequest {
|
||||
amount: String,
|
||||
amount: StringMajorUnit,
|
||||
currency: enums::Currency,
|
||||
card_transaction_type: BluesnapTxnType,
|
||||
pf_token: Secret<String>,
|
||||
@ -702,7 +700,7 @@ impl TryFrom<&types::PaymentsCancelRouterData> for BluesnapVoidRequest {
|
||||
pub struct BluesnapCaptureRequest {
|
||||
card_transaction_type: BluesnapTxnType,
|
||||
transaction_id: String,
|
||||
amount: Option<String>,
|
||||
amount: Option<StringMajorUnit>,
|
||||
}
|
||||
|
||||
impl TryFrom<&BluesnapRouterData<&types::PaymentsCaptureRouterData>> for BluesnapCaptureRequest {
|
||||
@ -829,7 +827,7 @@ pub struct BluesnapWalletTokenResponse {
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Refund {
|
||||
refund_transaction_id: String,
|
||||
amount: String,
|
||||
amount: StringMajorUnit,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
|
||||
@ -877,7 +875,7 @@ impl<F, T>
|
||||
|
||||
#[derive(Default, Debug, Serialize)]
|
||||
pub struct BluesnapRefundRequest {
|
||||
amount: Option<String>,
|
||||
amount: Option<StringMajorUnit>,
|
||||
reason: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
@ -323,7 +323,7 @@ impl ConnectorData {
|
||||
enums::Connector::Bankofamerica => Ok(Box::new(&connector::Bankofamerica)),
|
||||
enums::Connector::Billwerk => Ok(Box::new(&connector::Billwerk)),
|
||||
enums::Connector::Bitpay => Ok(Box::new(&connector::Bitpay)),
|
||||
enums::Connector::Bluesnap => Ok(Box::new(&connector::Bluesnap)),
|
||||
enums::Connector::Bluesnap => Ok(Box::new(connector::Bluesnap::new())),
|
||||
enums::Connector::Boku => Ok(Box::new(&connector::Boku)),
|
||||
enums::Connector::Braintree => Ok(Box::new(&connector::Braintree)),
|
||||
enums::Connector::Cashtocode => Ok(Box::new(&connector::Cashtocode)),
|
||||
|
||||
Reference in New Issue
Block a user