mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-28 04:04:55 +08:00
refactor(connector): [Checkout]Enhance currency Mapping with ConnectorCurrencyCommon Trait (#2192)
This commit is contained in:
@ -61,6 +61,10 @@ impl ConnectorCommon for Checkout {
|
|||||||
"checkout"
|
"checkout"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_currency_unit(&self) -> api::CurrencyUnit {
|
||||||
|
api::CurrencyUnit::Minor
|
||||||
|
}
|
||||||
|
|
||||||
fn common_get_content_type(&self) -> &'static str {
|
fn common_get_content_type(&self) -> &'static str {
|
||||||
"application/json"
|
"application/json"
|
||||||
}
|
}
|
||||||
@ -305,7 +309,13 @@ impl ConnectorIntegration<api::Capture, types::PaymentsCaptureData, types::Payme
|
|||||||
&self,
|
&self,
|
||||||
req: &types::PaymentsCaptureRouterData,
|
req: &types::PaymentsCaptureRouterData,
|
||||||
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
|
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
|
||||||
let connector_req = checkout::PaymentCaptureRequest::try_from(req)?;
|
let connector_router_data = checkout::CheckoutRouterData::try_from((
|
||||||
|
&self.get_currency_unit(),
|
||||||
|
req.request.currency,
|
||||||
|
req.request.amount_to_capture,
|
||||||
|
req,
|
||||||
|
))?;
|
||||||
|
let connector_req = checkout::PaymentCaptureRequest::try_from(&connector_router_data)?;
|
||||||
let checkout_req = types::RequestBody::log_and_get_request_body(
|
let checkout_req = types::RequestBody::log_and_get_request_body(
|
||||||
&connector_req,
|
&connector_req,
|
||||||
utils::Encode::<checkout::PaymentCaptureRequest>::encode_to_string_of_json,
|
utils::Encode::<checkout::PaymentCaptureRequest>::encode_to_string_of_json,
|
||||||
@ -484,7 +494,13 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
|
|||||||
&self,
|
&self,
|
||||||
req: &types::PaymentsAuthorizeRouterData,
|
req: &types::PaymentsAuthorizeRouterData,
|
||||||
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
|
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
|
||||||
let connector_req = checkout::PaymentsRequest::try_from(req)?;
|
let connector_router_data = checkout::CheckoutRouterData::try_from((
|
||||||
|
&self.get_currency_unit(),
|
||||||
|
req.request.currency,
|
||||||
|
req.request.amount,
|
||||||
|
req,
|
||||||
|
))?;
|
||||||
|
let connector_req = checkout::PaymentsRequest::try_from(&connector_router_data)?;
|
||||||
let checkout_req = types::RequestBody::log_and_get_request_body(
|
let checkout_req = types::RequestBody::log_and_get_request_body(
|
||||||
&connector_req,
|
&connector_req,
|
||||||
utils::Encode::<checkout::PaymentsRequest>::encode_to_string_of_json,
|
utils::Encode::<checkout::PaymentsRequest>::encode_to_string_of_json,
|
||||||
@ -656,7 +672,13 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
|
|||||||
&self,
|
&self,
|
||||||
req: &types::RefundsRouterData<api::Execute>,
|
req: &types::RefundsRouterData<api::Execute>,
|
||||||
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
|
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
|
||||||
let connector_req = checkout::RefundRequest::try_from(req)?;
|
let connector_router_data = checkout::CheckoutRouterData::try_from((
|
||||||
|
&self.get_currency_unit(),
|
||||||
|
req.request.currency,
|
||||||
|
req.request.refund_amount,
|
||||||
|
req,
|
||||||
|
))?;
|
||||||
|
let connector_req = checkout::RefundRequest::try_from(&connector_router_data)?;
|
||||||
let body = types::RequestBody::log_and_get_request_body(
|
let body = types::RequestBody::log_and_get_request_body(
|
||||||
&connector_req,
|
&connector_req,
|
||||||
utils::Encode::<checkout::RefundRequest>::encode_to_string_of_json,
|
utils::Encode::<checkout::RefundRequest>::encode_to_string_of_json,
|
||||||
|
|||||||
@ -13,6 +13,36 @@ use crate::{
|
|||||||
types::{self, api, storage::enums, transformers::ForeignFrom},
|
types::{self, api, storage::enums, transformers::ForeignFrom},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
pub struct CheckoutRouterData<T> {
|
||||||
|
pub amount: i64,
|
||||||
|
pub router_data: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T>
|
||||||
|
TryFrom<(
|
||||||
|
&types::api::CurrencyUnit,
|
||||||
|
types::storage::enums::Currency,
|
||||||
|
i64,
|
||||||
|
T,
|
||||||
|
)> for CheckoutRouterData<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> {
|
||||||
|
Ok(Self {
|
||||||
|
amount,
|
||||||
|
router_data: item,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
#[serde(rename_all = "lowercase")]
|
#[serde(rename_all = "lowercase")]
|
||||||
#[serde(tag = "type", content = "token_data")]
|
#[serde(tag = "type", content = "token_data")]
|
||||||
@ -235,10 +265,12 @@ impl TryFrom<&types::ConnectorAuthType> for CheckoutAuthType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl TryFrom<&types::PaymentsAuthorizeRouterData> for PaymentsRequest {
|
impl TryFrom<&CheckoutRouterData<&types::PaymentsAuthorizeRouterData>> for PaymentsRequest {
|
||||||
type Error = error_stack::Report<errors::ConnectorError>;
|
type Error = error_stack::Report<errors::ConnectorError>;
|
||||||
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
|
fn try_from(
|
||||||
let source_var = match item.request.payment_method_data.clone() {
|
item: &CheckoutRouterData<&types::PaymentsAuthorizeRouterData>,
|
||||||
|
) -> Result<Self, Self::Error> {
|
||||||
|
let source_var = match item.router_data.request.payment_method_data.clone() {
|
||||||
api::PaymentMethodData::Card(ccard) => {
|
api::PaymentMethodData::Card(ccard) => {
|
||||||
let a = PaymentSource::Card(CardSource {
|
let a = PaymentSource::Card(CardSource {
|
||||||
source_type: CheckoutSourceTypes::Card,
|
source_type: CheckoutSourceTypes::Card,
|
||||||
@ -254,7 +286,7 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for PaymentsRequest {
|
|||||||
| api_models::payments::WalletData::ApplePay(_) => {
|
| api_models::payments::WalletData::ApplePay(_) => {
|
||||||
Ok(PaymentSource::Wallets(WalletSource {
|
Ok(PaymentSource::Wallets(WalletSource {
|
||||||
source_type: CheckoutSourceTypes::Token,
|
source_type: CheckoutSourceTypes::Token,
|
||||||
token: match item.get_payment_method_token()? {
|
token: match item.router_data.get_payment_method_token()? {
|
||||||
types::PaymentMethodToken::Token(token) => token,
|
types::PaymentMethodToken::Token(token) => token,
|
||||||
types::PaymentMethodToken::ApplePayDecrypt(_) => {
|
types::PaymentMethodToken::ApplePayDecrypt(_) => {
|
||||||
Err(errors::ConnectorError::InvalidWalletToken)?
|
Err(errors::ConnectorError::InvalidWalletToken)?
|
||||||
@ -309,7 +341,7 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for PaymentsRequest {
|
|||||||
}
|
}
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
let three_ds = match item.auth_type {
|
let three_ds = match item.router_data.auth_type {
|
||||||
enums::AuthenticationType::ThreeDs => CheckoutThreeDS {
|
enums::AuthenticationType::ThreeDs => CheckoutThreeDS {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
force_3ds: true,
|
force_3ds: true,
|
||||||
@ -322,11 +354,13 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for PaymentsRequest {
|
|||||||
|
|
||||||
let return_url = ReturnUrl {
|
let return_url = ReturnUrl {
|
||||||
success_url: item
|
success_url: item
|
||||||
|
.router_data
|
||||||
.request
|
.request
|
||||||
.router_return_url
|
.router_return_url
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|return_url| format!("{return_url}?status=success")),
|
.map(|return_url| format!("{return_url}?status=success")),
|
||||||
failure_url: item
|
failure_url: item
|
||||||
|
.router_data
|
||||||
.request
|
.request
|
||||||
.router_return_url
|
.router_return_url
|
||||||
.as_ref()
|
.as_ref()
|
||||||
@ -334,22 +368,22 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for PaymentsRequest {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let capture = matches!(
|
let capture = matches!(
|
||||||
item.request.capture_method,
|
item.router_data.request.capture_method,
|
||||||
Some(enums::CaptureMethod::Automatic)
|
Some(enums::CaptureMethod::Automatic)
|
||||||
);
|
);
|
||||||
|
|
||||||
let connector_auth = &item.connector_auth_type;
|
let connector_auth = &item.router_data.connector_auth_type;
|
||||||
let auth_type: CheckoutAuthType = connector_auth.try_into()?;
|
let auth_type: CheckoutAuthType = connector_auth.try_into()?;
|
||||||
let processing_channel_id = auth_type.processing_channel_id;
|
let processing_channel_id = auth_type.processing_channel_id;
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
source: source_var,
|
source: source_var,
|
||||||
amount: item.request.amount,
|
amount: item.amount.to_owned(),
|
||||||
currency: item.request.currency.to_string(),
|
currency: item.router_data.request.currency.to_string(),
|
||||||
processing_channel_id,
|
processing_channel_id,
|
||||||
three_ds,
|
three_ds,
|
||||||
return_url,
|
return_url,
|
||||||
capture,
|
capture,
|
||||||
reference: item.connector_request_reference_id.clone(),
|
reference: item.router_data.connector_request_reference_id.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -665,24 +699,27 @@ pub struct PaymentCaptureRequest {
|
|||||||
pub reference: Option<String>,
|
pub reference: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<&types::PaymentsCaptureRouterData> for PaymentCaptureRequest {
|
impl TryFrom<&CheckoutRouterData<&types::PaymentsCaptureRouterData>> for PaymentCaptureRequest {
|
||||||
type Error = error_stack::Report<errors::ConnectorError>;
|
type Error = error_stack::Report<errors::ConnectorError>;
|
||||||
fn try_from(item: &types::PaymentsCaptureRouterData) -> Result<Self, Self::Error> {
|
fn try_from(
|
||||||
let connector_auth = &item.connector_auth_type;
|
item: &CheckoutRouterData<&types::PaymentsCaptureRouterData>,
|
||||||
|
) -> Result<Self, Self::Error> {
|
||||||
|
let connector_auth = &item.router_data.connector_auth_type;
|
||||||
let auth_type: CheckoutAuthType = connector_auth.try_into()?;
|
let auth_type: CheckoutAuthType = connector_auth.try_into()?;
|
||||||
let processing_channel_id = auth_type.processing_channel_id;
|
let processing_channel_id = auth_type.processing_channel_id;
|
||||||
let capture_type = if item.request.is_multiple_capture() {
|
let capture_type = if item.router_data.request.is_multiple_capture() {
|
||||||
CaptureType::NonFinal
|
CaptureType::NonFinal
|
||||||
} else {
|
} else {
|
||||||
CaptureType::Final
|
CaptureType::Final
|
||||||
};
|
};
|
||||||
let reference = item
|
let reference = item
|
||||||
|
.router_data
|
||||||
.request
|
.request
|
||||||
.multiple_capture_data
|
.multiple_capture_data
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|multiple_capture_data| multiple_capture_data.capture_reference.clone());
|
.map(|multiple_capture_data| multiple_capture_data.capture_reference.clone());
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
amount: Some(item.request.amount_to_capture),
|
amount: Some(item.amount.to_owned()),
|
||||||
capture_type: Some(capture_type),
|
capture_type: Some(capture_type),
|
||||||
processing_channel_id,
|
processing_channel_id,
|
||||||
reference, // hyperswitch's reference for this capture
|
reference, // hyperswitch's reference for this capture
|
||||||
@ -742,13 +779,14 @@ pub struct RefundRequest {
|
|||||||
reference: String,
|
reference: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F> TryFrom<&types::RefundsRouterData<F>> for RefundRequest {
|
impl<F> TryFrom<&CheckoutRouterData<&types::RefundsRouterData<F>>> for RefundRequest {
|
||||||
type Error = error_stack::Report<errors::ConnectorError>;
|
type Error = error_stack::Report<errors::ConnectorError>;
|
||||||
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
|
fn try_from(
|
||||||
let amount = item.request.refund_amount;
|
item: &CheckoutRouterData<&types::RefundsRouterData<F>>,
|
||||||
let reference = item.request.refund_id.clone();
|
) -> Result<Self, Self::Error> {
|
||||||
|
let reference = item.router_data.request.refund_id.clone();
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
amount: Some(amount),
|
amount: Some(item.amount.to_owned()),
|
||||||
reference,
|
reference,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user