mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-27 19:46:48 +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"
|
||||
}
|
||||
|
||||
fn get_currency_unit(&self) -> api::CurrencyUnit {
|
||||
api::CurrencyUnit::Minor
|
||||
}
|
||||
|
||||
fn common_get_content_type(&self) -> &'static str {
|
||||
"application/json"
|
||||
}
|
||||
@ -305,7 +309,13 @@ impl ConnectorIntegration<api::Capture, types::PaymentsCaptureData, types::Payme
|
||||
&self,
|
||||
req: &types::PaymentsCaptureRouterData,
|
||||
) -> 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(
|
||||
&connector_req,
|
||||
utils::Encode::<checkout::PaymentCaptureRequest>::encode_to_string_of_json,
|
||||
@ -484,7 +494,13 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
|
||||
&self,
|
||||
req: &types::PaymentsAuthorizeRouterData,
|
||||
) -> 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(
|
||||
&connector_req,
|
||||
utils::Encode::<checkout::PaymentsRequest>::encode_to_string_of_json,
|
||||
@ -656,7 +672,13 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
|
||||
&self,
|
||||
req: &types::RefundsRouterData<api::Execute>,
|
||||
) -> 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(
|
||||
&connector_req,
|
||||
utils::Encode::<checkout::RefundRequest>::encode_to_string_of_json,
|
||||
|
||||
@ -13,6 +13,36 @@ use crate::{
|
||||
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)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
#[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>;
|
||||
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
|
||||
let source_var = match item.request.payment_method_data.clone() {
|
||||
fn try_from(
|
||||
item: &CheckoutRouterData<&types::PaymentsAuthorizeRouterData>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let source_var = match item.router_data.request.payment_method_data.clone() {
|
||||
api::PaymentMethodData::Card(ccard) => {
|
||||
let a = PaymentSource::Card(CardSource {
|
||||
source_type: CheckoutSourceTypes::Card,
|
||||
@ -254,7 +286,7 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for PaymentsRequest {
|
||||
| api_models::payments::WalletData::ApplePay(_) => {
|
||||
Ok(PaymentSource::Wallets(WalletSource {
|
||||
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::ApplePayDecrypt(_) => {
|
||||
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 {
|
||||
enabled: true,
|
||||
force_3ds: true,
|
||||
@ -322,11 +354,13 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for PaymentsRequest {
|
||||
|
||||
let return_url = ReturnUrl {
|
||||
success_url: item
|
||||
.router_data
|
||||
.request
|
||||
.router_return_url
|
||||
.as_ref()
|
||||
.map(|return_url| format!("{return_url}?status=success")),
|
||||
failure_url: item
|
||||
.router_data
|
||||
.request
|
||||
.router_return_url
|
||||
.as_ref()
|
||||
@ -334,22 +368,22 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for PaymentsRequest {
|
||||
};
|
||||
|
||||
let capture = matches!(
|
||||
item.request.capture_method,
|
||||
item.router_data.request.capture_method,
|
||||
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 processing_channel_id = auth_type.processing_channel_id;
|
||||
Ok(Self {
|
||||
source: source_var,
|
||||
amount: item.request.amount,
|
||||
currency: item.request.currency.to_string(),
|
||||
amount: item.amount.to_owned(),
|
||||
currency: item.router_data.request.currency.to_string(),
|
||||
processing_channel_id,
|
||||
three_ds,
|
||||
return_url,
|
||||
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>,
|
||||
}
|
||||
|
||||
impl TryFrom<&types::PaymentsCaptureRouterData> for PaymentCaptureRequest {
|
||||
impl TryFrom<&CheckoutRouterData<&types::PaymentsCaptureRouterData>> for PaymentCaptureRequest {
|
||||
type Error = error_stack::Report<errors::ConnectorError>;
|
||||
fn try_from(item: &types::PaymentsCaptureRouterData) -> Result<Self, Self::Error> {
|
||||
let connector_auth = &item.connector_auth_type;
|
||||
fn try_from(
|
||||
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 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
|
||||
} else {
|
||||
CaptureType::Final
|
||||
};
|
||||
let reference = item
|
||||
.router_data
|
||||
.request
|
||||
.multiple_capture_data
|
||||
.as_ref()
|
||||
.map(|multiple_capture_data| multiple_capture_data.capture_reference.clone());
|
||||
Ok(Self {
|
||||
amount: Some(item.request.amount_to_capture),
|
||||
amount: Some(item.amount.to_owned()),
|
||||
capture_type: Some(capture_type),
|
||||
processing_channel_id,
|
||||
reference, // hyperswitch's reference for this capture
|
||||
@ -742,13 +779,14 @@ pub struct RefundRequest {
|
||||
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>;
|
||||
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
|
||||
let amount = item.request.refund_amount;
|
||||
let reference = item.request.refund_id.clone();
|
||||
fn try_from(
|
||||
item: &CheckoutRouterData<&types::RefundsRouterData<F>>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let reference = item.router_data.request.refund_id.clone();
|
||||
Ok(Self {
|
||||
amount: Some(amount),
|
||||
amount: Some(item.amount.to_owned()),
|
||||
reference,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user