Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

refactor(connector): convert init payment flow to preprocessing flow for shift4 (#4884)

Hrithikesh5b92371a734a
diff --git a/crates/hyperswitch_domain_models/src/router_request_types.rs b/crates/hyperswitch_domain_models/src/router_request_types.rs
index 66d0da168c..a82e9f6f28 100644
--- a/crates/hyperswitch_domain_models/src/router_request_types.rs
+++ b/crates/hyperswitch_domain_models/src/router_request_types.rs
@@ -241,6 +241,7 @@ pub struct PaymentsPreProcessingData {
pub surcharge_details: Option<SurchargeDetails>,
pub browser_info: Option<BrowserInformation>,
pub connector_transaction_id: Option<String>,
+ pub enrolled_for_3ds: bool,
pub mandate_id: Option<api_models::payments::MandateIds>,
pub related_transaction_id: Option<String>,
pub redirect_response: Option<CompleteAuthorizeRedirectResponse>,
@@ -268,6 +269,7 @@ impl TryFrom<PaymentsAuthorizeData> for PaymentsPreProcessingData {
mandate_id: data.mandate_id,
related_transaction_id: data.related_transaction_id,
redirect_response: None,
+ enrolled_for_3ds: data.enrolled_for_3ds,
})
}
}
@@ -294,6 +296,7 @@ impl TryFrom<CompleteAuthorizeData> for PaymentsPreProcessingData {
mandate_id: data.mandate_id,
related_transaction_id: None,
redirect_response: data.redirect_response,
+ enrolled_for_3ds: true,
})
}
}
diff --git a/crates/router/src/connector/shift4.rs b/crates/router/src/connector/shift4.rs
index f314d621de..895487db3b 100644
--- a/crates/router/src/connector/shift4.rs
+++ b/crates/router/src/connector/shift4.rs
@@ -11,12 +11,9 @@ use super::utils::{self as connector_utils, RefundsRequestData};
use crate::{
configs::settings,
consts,
- core::{
- errors::{self, CustomResult},
- payments,
- },
+ core::errors::{self, CustomResult},
events::connector_api_logs::ConnectorEvent,
- headers, routes,
+ headers,
services::{
self,
request::{self, Mask},
@@ -25,7 +22,6 @@ use crate::{
types::{
self,
api::{self, ConnectorCommon, ConnectorCommonExt},
- transformers::ForeignFrom,
ErrorResponse,
},
utils::BytesExt,
@@ -137,6 +133,7 @@ impl api::Refund for Shift4 {}
impl api::RefundExecute for Shift4 {}
impl api::RefundSync for Shift4 {}
impl api::PaymentToken for Shift4 {}
+impl api::PaymentsPreProcessing for Shift4 {}
impl
ConnectorIntegration<
@@ -213,45 +210,6 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
Ok(RequestContent::Json(Box::new(connector_req)))
}
- async fn execute_pretasks(
- &self,
- router_data: &mut types::PaymentsAuthorizeRouterData,
- app_state: &routes::SessionState,
- ) -> CustomResult<(), errors::ConnectorError> {
- if router_data.auth_type == enums::AuthenticationType::ThreeDs
- && router_data.payment_method == enums::PaymentMethod::Card
- {
- let integ: Box<
- &(dyn ConnectorIntegration<
- api::InitPayment,
- types::PaymentsAuthorizeData,
- types::PaymentsResponseData,
- > + Send
- + Sync
- + 'static),
- > = Box::new(&Self);
- let init_data = &types::PaymentsInitRouterData::foreign_from((
- &router_data.to_owned(),
- router_data.request.clone(),
- ));
- let init_resp = services::execute_connector_processing_step(
- app_state,
- integ,
- init_data,
- payments::CallConnectorAction::Trigger,
- None,
- )
- .await?;
- if init_resp.request.enrolled_for_3ds {
- router_data.response = init_resp.response;
- router_data.status = init_resp.status;
- } else {
- router_data.request.enrolled_for_3ds = false;
- }
- }
- Ok(())
- }
-
fn build_request(
&self,
req: &types::PaymentsAuthorizeRouterData,
@@ -470,14 +428,14 @@ impl ConnectorIntegration<api::Session, types::PaymentsSessionData, types::Payme
impl
ConnectorIntegration<
- api::InitPayment,
- types::PaymentsAuthorizeData,
+ api::PreProcessing,
+ types::PaymentsPreProcessingData,
types::PaymentsResponseData,
> for Shift4
{
fn get_headers(
&self,
- req: &types::PaymentsInitRouterData,
+ req: &types::PaymentsPreProcessingRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<Vec<(String, request::Maskable<String>)>, errors::ConnectorError> {
let mut headers = vec![
@@ -501,7 +459,7 @@ impl
fn get_url(
&self,
- _req: &types::PaymentsInitRouterData,
+ _req: &types::PaymentsPreProcessingRouterData,
connectors: &settings::Connectors,
) -> CustomResult<String, errors::ConnectorError> {
Ok(format!("{}3d-secure", self.base_url(connectors)))
@@ -509,7 +467,7 @@ impl
fn get_request_body(
&self,
- req: &types::PaymentsInitRouterData,
+ req: &types::PaymentsPreProcessingRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<RequestContent, errors::ConnectorError> {
let connector_req = shift4::Shift4PaymentsRequest::try_from(req)?;
@@ -518,16 +476,20 @@ impl
fn build_request(
&self,
- req: &types::PaymentsInitRouterData,
+ req: &types::PaymentsPreProcessingRouterData,
connectors: &settings::Connectors,
) -> CustomResult<Option<services::Request>, errors::ConnectorError> {
Ok(Some(
services::RequestBuilder::new()
.method(services::Method::Post)
- .url(&types::PaymentsInitType::get_url(self, req, connectors)?)
+ .url(&types::PaymentsPreProcessingType::get_url(
+ self, req, connectors,
+ )?)
.attach_default_headers()
- .headers(types::PaymentsInitType::get_headers(self, req, connectors)?)
- .set_body(types::PaymentsInitType::get_request_body(
+ .headers(types::PaymentsPreProcessingType::get_headers(
+ self, req, connectors,
+ )?)
+ .set_body(types::PaymentsPreProcessingType::get_request_body(
self, req, connectors,
)?)
.build(),
@@ -536,10 +498,10 @@ impl
fn handle_response(
&self,
- data: &types::PaymentsInitRouterData,
+ data: &types::PaymentsPreProcessingRouterData,
event_builder: Option<&mut ConnectorEvent>,
res: types::Response,
- ) -> CustomResult<types::PaymentsInitRouterData, errors::ConnectorError> {
+ ) -> CustomResult<types::PaymentsPreProcessingRouterData, errors::ConnectorError> {
let response: shift4::Shift4ThreeDsResponse = res
.response
.parse_struct("Shift4ThreeDsResponse")
diff --git a/crates/router/src/connector/shift4/transformers.rs b/crates/router/src/connector/shift4/transformers.rs
index 6831c502f5..ac597b7346 100644
--- a/crates/router/src/connector/shift4/transformers.rs
+++ b/crates/router/src/connector/shift4/transformers.rs
@@ -9,7 +9,7 @@ use url::Url;
use crate::{
connector::utils::{
self, to_connector_meta, PaymentsAuthorizeRequestData,
- PaymentsCompleteAuthorizeRequestData, RouterData,
+ PaymentsCompleteAuthorizeRequestData, PaymentsPreProcessingData, RouterData,
},
core::errors,
pii, services,
@@ -18,6 +18,81 @@ use crate::{
type Error = error_stack::Report<errors::ConnectorError>;
+trait Shift4AuthorizePreprocessingCommon {
+ fn is_automatic_capture(&self) -> Result<bool, Error>;
+ fn get_router_return_url(&self) -> Option<String>;
+ fn get_email_optional(&self) -> Option<pii::Email>;
+ fn get_complete_authorize_url(&self) -> Option<String>;
+ fn get_amount_required(&self) -> Result<i64, Error>;
+ fn get_currency_required(&self) -> Result<diesel_models::enums::Currency, Error>;
+ fn get_payment_method_data_required(&self) -> Result<domain::PaymentMethodData, Error>;
+}
+
+impl Shift4AuthorizePreprocessingCommon for types::PaymentsAuthorizeData {
+ fn get_email_optional(&self) -> Option<pii::Email> {
+ self.email.clone()
+ }
+
+ fn get_complete_authorize_url(&self) -> Option<String> {
+ self.complete_authorize_url.clone()
+ }
+
+ fn get_amount_required(&self) -> Result<i64, error_stack::Report<errors::ConnectorError>> {
+ Ok(self.amount)
+ }
+
+ fn get_currency_required(
+ &self,
+ ) -> Result<diesel_models::enums::Currency, error_stack::Report<errors::ConnectorError>> {
+ Ok(self.currency)
+ }
+ fn get_payment_method_data_required(
+ &self,
+ ) -> Result<domain::PaymentMethodData, error_stack::Report<errors::ConnectorError>> {
+ Ok(self.payment_method_data.clone())
+ }
+
+ fn is_automatic_capture(&self) -> Result<bool, Error> {
+ self.is_auto_capture()
+ }
+
+ fn get_router_return_url(&self) -> Option<String> {
+ self.router_return_url.clone()
+ }
+}
+
+impl Shift4AuthorizePreprocessingCommon for types::PaymentsPreProcessingData {
+ fn get_email_optional(&self) -> Option<pii::Email> {
+ self.email.clone()
+ }
+
+ fn get_complete_authorize_url(&self) -> Option<String> {
+ self.complete_authorize_url.clone()
+ }
+
+ fn get_amount_required(&self) -> Result<i64, Error> {
+ self.get_amount()
+ }
+
+ fn get_currency_required(&self) -> Result<diesel_models::enums::Currency, Error> {
+ self.get_currency()
+ }
+ fn get_payment_method_data_required(&self) -> Result<domain::PaymentMethodData, Error> {
+ self.payment_method_data.clone().ok_or(
+ errors::ConnectorError::MissingRequiredField {
+ field_name: "payment_method_data",
+ }
+ .into(),
+ )
+ }
+ fn is_automatic_capture(&self) -> Result<bool, Error> {
+ self.is_auto_capture()
+ }
+
+ fn get_router_return_url(&self) -> Option<String> {
+ self.router_return_url.clone()
+ }
+}
#[derive(Debug, Serialize)]
pub struct Shift4PaymentsRequest {
amount: String,
@@ -120,16 +195,18 @@ pub enum CardPayment {
CardToken(Secret<String>),
}
-impl<T> TryFrom<&types::RouterData<T, types::PaymentsAuthorizeData, types::PaymentsResponseData>>
+impl<T, Req> TryFrom<&types::RouterData<T, Req, types::PaymentsResponseData>>
for Shift4PaymentsRequest
+where
+ Req: Shift4AuthorizePreprocessingCommon,
{
type Error = Error;
fn try_from(
- item: &types::RouterData<T, types::PaymentsAuthorizeData, types::PaymentsResponseData>,
+ item: &types::RouterData<T, Req, types::PaymentsResponseData>,
) -> Result<Self, Self::Error> {
- let submit_for_settlement = item.request.is_auto_capture()?;
- let amount = item.request.amount.to_string();
- let currency = item.request.currency;
+ let submit_for_settlement = item.request.is_automatic_capture()?;
+ let amount = item.request.get_amount_required()?.to_string();
+ let currency = item.request.get_currency_required()?;
let payment_method = Shift4PaymentMethod::try_from(item)?;
Ok(Self {
amount,
@@ -140,14 +217,16 @@ impl<T> TryFrom<&types::RouterData<T, types::PaymentsAuthorizeData, types::Payme
}
}
-impl<T> TryFrom<&types::RouterData<T, types::PaymentsAuthorizeData, types::PaymentsResponseData>>
+impl<T, Req> TryFrom<&types::RouterData<T, Req, types::PaymentsResponseData>>
for Shift4PaymentMethod
+where
+ Req: Shift4AuthorizePreprocessingCommon,
{
type Error = Error;
fn try_from(
- item: &types::RouterData<T, types::PaymentsAuthorizeData, types::PaymentsResponseData>,
+ item: &types::RouterData<T, Req, types::PaymentsResponseData>,
) -> Result<Self, Self::Error> {
- match item.request.payment_method_data {
+ match item.request.get_payment_method_data_required()? {
domain::PaymentMethodData::Card(ref ccard) => Self::try_from((item, ccard)),
domain::PaymentMethodData::BankRedirect(ref redirect) => {
Self::try_from((item, redirect))
@@ -281,16 +360,18 @@ impl TryFrom<&domain::GiftCardData> for Shift4PaymentMethod {
}
}
-impl<T>
+impl<T, Req>
TryFrom<(
- &types::RouterData<T, types::PaymentsAuthorizeData, types::PaymentsResponseData>,
+ &types::RouterData<T, Req, types::PaymentsResponseData>,
&domain::Card,
)> for Shift4PaymentMethod
+where
+ Req: Shift4AuthorizePreprocessingCommon,
{
type Error = Error;
fn try_from(
(item, card): (
- &types::RouterData<T, types::PaymentsAuthorizeData, types::PaymentsResponseData>,
+ &types::RouterData<T, Req, types::PaymentsResponseData>,
&domain::Card,
),
) -> Result<Self, Self::Error> {
@@ -309,7 +390,7 @@ impl<T>
card_exp_year: card_object.exp_year,
return_url: item
.request
- .complete_authorize_url
+ .get_complete_authorize_url()
.clone()
.ok_or_else(|| errors::ConnectorError::RequestEncodingFailed)?,
})))
@@ -322,20 +403,22 @@ impl<T>
}
}
-impl<T>
+impl<T, Req>
TryFrom<(
- &types::RouterData<T, types::PaymentsAuthorizeData, types::PaymentsResponseData>,
+ &types::RouterData<T, Req, types::PaymentsResponseData>,
&domain::BankRedirectData,
)> for Shift4PaymentMethod
+where
+ Req: Shift4AuthorizePreprocessingCommon,
{
type Error = Error;
fn try_from(
(item, redirect_data): (
- &types::RouterData<T, types::PaymentsAuthorizeData, types::PaymentsResponseData>,
+ &types::RouterData<T, Req, types::PaymentsResponseData>,
&domain::BankRedirectData,
),
) -> Result<Self, Self::Error> {
- let flow = Flow::try_from(&item.request.router_return_url)?;
+ let flow = Flow::try_from(item.request.get_router_return_url())?;
let method_type = PaymentMethodType::try_from(redirect_data)?;
let billing = Billing::try_from(item)?;
let payment_method = PaymentMethod {
@@ -423,23 +506,22 @@ impl TryFrom<&domain::BankRedirectData> for PaymentMethodType {
}
}
-impl TryFrom<&Option<String>> for Flow {
+impl TryFrom<Option<String>> for Flow {
type Error = Error;
- fn try_from(router_return_url: &Option<String>) -> Result<Self, Self::Error> {
+ fn try_from(router_return_url: Option<String>) -> Result<Self, Self::Error> {
Ok(Self {
- return_url: router_return_url
- .clone()
- .ok_or(errors::ConnectorError::RequestEncodingFailed)?,
+ return_url: router_return_url.ok_or(errors::ConnectorError::RequestEncodingFailed)?,
})
}
}
-impl<T> TryFrom<&types::RouterData<T, types::PaymentsAuthorizeData, types::PaymentsResponseData>>
- for Billing
+impl<T, Req> TryFrom<&types::RouterData<T, Req, types::PaymentsResponseData>> for Billing
+where
+ Req: Shift4AuthorizePreprocessingCommon,
{
type Error = Error;
fn try_from(
- item: &types::RouterData<T, types::PaymentsAuthorizeData, types::PaymentsResponseData>,
+ item: &types::RouterData<T, Req, types::PaymentsResponseData>,
) -> Result<Self, Self::Error> {
let billing_address = item
.get_optional_billing()
@@ -450,7 +532,7 @@ impl<T> TryFrom<&types::RouterData<T, types::PaymentsAuthorizeData, types::Payme
name: billing_address.map(|billing| {
Secret::new(format!("{:?} {:?}", billing.first_name, billing.last_name))
}),
- email: item.request.email.clone(),
+ email: item.request.get_email_optional(),
address,
})
}
@@ -632,24 +714,12 @@ pub struct Shift4CardToken {
pub id: Secret<String>,
}
-impl<F>
- TryFrom<
- types::ResponseRouterData<
- F,
- Shift4ThreeDsResponse,
- types::PaymentsAuthorizeData,
- types::PaymentsResponseData,
- >,
- > for types::RouterData<F, types::PaymentsAuthorizeData, types::PaymentsResponseData>
+impl TryFrom<types::PaymentsPreprocessingResponseRouterData<Shift4ThreeDsResponse>>
+ for types::PaymentsPreProcessingRouterData
{
type Error = Error;
fn try_from(
- item: types::ResponseRouterData<
- F,
- Shift4ThreeDsResponse,
- types::PaymentsAuthorizeData,
- types::PaymentsResponseData,
- >,
+ item: types::PaymentsPreprocessingResponseRouterData<Shift4ThreeDsResponse>,
) -> Result<Self, Self::Error> {
let redirection_data = item
.response
@@ -661,7 +731,7 @@ impl<F>
} else {
enums::AttemptStatus::Pending
},
- request: types::PaymentsAuthorizeData {
+ request: types::PaymentsPreProcessingData {
enrolled_for_3ds: item.response.enrolled,
..item.data.request
},
diff --git a/crates/router/src/connector/utils.rs b/crates/router/src/connector/utils.rs
index f911ea9555..c2a5dc4c53 100644
--- a/crates/router/src/connector/utils.rs
+++ b/crates/router/src/connector/utils.rs
@@ -672,7 +672,7 @@ impl PaymentsPreProcessingData for types::PaymentsPreProcessingData {
Some(payments::MandateReferenceId::ConnectorMandateId(connector_mandate_ids)) => {
connector_mandate_ids.connector_mandate_id.clone()
}
- _ => None,
+ Some(payments::MandateReferenceId::NetworkMandateId(_)) | None => None,
})
}
}
@@ -809,7 +809,7 @@ impl PaymentsAuthorizeRequestData for types::PaymentsAuthorizeData {
Some(payments::MandateReferenceId::ConnectorMandateId(connector_mandate_ids)) => {
connector_mandate_ids.connector_mandate_id.clone()
}
- _ => None,
+ Some(payments::MandateReferenceId::NetworkMandateId(_)) | None => None,
})
}
fn is_mandate_payment(&self) -> bool {
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs
index 6d3aced7c3..09787bc273 100644
--- a/crates/router/src/core/payments.rs
+++ b/crates/router/src/core/payments.rs
@@ -1965,7 +1965,8 @@ where
) && router_data.status
!= common_enums::AttemptStatus::AuthenticationFailed;
(router_data, should_continue)
- } else if connector.connector_name == router_types::Connector::Nuvei
+ } else if (connector.connector_name == router_types::Connector::Nuvei
+ || connector.connector_name == router_types::Connector::Shift4)
&& router_data.auth_type == common_enums::AuthenticationType::ThreeDs
&& !is_operation_complete_authorize(&operation)
{
diff --git a/crates/router/src/core/payments/flows.rs b/crates/router/src/core/payments/flows.rs
index a37f2f8efd..bc5bc3ff0e 100644
--- a/crates/router/src/core/payments/flows.rs
+++ b/crates/router/src/core/payments/flows.rs
@@ -988,7 +988,6 @@ default_imp_for_pre_processing_steps!(
connector::Prophetpay,
connector::Rapyd,
connector::Riskified,
- connector::Shift4,
connector::Signifyd,
connector::Square,
connector::Stax,
diff --git a/crates/router/src/core/payments/flows/authorize_flow.rs b/crates/router/src/core/payments/flows/authorize_flow.rs
index 4005ae78e6..7057b31cc5 100644
--- a/crates/router/src/core/payments/flows/authorize_flow.rs
+++ b/crates/router/src/core/payments/flows/authorize_flow.rs
@@ -357,7 +357,7 @@ pub async fn authorize_preprocessing_steps<F: Clone>(
let mut authorize_router_data = helpers::router_data_type_conversion::<_, F, _, _, _, _>(
resp.clone(),
router_data.request.to_owned(),
- resp.response,
+ resp.response.clone(),
);
if connector.connector_name == api_models::enums::Connector::Airwallex {
authorize_router_data.reference_id = resp.reference_id;
@@ -371,6 +371,13 @@ pub async fn authorize_preprocessing_steps<F: Clone>(
};
authorize_router_data.request.enrolled_for_3ds = enrolled_for_3ds;
authorize_router_data.request.related_transaction_id = related_transaction_id;
+ } else if connector.connector_name == api_models::enums::Connector::Shift4 {
+ if resp.request.enrolled_for_3ds {
+ authorize_router_data.response = resp.response;
+ authorize_router_data.status = resp.status;
+ } else {
+ authorize_router_data.request.enrolled_for_3ds = false;
+ }
}
Ok(authorize_router_data)
} else {
diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs
index 01df5a09b9..49319dff3a 100644
--- a/crates/router/src/core/payments/transformers.rs
+++ b/crates/router/src/core/payments/transformers.rs
@@ -1799,6 +1799,7 @@ impl<F: Clone> TryFrom<PaymentAdditionalData<'_, F>> for types::PaymentsPreProce
redirect_response: None,
mandate_id: payment_data.mandate_id,
related_transaction_id: None,
+ enrolled_for_3ds: true,
})
}
}