If a person intended to do a mitzvah and was prevented and did not do it, Scripture considers it as if he had done it.

חשב אדם לעשות מצוה ונאנס ולא עשאה מעלה עליו הכתוב כאילו עשאה
Shiurim in this topic
7456. The Value of Intent in Mitzvah Performance and the Merit of Tzedakah ערך הכוונה בקיום מצוה וזכות הצדקה