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

feat: Added Create connector customer granular flow HS<>UCS tunnel (#10397)

Amitsingh Tanwar158dd75714de
diff --git a/crates/external_services/src/grpc_client/unified_connector_service.rs b/crates/external_services/src/grpc_client/unified_connector_service.rs
index 9a6098d9a3..063ab2387a 100644
--- a/crates/external_services/src/grpc_client/unified_connector_service.rs
+++ b/crates/external_services/src/grpc_client/unified_connector_service.rs
@@ -182,6 +182,37 @@ impl UnifiedConnectorServiceClient {
})
}
+ /// Performs Create Connector Customer Granular
+ pub async fn create_connector_customer(
+ &self,
+ create_customer_request: payments_grpc::PaymentServiceCreateConnectorCustomerRequest,
+ connector_auth_metadata: ConnectorAuthMetadata,
+ grpc_headers: GrpcHeadersUcs,
+ ) -> UnifiedConnectorServiceResult<
+ tonic::Response<payments_grpc::PaymentServiceCreateConnectorCustomerResponse>,
+ > {
+ let mut request = tonic::Request::new(create_customer_request);
+
+ let connector_name = connector_auth_metadata.connector_name.clone();
+ let metadata =
+ build_unified_connector_service_grpc_headers(connector_auth_metadata, grpc_headers)?;
+ *request.metadata_mut() = metadata;
+
+ self.client
+ .clone()
+ .create_connector_customer(request)
+ .await
+ .change_context(UnifiedConnectorServiceError::PaymentConnectorCustomerCreateFailure)
+ .inspect_err(|error| {
+ logger::error!(
+ grpc_error=?error,
+ method="create_connector_customer_granular",
+ connector_name=?connector_name,
+ "UCS create connector customer granular gRPC call failed"
+ )
+ })
+ }
+
/// Performs Payment Pre Authenticate
pub async fn payment_pre_authenticate(
&self,
diff --git a/crates/router/src/core/payments/gateway.rs b/crates/router/src/core/payments/gateway.rs
index 809483cd71..082cbf73d4 100644
--- a/crates/router/src/core/payments/gateway.rs
+++ b/crates/router/src/core/payments/gateway.rs
@@ -1,6 +1,7 @@
pub mod access_token_gateway;
pub mod authorize_gateway;
pub mod context;
+pub mod create_customer_gateway;
pub mod psync_gateway;
pub mod session_token_gateway;
use std::sync;
diff --git a/crates/router/src/core/payments/gateway/create_customer_gateway.rs b/crates/router/src/core/payments/gateway/create_customer_gateway.rs
new file mode 100644
index 0000000000..544f4a0af1
--- /dev/null
+++ b/crates/router/src/core/payments/gateway/create_customer_gateway.rs
@@ -0,0 +1,174 @@
+use std::str::FromStr;
+
+use async_trait::async_trait;
+use common_enums::{CallConnectorAction, ExecutionPath};
+use common_utils::{errors::CustomResult, request::Request};
+use error_stack::ResultExt;
+use hyperswitch_domain_models::{
+ router_data::RouterData, router_flow_types as domain,
+ router_request_types::ConnectorCustomerData,
+};
+use hyperswitch_interfaces::{
+ api::gateway as payment_gateway,
+ connector_integration_interface::{BoxedConnectorIntegrationInterface, RouterDataConversion},
+ errors::ConnectorError,
+};
+use unified_connector_service_client::payments as payments_grpc;
+
+use crate::{
+ core::{
+ payments::gateway::context::RouterGatewayContext, unified_connector_service,
+ unified_connector_service::handle_unified_connector_service_response_for_create_connector_customer,
+ },
+ routes::SessionState,
+ types::{self, transformers::ForeignTryFrom},
+};
+
+// =============================================================================
+// PaymentGateway Implementation for domain::CreateConnectorCustomer
+// =============================================================================
+
+/// Implementation of PaymentGateway for CreateConnectorCustomer flow
+#[async_trait]
+impl<RCD>
+ payment_gateway::PaymentGateway<
+ SessionState,
+ RCD,
+ Self,
+ ConnectorCustomerData,
+ types::PaymentsResponseData,
+ RouterGatewayContext,
+ > for domain::CreateConnectorCustomer
+where
+ RCD: Clone
+ + Send
+ + Sync
+ + 'static
+ + RouterDataConversion<Self, ConnectorCustomerData, types::PaymentsResponseData>,
+{
+ async fn execute(
+ self: Box<Self>,
+ state: &SessionState,
+ _connector_integration: BoxedConnectorIntegrationInterface<
+ Self,
+ RCD,
+ ConnectorCustomerData,
+ types::PaymentsResponseData,
+ >,
+ router_data: &RouterData<Self, ConnectorCustomerData, types::PaymentsResponseData>,
+ call_connector_action: CallConnectorAction,
+ _connector_request: Option<Request>,
+ _return_raw_connector_response: Option<bool>,
+ context: RouterGatewayContext,
+ ) -> CustomResult<
+ RouterData<Self, ConnectorCustomerData, types::PaymentsResponseData>,
+ ConnectorError,
+ > {
+ let connector_name = router_data.connector.clone();
+ let _connector_enum = common_enums::connector_enums::Connector::from_str(&connector_name)
+ .change_context(ConnectorError::InvalidConnectorName)?;
+ let merchant_connector_account = context.merchant_connector_account;
+ let platform = context.platform;
+ let lineage_ids = context.lineage_ids;
+ let unified_connector_service_execution_mode = context.execution_mode;
+
+ let client = state
+ .grpc_client
+ .unified_connector_service_client
+ .clone()
+ .ok_or(ConnectorError::RequestEncodingFailed)
+ .attach_printable("Failed to fetch Unified Connector Service client")?;
+
+ let create_connector_customer_request =
+ payments_grpc::PaymentServiceCreateConnectorCustomerRequest::foreign_try_from((
+ router_data,
+ call_connector_action,
+ ))
+ .change_context(ConnectorError::RequestEncodingFailed)
+ .attach_printable("Failed to construct Create Connector Customer Request")?;
+
+ let connector_auth_metadata =
+ unified_connector_service::build_unified_connector_service_auth_metadata(
+ merchant_connector_account,
+ &platform,
+ )
+ .change_context(ConnectorError::RequestEncodingFailed)
+ .attach_printable("Failed to construct request metadata")?;
+
+ let grpc_headers = state
+ .get_grpc_headers_ucs(unified_connector_service_execution_mode)
+ .external_vault_proxy_metadata(None)
+ .merchant_reference_id(None)
+ .lineage_ids(lineage_ids);
+ let updated_router_data = Box::pin(unified_connector_service::ucs_logging_wrapper_new(
+ router_data.clone(),
+ state,
+ create_connector_customer_request,
+ grpc_headers,
+ |mut router_data, create_connector_customer_request, grpc_headers| async move {
+ let response = Box::pin(client.create_connector_customer(
+ create_connector_customer_request,
+ connector_auth_metadata,
+ grpc_headers,
+ ))
+ .await
+ .attach_printable("Failed to create connector customer")?;
+
+ let create_connector_customer_response = response.into_inner();
+
+ let (connector_customer_result, status_code) =
+ handle_unified_connector_service_response_for_create_connector_customer(
+ create_connector_customer_response.clone(),
+ )
+ .attach_printable("Failed to deserialize UCS response")?;
+
+ router_data.response = connector_customer_result;
+ router_data.connector_http_status_code = Some(status_code);
+
+ Ok((router_data, create_connector_customer_response))
+ },
+ ))
+ .await
+ .change_context(ConnectorError::ResponseHandlingFailed)?;
+
+ Ok(updated_router_data)
+ }
+}
+
+/// Implementation of FlowGateway for CreateConnectorCustomer
+///
+/// This allows the flow to provide its specific gateway based on execution path
+impl<RCD>
+ payment_gateway::FlowGateway<
+ SessionState,
+ RCD,
+ ConnectorCustomerData,
+ types::PaymentsResponseData,
+ RouterGatewayContext,
+ > for domain::CreateConnectorCustomer
+where
+ RCD: Clone
+ + Send
+ + Sync
+ + 'static
+ + RouterDataConversion<Self, ConnectorCustomerData, types::PaymentsResponseData>,
+{
+ fn get_gateway(
+ execution_path: ExecutionPath,
+ ) -> Box<
+ dyn payment_gateway::PaymentGateway<
+ SessionState,
+ RCD,
+ Self,
+ ConnectorCustomerData,
+ types::PaymentsResponseData,
+ RouterGatewayContext,
+ >,
+ > {
+ match execution_path {
+ ExecutionPath::Direct => Box::new(payment_gateway::DirectGateway),
+ ExecutionPath::UnifiedConnectorService
+ | ExecutionPath::ShadowUnifiedConnectorService => Box::new(Self),
+ }
+ }
+}
diff --git a/crates/router/src/core/unified_connector_service.rs b/crates/router/src/core/unified_connector_service.rs
index c95e9f3967..6a76d29ea2 100644
--- a/crates/router/src/core/unified_connector_service.rs
+++ b/crates/router/src/core/unified_connector_service.rs
@@ -1157,6 +1157,18 @@ pub fn handle_unified_connector_service_response_for_payment_authorize(
})
}
+pub fn handle_unified_connector_service_response_for_create_connector_customer(
+ response: payments_grpc::PaymentServiceCreateConnectorCustomerResponse,
+) -> CustomResult<(Result<PaymentsResponseData, ErrorResponse>, u16), UnifiedConnectorServiceError>
+{
+ let status_code = transformers::convert_connector_service_status_code(response.status_code)?;
+
+ let connector_customer_result =
+ Result::<PaymentsResponseData, ErrorResponse>::foreign_try_from(response)?;
+
+ Ok((connector_customer_result, status_code))
+}
+
pub fn handle_unified_connector_service_response_for_payment_post_authenticate(
response: payments_grpc::PaymentServicePostAuthenticateResponse,
) -> UnifiedConnectorServiceResult {
diff --git a/crates/router/src/core/unified_connector_service/transformers.rs b/crates/router/src/core/unified_connector_service/transformers.rs
index 9c54ce02ba..6d864da8ed 100644
--- a/crates/router/src/core/unified_connector_service/transformers.rs
+++ b/crates/router/src/core/unified_connector_service/transformers.rs
@@ -241,6 +241,56 @@ impl
}
}
+impl
+ transformers::ForeignTryFrom<(
+ &RouterData<
+ hyperswitch_domain_models::router_flow_types::payments::CreateConnectorCustomer,
+ router_request_types::ConnectorCustomerData,
+ PaymentsResponseData,
+ >,
+ common_enums::CallConnectorAction,
+ )> for payments_grpc::PaymentServiceCreateConnectorCustomerRequest
+{
+ type Error = error_stack::Report<UnifiedConnectorServiceError>;
+
+ fn foreign_try_from(
+ (router_data, _call_connector_action): (
+ &RouterData<
+ hyperswitch_domain_models::router_flow_types::payments::CreateConnectorCustomer,
+ router_request_types::ConnectorCustomerData,
+ PaymentsResponseData,
+ >,
+ common_enums::CallConnectorAction,
+ ),
+ ) -> Result<Self, Self::Error> {
+ let request_ref_id = router_data.connector_request_reference_id.clone();
+
+ Ok(Self {
+ request_ref_id: Some(Identifier {
+ id_type: Some(payments_grpc::identifier::IdType::Id(request_ref_id)),
+ }),
+ merchant_account_metadata: HashMap::new(),
+ customer_name: router_data.request.description.clone(),
+ email: router_data
+ .request
+ .email
+ .clone()
+ .map(|e| e.expose().expose().into()),
+ customer_id: router_data
+ .customer_id
+ .clone()
+ .map(|id| id.get_string_repr().to_string()),
+ phone_number: router_data
+ .request
+ .phone
+ .as_ref()
+ .map(|phone| phone.peek().to_string()),
+ address: None,
+ metadata: HashMap::new(),
+ })
+ }
+}
+
impl
transformers::ForeignTryFrom<(
&RouterData<PSync, PaymentsSyncData, PaymentsResponseData>,
@@ -1692,6 +1742,47 @@ impl transformers::ForeignTryFrom<payments_grpc::PaymentServiceCaptureResponse>
}
}
+impl transformers::ForeignTryFrom<payments_grpc::PaymentServiceCreateConnectorCustomerResponse>
+ for Result<PaymentsResponseData, ErrorResponse>
+{
+ type Error = error_stack::Report<UnifiedConnectorServiceError>;
+
+ fn foreign_try_from(
+ response: payments_grpc::PaymentServiceCreateConnectorCustomerResponse,
+ ) -> Result<Self, Self::Error> {
+ let status_code = convert_connector_service_status_code(response.status_code)?;
+
+ let response = if response.error_code.is_some() {
+ router_env::logger::error!(
+ error_message = ?response.error_message,
+ error_code = ?response.error_code,
+ status_code,
+ "UCS create connector customer failed"
+ );
+
+ Err(ErrorResponse {
+ code: response.error_code().to_owned(),
+ message: response.error_message().to_owned(),
+ reason: Some(response.error_message().to_owned()),
+ status_code,
+ attempt_status: None,
+ connector_transaction_id: None,
+ network_decline_code: None,
+ network_advice_code: None,
+ network_error_message: None,
+ connector_metadata: None,
+ })
+ } else {
+ use hyperswitch_domain_models::router_response_types::ConnectorCustomerResponseData;
+ Ok(PaymentsResponseData::ConnectorCustomerResponse(
+ ConnectorCustomerResponseData::new_with_customer_id(response.connector_customer_id),
+ ))
+ };
+
+ Ok(response)
+ }
+}
+
impl transformers::ForeignTryFrom<payments_grpc::PaymentServiceRegisterResponse>
for Result<(PaymentsResponseData, AttemptStatus), ErrorResponse>
{