@@ -519,14 +519,13 @@ pub async fn validate_and_create_refund(
519
519
creds_identifier : Option < String > ,
520
520
) -> RouterResult < refunds:: RefundResponse > {
521
521
let db = & * state. store ;
522
- let ( refund_id, all_refunds, currency, refund_create_req, refund) ;
523
522
524
523
// Only for initial dev and testing
525
524
let refund_type = req. refund_type . unwrap_or_default ( ) ;
526
525
527
526
// If Refund Id not passed in request Generate one.
528
527
529
- refund_id = core_utils:: get_or_generate_id ( "refund_id" , & req. refund_id , "ref" ) ?;
528
+ let refund_id = core_utils:: get_or_generate_id ( "refund_id" , & req. refund_id , "ref" ) ?;
530
529
531
530
let predicate = req
532
531
. merchant_id
@@ -546,7 +545,7 @@ pub async fn validate_and_create_refund(
546
545
. attach_printable ( "Transaction in invalid. Missing field \" connector_transaction_id\" in payment_attempt." )
547
546
} ) ?;
548
547
549
- all_refunds = db
548
+ let all_refunds = db
550
549
. find_refund_by_merchant_id_connector_transaction_id (
551
550
& merchant_account. merchant_id ,
552
551
& connecter_transaction_id,
@@ -555,7 +554,7 @@ pub async fn validate_and_create_refund(
555
554
. await
556
555
. to_not_found_response ( errors:: ApiErrorResponse :: RefundNotFound ) ?;
557
556
558
- currency = payment_attempt. currency . get_required_value ( "currency" ) ?;
557
+ let currency = payment_attempt. currency . get_required_value ( "currency" ) ?;
559
558
560
559
//[#249]: Add Connector Based Validation here.
561
560
validator:: validate_payment_order_age ( & payment_intent. created_at , state. conf . refund . max_age )
@@ -583,10 +582,10 @@ pub async fn validate_and_create_refund(
583
582
. into_report ( )
584
583
. attach_printable ( "No connector populated in payment attempt" ) ?;
585
584
586
- refund_create_req = storage:: RefundNew :: default ( )
585
+ let refund_create_req = storage:: RefundNew :: default ( )
587
586
. set_refund_id ( refund_id. to_string ( ) )
588
587
. set_internal_reference_id ( utils:: generate_id ( consts:: ID_LENGTH , "refid" ) )
589
- . set_external_reference_id ( Some ( refund_id) )
588
+ . set_external_reference_id ( Some ( refund_id. clone ( ) ) )
590
589
. set_payment_id ( req. payment_id )
591
590
. set_merchant_id ( merchant_account. merchant_id . clone ( ) )
592
591
. set_connector_transaction_id ( connecter_transaction_id. to_string ( ) )
@@ -605,22 +604,39 @@ pub async fn validate_and_create_refund(
605
604
. set_profile_id ( payment_intent. profile_id . clone ( ) )
606
605
. to_owned ( ) ;
607
606
608
- refund = db
607
+ let refund = match db
609
608
. insert_refund ( refund_create_req, merchant_account. storage_scheme )
610
609
. await
611
- . to_duplicate_response ( errors:: ApiErrorResponse :: DuplicateRefundRequest ) ?;
612
-
613
- schedule_refund_execution (
614
- state,
615
- refund. clone ( ) ,
616
- refund_type,
617
- merchant_account,
618
- key_store,
619
- payment_attempt,
620
- payment_intent,
621
- creds_identifier,
622
- )
623
- . await ?;
610
+ {
611
+ Ok ( refund) => {
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 ?
623
+ }
624
+ Err ( err) => {
625
+ if err. current_context ( ) . is_db_unique_violation ( ) {
626
+ db. find_refund_by_merchant_id_refund_id (
627
+ merchant_account. merchant_id . as_str ( ) ,
628
+ refund_id. as_str ( ) ,
629
+ merchant_account. storage_scheme ,
630
+ )
631
+ . await
632
+ . to_not_found_response ( errors:: ApiErrorResponse :: RefundNotFound ) ?
633
+ } else {
634
+ return Err ( err)
635
+ . change_context ( errors:: ApiErrorResponse :: RefundNotFound )
636
+ . attach_printable ( "Inserting Refund failed" ) ;
637
+ }
638
+ }
639
+ } ;
624
640
625
641
Ok ( refund. foreign_into ( ) )
626
642
}
0 commit comments