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(core): Hyperswitch <|> UCS Void flow integration (#9714)

Chaitak Gorai10587a0c96b1
diff --git a/Cargo.lock b/Cargo.lock
index aad1a08dc0..ceeb9451a5 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3813,7 +3813,7 @@ dependencies = [
[[package]]
name = "grpc-api-types"
version = "0.1.0"
-source = "git+https://github.com/juspay/connector-service?rev=36731a85e6b7877139ca3b6ee5d6b919702df8b4#36731a85e6b7877139ca3b6ee5d6b919702df8b4"
+source = "git+https://github.com/juspay/connector-service?rev=2f8b321665485d4ccf12a4ab06f4b8f36ece5135#2f8b321665485d4ccf12a4ab06f4b8f36ece5135"
dependencies = [
"axum 0.8.4",
"error-stack 0.5.0",
@@ -7456,7 +7456,7 @@ dependencies = [
[[package]]
name = "rust-grpc-client"
version = "0.1.0"
-source = "git+https://github.com/juspay/connector-service?rev=36731a85e6b7877139ca3b6ee5d6b919702df8b4#36731a85e6b7877139ca3b6ee5d6b919702df8b4"
+source = "git+https://github.com/juspay/connector-service?rev=2f8b321665485d4ccf12a4ab06f4b8f36ece5135#2f8b321665485d4ccf12a4ab06f4b8f36ece5135"
dependencies = [
"grpc-api-types",
]
@@ -10243,7 +10243,7 @@ checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9"
[[package]]
name = "ucs_cards"
version = "0.1.0"
-source = "git+https://github.com/juspay/connector-service?rev=36731a85e6b7877139ca3b6ee5d6b919702df8b4#36731a85e6b7877139ca3b6ee5d6b919702df8b4"
+source = "git+https://github.com/juspay/connector-service?rev=2f8b321665485d4ccf12a4ab06f4b8f36ece5135#2f8b321665485d4ccf12a4ab06f4b8f36ece5135"
dependencies = [
"bytes 1.10.1",
"error-stack 0.4.1",
@@ -10259,7 +10259,7 @@ dependencies = [
[[package]]
name = "ucs_common_enums"
version = "0.1.0"
-source = "git+https://github.com/juspay/connector-service?rev=36731a85e6b7877139ca3b6ee5d6b919702df8b4#36731a85e6b7877139ca3b6ee5d6b919702df8b4"
+source = "git+https://github.com/juspay/connector-service?rev=2f8b321665485d4ccf12a4ab06f4b8f36ece5135#2f8b321665485d4ccf12a4ab06f4b8f36ece5135"
dependencies = [
"serde",
"strum 0.26.3",
@@ -10270,7 +10270,7 @@ dependencies = [
[[package]]
name = "ucs_common_utils"
version = "0.1.0"
-source = "git+https://github.com/juspay/connector-service?rev=36731a85e6b7877139ca3b6ee5d6b919702df8b4#36731a85e6b7877139ca3b6ee5d6b919702df8b4"
+source = "git+https://github.com/juspay/connector-service?rev=2f8b321665485d4ccf12a4ab06f4b8f36ece5135#2f8b321665485d4ccf12a4ab06f4b8f36ece5135"
dependencies = [
"anyhow",
"base64 0.21.7",
diff --git a/crates/external_services/Cargo.toml b/crates/external_services/Cargo.toml
index 4cef3ae026..582d437d1a 100644
--- a/crates/external_services/Cargo.toml
+++ b/crates/external_services/Cargo.toml
@@ -69,7 +69,7 @@ reqwest = { version = "0.11.27", features = ["rustls-tls"] }
http = "0.2.12"
url = { version = "2.5.4", features = ["serde"] }
quick-xml = { version = "0.31.0", features = ["serialize"] }
-unified-connector-service-client = { git = "https://github.com/juspay/connector-service", rev = "36731a85e6b7877139ca3b6ee5d6b919702df8b4", package = "rust-grpc-client" }
+unified-connector-service-client = { git = "https://github.com/juspay/connector-service", rev = "2f8b321665485d4ccf12a4ab06f4b8f36ece5135", package = "rust-grpc-client" }
open-feature = { version = "0.2.5", optional = true }
superposition_provider = { version = "0.88.1", optional = true }
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 a7d7a20542..a4b0f71156 100644
--- a/crates/external_services/src/grpc_client/unified_connector_service.rs
+++ b/crates/external_services/src/grpc_client/unified_connector_service.rs
@@ -259,6 +259,36 @@ impl UnifiedConnectorServiceClient {
})
}
+ /// Performs Payment Cancel/Void
+ pub async fn payment_cancel(
+ &self,
+ payment_void_request: payments_grpc::PaymentServiceVoidRequest,
+ connector_auth_metadata: ConnectorAuthMetadata,
+ grpc_headers: GrpcHeadersUcs,
+ ) -> UnifiedConnectorServiceResult<tonic::Response<payments_grpc::PaymentServiceVoidResponse>>
+ {
+ let mut request = tonic::Request::new(payment_void_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()
+ .void(request)
+ .await
+ .change_context(UnifiedConnectorServiceError::PaymentCancelFailure)
+ .inspect_err(|error| {
+ logger::error!(
+ grpc_error=?error,
+ method="payment_cancel",
+ connector_name=?connector_name,
+ "UCS payment cancel gRPC call failed"
+ )
+ })
+ }
+
/// Transforms incoming webhook through UCS
pub async fn transform_incoming_webhook(
&self,
diff --git a/crates/hyperswitch_interfaces/Cargo.toml b/crates/hyperswitch_interfaces/Cargo.toml
index d7424fc812..043793acf6 100644
--- a/crates/hyperswitch_interfaces/Cargo.toml
+++ b/crates/hyperswitch_interfaces/Cargo.toml
@@ -29,7 +29,7 @@ serde_json = "1.0.140"
strum = { version = "0.26", features = ["derive"] }
thiserror = "1.0.69"
time = "0.3.41"
-unified-connector-service-client = { git = "https://github.com/juspay/connector-service", rev = "36731a85e6b7877139ca3b6ee5d6b919702df8b4", package = "rust-grpc-client" }
+unified-connector-service-client = { git = "https://github.com/juspay/connector-service", rev = "2f8b321665485d4ccf12a4ab06f4b8f36ece5135", package = "rust-grpc-client" }
unified-connector-service-masking = { git = "https://github.com/juspay/hyperswitch", tag = "2025.09.24.0", package = "masking" }
# First party crates
diff --git a/crates/hyperswitch_interfaces/src/unified_connector_service/transformers.rs b/crates/hyperswitch_interfaces/src/unified_connector_service/transformers.rs
index 1d9768fd53..ab94501e2b 100644
--- a/crates/hyperswitch_interfaces/src/unified_connector_service/transformers.rs
+++ b/crates/hyperswitch_interfaces/src/unified_connector_service/transformers.rs
@@ -92,6 +92,10 @@ pub enum UnifiedConnectorServiceError {
/// Failed to transform incoming webhook from gRPC Server
#[error("Failed to transform incoming webhook from gRPC Server")]
WebhookTransformFailure,
+
+ /// Failed to perform Payment Cancel from gRPC Server
+ #[error("Failed to perform Cancel from gRPC Server")]
+ PaymentCancelFailure,
}
/// UCS Webhook transformation status
diff --git a/crates/router/Cargo.toml b/crates/router/Cargo.toml
index 8fba5d4a65..5610d69502 100644
--- a/crates/router/Cargo.toml
+++ b/crates/router/Cargo.toml
@@ -89,8 +89,8 @@ reqwest = { version = "0.11.27", features = ["json", "rustls-tls", "gzip", "mult
ring = "0.17.14"
rust_decimal = { version = "1.37.1", features = ["serde-with-float", "serde-with-str"] }
rust-i18n = { git = "https://github.com/kashif-m/rust-i18n", rev = "f2d8096aaaff7a87a847c35a5394c269f75e077a" }
-unified-connector-service-client = { git = "https://github.com/juspay/connector-service", rev = "36731a85e6b7877139ca3b6ee5d6b919702df8b4", package = "rust-grpc-client" }
-unified-connector-service-cards = { git = "https://github.com/juspay/connector-service", rev = "36731a85e6b7877139ca3b6ee5d6b919702df8b4", package = "ucs_cards" }
+unified-connector-service-client = { git = "https://github.com/juspay/connector-service", rev = "2f8b321665485d4ccf12a4ab06f4b8f36ece5135", package = "rust-grpc-client" }
+unified-connector-service-cards = { git = "https://github.com/juspay/connector-service", rev = "2f8b321665485d4ccf12a4ab06f4b8f36ece5135", package = "ucs_cards" }
unified-connector-service-masking = { git = "https://github.com/juspay/hyperswitch", tag = "2025.09.24.0", package = "masking" }
rustc-hash = "1.1.0"
rustls = "0.22"
diff --git a/crates/router/src/core/payments/flows/cancel_flow.rs b/crates/router/src/core/payments/flows/cancel_flow.rs
index 2dd933ba50..5702778761 100644
--- a/crates/router/src/core/payments/flows/cancel_flow.rs
+++ b/crates/router/src/core/payments/flows/cancel_flow.rs
@@ -1,14 +1,26 @@
+use std::str::FromStr;
+
use async_trait::async_trait;
+use common_utils::{id_type, ucs_types};
+use error_stack::ResultExt;
+use external_services::grpc_client;
+use hyperswitch_domain_models::payments as domain_payments;
+use router_env::logger;
+use unified_connector_service_client::payments as payments_grpc;
use super::{ConstructFlowSpecificData, Feature};
use crate::{
core::{
- errors::{ConnectorErrorExt, RouterResult},
+ errors::{ApiErrorResponse, ConnectorErrorExt, RouterResult},
payments::{self, access_token, helpers, transformers, PaymentData},
+ unified_connector_service::{
+ build_unified_connector_service_auth_metadata,
+ handle_unified_connector_service_response_for_payment_cancel, ucs_logging_wrapper,
+ },
},
routes::{metrics, SessionState},
services,
- types::{self, api, domain},
+ types::{self, api, domain, transformers::ForeignTryFrom},
};
#[cfg(feature = "v1")]
#[async_trait]
@@ -152,4 +164,91 @@ impl Feature<api::Void, types::PaymentsCancelData>
Ok((request, true))
}
+
+ async fn call_unified_connector_service<'a>(
+ &mut self,
+ state: &SessionState,
+ header_payload: &domain_payments::HeaderPayload,
+ lineage_ids: grpc_client::LineageIds,
+ #[cfg(feature = "v1")] merchant_connector_account: helpers::MerchantConnectorAccountType,
+ #[cfg(feature = "v2")]
+ merchant_connector_account: domain::MerchantConnectorAccountTypeDetails,
+ merchant_context: &domain::MerchantContext,
+ _connector_data: &api::ConnectorData,
+ unified_connector_service_execution_mode: common_enums::ExecutionMode,
+ merchant_order_reference_id: Option<String>,
+ _call_connector_action: common_enums::CallConnectorAction,
+ ) -> RouterResult<()> {
+ let client = state
+ .grpc_client
+ .unified_connector_service_client
+ .clone()
+ .ok_or(ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to fetch Unified Connector Service client")?;
+
+ let payment_void_request =
+ payments_grpc::PaymentServiceVoidRequest::foreign_try_from(&*self)
+ .change_context(ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to construct Payment Void Request")?;
+
+ let connector_auth_metadata = build_unified_connector_service_auth_metadata(
+ merchant_connector_account,
+ merchant_context,
+ )
+ .change_context(ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to construct request metadata")?;
+
+ let merchant_reference_id = header_payload
+ .x_reference_id
+ .clone()
+ .or(merchant_order_reference_id)
+ .map(|id| id_type::PaymentReferenceId::from_str(id.as_str()))
+ .transpose()
+ .inspect_err(|err| logger::warn!(error=?err, "Invalid Merchant ReferenceId found"))
+ .ok()
+ .flatten()
+ .map(ucs_types::UcsReferenceId::Payment);
+
+ let header_payload = state
+ .get_grpc_headers_ucs(unified_connector_service_execution_mode)
+ .external_vault_proxy_metadata(None)
+ .merchant_reference_id(merchant_reference_id)
+ .lineage_ids(lineage_ids);
+
+ let updated_router_data = Box::pin(ucs_logging_wrapper(
+ self.clone(),
+ state,
+ payment_void_request,
+ header_payload,
+ |mut router_data, payment_void_request, grpc_headers| async move {
+ let response = client
+ .payment_cancel(payment_void_request, connector_auth_metadata, grpc_headers)
+ .await
+ .change_context(ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to Cancel payment")?;
+
+ let payment_void_response = response.into_inner();
+
+ let (router_data_response, status_code) =
+ handle_unified_connector_service_response_for_payment_cancel(
+ payment_void_response.clone(),
+ )
+ .change_context(ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to deserialize UCS response")?;
+
+ let router_data_response = router_data_response.map(|(response, status)| {
+ router_data.status = status;
+ response
+ });
+ router_data.response = router_data_response;
+ router_data.connector_http_status_code = Some(status_code);
+
+ Ok((router_data, payment_void_response))
+ },
+ ))
+ .await?;
+
+ *self = updated_router_data;
+ Ok(())
+ }
}
diff --git a/crates/router/src/core/unified_connector_service.rs b/crates/router/src/core/unified_connector_service.rs
index 32155400fd..2ef7d89488 100644
--- a/crates/router/src/core/unified_connector_service.rs
+++ b/crates/router/src/core/unified_connector_service.rs
@@ -792,6 +792,17 @@ pub fn handle_unified_connector_service_response_for_payment_repeat(
Ok((router_data_response, status_code))
}
+pub fn handle_unified_connector_service_response_for_payment_cancel(
+ response: payments_grpc::PaymentServiceVoidResponse,
+) -> UnifiedConnectorServiceResult {
+ let status_code = transformers::convert_connector_service_status_code(response.status_code)?;
+
+ let router_data_response =
+ Result::<(PaymentsResponseData, AttemptStatus), ErrorResponse>::foreign_try_from(response)?;
+
+ Ok((router_data_response, status_code))
+}
+
pub fn build_webhook_secrets_from_merchant_connector_account(
#[cfg(feature = "v1")] merchant_connector_account: &MerchantConnectorAccountType,
#[cfg(feature = "v2")] merchant_connector_account: &MerchantConnectorAccountTypeDetails,
diff --git a/crates/router/src/core/unified_connector_service/transformers.rs b/crates/router/src/core/unified_connector_service/transformers.rs
index becd2b1a42..9ef122958d 100644
--- a/crates/router/src/core/unified_connector_service/transformers.rs
+++ b/crates/router/src/core/unified_connector_service/transformers.rs
@@ -13,7 +13,7 @@ use hyperswitch_domain_models::{
},
router_request_types::{
AuthenticationData, ExternalVaultProxyPaymentsData, PaymentsAuthorizeData,
- PaymentsSyncData, SetupMandateRequestData,
+ PaymentsCancelData, PaymentsSyncData, SetupMandateRequestData,
},
router_response_types::{PaymentsResponseData, RedirectForm},
};
@@ -34,7 +34,7 @@ use url::Url;
use crate::{
core::{errors, unified_connector_service},
- types::transformers,
+ types::{api, transformers},
};
impl
transformers::ForeignTryFrom<(
@@ -235,6 +235,7 @@ impl
.unwrap_or_default(),
test_mode: router_data.test_mode,
connector_customer_id: router_data.connector_customer.clone(),
+ merchant_account_metadata: HashMap::new(),
})
}
}
@@ -366,6 +367,7 @@ impl
.unwrap_or_default(),
test_mode: None,
connector_customer_id: router_data.connector_customer.clone(),
+ merchant_account_metadata: HashMap::new(),
})
}
}
@@ -430,7 +432,7 @@ impl
.customer_name
.clone()
.map(|customer_name| customer_name.peek().to_owned()),
- connector_customer_id: router_data
+ customer_id: router_data
.request
.customer_id
.as_ref()
@@ -472,6 +474,8 @@ impl
customer_acceptance,
browser_info,
payment_experience: None,
+ connector_customer_id: router_data.connector_customer.clone(),
+ merchant_account_metadata: HashMap::new(),
})
}
}
@@ -555,6 +559,7 @@ impl
test_mode: None,
payment_method_type: None,
access_token: None,
+ merchant_account_metadata: HashMap::new(),
})
}
}
@@ -725,7 +730,7 @@ impl transformers::ForeignTryFrom<payments_grpc::PaymentServiceRegisterResponse>
response.mandate_reference.map(|grpc_mandate| {
hyperswitch_domain_models::router_response_types::MandateReference {
connector_mandate_id: grpc_mandate.mandate_id,
- payment_method_id: None,
+ payment_method_id: grpc_mandate.payment_method_id,
mandate_metadata: None,
connector_mandate_request_reference_id: None,
}
@@ -1245,3 +1250,146 @@ pub fn build_webhook_transform_request(
access_token: None,
})
}
+
+impl transformers::ForeignTryFrom<&RouterData<api::Void, PaymentsCancelData, PaymentsResponseData>>
+ for payments_grpc::PaymentServiceVoidRequest
+{
+ type Error = error_stack::Report<UnifiedConnectorServiceError>;
+
+ fn foreign_try_from(
+ router_data: &RouterData<api::Void, PaymentsCancelData, PaymentsResponseData>,
+ ) -> Result<Self, Self::Error> {
+ let browser_info = router_data
+ .request
+ .browser_info
+ .clone()
+ .map(payments_grpc::BrowserInformation::foreign_try_from)
+ .transpose()?;
+
+ let currency = router_data
+ .request
+ .currency
+ .map(payments_grpc::Currency::foreign_try_from)
+ .transpose()?;
+
+ Ok(Self {
+ request_ref_id: Some(Identifier {
+ id_type: Some(payments_grpc::identifier::IdType::Id(
+ router_data.connector_request_reference_id.clone(),
+ )),
+ }),
+ transaction_id: if router_data.request.connector_transaction_id.is_empty() {
+ None
+ } else {
+ Some(Identifier {
+ id_type: Some(payments_grpc::identifier::IdType::Id(
+ router_data.request.connector_transaction_id.clone(),
+ )),
+ })
+ },
+ cancellation_reason: router_data.request.cancellation_reason.clone(),
+ all_keys_required: None,
+ browser_info,
+ access_token: None,
+ amount: router_data.request.amount,
+ currency: currency.map(|c| c.into()),
+ connector_metadata: router_data
+ .request
+ .metadata
+ .as_ref()
+ .and_then(|val| val.as_object())
+ .map(|map| {
+ map.iter()
+ .filter_map(|(k, v)| v.as_str().map(|s| (k.clone(), s.to_string())))
+ .collect::<HashMap<String, String>>()
+ })
+ .unwrap_or_default(),
+ })
+ }
+}
+
+impl transformers::ForeignTryFrom<payments_grpc::PaymentServiceVoidResponse>
+ for Result<(PaymentsResponseData, AttemptStatus), ErrorResponse>
+{
+ type Error = error_stack::Report<UnifiedConnectorServiceError>;
+
+ fn foreign_try_from(
+ response: payments_grpc::PaymentServiceVoidResponse,
+ ) -> Result<Self, Self::Error> {
+ let connector_response_reference_id =
+ response.response_ref_id.as_ref().and_then(|identifier| {
+ identifier
+ .id_type
+ .clone()
+ .and_then(|id_type| match id_type {
+ payments_grpc::identifier::IdType::Id(id) => Some(id),
+ payments_grpc::identifier::IdType::EncodedData(encoded_data) => {
+ Some(encoded_data)
+ }
+ payments_grpc::identifier::IdType::NoResponseIdMarker(_) => None,
+ })
+ });
+
+ let status_code = convert_connector_service_status_code(response.status_code)?;
+
+ let response = if response.error_code.is_some() {
+ let attempt_status = match response.status() {
+ payments_grpc::PaymentStatus::AttemptStatusUnspecified => None,
+ _ => Some(AttemptStatus::foreign_try_from(response.status())?),
+ };
+
+ 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,
+ connector_transaction_id: connector_response_reference_id,
+ network_decline_code: None,
+ network_advice_code: None,
+ network_error_message: None,
+ connector_metadata: None,
+ })
+ } else {
+ let status = AttemptStatus::foreign_try_from(response.status())?;
+
+ Ok((
+ PaymentsResponseData::TransactionResponse {
+ resource_id: response.transaction_id.as_ref().and_then(|identifier| {
+ identifier
+ .id_type
+ .clone()
+ .and_then(|id_type| match id_type {
+ payments_grpc::identifier::IdType::Id(id) => Some(
+ hyperswitch_domain_models::router_request_types::ResponseId::ConnectorTransactionId(id),
+ ),
+ payments_grpc::identifier::IdType::EncodedData(encoded_data) => Some(
+ hyperswitch_domain_models::router_request_types::ResponseId::ConnectorTransactionId(encoded_data),
+ ),
+ payments_grpc::identifier::IdType::NoResponseIdMarker(_) => None,
+ })
+ }).unwrap_or(hyperswitch_domain_models::router_request_types::ResponseId::NoResponseId),
+ redirection_data: Box::new(None),
+ mandate_reference: Box::new(
+ response.mandate_reference.map(|grpc_mandate| {
+ hyperswitch_domain_models::router_response_types::MandateReference {
+ connector_mandate_id: grpc_mandate.mandate_id,
+ payment_method_id: grpc_mandate.payment_method_id,
+ mandate_metadata: None,
+ connector_mandate_request_reference_id: None,
+ }
+ })
+ ),
+ connector_metadata: None,
+ network_txn_id: None,
+ connector_response_reference_id,
+ incremental_authorization_allowed: response.incremental_authorization_allowed,
+ charges: None,
+ },
+ status,
+ ))
+ };
+
+ Ok(response)
+ }
+}