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
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
refactor: Added PreAuth flow for nuvei HS<>UCS tunnel (#10726)
Amitsingh Tanwar3831ecdb285e
diff --git a/crates/hyperswitch_connectors/src/connectors/nuvei.rs b/crates/hyperswitch_connectors/src/connectors/nuvei.rsindex 125335de3f..028a0b50a7 100644--- a/crates/hyperswitch_connectors/src/connectors/nuvei.rs+++ b/crates/hyperswitch_connectors/src/connectors/nuvei.rs@@ -27,13 +27,15 @@ use hyperswitch_domain_models::{access_token_auth::AccessTokenAuth,payments::{Authorize, Capture, PSync, PaymentMethodToken, Session, SetupMandate, Void},refunds::{Execute, RSync},+ unified_authentication_service::PreAuthenticate,AuthorizeSessionToken, CompleteAuthorize, PostCaptureVoid, PreProcessing,},router_request_types::{AccessTokenRequestData, AuthorizeSessionTokenData, CompleteAuthorizeData,PaymentMethodTokenizationData, PaymentsAuthorizeData, PaymentsCancelData,- PaymentsCancelPostCaptureData, PaymentsCaptureData, PaymentsPreProcessingData,- PaymentsSessionData, PaymentsSyncData, RefundsData, SetupMandateRequestData,+ PaymentsCancelPostCaptureData, PaymentsCaptureData, PaymentsPreAuthenticateData,+ PaymentsPreProcessingData, PaymentsSessionData, PaymentsSyncData, RefundsData,+ SetupMandateRequestData,},router_response_types::{ConnectorInfo, PaymentMethodDetails, PaymentsResponseData, RefundsResponseData,@@ -42,8 +44,8 @@ use hyperswitch_domain_models::{types::{PaymentsAuthorizeRouterData, PaymentsAuthorizeSessionTokenRouterData,PaymentsCancelPostCaptureRouterData, PaymentsCancelRouterData, PaymentsCaptureRouterData,- PaymentsCompleteAuthorizeRouterData, PaymentsPreProcessingRouterData,- PaymentsSyncRouterData, RefundsRouterData,+ PaymentsCompleteAuthorizeRouterData, PaymentsPreAuthenticateRouterData,+ PaymentsPreProcessingRouterData, PaymentsSyncRouterData, RefundsRouterData,},};#[cfg(feature = "payouts")]@@ -170,6 +172,7 @@ impl api::PaymentsCompleteAuthorize for Nuvei {}impl api::ConnectorAccessToken for Nuvei {}impl api::PaymentsPreProcessing for Nuvei {}impl api::PaymentPostCaptureVoid for Nuvei {}+impl api::PaymentsPreAuthenticate for Nuvei {}impl api::Payouts for Nuvei {}#[cfg(feature = "payouts")]@@ -920,6 +923,93 @@ impl ConnectorIntegration<AuthorizeSessionToken, AuthorizeSessionTokenData, Paym}}+impl ConnectorIntegration<PreAuthenticate, PaymentsPreAuthenticateData, PaymentsResponseData>+ for Nuvei+{+ fn get_headers(+ &self,+ req: &PaymentsPreAuthenticateRouterData,+ connectors: &Connectors,+ ) -> CustomResult<Vec<(String, masking::Maskable<String>)>, errors::ConnectorError> {+ self.build_headers(req, connectors)+ }++ fn get_content_type(&self) -> &'static str {+ self.common_get_content_type()+ }++ fn get_url(+ &self,+ _req: &PaymentsPreAuthenticateRouterData,+ connectors: &Connectors,+ ) -> CustomResult<String, errors::ConnectorError> {+ Ok(format!(+ "{}ppp/api/v1/initPayment.do",+ ConnectorCommon::base_url(self, connectors)+ ))+ }++ fn get_request_body(+ &self,+ req: &PaymentsPreAuthenticateRouterData,+ _connectors: &Connectors,+ ) -> CustomResult<RequestContent, errors::ConnectorError> {+ let connector_req =+ nuvei::NuveiThreeDSInitPaymentRequest::try_from((req, req.get_session_token()?))?;+ Ok(RequestContent::Json(Box::new(connector_req)))+ }++ fn build_request(+ &self,+ req: &PaymentsPreAuthenticateRouterData,+ connectors: &Connectors,+ ) -> CustomResult<Option<Request>, errors::ConnectorError> {+ Ok(Some(+ RequestBuilder::new()+ .method(Method::Post)+ .url(&types::PaymentsPreAuthenticateType::get_url(+ self, req, connectors,+ )?)+ .attach_default_headers()+ .headers(types::PaymentsPreAuthenticateType::get_headers(+ self, req, connectors,+ )?)+ .set_body(types::PaymentsPreAuthenticateType::get_request_body(+ self, req, connectors,+ )?)+ .build(),+ ))+ }++ fn handle_response(+ &self,+ data: &PaymentsPreAuthenticateRouterData,+ event_builder: Option<&mut ConnectorEvent>,+ res: Response,+ ) -> CustomResult<PaymentsPreAuthenticateRouterData, errors::ConnectorError> {+ let response: NuveiPaymentsResponse = res+ .response+ .parse_struct("NuveiPaymentsResponse")+ .switch()?;+ event_builder.map(|i| i.set_response_body(&response));+ router_env::logger::info!(connector_response=?response);+ RouterData::try_from(ResponseRouterData {+ response,+ data: data.clone(),+ http_code: res.status_code,+ })+ .change_context(errors::ConnectorError::ResponseHandlingFailed)+ }++ fn get_error_response(+ &self,+ res: Response,+ event_builder: Option<&mut ConnectorEvent>,+ ) -> CustomResult<ErrorResponse, errors::ConnectorError> {+ self.build_error_response(res, event_builder)+ }+}+impl ConnectorIntegration<PreProcessing, PaymentsPreProcessingData, PaymentsResponseData>for Nuvei{diff --git a/crates/hyperswitch_connectors/src/connectors/nuvei/transformers.rs b/crates/hyperswitch_connectors/src/connectors/nuvei/transformers.rsindex 73a3037027..d4c62f532b 100644--- a/crates/hyperswitch_connectors/src/connectors/nuvei/transformers.rs+++ b/crates/hyperswitch_connectors/src/connectors/nuvei/transformers.rs@@ -56,13 +56,15 @@ use url::Url;use crate::{types::PayoutsResponseRouterData, utils::PayoutsData as _};use crate::{types::{- PaymentsPreprocessingResponseRouterData, RefundsResponseRouterData, ResponseRouterData,+ PaymentsPreAuthenticateResponseRouterData, PaymentsPreprocessingResponseRouterData,+ RefundsResponseRouterData, ResponseRouterData,},utils::{self, convert_amount, missing_field_err, AddressData, AddressDetailsData,BrowserInformationData, CardData, ForeignTryFrom, PaymentsAuthorizeRequestData,PaymentsCancelRequestData, PaymentsCompleteAuthorizeRequestData,- PaymentsPreProcessingRequestData, PaymentsSetupMandateRequestData, RouterData as _,+ PaymentsPreAuthenticateRequestData, PaymentsPreProcessingRequestData,+ PaymentsSetupMandateRequestData, RouterData as _,},};@@ -100,6 +102,70 @@ pub struct CardPaymentOption {pub card: Card,}+impl TryFrom<(&types::PaymentsPreAuthenticateRouterData, String)>+ for NuveiThreeDSInitPaymentRequest+{+ type Error = error_stack::Report<errors::ConnectorError>;+ fn try_from(+ (item, session_token): (&types::PaymentsPreAuthenticateRouterData, String),+ ) -> Result<Self, Self::Error> {+ let currency = item.request.get_currency()?;+ let connector_auth: NuveiAuthType = NuveiAuthType::try_from(&item.connector_auth_type)?;+ let amount = item.request.get_minor_amount()?.to_nuvei_amount(currency)?;+ let payment_method_data = item.request.get_payment_method_data()?.clone();+ let card = match payment_method_data {+ PaymentMethodData::Card(card) => card,+ _ => Err(errors::ConnectorError::NotImplemented(+ utils::get_unimplemented_payment_method_error_message("nuvei"),+ ))?,+ };++ let browser_info = item+ .request+ .browser_info+ .clone()+ .ok_or_else(missing_field_err("browser_info"))?;++ let return_url = item+ .request+ .router_return_url+ .clone()+ .ok_or_else(missing_field_err("return_url"))?;++ let billing_address = item.get_billing().ok().map(|billing| billing.into());++ Ok(Self {+ session_token: session_token.into(),+ merchant_id: connector_auth.merchant_id,+ merchant_site_id: connector_auth.merchant_site_id,+ client_request_id: item.connector_request_reference_id.clone().into(),+ client_unique_id: item.connector_request_reference_id.clone(),+ amount,+ currency,+ payment_option: CardPaymentOption {+ card: Card {+ card_number: Some(card.card_number),+ card_holder_name: item.get_optional_billing_full_name(),+ expiration_month: Some(card.card_exp_month),+ expiration_year: Some(card.card_exp_year),+ cvv: Some(card.card_cvc),+ ..Default::default()+ },+ },+ device_details: DeviceDetails {+ ip_address: browser_info.get_ip_address()?,+ },+ user_token_id: item.customer_id.clone(),+ billing_address,+ url_details: UrlDetails {+ success_url: return_url.clone(),+ failure_url: return_url.clone(),+ pending_url: return_url,+ },+ })+ }+}+impl TryFrom<(&types::PaymentsPreProcessingRouterData, String)> for NuveiThreeDSInitPaymentRequest {type Error = error_stack::Report<errors::ConnectorError>;fn try_from(@@ -2440,6 +2506,39 @@ where}}+impl TryFrom<PaymentsPreAuthenticateResponseRouterData<NuveiPaymentsResponse>>+ for types::PaymentsPreAuthenticateRouterData+{+ type Error = error_stack::Report<errors::ConnectorError>;+ fn try_from(+ item: PaymentsPreAuthenticateResponseRouterData<NuveiPaymentsResponse>,+ ) -> Result<Self, Self::Error> {+ let response = item.response;+ let is_enrolled_for_3ds = response+ .clone()+ .payment_option+ .and_then(|po| po.card)+ .and_then(|c| c.three_d)+ .and_then(|t| t.v2supported)+ .map(to_boolean)+ .unwrap_or_default();+ Ok(Self {+ status: get_payment_status(+ Some(item.data.request.amount),+ false,+ response.transaction_type,+ response.transaction_status,+ response.status,+ ),+ response: Ok(PaymentsResponseData::ThreeDSEnrollmentResponse {+ enrolled_v2: is_enrolled_for_3ds,+ related_transaction_id: response.transaction_id,+ }),+ ..item.data+ })+ }+}+impl TryFrom<PaymentsPreprocessingResponseRouterData<NuveiPaymentsResponse>>for types::PaymentsPreProcessingRouterData{diff --git a/crates/hyperswitch_connectors/src/default_implementations.rs b/crates/hyperswitch_connectors/src/default_implementations.rsindex 7c7959a433..1420bfb193 100644--- a/crates/hyperswitch_connectors/src/default_implementations.rs+++ b/crates/hyperswitch_connectors/src/default_implementations.rs@@ -2014,7 +2014,6 @@ default_imp_for_pre_authenticate_steps!(connectors::Noon,connectors::Nordea,connectors::Novalnet,- connectors::Nuvei,connectors::Opayo,connectors::Opennode,connectors::Paybox,diff --git a/crates/hyperswitch_connectors/src/utils.rs b/crates/hyperswitch_connectors/src/utils.rsindex 1b7c9f92b0..b9a2c115cc 100644--- a/crates/hyperswitch_connectors/src/utils.rs+++ b/crates/hyperswitch_connectors/src/utils.rs@@ -2817,6 +2817,9 @@ impl AddressData for Address {pub trait PaymentsPreAuthenticateRequestData {fn get_webhook_url(&self) -> Result<String, Error>;fn is_auto_capture(&self) -> Result<bool, Error>;+ fn get_payment_method_data(&self) -> Result<PaymentMethodData, Error>;+ fn get_minor_amount(&self) -> Result<MinorUnit, Error>;+ fn get_currency(&self) -> Result<enums::Currency, Error>;}impl PaymentsPreAuthenticateRequestData for PaymentsPreAuthenticateData {fn get_webhook_url(&self) -> Result<String, Error> {@@ -2835,6 +2838,15 @@ impl PaymentsPreAuthenticateRequestData for PaymentsPreAuthenticateData {}}}+ fn get_payment_method_data(&self) -> Result<PaymentMethodData, Error> {+ Ok(self.payment_method_data.clone())+ }+ fn get_minor_amount(&self) -> Result<MinorUnit, Error> {+ Ok(self.minor_amount)+ }+ fn get_currency(&self) -> Result<enums::Currency, Error> {+ self.currency.ok_or_else(missing_field_err("currency"))+ }}pub trait PaymentsPreProcessingRequestData {fn get_redirect_response_payload(&self) -> Result<pii::SecretSerdeValue, Error>;