refactor(refunds): refactor refunds create to check for unintended 5xx (#1332)

Co-authored-by: Nitesh Balla <nitesh.balla@juspay.in>
Co-authored-by: Arun Raj M <jarnura47@gmail.com>
This commit is contained in:
Abhishek Marrivagu
2023-06-23 15:46:18 +05:30
committed by GitHub
parent 6ec6272f2a
commit ff17b62dc2
2 changed files with 23 additions and 23 deletions

View File

@ -45,7 +45,7 @@ pub async fn refund_create_core(
merchant_account.storage_scheme, merchant_account.storage_scheme,
) )
.await .await
.change_context(errors::ApiErrorResponse::PaymentNotFound)?; .to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?;
utils::when( utils::when(
payment_intent.status != enums::IntentStatus::Succeeded, 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( amount = req.amount.unwrap_or(
payment_intent payment_intent
.amount_captured .amount_captured
@ -63,13 +63,14 @@ pub async fn refund_create_core(
.into_report() .into_report()
.attach_printable("amount captured is none in a successful payment")?, .attach_printable("amount captured is none in a successful payment")?,
); );
//[#299]: Can we change the flow based on some workflow idea //[#299]: Can we change the flow based on some workflow idea
utils::when(amount <= 0, || { utils::when(amount <= 0, || {
Err(report!(errors::ApiErrorResponse::InvalidDataFormat { Err(report!(errors::ApiErrorResponse::InvalidDataFormat {
field_name: "amount".to_string(), field_name: "amount".to_string(),
expected_format: "positive integer".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 payment_attempt = db
@ -79,7 +80,7 @@ pub async fn refund_create_core(
merchant_account.storage_scheme, merchant_account.storage_scheme,
) )
.await .await
.change_context(errors::ApiErrorResponse::SuccessfulPaymentNotFound)?; .to_not_found_response(errors::ApiErrorResponse::SuccessfulPaymentNotFound)?;
let creds_identifier = req let creds_identifier = req
.merchant_connector_details .merchant_connector_details
@ -142,15 +143,12 @@ pub async fn trigger_refund_to_gateway(
&state.conf.connectors, &state.conf.connectors,
&routed_through, &routed_through,
api::GetToken::Connector, api::GetToken::Connector,
) )?;
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to get the connector")?;
let currency = payment_attempt.currency.ok_or_else(|| { let currency = payment_attempt.currency.ok_or_else(|| {
report!(errors::ApiErrorResponse::MissingRequiredField { report!(errors::ApiErrorResponse::InternalServerError).attach_printable(
field_name: "currency" "Transaction in invalid. Missing field \"currency\" in payment_attempt.",
}) )
.attach_printable("Transaction in invalid")
})?; })?;
validator::validate_for_valid_refunds(payment_attempt, connector.connector_name)?; 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, merchant_account.storage_scheme,
) )
.await .await
.change_context(errors::ApiErrorResponse::InternalServerError) .to_not_found_response(errors::ApiErrorResponse::InternalServerError)
.attach_printable_lazy(|| { .attach_printable_lazy(|| {
format!( format!(
"Failed while updating refund: refund_id: {}", "Failed while updating refund: refund_id: {}",
@ -307,7 +305,7 @@ pub async fn refund_retrieve_core(
merchant_account.storage_scheme, merchant_account.storage_scheme,
) )
.await .await
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?; .to_not_found_response(errors::ApiErrorResponse::InternalServerError)?;
let creds_identifier = request let creds_identifier = request
.merchant_connector_details .merchant_connector_details
@ -451,7 +449,7 @@ pub async fn sync_refund_with_gateway(
merchant_account.storage_scheme, merchant_account.storage_scheme,
) )
.await .await
.change_context(errors::ApiErrorResponse::InternalServerError) .to_not_found_response(errors::ApiErrorResponse::RefundNotFound)
.attach_printable_lazy(|| { .attach_printable_lazy(|| {
format!( format!(
"Unable to update refund with refund_id: {}", "Unable to update refund with refund_id: {}",
@ -548,20 +546,19 @@ pub async fn validate_and_create_refund(
})? { })? {
Some(refund) => refund, Some(refund) => refund,
None => { None => {
let connecter_transaction_id = match &payment_attempt.connector_transaction_id { let connecter_transaction_id = payment_attempt.clone().connector_transaction_id.ok_or_else(|| {
Some(id) => id, report!(errors::ApiErrorResponse::InternalServerError)
None => "", .attach_printable("Transaction in invalid. Missing field \"connector_transaction_id\" in payment_attempt.")
}; })?;
all_refunds = db all_refunds = db
.find_refund_by_merchant_id_connector_transaction_id( .find_refund_by_merchant_id_connector_transaction_id(
&merchant_account.merchant_id, &merchant_account.merchant_id,
connecter_transaction_id, &connecter_transaction_id,
merchant_account.storage_scheme, merchant_account.storage_scheme,
) )
.await .await
.change_context(errors::ApiErrorResponse::RefundNotFound) .to_not_found_response(errors::ApiErrorResponse::RefundNotFound)?;
.attach_printable("Failed to fetch refund")?;
currency = payment_attempt.currency.get_required_value("currency")?; currency = payment_attempt.currency.get_required_value("currency")?;
//[#249]: Add Connector Based Validation here. //[#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) .insert_refund(refund_create_req, merchant_account.storage_scheme)
.await .await
.to_duplicate_response(errors::ApiErrorResponse::DuplicateRefundRequest)?; .to_duplicate_response(errors::ApiErrorResponse::DuplicateRefundRequest)?;
schedule_refund_execution( schedule_refund_execution(
state, state,
refund, refund,
@ -715,6 +713,7 @@ pub async fn schedule_refund_execution(
.await .await
.change_context(errors::ApiErrorResponse::InternalServerError) .change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to find the process id")?; .attach_printable("Failed to find the process id")?;
let result = match refund.refund_status { let result = match refund.refund_status {
enums::RefundStatus::Pending | enums::RefundStatus::ManualReview => { enums::RefundStatus::Pending | enums::RefundStatus::ManualReview => {
match (refund.sent_to_gateway, refund_process) { match (refund.sent_to_gateway, refund_process) {

View File

@ -58,7 +58,8 @@ pub async fn construct_refund_router_data<'a, F>(
let payment_method_type = payment_attempt let payment_method_type = payment_attempt
.payment_method .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( let webhook_url = Some(helpers::create_webhook_url(
&state.conf.server.base_url.clone(), &state.conf.server.base_url.clone(),