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
feat(relay): store connector response_data in relay table (#13440)
Aniket Burman8106cea97952
diff --git a/crates/hyperswitch_connectors/src/connector_relay/fiservcommercehub.rs b/crates/hyperswitch_connectors/src/connector_relay/fiservcommercehub.rsindex 3e3c296223..72c705968f 100644--- a/crates/hyperswitch_connectors/src/connector_relay/fiservcommercehub.rs+++ b/crates/hyperswitch_connectors/src/connector_relay/fiservcommercehub.rs@@ -163,6 +163,13 @@ struct TransactionInteraction {pos_condition_code: TransactionInteractionPosConditionCode,}+#[derive(Debug, Serialize)]+#[serde(rename_all = "camelCase")]+struct TransactionDetails {+ #[serde(skip_serializing_if = "Option::is_none")]+ merchant_transaction_id: Option<String>,+}+#[derive(Debug, Serialize)]#[serde(rename_all = "camelCase")]struct FiservcommercehubCreditRequestEncrypted {@@ -170,6 +177,8 @@ struct FiservcommercehubCreditRequestEncrypted {source: EncryptedSource,merchant_details: MerchantDetails,#[serde(skip_serializing_if = "Option::is_none")]+ transaction_details: Option<TransactionDetails>,+ #[serde(skip_serializing_if = "Option::is_none")]transaction_interaction: Option<TransactionInteraction>,}@@ -178,20 +187,75 @@ struct FiservcommercehubCreditRequestEncrypted {#[derive(Debug, Deserialize)]#[serde(rename_all = "camelCase")]struct TransactionProcessingDetails {- transaction_id: Option<String>,+ transaction_id: String,}#[derive(Debug, Deserialize)]#[serde(rename_all = "camelCase")]struct GatewayResponse {transaction_state: FiservcommercehubRefundState,- transaction_processing_details: Option<TransactionProcessingDetails>,+ transaction_processing_details: TransactionProcessingDetails,+}++#[derive(Debug, Deserialize)]+#[serde(rename_all = "camelCase")]+struct ProcessorResponseDetails {+ response_code: String,+ response_message: String,+ approval_code: Option<String>,+ network_routed: Option<String>,+}++#[derive(Debug, Deserialize)]+#[serde(rename_all = "camelCase")]+struct PaymentReceipt {+ processor_response_details: ProcessorResponseDetails,+}++#[derive(Debug, Deserialize)]+#[serde(rename_all = "camelCase")]+struct NetworkDetails {+ network_response_code: Option<String>,+ transaction_identifier: Option<String>,}#[derive(Debug, Deserialize)]#[serde(rename_all = "camelCase")]struct FiservcommercehubCreditResponse {gateway_response: GatewayResponse,+ payment_receipt: Option<PaymentReceipt>,+ network_details: Option<NetworkDetails>,+}++impl TryFrom<&FiservcommercehubCreditResponse>+ for hyperswitch_domain_models::relay::RelayResponseData+{+ type Error = error_stack::Report<ConnectorError>;++ fn try_from(response: &FiservcommercehubCreditResponse) -> Result<Self, Self::Error> {+ let processor_response_details = response+ .payment_receipt+ .as_ref()+ .map(|receipt| &receipt.processor_response_details);+ let network_details = response.network_details.as_ref();++ Ok(Self {+ approval_code: processor_response_details+ .and_then(|processor| processor.approval_code.clone()),+ network: processor_response_details+ .and_then(|processor| processor.network_routed.as_deref())+ .and_then(|network_routed| {+ serde_json::from_value::<common_enums::CardNetwork>(serde_json::Value::String(+ network_routed.to_owned(),+ ))+ .ok()+ }),+ network_transaction_id: network_details+ .and_then(|details| details.transaction_identifier.clone()),+ network_response_code: network_details+ .and_then(|details| details.network_response_code.clone()),+ })+ }}#[derive(Debug, Deserialize)]@@ -353,6 +417,12 @@ impl ConnectorRelayIntegration for Fiservcommercehub {let encryption_data = Self::encrypt_card(card, access_token)?;+ let transaction_details = router_data+ .connector_resource_id+ .map(|connector_resource_id| TransactionDetails {+ merchant_transaction_id: Some(connector_resource_id.to_owned()),+ });+let body = FiservcommercehubCreditRequestEncrypted {amount,source: EncryptedSource {@@ -360,6 +430,7 @@ impl ConnectorRelayIntegration for Fiservcommercehub {encryption_data,},merchant_details,+ transaction_details,transaction_interaction,};@@ -391,19 +462,39 @@ impl ConnectorRelayIntegration for Fiservcommercehub {.attach_printable("Failed to parse success response from Commerce Hub")?;let refund_status =- common_enums::RefundStatus::from(parsed.gateway_response.transaction_state);+ common_enums::RefundStatus::from(parsed.gateway_response.transaction_state.clone());++ let connector_refund_id = Some(+ parsed+ .gateway_response+ .transaction_processing_details+ .transaction_id+ .clone(),+ );++ let (error_code, error_message) = parsed+ .payment_receipt+ .as_ref()+ .filter(|_| refund_status == common_enums::RefundStatus::Failure)+ .map(|receipt| {+ let processor_response = &receipt.processor_response_details;+ (+ Some(processor_response.response_code.clone()),+ Some(processor_response.response_message.clone()),+ )+ })+ .unwrap_or((None, None));- let connector_refund_id = parsed- .gateway_response- .transaction_processing_details- .and_then(|d| d.transaction_id);+ let response_data =+ hyperswitch_domain_models::relay::RelayResponseData::try_from(&parsed).ok();Ok(UnreferencedRefundResponse {connector_refund_id,refund_status,- error_code: None,- error_message: None,+ error_code,+ error_message,raw_response,+ response_data,})}@@ -435,6 +526,7 @@ impl ConnectorRelayIntegration for Fiservcommercehub {error_code: Some(code),error_message: Some(message),raw_response,+ response_data: None,})}}diff --git a/crates/hyperswitch_domain_models/src/relay.rs b/crates/hyperswitch_domain_models/src/relay.rsindex ce68529175..b694d466eb 100644--- a/crates/hyperswitch_domain_models/src/relay.rs+++ b/crates/hyperswitch_domain_models/src/relay.rs@@ -559,6 +559,18 @@ impl RelayUnreferencedRefundData {}}+#[derive(Debug, Serialize)]+pub struct RelayResponseData {+ #[serde(skip_serializing_if = "Option::is_none")]+ pub approval_code: Option<String>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub network: Option<enums::CardNetwork>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub network_transaction_id: Option<String>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub network_response_code: Option<String>,+}+#[derive(Debug)]pub enum RelayUpdate {ErrorUpdate {diff --git a/crates/hyperswitch_interfaces/src/relay.rs b/crates/hyperswitch_interfaces/src/relay.rsindex c1bad8bffa..fafcb11772 100644--- a/crates/hyperswitch_interfaces/src/relay.rs+++ b/crates/hyperswitch_interfaces/src/relay.rs@@ -13,6 +13,8 @@ use crate::errors::ConnectorError;pub struct UnreferencedRefundRouterData<'a> {/// Merchant-supplied request fields (amount, currency, card data, etc.)pub request: &'a UnreferencedRefundRequest,+ /// Connector-side resource identifier to tag the outbound request with.+ pub connector_resource_id: Option<&'a str>,/// Access token fetched from the Redis cache by Hyperswitch — never supplied by the merchant.pub access_token: Option<AccessToken>,/// Parsed connector credentials.@@ -34,6 +36,8 @@ pub struct UnreferencedRefundResponse {pub error_message: Option<String>,/// Raw JSON response from the connectorpub raw_response: Option<Secret<serde_json::Value>>,+ /// Structured response fields for DB storage+ pub response_data: Option<hyperswitch_domain_models::relay::RelayResponseData>,}/// Trait implemented by connectors that support relay operations (e.g. unreferenced refund).@@ -77,6 +81,7 @@ pub trait ConnectorRelayIntegration {error_code: Some(status_code.to_string()),error_message: Some(format!("Server error: HTTP {status_code}")),raw_response: None,+ response_data: None,})}diff --git a/crates/router/src/core/relay.rs b/crates/router/src/core/relay.rsindex db2f24c394..ce6f326458 100644--- a/crates/router/src/core/relay.rs+++ b/crates/router/src/core/relay.rs@@ -1099,6 +1099,7 @@ async fn process_unreferenced_refund(let relay_router_data = UnreferencedRefundRouterData {request,+ connector_resource_id: request.connector_resource_id.as_deref(),access_token,auth_type: &auth_type,base_url,@@ -1128,7 +1129,18 @@ async fn process_unreferenced_refund(status: relay_status,error_code: connector_resp.error_code,error_message: connector_resp.error_message,- response_data: None,+ response_data: connector_resp+ .response_data+ .and_then(|d| {+ serde_json::to_value(d)+ .inspect_err(|err| {+ router_env::logger::error!(+ "Failed to serialize relay response_data: {err:?}"+ )+ })+ .ok()+ })+ .map(Secret::new),};(bytes, relay_update)}