Skip to content

Commit 3399328

Browse files
AaronTJDevAaron Jackson
andauthored
refactor(router): remove unnecessary function from Refunds Validate Flow (#2609)
Co-authored-by: Aaron Jackson <[email protected]>
1 parent 2f9a355 commit 3399328

File tree

2 files changed

+71
-129
lines changed

2 files changed

+71
-129
lines changed

crates/router/src/core/refunds.rs

Lines changed: 71 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -541,106 +541,85 @@ pub async fn validate_and_create_refund(
541541
.attach_printable("invalid merchant_id in request"))
542542
})?;
543543

544-
let refund = match validator::validate_uniqueness_of_refund_id_against_merchant_id(
545-
db,
546-
&payment_intent.payment_id,
547-
&merchant_account.merchant_id,
548-
&refund_id,
549-
merchant_account.storage_scheme,
550-
)
551-
.await
552-
.change_context(errors::ApiErrorResponse::InternalServerError)
553-
.attach_printable_lazy(|| {
554-
format!(
555-
"Unique violation while checking refund_id: {} against merchant_id: {}",
556-
refund_id, merchant_account.merchant_id
544+
let connecter_transaction_id = payment_attempt.clone().connector_transaction_id.ok_or_else(|| {
545+
report!(errors::ApiErrorResponse::InternalServerError)
546+
.attach_printable("Transaction in invalid. Missing field \"connector_transaction_id\" in payment_attempt.")
547+
})?;
548+
549+
all_refunds = db
550+
.find_refund_by_merchant_id_connector_transaction_id(
551+
&merchant_account.merchant_id,
552+
&connecter_transaction_id,
553+
merchant_account.storage_scheme,
557554
)
558-
})? {
559-
Some(refund) => refund,
560-
None => {
561-
let connecter_transaction_id = payment_attempt.clone().connector_transaction_id.ok_or_else(|| {
562-
report!(errors::ApiErrorResponse::InternalServerError)
563-
.attach_printable("Transaction in invalid. Missing field \"connector_transaction_id\" in payment_attempt.")
564-
})?;
565-
all_refunds = db
566-
.find_refund_by_merchant_id_connector_transaction_id(
567-
&merchant_account.merchant_id,
568-
&connecter_transaction_id,
569-
merchant_account.storage_scheme,
570-
)
571-
.await
572-
.to_not_found_response(errors::ApiErrorResponse::RefundNotFound)?;
555+
.await
556+
.to_not_found_response(errors::ApiErrorResponse::RefundNotFound)?;
573557

574-
currency = payment_attempt.currency.get_required_value("currency")?;
558+
currency = payment_attempt.currency.get_required_value("currency")?;
575559

576-
//[#249]: Add Connector Based Validation here.
577-
validator::validate_payment_order_age(
578-
&payment_intent.created_at,
560+
//[#249]: Add Connector Based Validation here.
561+
validator::validate_payment_order_age(&payment_intent.created_at, state.conf.refund.max_age)
562+
.change_context(errors::ApiErrorResponse::InvalidDataFormat {
563+
field_name: "created_at".to_string(),
564+
expected_format: format!(
565+
"created_at not older than {} days",
579566
state.conf.refund.max_age,
580-
)
581-
.change_context(errors::ApiErrorResponse::InvalidDataFormat {
582-
field_name: "created_at".to_string(),
583-
expected_format: format!(
584-
"created_at not older than {} days",
585-
state.conf.refund.max_age,
586-
),
587-
})?;
567+
),
568+
})?;
588569

589-
validator::validate_refund_amount(payment_attempt.amount, &all_refunds, refund_amount)
590-
.change_context(errors::ApiErrorResponse::RefundAmountExceedsPaymentAmount)?;
570+
validator::validate_refund_amount(payment_attempt.amount, &all_refunds, refund_amount)
571+
.change_context(errors::ApiErrorResponse::RefundAmountExceedsPaymentAmount)?;
591572

592-
validator::validate_maximum_refund_against_payment_attempt(
593-
&all_refunds,
594-
state.conf.refund.max_attempts,
595-
)
596-
.change_context(errors::ApiErrorResponse::MaximumRefundCount)?;
573+
validator::validate_maximum_refund_against_payment_attempt(
574+
&all_refunds,
575+
state.conf.refund.max_attempts,
576+
)
577+
.change_context(errors::ApiErrorResponse::MaximumRefundCount)?;
597578

598-
let connector = payment_attempt
599-
.connector
600-
.clone()
601-
.ok_or(errors::ApiErrorResponse::InternalServerError)
602-
.into_report()
603-
.attach_printable("No connector populated in payment attempt")?;
604-
605-
refund_create_req = storage::RefundNew::default()
606-
.set_refund_id(refund_id.to_string())
607-
.set_internal_reference_id(utils::generate_id(consts::ID_LENGTH, "refid"))
608-
.set_external_reference_id(Some(refund_id))
609-
.set_payment_id(req.payment_id)
610-
.set_merchant_id(merchant_account.merchant_id.clone())
611-
.set_connector_transaction_id(connecter_transaction_id.to_string())
612-
.set_connector(connector)
613-
.set_refund_type(req.refund_type.unwrap_or_default().foreign_into())
614-
.set_total_amount(payment_attempt.amount)
615-
.set_refund_amount(refund_amount)
616-
.set_currency(currency)
617-
.set_created_at(Some(common_utils::date_time::now()))
618-
.set_modified_at(Some(common_utils::date_time::now()))
619-
.set_refund_status(enums::RefundStatus::Pending)
620-
.set_metadata(req.metadata)
621-
.set_description(req.reason.clone())
622-
.set_attempt_id(payment_attempt.attempt_id.clone())
623-
.set_refund_reason(req.reason)
624-
.to_owned();
625-
626-
refund = db
627-
.insert_refund(refund_create_req, merchant_account.storage_scheme)
628-
.await
629-
.to_duplicate_response(errors::ApiErrorResponse::DuplicateRefundRequest)?;
579+
let connector = payment_attempt
580+
.connector
581+
.clone()
582+
.ok_or(errors::ApiErrorResponse::InternalServerError)
583+
.into_report()
584+
.attach_printable("No connector populated in payment attempt")?;
585+
586+
refund_create_req = storage::RefundNew::default()
587+
.set_refund_id(refund_id.to_string())
588+
.set_internal_reference_id(utils::generate_id(consts::ID_LENGTH, "refid"))
589+
.set_external_reference_id(Some(refund_id))
590+
.set_payment_id(req.payment_id)
591+
.set_merchant_id(merchant_account.merchant_id.clone())
592+
.set_connector_transaction_id(connecter_transaction_id.to_string())
593+
.set_connector(connector)
594+
.set_refund_type(req.refund_type.unwrap_or_default().foreign_into())
595+
.set_total_amount(payment_attempt.amount)
596+
.set_refund_amount(refund_amount)
597+
.set_currency(currency)
598+
.set_created_at(Some(common_utils::date_time::now()))
599+
.set_modified_at(Some(common_utils::date_time::now()))
600+
.set_refund_status(enums::RefundStatus::Pending)
601+
.set_metadata(req.metadata)
602+
.set_description(req.reason.clone())
603+
.set_attempt_id(payment_attempt.attempt_id.clone())
604+
.set_refund_reason(req.reason)
605+
.to_owned();
630606

631-
schedule_refund_execution(
632-
state,
633-
refund,
634-
refund_type,
635-
merchant_account,
636-
key_store,
637-
payment_attempt,
638-
payment_intent,
639-
creds_identifier,
640-
)
641-
.await?
642-
}
643-
};
607+
refund = db
608+
.insert_refund(refund_create_req, merchant_account.storage_scheme)
609+
.await
610+
.to_duplicate_response(errors::ApiErrorResponse::DuplicateRefundRequest)?;
611+
612+
schedule_refund_execution(
613+
state,
614+
refund.clone(),
615+
refund_type,
616+
merchant_account,
617+
key_store,
618+
payment_attempt,
619+
payment_intent,
620+
creds_identifier,
621+
)
622+
.await?;
644623

645624
Ok(refund.foreign_into())
646625
}

crates/router/src/core/refunds/validator.rs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use time::PrimitiveDateTime;
44

55
use crate::{
66
core::errors::{self, CustomResult, RouterResult},
7-
db::StorageInterface,
8-
logger,
97
types::storage::{self, enums},
108
utils::{self, OptionExt},
119
};
@@ -92,41 +90,6 @@ pub fn validate_maximum_refund_against_payment_attempt(
9290
})
9391
}
9492

95-
#[instrument(skip(db))]
96-
pub async fn validate_uniqueness_of_refund_id_against_merchant_id(
97-
db: &dyn StorageInterface,
98-
payment_id: &str,
99-
merchant_id: &str,
100-
refund_id: &str,
101-
storage_scheme: enums::MerchantStorageScheme,
102-
) -> RouterResult<Option<storage::Refund>> {
103-
let refund = db
104-
.find_refund_by_merchant_id_refund_id(merchant_id, refund_id, storage_scheme)
105-
.await;
106-
logger::debug!(?refund);
107-
match refund {
108-
Err(err) => {
109-
if err.current_context().is_db_not_found() {
110-
// Empty vec should be returned by query in case of no results, this check exists just
111-
// to be on the safer side. Fixed this, now vector is not returned but should check the flow in detail later.
112-
Ok(None)
113-
} else {
114-
Err(err
115-
.change_context(errors::ApiErrorResponse::InternalServerError)
116-
.attach_printable("Failed while finding refund, database error"))
117-
}
118-
}
119-
120-
Ok(refund) => {
121-
if refund.payment_id == payment_id {
122-
Ok(Some(refund))
123-
} else {
124-
Ok(None)
125-
}
126-
}
127-
}
128-
}
129-
13093
pub fn validate_refund_list(limit: Option<i64>) -> CustomResult<i64, errors::ApiErrorResponse> {
13194
match limit {
13295
Some(limit_val) => {

0 commit comments

Comments
 (0)