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
refactor(refunds): refactor refunds create to check for unintended 5xx (#1332)
Abhishek Marrivaguff17b62dc270
diff --git a/crates/router/src/core/refunds.rs b/crates/router/src/core/refunds.rsindex e749fc2550..710bef7235 100644--- a/crates/router/src/core/refunds.rs+++ b/crates/router/src/core/refunds.rs@@ -45,7 +45,7 @@ pub async fn refund_create_core(merchant_account.storage_scheme,).await- .change_context(errors::ApiErrorResponse::PaymentNotFound)?;+ .to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?;utils::when(payment_intent.status != enums::IntentStatus::Succeeded,@@ -55,7 +55,7 @@ pub async fn refund_create_core(},)?;- // Amount is not passed in request refer from payment attempt.+ // Amount is not passed in request refer from payment intent.amount = req.amount.unwrap_or(payment_intent.amount_captured@@ -63,13 +63,14 @@ pub async fn refund_create_core(.into_report().attach_printable("amount captured is none in a successful payment")?,);+//[#299]: Can we change the flow based on some workflow ideautils::when(amount <= 0, || {Err(report!(errors::ApiErrorResponse::InvalidDataFormat {field_name: "amount".to_string(),expected_format: "positive integer".to_string()})- .attach_printable("amount less than zero"))+ .attach_printable("amount less than or equal to zero"))})?;payment_attempt = db@@ -79,7 +80,7 @@ pub async fn refund_create_core(merchant_account.storage_scheme,).await- .change_context(errors::ApiErrorResponse::SuccessfulPaymentNotFound)?;+ .to_not_found_response(errors::ApiErrorResponse::SuccessfulPaymentNotFound)?;let creds_identifier = req.merchant_connector_details@@ -142,15 +143,12 @@ pub async fn trigger_refund_to_gateway(&state.conf.connectors,&routed_through,api::GetToken::Connector,- )- .change_context(errors::ApiErrorResponse::InternalServerError)- .attach_printable("Failed to get the connector")?;+ )?;let currency = payment_attempt.currency.ok_or_else(|| {- report!(errors::ApiErrorResponse::MissingRequiredField {- field_name: "currency"- })- .attach_printable("Transaction in invalid")+ report!(errors::ApiErrorResponse::InternalServerError).attach_printable(+ "Transaction in invalid. Missing field \"currency\" in payment_attempt.",+ )})?;validator::validate_for_valid_refunds(payment_attempt, connector.connector_name)?;@@ -236,7 +234,7 @@ pub async fn trigger_refund_to_gateway(merchant_account.storage_scheme,).await- .change_context(errors::ApiErrorResponse::InternalServerError)+ .to_not_found_response(errors::ApiErrorResponse::InternalServerError).attach_printable_lazy(|| {format!("Failed while updating refund: refund_id: {}",@@ -307,7 +305,7 @@ pub async fn refund_retrieve_core(merchant_account.storage_scheme,).await- .to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?;+ .to_not_found_response(errors::ApiErrorResponse::InternalServerError)?;let creds_identifier = request.merchant_connector_details@@ -451,7 +449,7 @@ pub async fn sync_refund_with_gateway(merchant_account.storage_scheme,).await- .change_context(errors::ApiErrorResponse::InternalServerError)+ .to_not_found_response(errors::ApiErrorResponse::RefundNotFound).attach_printable_lazy(|| {format!("Unable to update refund with refund_id: {}",@@ -548,20 +546,19 @@ pub async fn validate_and_create_refund(})? {Some(refund) => refund,None => {- let connecter_transaction_id = match &payment_attempt.connector_transaction_id {- Some(id) => id,- None => "",- };-+ let connecter_transaction_id = payment_attempt.clone().connector_transaction_id.ok_or_else(|| {+ report!(errors::ApiErrorResponse::InternalServerError)+ .attach_printable("Transaction in invalid. Missing field \"connector_transaction_id\" in payment_attempt.")+ })?;all_refunds = db.find_refund_by_merchant_id_connector_transaction_id(&merchant_account.merchant_id,- connecter_transaction_id,+ &connecter_transaction_id,merchant_account.storage_scheme,).await- .change_context(errors::ApiErrorResponse::RefundNotFound)- .attach_printable("Failed to fetch refund")?;+ .to_not_found_response(errors::ApiErrorResponse::RefundNotFound)?;+currency = payment_attempt.currency.get_required_value("currency")?;//[#249]: Add Connector Based Validation here.@@ -618,6 +615,7 @@ pub async fn validate_and_create_refund(.insert_refund(refund_create_req, merchant_account.storage_scheme).await.to_duplicate_response(errors::ApiErrorResponse::DuplicateRefundRequest)?;+schedule_refund_execution(state,refund,@@ -715,6 +713,7 @@ pub async fn schedule_refund_execution(.await.change_context(errors::ApiErrorResponse::InternalServerError).attach_printable("Failed to find the process id")?;+let result = match refund.refund_status {enums::RefundStatus::Pending | enums::RefundStatus::ManualReview => {match (refund.sent_to_gateway, refund_process) {diff --git a/crates/router/src/core/utils.rs b/crates/router/src/core/utils.rsindex 53c97ffdb3..f748ea2f0c 100644--- a/crates/router/src/core/utils.rs+++ b/crates/router/src/core/utils.rs@@ -58,7 +58,8 @@ pub async fn construct_refund_router_data<'a, F>(let payment_method_type = payment_attempt.payment_method- .get_required_value("payment_method_type")?;+ .get_required_value("payment_method_type")+ .change_context(errors::ApiErrorResponse::InternalServerError)?;let webhook_url = Some(helpers::create_webhook_url(&state.conf.server.base_url.clone(),