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(core): throw bad request error for refunds when the flow is not supported or implemented (#10735)
Vani Gupta66f34b91dfec
diff --git a/crates/common_enums/src/enums.rs b/crates/common_enums/src/enums.rsindex 72658a00d1..63d98faa1e 100644--- a/crates/common_enums/src/enums.rs+++ b/crates/common_enums/src/enums.rs@@ -9987,6 +9987,12 @@ pub enum FeatureStatus {Supported,}+impl FeatureStatus {+ pub fn is_supported(&self) -> bool {+ matches!(self, Self::Supported)+ }+}+/// The type of tokenization to use for the payment method#[derive(Clone,diff --git a/crates/hyperswitch_domain_models/src/router_response_types.rs b/crates/hyperswitch_domain_models/src/router_response_types.rsindex 1cb3aa5a2a..b57f10bc99 100644--- a/crates/hyperswitch_domain_models/src/router_response_types.rs+++ b/crates/hyperswitch_domain_models/src/router_response_types.rs@@ -725,6 +725,12 @@ pub trait SupportedPaymentMethodsExt {payment_method_type: common_enums::PaymentMethodType,payment_method_details: PaymentMethodDetails,);++ fn is_refund_supported(+ &self,+ payment_method: &common_enums::PaymentMethod,+ payment_method_type: &common_enums::PaymentMethodType,+ ) -> bool;}impl SupportedPaymentMethodsExt for SupportedPaymentMethods {@@ -743,6 +749,23 @@ impl SupportedPaymentMethodsExt for SupportedPaymentMethods {self.insert(payment_method, payment_method_type_metadata);}}++ fn is_refund_supported(+ &self,+ payment_method: &common_enums::PaymentMethod,+ payment_method_type: &common_enums::PaymentMethodType,+ ) -> bool {+ self.get(payment_method)+ .and_then(|pm_types| pm_types.get(payment_method_type))+ .map(|details| details.supports_refund())+ .unwrap_or(true)+ }+}++impl PaymentMethodDetails {+ fn supports_refund(&self) -> bool {+ self.refunds.is_supported()+ }}#[derive(Debug, Clone)]diff --git a/crates/router/src/core/refunds.rs b/crates/router/src/core/refunds.rsindex 91fa2fd4c6..34938c4561 100644--- a/crates/router/src/core/refunds.rs+++ b/crates/router/src/core/refunds.rs@@ -188,8 +188,6 @@ pub async fn trigger_refund_to_gateway()})?;- validator::validate_for_valid_refunds(payment_attempt, connector.connector_name)?;-// Fetch merchant connector accountlet profile_id = payment_intent.profile_id@@ -1354,6 +1352,22 @@ pub async fn validate_and_create_refund(.clone().ok_or(errors::ApiErrorResponse::InternalServerError).attach_printable("No connector populated in payment attempt")?;++ // Get connector enum for validation+ let connector_enum = api::ConnectorData::get_connector_by_name(+ &state.conf.connectors,+ &connector,+ api::GetToken::Connector,+ payment_attempt.merchant_connector_id.clone(),+ )?;++ // Validate refund support before creating the refund record+ validator::validate_for_valid_refunds(+ payment_attempt,+ connector_enum.connector,+ connector_enum.connector_name,+ )?;+let (connector_transaction_id, processor_transaction_data) =ConnectorTransactionId::form_id_and_data(connector_transaction_id);let refund_create_req = diesel_refund::RefundNew {diff --git a/crates/router/src/core/utils/refunds_validator.rs b/crates/router/src/core/utils/refunds_validator.rsindex 8d532b2c70..47236cea1c 100644--- a/crates/router/src/core/utils/refunds_validator.rs+++ b/crates/router/src/core/utils/refunds_validator.rs@@ -1,5 +1,9 @@use diesel_models::refund as diesel_refund;use error_stack::report;+#[cfg(feature = "v1")]+use hyperswitch_domain_models::router_response_types::SupportedPaymentMethodsExt;+#[cfg(feature = "v1")]+use hyperswitch_interfaces::{self, api::ConnectorSpecifications};use router_env::{instrument, tracing};use time::PrimitiveDateTime;@@ -114,6 +118,7 @@ pub fn validate_refund_list(limit: Option<i64>) -> CustomResult<i64, errors::Api#[cfg(feature = "v1")]pub fn validate_for_valid_refunds(payment_attempt: &hyperswitch_domain_models::payments::payment_attempt::PaymentAttempt,+ connector_enum: hyperswitch_interfaces::connector_integration_interface::ConnectorEnum,connector: api_models::enums::Connector,) -> RouterResult<()> {let payment_method = payment_attempt@@ -121,30 +126,23 @@ pub fn validate_for_valid_refunds(.as_ref().get_required_value("payment_method")?;- match payment_method {- diesel_models::enums::PaymentMethod::PayLater- | diesel_models::enums::PaymentMethod::Wallet => {- let payment_method_type = payment_attempt- .payment_method_type- .get_required_value("payment_method_type")?;+ let payment_method_type = payment_attempt+ .payment_method_type+ .get_required_value("payment_method_type")?;- utils::when(- matches!(- (connector, payment_method_type),- (- api_models::enums::Connector::Braintree,- diesel_models::enums::PaymentMethodType::Paypal,- )- ),- || {- Err(errors::ApiErrorResponse::RefundNotPossible {- connector: connector.to_string(),- }- .into())- },- )- }- _ => Ok(()),+ let supported_payment_methods = connector_enum.get_supported_payment_methods();++ let is_refund_supported = supported_payment_methods.is_none_or(|supported_payment_method| {+ supported_payment_method.is_refund_supported(payment_method, &payment_method_type)+ });++ if !is_refund_supported {+ Err(errors::ApiErrorResponse::InvalidRequestData {+ message: format!("Refunds are currently not supported for {payment_method_type} transactions via {connector}"),+ }+ .into())+ } else {+ Ok(())}}