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(payouts): route merchant-authentication (access-token) calls through the payout connector (#13150)
gopikrishna000f02f42c45dcb
diff --git a/crates/external_services/src/grpc_client/unified_connector_service.rs b/crates/external_services/src/grpc_client/unified_connector_service.rsindex 15b55d04d4..16d22f4720 100644--- a/crates/external_services/src/grpc_client/unified_connector_service.rs+++ b/crates/external_services/src/grpc_client/unified_connector_service.rs@@ -1,6 +1,6 @@use std::collections::{HashMap, HashSet};-use common_enums::connector_enums::Connector;+use common_enums::{connector_enums::Connector, ConnectorType};use common_utils::{consts as common_utils_consts, errors::CustomResult, types::Url};use error_stack::ResultExt;pub use hyperswitch_interfaces::unified_connector_service::transformers::UnifiedConnectorServiceError;@@ -1090,11 +1090,14 @@ impl UnifiedConnectorServiceClient {}/// Performs Create Access Token Granular+ ///+ /// The connector type determines which UCS connector header namespace is used.pub async fn create_access_token(&self,create_access_token_request: payments_grpc::MerchantAuthenticationServiceCreateServerAuthenticationTokenRequest,connector_auth_metadata: ConnectorAuthMetadata,grpc_headers: GrpcHeadersUcs,+ connector_type: ConnectorType,) -> UnifiedConnectorServiceResult<tonic::Response<payments_grpc::MerchantAuthenticationServiceCreateServerAuthenticationTokenResponse,@@ -1102,8 +1105,11 @@ impl UnifiedConnectorServiceClient {> {let mut request = tonic::Request::new(create_access_token_request);let connector_name = connector_auth_metadata.connector_name.clone();- let metadata =- build_unified_connector_service_grpc_headers(connector_auth_metadata, grpc_headers)?;+ let metadata = build_unified_connector_service_grpc_headers_for_connector_type(+ connector_auth_metadata,+ grpc_headers,+ connector_type,+ )?;*request.metadata_mut() = metadata;self.merchant_authentication_service_client@@ -1445,6 +1451,27 @@ pub fn build_unified_connector_service_grpc_headers_for_payouts(build_grpc_headers_internal(meta, grpc_headers, consts::UCS_HEADER_PAYOUT_CONNECTOR)}+fn build_unified_connector_service_grpc_headers_for_connector_type(+ meta: ConnectorAuthMetadata,+ grpc_headers: GrpcHeadersUcs,+ connector_type: ConnectorType,+) -> Result<MetadataMap, UnifiedConnectorServiceError> {+ let connector_header_key = match connector_type {+ ConnectorType::PaymentProcessor => consts::UCS_HEADER_CONNECTOR,+ ConnectorType::PayoutProcessor => consts::UCS_HEADER_PAYOUT_CONNECTOR,+ ConnectorType::SurchargeProcessor => consts::UCS_HEADER_SURCHARGE_CONNECTOR,+ connector_type => {+ return Err(+ UnifiedConnectorServiceError::RequestEncodingFailedWithReason(format!(+ "unsupported UCS connector type for connector headers: {connector_type:?}"+ )),+ )+ }+ };++ build_grpc_headers_internal(meta, grpc_headers, connector_header_key)+}+fn build_grpc_headers_internal(meta: ConnectorAuthMetadata,grpc_headers: GrpcHeadersUcs,diff --git a/crates/router/src/core/payments/access_token.rs b/crates/router/src/core/payments/access_token.rsindex 07c1a67ce7..92b0ec4e00 100644--- a/crates/router/src/core/payments/access_token.rs+++ b/crates/router/src/core/payments/access_token.rs@@ -406,7 +406,12 @@ async fn fetch_access_token_from_ucs(};let response = client- .create_access_token(create_request, connector_auth_metadata, grpc_headers)+ .create_access_token(+ create_request,+ connector_auth_metadata,+ grpc_headers,+ common_enums::ConnectorType::PaymentProcessor,+ ).await.change_context(errors::ApiErrorResponse::InternalServerError).attach_printable("UCS create_access_token gRPC call failed")?;diff --git a/crates/router/src/core/payments/gateway/access_token_gateway.rs b/crates/router/src/core/payments/gateway/access_token_gateway.rsindex c68af058cd..6bc7af69b2 100644--- a/crates/router/src/core/payments/gateway/access_token_gateway.rs+++ b/crates/router/src/core/payments/gateway/access_token_gateway.rs@@ -1,7 +1,7 @@use std::str::FromStr;use async_trait::async_trait;-use common_enums::{CallConnectorAction, ExecutionPath};+use common_enums::{CallConnectorAction, ConnectorType, ExecutionPath};use common_utils::{errors::CustomResult, id_type, request::Request, ucs_types};use error_stack::ResultExt;use hyperswitch_domain_models::{@@ -102,20 +102,54 @@ where).change_context(ConnectorError::RequestEncodingFailed).attach_printable("Failed to construct request metadata")?;- let merchant_reference_id = unified_connector_service::parse_merchant_reference_id(- header_payload- .x_reference_id- .as_deref()- .unwrap_or(router_data.payment_id.as_str()),- )- .map(ucs_types::UcsReferenceId::Payment);-- let resource_id = id_type::PaymentResourceId::from_str(router_data.attempt_id.as_str())- .inspect_err(- |err| logger::warn!(error=?err, "Invalid Payment AttemptId for UCS resource id"),+ // A merchant-authentication (access-token) call can originate from either a+ // payment or a payout. The connector type selects the UCS connector header+ // namespace, while the ids below carry the payment/payout reference context.+ let connector_type = if router_data.payout_id.is_some() {+ ConnectorType::PayoutProcessor+ } else {+ ConnectorType::PaymentProcessor+ };++ let (merchant_reference_id, resource_id) = if let Some(payout_id) =+ router_data.payout_id.as_deref()+ {+ let merchant_reference_id =+ unified_connector_service::parse_merchant_payout_reference_id(+ header_payload+ .x_reference_id+ .as_deref()+ .unwrap_or(payout_id),+ )+ .map(ucs_types::UcsReferenceId::Payout);++ let resource_id = id_type::PayoutResourceId::from_str(router_data.attempt_id.as_str())+ .inspect_err(+ |err| logger::warn!(error=?err, "Invalid Payout AttemptId for UCS resource id"),+ )+ .ok()+ .map(ucs_types::UcsResourceId::PayoutAttempt);++ (merchant_reference_id, resource_id)+ } else {+ let merchant_reference_id = unified_connector_service::parse_merchant_reference_id(+ header_payload+ .x_reference_id+ .as_deref()+ .unwrap_or(router_data.payment_id.as_str()),)- .ok()- .map(ucs_types::UcsResourceId::PaymentAttempt);+ .map(ucs_types::UcsReferenceId::Payment);++ let resource_id =+ id_type::PaymentResourceId::from_str(router_data.attempt_id.as_str())+ .inspect_err(|err| {+ logger::warn!(error=?err, "Invalid Payment AttemptId for UCS resource id")+ })+ .ok()+ .map(ucs_types::UcsResourceId::PaymentAttempt);++ (merchant_reference_id, resource_id)+ };let header_payload = state.get_grpc_headers_ucs(unified_connector_service_execution_mode)@@ -136,6 +170,7 @@ wherecreate_access_token_request,connector_auth_metadata,grpc_headers,+ connector_type,).await.attach_printable("Failed to create access token")?;