@@ -541,106 +541,85 @@ pub async fn validate_and_create_refund(
541
541
. attach_printable ( "invalid merchant_id in request" ) )
542
542
} ) ?;
543
543
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 ,
557
554
)
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 ) ?;
573
557
574
- currency = payment_attempt. currency . get_required_value ( "currency" ) ?;
558
+ currency = payment_attempt. currency . get_required_value ( "currency" ) ?;
575
559
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" ,
579
566
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
+ } ) ?;
588
569
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 ) ?;
591
572
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 ) ?;
597
578
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 ( ) ;
630
606
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 ?;
644
623
645
624
Ok ( refund. foreign_into ( ) )
646
625
}
0 commit comments