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(core): Moved `get_access_token_key` to `ConnectorValidation` & `is_payment_recurrence_operation_needed` to `ConnectorSpecifications` trait (#11760)

Sayak Bhattacharya0d9d57326832
diff --git a/crates/hyperswitch_connectors/src/connectors/dummyconnector.rs b/crates/hyperswitch_connectors/src/connectors/dummyconnector.rs
index 23a30be93a..18224a2a16 100644
--- a/crates/hyperswitch_connectors/src/connectors/dummyconnector.rs
+++ b/crates/hyperswitch_connectors/src/connectors/dummyconnector.rs
@@ -654,5 +654,3 @@ impl<const T: u8> IncomingWebhook for DummyConnector<T> {
}
impl<const T: u8> ConnectorSpecifications for DummyConnector<T> {}
-
-impl<const T: u8> hyperswitch_interfaces::api::ConnectorAccessTokenSuffix for DummyConnector<T> {}
diff --git a/crates/hyperswitch_connectors/src/connectors/santander.rs b/crates/hyperswitch_connectors/src/connectors/santander.rs
index 4ba855c8d7..8d578ac737 100644
--- a/crates/hyperswitch_connectors/src/connectors/santander.rs
+++ b/crates/hyperswitch_connectors/src/connectors/santander.rs
@@ -42,8 +42,8 @@ use hyperswitch_domain_models::{
};
use hyperswitch_interfaces::{
api::{
- self, ConnectorAccessTokenSuffix, ConnectorCommon, ConnectorCommonExt,
- ConnectorIntegration, ConnectorSpecifications, ConnectorValidation, CurrentFlowInfo,
+ self, ConnectorCommon, ConnectorCommonExt, ConnectorIntegration, ConnectorSpecifications,
+ ConnectorValidation, CurrentFlowInfo,
},
configs::Connectors,
consts::{NO_ERROR_CODE, NO_ERROR_MESSAGE},
@@ -600,17 +600,37 @@ impl ConnectorCommon for Santander {
}
impl ConnectorValidation for Santander {
- fn should_continue_further(
+ fn get_access_token_key(
&self,
- payment_intent: &hyperswitch_domain_models::payments::PaymentIntent,
- ) -> Option<bool> {
- #[cfg(feature = "v1")]
- {
- Some(payment_intent.setup_future_usage == Some(common_enums::FutureUsage::OffSession))
- }
- #[cfg(feature = "v2")]
- {
- Some(payment_intent.setup_future_usage == common_enums::FutureUsage::OffSession)
+ merchant_id: &common_utils::id_type::MerchantId,
+ merchant_connector_id_or_connector_name: String,
+ current_flow: Option<CurrentFlowInfo>,
+ payment_method_type: Option<enums::PaymentMethodType>,
+ is_mit_payment: Option<bool>,
+ ) -> CustomResult<String, errors::ConnectorError> {
+ let url_path = transformers::decide_access_token_key_suffix(
+ current_flow.clone(),
+ payment_method_type,
+ is_mit_payment.unwrap_or(false),
+ );
+
+ let suffix = url_path.map(|path| match path {
+ AccessTokenUrlPath::Leg1 => "pix",
+ AccessTokenUrlPath::Leg2 => "pix_automatico",
+ AccessTokenUrlPath::Boleto => "boleto",
+ });
+
+ match suffix {
+ Some(suffix) => Ok(format!(
+ "access_token_{}_{}_{}",
+ merchant_id.get_string_repr(),
+ merchant_connector_id_or_connector_name,
+ suffix,
+ )),
+ None => Ok(common_utils::access_token::get_default_access_token_key(
+ merchant_id,
+ merchant_connector_id_or_connector_name,
+ )),
}
}
}
@@ -2015,6 +2035,20 @@ static SANTANDER_CONNECTOR_INFO: ConnectorInfo = ConnectorInfo {
static SANTANDER_SUPPORTED_WEBHOOK_FLOWS: [enums::EventClass; 0] = [];
impl ConnectorSpecifications for Santander {
+ fn is_payment_recurrence_operation_needed(
+ &self,
+ payment_intent: &hyperswitch_domain_models::payments::PaymentIntent,
+ ) -> Option<bool> {
+ #[cfg(feature = "v1")]
+ {
+ Some(payment_intent.setup_future_usage == Some(common_enums::FutureUsage::OffSession))
+ }
+ #[cfg(feature = "v2")]
+ {
+ Some(payment_intent.setup_future_usage == common_enums::FutureUsage::OffSession)
+ }
+ }
+
fn get_connector_about(&self) -> Option<&'static ConnectorInfo> {
Some(&SANTANDER_CONNECTOR_INFO)
}
@@ -2106,41 +2140,6 @@ impl ConnectorSpecifications for Santander {
}
}
-impl ConnectorAccessTokenSuffix for Santander {
- fn get_access_token_key(
- &self,
- router_data: &dyn api::AccessTokenData,
- merchant_connector_id_or_connector_name: String,
- current_flow: Option<CurrentFlowInfo>,
- ) -> CustomResult<String, errors::ConnectorError> {
- let merchant_id = &router_data.get_merchant_id();
- let url_path = transformers::decide_access_token_key_suffix(
- current_flow.clone(),
- router_data.get_payment_method_type(),
- router_data.is_mit_payment(),
- );
-
- let suffix = url_path.map(|path| match path {
- AccessTokenUrlPath::Leg1 => "pix",
- AccessTokenUrlPath::Leg2 => "pix_automatico",
- AccessTokenUrlPath::Boleto => "boleto",
- });
-
- match suffix {
- Some(suffix) => Ok(format!(
- "access_token_{}_{}_{}",
- merchant_id.get_string_repr(),
- merchant_connector_id_or_connector_name,
- suffix,
- )),
- None => Ok(common_utils::access_token::get_default_access_token_key(
- merchant_id,
- merchant_connector_id_or_connector_name,
- )),
- }
- }
-}
-
impl ConnectorIntegration<GenerateQr, GenerateQrRequestData, PaymentsResponseData> for Santander {
fn get_headers(
&self,
diff --git a/crates/hyperswitch_connectors/src/default_implementations.rs b/crates/hyperswitch_connectors/src/default_implementations.rs
index 2867f2007c..073718503d 100644
--- a/crates/hyperswitch_connectors/src/default_implementations.rs
+++ b/crates/hyperswitch_connectors/src/default_implementations.rs
@@ -11013,157 +11013,6 @@ impl<const T: u8>
{
}
-macro_rules! default_impl_connector_access_token {
- ($($path:ident::$connector:ident),*) => {
- $(
- impl api::ConnectorAccessTokenSuffix for $path::$connector {}
- )*
- };
-}
-
-default_impl_connector_access_token!(
- connectors::Aci,
- connectors::Authipay,
- connectors::Adyenplatform,
- connectors::Adyen,
- connectors::Affirm,
- connectors::Airwallex,
- connectors::Amazonpay,
- connectors::Archipel,
- connectors::Authorizedotnet,
- connectors::Bambora,
- connectors::Bamboraapac,
- connectors::Bankofamerica,
- connectors::Barclaycard,
- connectors::Billwerk,
- connectors::Bitpay,
- connectors::Bluesnap,
- connectors::Blackhawknetwork,
- connectors::Calida,
- connectors::Boku,
- connectors::Braintree,
- connectors::Breadpay,
- connectors::Cashtocode,
- connectors::Celero,
- connectors::Chargebee,
- connectors::Checkbook,
- connectors::Checkout,
- connectors::Coinbase,
- connectors::Coingate,
- connectors::Custombilling,
- connectors::Cryptopay,
- connectors::CtpMastercard,
- connectors::Cybersource,
- connectors::Cybersourcedecisionmanager,
- connectors::Datatrans,
- connectors::Deutschebank,
- connectors::Digitalvirgo,
- connectors::Dlocal,
- connectors::Dwolla,
- connectors::Ebanx,
- connectors::Elavon,
- connectors::Facilitapay,
- connectors::Finix,
- connectors::Fiserv,
- connectors::Fiservemea,
- connectors::Fiuu,
- connectors::Flexiti,
- connectors::Forte,
- connectors::Getnet,
- connectors::Gigadat,
- connectors::Globalpay,
- connectors::Globepay,
- connectors::Gocardless,
- connectors::Gpayments,
- connectors::Hipay,
- connectors::Helcim,
- connectors::Hyperpg,
- connectors::HyperswitchVault,
- connectors::Inespay,
- connectors::Iatapay,
- connectors::Itaubank,
- connectors::Jpmorgan,
- connectors::Juspaythreedsserver,
- connectors::Klarna,
- connectors::Loonio,
- connectors::Mifinity,
- connectors::Mollie,
- connectors::Moneris,
- connectors::Multisafepay,
- connectors::Netcetera,
- connectors::Nexinets,
- connectors::Nexixpay,
- connectors::Nmi,
- connectors::Nomupay,
- connectors::Noon,
- connectors::Nordea,
- connectors::Novalnet,
- connectors::Nuvei,
- connectors::Opennode,
- connectors::Paybox,
- connectors::Payload,
- connectors::Payme,
- connectors::Payone,
- connectors::Paypal,
- connectors::Paysafe,
- connectors::Paystack,
- connectors::Paytm,
- connectors::Payu,
- connectors::Peachpayments,
- connectors::Payjustnow,
- connectors::Payjustnowinstore,
- connectors::Phonepe,
- connectors::Placetopay,
- connectors::Powertranz,
- connectors::Prophetpay,
- connectors::Rapyd,
- connectors::Razorpay,
- connectors::Recurly,
- connectors::Redsys,
- connectors::Revolv3,
- connectors::Shift4,
- connectors::Silverflow,
- connectors::Square,
- connectors::Stax,
- connectors::Stripe,
- connectors::Stripebilling,
- connectors::Taxjar,
- connectors::Threedsecureio,
- connectors::Tesouro,
- connectors::Tokenex,
- connectors::Tokenio,
- connectors::Truelayer,
- connectors::Trustly,
- connectors::Trustpay,
- connectors::Trustpayments,
- connectors::Tsys,
- connectors::Vgs,
- connectors::Volt,
- connectors::Wellsfargo,
- connectors::Wise,
- connectors::Worldline,
- connectors::Worldpay,
- connectors::Worldpayvantiv,
- connectors::Worldpayxml,
- connectors::Worldpaymodular,
- connectors::Signifyd,
- connectors::Plaid,
- connectors::Riskified,
- connectors::Xendit,
- connectors::Zen,
- connectors::Zift,
- connectors::Zsl,
- connectors::UnifiedAuthenticationService,
- connectors::Envoy,
- connectors::Hyperwallet,
- connectors::Katapult,
- connectors::Mpgs,
- connectors::Opayo,
- connectors::Payeezy,
- connectors::Sift,
- connectors::Fiservcommercehub
-);
-
macro_rules! default_imp_for_generate_qr_flow{
($($path:ident::$connector:ident),*)=> {
$(
diff --git a/crates/hyperswitch_interfaces/src/api.rs b/crates/hyperswitch_interfaces/src/api.rs
index e88aa948c1..facbd7384f 100644
--- a/crates/hyperswitch_interfaces/src/api.rs
+++ b/crates/hyperswitch_interfaces/src/api.rs
@@ -124,7 +124,6 @@ pub trait Connector:
+ revenue_recovery::RevenueRecovery
+ ExternalVault
+ Subscriptions
- + ConnectorAccessTokenSuffix
+ WebhookRegister
{
}
@@ -150,8 +149,7 @@ impl<
+ UnifiedAuthenticationService
+ revenue_recovery::RevenueRecovery
+ ExternalVault
- + Subscriptions
- + ConnectorAccessTokenSuffix,
+ + Subscriptions,
> Connector for T
{
}
@@ -401,23 +399,6 @@ pub trait ConnectorCommon {
}
}
-impl ConnectorAccessTokenSuffix for BoxedConnector {
- fn get_access_token_key(
- &self,
- router_data: &dyn AccessTokenData,
- merchant_connector_id_or_connector_name: String,
- current_flow: Option<CurrentFlowInfo>,
- ) -> CustomResult<String, errors::ConnectorError> {
- // 'self' is the BoxedConnector (the Box)
- // We dereference it to get the 'dyn Connector' and call the method
- self.as_ref().get_access_token_key(
- router_data,
- merchant_connector_id_or_connector_name,
- current_flow,
- )
- }
-}
-
/// Alternate API flow that must be made instead of the current flow.
/// For example, PreAuthenticate flow must be made instead of Authorize flow.
#[derive(Debug, Clone, Copy)]
@@ -526,6 +507,14 @@ pub trait ConnectorSpecifications {
false
}
+ /// Validate if another operation is required
+ fn is_payment_recurrence_operation_needed(
+ &self,
+ _payment_intent: &hyperswitch_domain_models::payments::PaymentIntent,
+ ) -> Option<bool> {
+ Some(false)
+ }
+
/// Whether SDK session token generation is enabled for this connector
fn is_sdk_client_token_generation_enabled(&self) -> bool {
false
@@ -924,12 +913,19 @@ pub trait ConnectorValidation: ConnectorCommon + ConnectorSpecifications {
false
}
- /// Validate if another operation is required
- fn should_continue_further(
+ /// Function to get dynamic access token key suffix from Connector
+ fn get_access_token_key(
&self,
- _payment_intent: &hyperswitch_domain_models::payments::PaymentIntent,
- ) -> Option<bool> {
- Some(false)
+ merchant_id: &common_utils::id_type::MerchantId,
+ merchant_connector_id_or_connector_name: String,
+ _current_flow: Option<CurrentFlowInfo>,
+ _payment_method_type: Option<PaymentMethodType>,
+ _is_mit_payment: Option<bool>,
+ ) -> CustomResult<String, errors::ConnectorError> {
+ Ok(common_utils::access_token::get_default_access_token_key(
+ merchant_id,
+ merchant_connector_id_or_connector_name,
+ ))
}
}
@@ -999,42 +995,3 @@ pub trait ConnectorTransactionId: ConnectorCommon + Sync {
.map(ToString::to_string))
}
}
-
-/// Trait to provide data required for access token key generation
-/// Add methods as required
-pub trait AccessTokenData {
- /// Get the payment method type from RouterData
- fn get_payment_method_type(&self) -> Option<PaymentMethodType>;
- /// Get the merchant id from RouterData
- fn get_merchant_id(&self) -> common_utils::id_type::MerchantId;
- /// Check if the payment is a MIT payment
- fn is_mit_payment(&self) -> bool;
-}
-
-impl<F, Req, Res> AccessTokenData for RouterData<F, Req, Res> {
- fn get_payment_method_type(&self) -> Option<PaymentMethodType> {
- self.payment_method_type
- }
- fn get_merchant_id(&self) -> common_utils::id_type::MerchantId {
- self.merchant_id.clone()
- }
- fn is_mit_payment(&self) -> bool {
- self.recurring_mandate_payment_data.is_some()
- }
-}
-
-/// Trait ConnectorAccessTokenSuffix
-pub trait ConnectorAccessTokenSuffix {
- /// Function to get dynamic access token key suffix from Connector
- fn get_access_token_key(
- &self,
- router_data: &dyn AccessTokenData,
- merchant_connector_id_or_connector_name: String,
- _current_flow: Option<CurrentFlowInfo>,
- ) -> CustomResult<String, errors::ConnectorError> {
- Ok(common_utils::access_token::get_default_access_token_key(
- &router_data.get_merchant_id(),
- merchant_connector_id_or_connector_name,
- ))
- }
-}
diff --git a/crates/hyperswitch_interfaces/src/connector_integration_interface.rs b/crates/hyperswitch_interfaces/src/connector_integration_interface.rs
index 6a734744fd..420362b187 100644
--- a/crates/hyperswitch_interfaces/src/connector_integration_interface.rs
+++ b/crates/hyperswitch_interfaces/src/connector_integration_interface.rs
@@ -15,8 +15,8 @@ use hyperswitch_domain_models::{
use crate::{
api,
api::{
- BoxedConnectorIntegration, CaptureSyncMethod, Connector, ConnectorAccessTokenSuffix,
- ConnectorCommon, ConnectorIntegration, ConnectorRedirectResponse, ConnectorSpecifications,
+ BoxedConnectorIntegration, CaptureSyncMethod, Connector, ConnectorCommon,
+ ConnectorIntegration, ConnectorRedirectResponse, ConnectorSpecifications,
ConnectorValidation, CurrencyUnit,
},
authentication::ExternalAuthenticationPayload,
@@ -145,33 +145,6 @@ impl ConnectorEnum {
}
}
}
- /// This keeps the generics <F, Req, Res> so existing callers don't break
- pub fn get_access_token_key<F, Req, Res>(
- &self,
- router_data: &RouterData<F, Req, Res>,
- merchant_connector_id_or_connector_name: String,
- current_flow: Option<CurrentFlowInfo>,
- ) -> CustomResult<String, errors::ConnectorError> {
- match self {
- Self::Old(connector) => {
- // router_data is automatically coerced to &dyn AccessTokenData
- connector.get_access_token_key(
- // router_data as &dyn AccessTokenData,
- router_data,
- merchant_connector_id_or_connector_name,
- current_flow,
- )
- }
- Self::New(connector) => {
- connector.get_access_token_key(
- // router_data as &dyn AccessTokenData,
- router_data,
- merchant_connector_id_or_connector_name,
- current_flow,
- )
- }
- }
- }
}
#[async_trait::async_trait]
@@ -576,13 +549,29 @@ impl ConnectorValidation for ConnectorEnum {
}
}
- fn should_continue_further(
+ fn get_access_token_key(
&self,
- payment_intent: &hyperswitch_domain_models::payments::PaymentIntent,
- ) -> Option<bool> {
+ merchant_id: &common_utils::id_type::MerchantId,
+ merchant_connector_id_or_connector_name: String,
+ current_flow: Option<CurrentFlowInfo>,
+ payment_method_type: Option<common_enums::enums::PaymentMethodType>,
+ is_mit_payment: Option<bool>,
+ ) -> CustomResult<String, errors::ConnectorError> {
match self {
- Self::Old(connector) => connector.should_continue_further(payment_intent),
- Self::New(connector) => connector.should_continue_further(payment_intent),
+ Self::Old(connector) => connector.get_access_token_key(
+ merchant_id,
+ merchant_connector_id_or_connector_name,
+ current_flow,
+ payment_method_type,
+ is_mit_payment,
+ ),
+ Self::New(connector) => connector.get_access_token_key(
+ merchant_id,
+ merchant_connector_id_or_connector_name,
+ current_flow,
+ payment_method_type,
+ is_mit_payment,
+ ),
}
}
}
@@ -816,6 +805,20 @@ impl ConnectorSpecifications for ConnectorEnum {
}
}
+ fn is_payment_recurrence_operation_needed(
+ &self,
+ payment_intent: &hyperswitch_domain_models::payments::PaymentIntent,
+ ) -> Option<bool> {
+ match self {
+ Self::Old(connector) => {
+ connector.is_payment_recurrence_operation_needed(payment_intent)
+ }
+ Self::New(connector) => {
+ connector.is_payment_recurrence_operation_needed(payment_intent)
+ }
+ }
+ }
+
fn should_call_tokenization_before_setup_mandate(&self) -> bool {
match self {
Self::Old(connector) => connector.should_call_tokenization_before_setup_mandate(),
@@ -1084,6 +1087,3 @@ impl api::ConnectorTransactionId for ConnectorEnum {
}
}
}
-
-//re-add if stops working
-// impl ConnectorAccessTokenSuffix for BoxedConnectorV2 {}
diff --git a/crates/hyperswitch_interfaces/src/connector_integration_v2.rs b/crates/hyperswitch_interfaces/src/connector_integration_v2.rs
index 2a1fcba8b5..9599ea2e60 100644
--- a/crates/hyperswitch_interfaces/src/connector_integration_v2.rs
+++ b/crates/hyperswitch_interfaces/src/connector_integration_v2.rs
@@ -67,8 +67,6 @@ impl<
/// Alias for Box<&'static (dyn ConnectorV2 + Sync)>
pub type BoxedConnectorV2 = Box<&'static (dyn ConnectorV2 + Sync)>;
-impl api::ConnectorAccessTokenSuffix for BoxedConnectorV2 {}
-
/// alias for Box of a type that implements trait ConnectorIntegrationV2
pub type BoxedConnectorIntegrationV2<'a, Flow, ResourceCommonData, Req, Resp> =
Box<&'a (dyn ConnectorIntegrationV2<Flow, ResourceCommonData, Req, Resp> + Send + Sync)>;
diff --git a/crates/router/src/core/payments/access_token.rs b/crates/router/src/core/payments/access_token.rs
index 76e0555a6f..2842a3e7d7 100644
--- a/crates/router/src/core/payments/access_token.rs
+++ b/crates/router/src/core/payments/access_token.rs
@@ -3,7 +3,7 @@ use std::fmt::Debug;
use common_utils::ext_traits::AsyncExt;
use error_stack::ResultExt;
use hyperswitch_interfaces::{
- api::{gateway, ConnectorSpecifications},
+ api::{gateway, ConnectorSpecifications, ConnectorValidation},
consts as interfaces_consts,
};
@@ -149,9 +149,11 @@ pub async fn add_access_token<
let key = connector
.connector
.get_access_token_key(
- router_data,
+ &router_data.merchant_id,
merchant_connector_id_or_connector_name.clone(),
current_flow.clone(),
+ router_data.payment_method_type,
+ Some(router_data.recurring_mandate_payment_data.is_some()),
)
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable(format!(
diff --git a/crates/router/src/core/payouts/access_token.rs b/crates/router/src/core/payouts/access_token.rs
index 3064016e53..410cea1228 100644
--- a/crates/router/src/core/payouts/access_token.rs
+++ b/crates/router/src/core/payouts/access_token.rs
@@ -1,7 +1,7 @@
use common_utils::ext_traits::AsyncExt;
use error_stack::ResultExt;
use hyperswitch_interfaces::{
- api::{gateway, ConnectorCommon},
+ api::{gateway, ConnectorCommon, ConnectorValidation},
consts,
};
@@ -68,7 +68,13 @@ pub async fn add_access_token_for_payout<F: Clone + 'static>(
let key = connector
.connector
- .get_access_token_key(router_data, connector.connector.id().to_string(), None)
+ .get_access_token_key(
+ &router_data.merchant_id,
+ connector.connector.id().to_string(),
+ None,
+ None,
+ None,
+ )
.change_context(errors::ApiErrorResponse::InternalServerError)?;
let old_access_token = store
diff --git a/crates/router/src/routes/payments.rs b/crates/router/src/routes/payments.rs
index 0ceeb2fd12..2178d4ad6e 100644
--- a/crates/router/src/routes/payments.rs
+++ b/crates/router/src/routes/payments.rs
@@ -7,7 +7,7 @@ use actix_web::{web, Responder};
use error_stack::report;
use hyperswitch_domain_models::{ext_traits::OptionExt, payments::HeaderPayload};
#[cfg(feature = "v1")]
-use hyperswitch_interfaces::api::ConnectorValidation;
+use hyperswitch_interfaces::api::ConnectorSpecifications;
use hyperswitch_masking::{PeekInterface, Secret};
use router_env::{env, instrument, logger, tracing, types, Flow};
@@ -2415,7 +2415,9 @@ where
)?;
let should_continue_further = connector_data
.connector
- .should_continue_further(&payment_data.payment_intent.clone())
+ .is_payment_recurrence_operation_needed(
+ &payment_data.payment_intent.clone(),
+ )
.unwrap_or(false);
if should_continue_further {
logger::info!(
diff --git a/crates/router/tests/connectors/cybersourcedecisionmanager.rs b/crates/router/tests/connectors/cybersourcedecisionmanager.rs
index a90b4ba634..2c6565d56b 100644
--- a/crates/router/tests/connectors/cybersourcedecisionmanager.rs
+++ b/crates/router/tests/connectors/cybersourcedecisionmanager.rs
@@ -419,5 +419,3 @@ async fn should_fail_for_refund_amount_higher_than_payment_amount() {
// Connector dependent test cases goes here
// [#478]: add unit tests for non 3DS, wallets & webhooks in connector tests
-
-impl hyperswitch_interfaces::api::ConnectorAccessTokenSuffix for CybersourcedecisionmanagerTest {}