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
fix(access_token): use fallback to `connector_name` if `merchant_connector_id` is not present (#4503)
Narayan Bhat632a00cb6803
diff --git a/crates/common_utils/src/access_token.rs b/crates/common_utils/src/access_token.rsindex 20bac0dafb..56c2a4f784 100644--- a/crates/common_utils/src/access_token.rs+++ b/crates/common_utils/src/access_token.rs@@ -5,7 +5,7 @@ use std::fmt::Display;/// Create a key for fetching the access token from redispub fn create_access_token_key(merchant_id: impl Display,- merchant_connector_id: impl Display,+ merchant_connector_id_or_connector_name: impl Display,) -> String {- format!("access_token_{merchant_id}_{merchant_connector_id}")+ format!("access_token_{merchant_id}_{merchant_connector_id_or_connector_name}")}diff --git a/crates/router/src/core/payments/access_token.rs b/crates/router/src/core/payments/access_token.rsindex 49d27d8150..1de9cb60ca 100644--- a/crates/router/src/core/payments/access_token.rs+++ b/crates/router/src/core/payments/access_token.rs@@ -64,14 +64,20 @@ pub async fn add_access_token<{let merchant_id = &merchant_account.merchant_id;let store = &*state.store;- let merchant_connector_id = connector++ // `merchant_connector_id` may not be present in the below cases+ // - when straight through routing is used without passing the `merchant_connector_id`+ // - when creds identifier is passed+ //+ // In these cases fallback to `connector_name`.+ // We cannot use multiple merchant connector account in these cases+ let merchant_connector_id_or_connector_name = connector.merchant_connector_id- .as_ref()- .ok_or(errors::ApiErrorResponse::InternalServerError)- .attach_printable("Missing merchant_connector_id in ConnectorData")?;+ .clone()+ .unwrap_or(connector.connector_name.to_string());let old_access_token = store- .get_access_token(merchant_id, merchant_connector_id)+ .get_access_token(merchant_id, &merchant_connector_id_or_connector_name).await.change_context(errors::ApiErrorResponse::InternalServerError).attach_printable("DB error when accessing the access token")?;@@ -115,7 +121,7 @@ pub async fn add_access_token<if let Err(access_token_set_error) = store.set_access_token(merchant_id,- merchant_connector_id.as_str(),+ &merchant_connector_id_or_connector_name,access_token.clone(),).awaitdiff --git a/crates/router/src/db/merchant_connector_account.rs b/crates/router/src/db/merchant_connector_account.rsindex 06d1e505c6..8f50f5f542 100644--- a/crates/router/src/db/merchant_connector_account.rs+++ b/crates/router/src/db/merchant_connector_account.rs@@ -24,13 +24,13 @@ pub trait ConnectorAccessToken {async fn get_access_token(&self,merchant_id: &str,- merchant_connector_id: &str,+ merchant_connector_id_or_connector_name: &str,) -> CustomResult<Option<types::AccessToken>, errors::StorageError>;async fn set_access_token(&self,merchant_id: &str,- merchant_connector_id: &str,+ merchant_connector_id_or_connector_name: &str,access_token: types::AccessToken,) -> CustomResult<(), errors::StorageError>;}@@ -41,13 +41,15 @@ impl ConnectorAccessToken for Store {async fn get_access_token(&self,merchant_id: &str,- merchant_connector_id: &str,+ merchant_connector_id_or_connector_name: &str,) -> CustomResult<Option<types::AccessToken>, errors::StorageError> {//TODO: Handle race condition// This function should acquire a global lock on some resource, if access token is already// being refreshed by other request then wait till it finishes and use the same access token- let key =- common_utils::access_token::create_access_token_key(merchant_id, merchant_connector_id);+ let key = common_utils::access_token::create_access_token_key(+ merchant_id,+ merchant_connector_id_or_connector_name,+ );let maybe_token = self.get_redis_conn()@@ -69,11 +71,13 @@ impl ConnectorAccessToken for Store {async fn set_access_token(&self,merchant_id: &str,- merchant_connector_id: &str,+ merchant_connector_id_or_connector_name: &str,access_token: types::AccessToken,) -> CustomResult<(), errors::StorageError> {- let key =- common_utils::access_token::create_access_token_key(merchant_id, merchant_connector_id);+ let key = common_utils::access_token::create_access_token_key(+ merchant_id,+ merchant_connector_id_or_connector_name,+ );let serialized_access_token = access_token.encode_to_string_of_json().change_context(errors::StorageError::SerializationFailed)?;@@ -90,7 +94,7 @@ impl ConnectorAccessToken for MockDb {async fn get_access_token(&self,_merchant_id: &str,- _merchant_connector_id: &str,+ _merchant_connector_id_or_connector_name: &str,) -> CustomResult<Option<types::AccessToken>, errors::StorageError> {Ok(None)}@@ -98,7 +102,7 @@ impl ConnectorAccessToken for MockDb {async fn set_access_token(&self,_merchant_id: &str,- _merchant_connector_id: &str,+ _merchant_connector_id_or_connector_name: &str,_access_token: types::AccessToken,) -> CustomResult<(), errors::StorageError> {Ok(())