Repository: juspay/hyperswitch
File: crates/router/src/core/payments/flows/capture_flow.rs (Rust)

Code before:
        merchant_context: &domain::MerchantContext,
        _connector_data: &api::ConnectorData,
        unified_connector_service_execution_mode: common_enums::ExecutionMode,
        merchant_order_reference_id: Option<String>,
    ) -> 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_capture_request =
            payments_grpc::PaymentServiceCaptureRequest::foreign_try_from(&*self)
                .change_context(ApiErrorResponse::InternalServerError)
                .attach_printable("Failed to construct Payment Capture 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_capture_request,
            header_payload,
            |mut router_data, payment_capture_request, grpc_headers| async move {
                let response = client
                    .payment_capture(
                        payment_capture_request,
                        connector_auth_metadata,
                        grpc_headers,
                    )
                    .await

Review comment (security):
should fall back to merchant_order_reference_id, Please refer to `call_unified_connector_service` of authorize flow

Code after:
        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_capture_request =
            payments_grpc::PaymentServiceCaptureRequest::foreign_try_from(&*self)
                .change_context(ApiErrorResponse::InternalServerError)
                .attach_printable("Failed to construct Payment Capture 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_capture_request,
            header_payload,
            |mut router_data, payment_capture_request, grpc_headers| async move {
                let response = client
                    .payment_capture(
                        payment_capture_request,
                        connector_auth_metadata,
                        grpc_headers,
                    )