Skip to content

Commit aea3fe2

Browse files
Revert "fix(refunds): fetch refund if insert fails due to duplicate response (#2682)"
This reverts commit 433cdfa.
1 parent 6dc71fe commit aea3fe2

File tree

1 file changed

+20
-36
lines changed

1 file changed

+20
-36
lines changed

crates/router/src/core/refunds.rs

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -521,13 +521,14 @@ pub async fn validate_and_create_refund(
521521
creds_identifier: Option<String>,
522522
) -> RouterResult<refunds::RefundResponse> {
523523
let db = &*state.store;
524+
let (refund_id, all_refunds, currency, refund_create_req, refund);
524525

525526
// Only for initial dev and testing
526527
let refund_type = req.refund_type.unwrap_or_default();
527528

528529
// If Refund Id not passed in request Generate one.
529530

530-
let refund_id = core_utils::get_or_generate_id("refund_id", &req.refund_id, "ref")?;
531+
refund_id = core_utils::get_or_generate_id("refund_id", &req.refund_id, "ref")?;
531532

532533
let predicate = req
533534
.merchant_id
@@ -547,7 +548,7 @@ pub async fn validate_and_create_refund(
547548
.attach_printable("Transaction in invalid. Missing field \"connector_transaction_id\" in payment_attempt.")
548549
})?;
549550

550-
let all_refunds = db
551+
all_refunds = db
551552
.find_refund_by_merchant_id_connector_transaction_id(
552553
&merchant_account.merchant_id,
553554
&connecter_transaction_id,
@@ -556,7 +557,7 @@ pub async fn validate_and_create_refund(
556557
.await
557558
.to_not_found_response(errors::ApiErrorResponse::RefundNotFound)?;
558559

559-
let currency = payment_attempt.currency.get_required_value("currency")?;
560+
currency = payment_attempt.currency.get_required_value("currency")?;
560561

561562
//[#249]: Add Connector Based Validation here.
562563
validator::validate_payment_order_age(&payment_intent.created_at, state.conf.refund.max_age)
@@ -584,10 +585,10 @@ pub async fn validate_and_create_refund(
584585
.into_report()
585586
.attach_printable("No connector populated in payment attempt")?;
586587

587-
let refund_create_req = storage::RefundNew::default()
588+
refund_create_req = storage::RefundNew::default()
588589
.set_refund_id(refund_id.to_string())
589590
.set_internal_reference_id(utils::generate_id(consts::ID_LENGTH, "refid"))
590-
.set_external_reference_id(Some(refund_id.clone()))
591+
.set_external_reference_id(Some(refund_id))
591592
.set_payment_id(req.payment_id)
592593
.set_merchant_id(merchant_account.merchant_id.clone())
593594
.set_connector_transaction_id(connecter_transaction_id.to_string())
@@ -606,39 +607,22 @@ pub async fn validate_and_create_refund(
606607
.set_profile_id(payment_intent.profile_id.clone())
607608
.to_owned();
608609

609-
let refund = match db
610+
refund = db
610611
.insert_refund(refund_create_req, merchant_account.storage_scheme)
611612
.await
612-
{
613-
Ok(refund) => {
614-
schedule_refund_execution(
615-
state,
616-
refund.clone(),
617-
refund_type,
618-
merchant_account,
619-
key_store,
620-
payment_attempt,
621-
payment_intent,
622-
creds_identifier,
623-
)
624-
.await?
625-
}
626-
Err(err) => {
627-
if err.current_context().is_db_unique_violation() {
628-
db.find_refund_by_merchant_id_refund_id(
629-
merchant_account.merchant_id.as_str(),
630-
refund_id.as_str(),
631-
merchant_account.storage_scheme,
632-
)
633-
.await
634-
.to_not_found_response(errors::ApiErrorResponse::RefundNotFound)?
635-
} else {
636-
return Err(err)
637-
.change_context(errors::ApiErrorResponse::RefundNotFound)
638-
.attach_printable("Inserting Refund failed");
639-
}
640-
}
641-
};
613+
.to_duplicate_response(errors::ApiErrorResponse::DuplicateRefundRequest)?;
614+
615+
schedule_refund_execution(
616+
state,
617+
refund.clone(),
618+
refund_type,
619+
merchant_account,
620+
key_store,
621+
payment_attempt,
622+
payment_intent,
623+
creds_identifier,
624+
)
625+
.await?;
642626

643627
Ok(refund.foreign_into())
644628
}

0 commit comments

Comments
 (0)