@@ -521,13 +521,14 @@ pub async fn validate_and_create_refund(
521
521
creds_identifier : Option < String > ,
522
522
) -> RouterResult < refunds:: RefundResponse > {
523
523
let db = & * state. store ;
524
+ let ( refund_id, all_refunds, currency, refund_create_req, refund) ;
524
525
525
526
// Only for initial dev and testing
526
527
let refund_type = req. refund_type . unwrap_or_default ( ) ;
527
528
528
529
// If Refund Id not passed in request Generate one.
529
530
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" ) ?;
531
532
532
533
let predicate = req
533
534
. merchant_id
@@ -547,7 +548,7 @@ pub async fn validate_and_create_refund(
547
548
. attach_printable ( "Transaction in invalid. Missing field \" connector_transaction_id\" in payment_attempt." )
548
549
} ) ?;
549
550
550
- let all_refunds = db
551
+ all_refunds = db
551
552
. find_refund_by_merchant_id_connector_transaction_id (
552
553
& merchant_account. merchant_id ,
553
554
& connecter_transaction_id,
@@ -556,7 +557,7 @@ pub async fn validate_and_create_refund(
556
557
. await
557
558
. to_not_found_response ( errors:: ApiErrorResponse :: RefundNotFound ) ?;
558
559
559
- let currency = payment_attempt. currency . get_required_value ( "currency" ) ?;
560
+ currency = payment_attempt. currency . get_required_value ( "currency" ) ?;
560
561
561
562
//[#249]: Add Connector Based Validation here.
562
563
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(
584
585
. into_report ( )
585
586
. attach_printable ( "No connector populated in payment attempt" ) ?;
586
587
587
- let refund_create_req = storage:: RefundNew :: default ( )
588
+ refund_create_req = storage:: RefundNew :: default ( )
588
589
. set_refund_id ( refund_id. to_string ( ) )
589
590
. 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) )
591
592
. set_payment_id ( req. payment_id )
592
593
. set_merchant_id ( merchant_account. merchant_id . clone ( ) )
593
594
. set_connector_transaction_id ( connecter_transaction_id. to_string ( ) )
@@ -606,39 +607,22 @@ pub async fn validate_and_create_refund(
606
607
. set_profile_id ( payment_intent. profile_id . clone ( ) )
607
608
. to_owned ( ) ;
608
609
609
- let refund = match db
610
+ refund = db
610
611
. insert_refund ( refund_create_req, merchant_account. storage_scheme )
611
612
. 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 ?;
642
626
643
627
Ok ( refund. foreign_into ( ) )
644
628
}
0 commit comments