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(ucs): retain previous refund status when UCS returns Unspecified (#12842)
Sai Harsha Vardhanc7cf0c5c9e35
diff --git a/crates/router/src/core/unified_connector_service.rs b/crates/router/src/core/unified_connector_service.rsindex ab9cc9d2b8..df1de36d0b 100644--- a/crates/router/src/core/unified_connector_service.rs+++ b/crates/router/src/core/unified_connector_service.rs@@ -2332,22 +2332,24 @@ pub fn extract_connector_response_from_ucs(pub fn handle_unified_connector_service_response_for_payment_refund(response: payments_grpc::RefundResponse,+ prev_status: common_enums::RefundStatus,) -> UnifiedConnectorServiceRefundResult {let status_code = transformers::convert_connector_service_status_code(response.status_code)?;let router_data_response: Result<RefundsResponseData, ErrorResponse> =- Result::<RefundsResponseData, ErrorResponse>::foreign_try_from(response)?;+ Result::<RefundsResponseData, ErrorResponse>::foreign_try_from((response, prev_status))?;Ok((router_data_response, status_code))}pub fn handle_unified_connector_service_response_for_refund_get(response: payments_grpc::RefundResponse,+ prev_status: common_enums::RefundStatus,) -> UnifiedConnectorServiceRefundResult {let status_code = transformers::convert_connector_service_status_code(response.status_code)?;let router_data_response =- Result::<RefundsResponseData, ErrorResponse>::foreign_try_from(response)?;+ Result::<RefundsResponseData, ErrorResponse>::foreign_try_from((response, prev_status))?;Ok((router_data_response, status_code))}@@ -3056,6 +3058,8 @@ pub async fn call_unified_connector_service_for_refund_execute(.merchant_reference_id(merchant_reference_id).resource_id(resource_id);+ let prev_refund_status = router_data.request.refund_status;+// Make UCS refund call with logging wrapperBox::pin(ucs_logging_wrapper(router_data,@@ -3113,9 +3117,12 @@ pub async fn call_unified_connector_service_for_refund_execute(// Transform UCS response back to RouterDatalet (refund_response_data, status_code) =- handle_unified_connector_service_response_for_payment_refund(grpc_response.clone())- .change_context(errors::ApiErrorResponse::InternalServerError)- .attach_printable("Failed to transform UCS refund response")?;+ handle_unified_connector_service_response_for_payment_refund(+ grpc_response.clone(),+ prev_refund_status,+ )+ .change_context(errors::ApiErrorResponse::InternalServerError)+ .attach_printable("Failed to transform UCS refund response")?;router_data.response = refund_response_data;router_data.connector_http_status_code = Some(status_code);@@ -3186,6 +3193,8 @@ pub async fn call_unified_connector_service_for_refund_sync(.merchant_reference_id(merchant_reference_id).resource_id(resource_id);+ let prev_refund_status = router_data.request.refund_status;+// Make UCS refund sync call with logging wrapperBox::pin(ucs_logging_wrapper(router_data,@@ -3243,9 +3252,12 @@ pub async fn call_unified_connector_service_for_refund_sync(// Transform UCS response back to RouterDatalet (refund_response_data, status_code) =- handle_unified_connector_service_response_for_refund_get(grpc_response.clone())- .change_context(errors::ApiErrorResponse::InternalServerError)- .attach_printable("Failed to transform UCS refund get response")?;+ handle_unified_connector_service_response_for_refund_get(+ grpc_response.clone(),+ prev_refund_status,+ )+ .change_context(errors::ApiErrorResponse::InternalServerError)+ .attach_printable("Failed to transform UCS refund get response")?;router_data.response = refund_response_data;router_data.connector_http_status_code = Some(status_code);diff --git a/crates/router/src/core/unified_connector_service/transformers.rs b/crates/router/src/core/unified_connector_service/transformers.rsindex 4024b0b5d6..b901f4cd61 100644--- a/crates/router/src/core/unified_connector_service/transformers.rs+++ b/crates/router/src/core/unified_connector_service/transformers.rs@@ -6025,13 +6025,17 @@ impl transformers::ForeignTryFrom<&RouterData<RSync, RefundsData, RefundsRespons}}-/// Transform UCS RefundResponse into Result<RefundsResponseData, ErrorResponse>-impl transformers::ForeignTryFrom<payments_grpc::RefundResponse>+/// Transform UCS RefundResponse into Result<RefundsResponseData, ErrorResponse>.+/// prev_status is the refund status prior to the UCS call; it is used as a fallback when UCS+/// returns Unspecified (i.e. an unknown connector refund status) so the DB state is not corrupted.+impl transformers::ForeignTryFrom<(payments_grpc::RefundResponse, RefundStatus)>for Result<RefundsResponseData, ErrorResponse>{type Error = error_stack::Report<UnifiedConnectorServiceError>;- fn foreign_try_from(response: payments_grpc::RefundResponse) -> Result<Self, Self::Error> {+ fn foreign_try_from(+ (response, prev_status): (payments_grpc::RefundResponse, RefundStatus),+ ) -> Result<Self, Self::Error> {let status_code = convert_connector_service_status_code(response.status_code)?;let response = if let Some(error_info) = response.error.as_ref() {@@ -6085,7 +6089,7 @@ impl transformers::ForeignTryFrom<payments_grpc::RefundResponse>connector_metadata: None,})} else {- let refund_status = RefundStatus::foreign_try_from(response.status())?;+ let refund_status = RefundStatus::foreign_try_from((response.status(), prev_status))?;Ok(RefundsResponseData {connector_refund_id: response.connector_refund_id,@@ -6197,12 +6201,20 @@ impl}}-impl transformers::ForeignTryFrom<payments_grpc::RefundStatus> for RefundStatus {+impl transformers::ForeignTryFrom<(payments_grpc::RefundStatus, Self)> for RefundStatus {type Error = error_stack::Report<UnifiedConnectorServiceError>;- fn foreign_try_from(grpc_status: payments_grpc::RefundStatus) -> Result<Self, Self::Error> {+ fn foreign_try_from(+ (grpc_status, prev_status): (payments_grpc::RefundStatus, Self),+ ) -> Result<Self, Self::Error> {match grpc_status {- payments_grpc::RefundStatus::Unspecified => Ok(Self::Pending),+ payments_grpc::RefundStatus::Unspecified => {+ router_env::logger::warn!(+ "Received Unspecified refund status from UCS; retaining previous status {:?}",+ prev_status+ );+ Ok(prev_status)+ }payments_grpc::RefundStatus::RefundFailure => Ok(Self::Failure),payments_grpc::RefundStatus::RefundManualReview => Ok(Self::ManualReview),payments_grpc::RefundStatus::RefundPending => Ok(Self::Pending),