Our Gemara engages in a complex legal discussion involving situations where a person provides an unsolicited service of value to another. Say, for example, you enter into another person’s field and plow and plant it. While he did not ask for this, the owner will receive some benefit for this, and therefore the halacha is that he must pay for the service nonetheless. The amount is subject to adjustment though, and depends on different circumstances (See Shulkhan Arukh CM:375), but payment must be made. Yet, notably and probably contrary to secular law, he does have to pay something even if he didn’t ask for or denies wanting the service. In halakhic logic, receiving the benefit incurs the liability, even if not desired.
Later on (107b) we learn a Mishna:
מִי שֶׁהָלַךְ לִמְדִינַת הַיָּם וְעָמַד אֶחָד וּפִירְנֵס אֶת אִשְׁתּוֹ חָנָן אוֹמֵר אִיבֵּד אֶת מְעוֹתָיו
In the case of a husband who went overseas, and someone arose and sustained his wife in his absence, and upon the husband’s return the provider demands from him the money he spent on his wife, Chanan says: He has lost his money, i.e., the husband is not obligated to repay him, as the provider acted of his own free will and was not instructed to do so by the husband.
Tosafos over there asks how can Chanan hold that the person lost his money, why is this not comparable to the case of the person who offered unsolicited, but still provided work of value? If in that case he is entitled to some compensation, why not here? Tosafos answers with the following distinction:
In the case of the worker, there was a concrete profit and benefit that came out of the work, i.e., the crops grew. Here, though this man is taking care of this husband’s obligation to support his wife, it is not producing a concrete benefit but merely avoiding a deficit.
The Chida (Rosh Dovid, Ki Tisa) explains this halakha metaphorically as applying to tzedaka as well. Since Tzedaka really is God’s job, so to speak, according to Tosafos and Chanan it might be considered as if one was forfeiting his money, like the Good Samaritan who provided financial assistance to someone’s wife who was overseas. Chida says, that might be true regarding tzedakah to someone who is not a Talmid Chacham, but since the Talmid Chacham generates “profits”, i.e. his chiddushei Torah, it falls under the rubric of unsolicited work that Tosafos says indebts the owner. So too, this kind of tzedaka, so to speak, indebts God to pay the donor back. This is why Koheles Rabbah (11:1) instructs you to hedge your tzedaka investments by supporting Talmidei Chachamim amongst your donating.
Chida here holds that by regular tzedaka there is not a guarantee for repayment, which seems to contradict the teaching, that one can test Hashem to see if they become wealthy, as the verse in Malachi (3:10 states, also see Ta’anis 9a):
הָבִ֨יאוּ אֶת־כׇּל־הַֽמַּעֲשֵׂ֜ר אֶל־בֵּ֣ית הָאוֹצָ֗ר וִיהִ֥י טֶ֙רֶף֙ בְּבֵיתִ֔י וּבְחָנ֤וּנִי נָא֙ בָּזֹ֔את אָמַ֖ר ה׳ צבקות אִם־לֹ֧א אֶפְתַּ֣ח לָכֶ֗ם אֵ֚ת אֲרֻבּ֣וֹת הַשָּׁמַ֔יִם וַהֲרִיקֹתִ֥י לָכֶ֛ם בְּרָכָ֖ה עַד־בְּלִי־דָֽי׃
Bring the full tithe into the storehouse, and let there be food in My House, and thus put Me to the test—said the LORD of Hosts. I will surely open the floodgates of the sky for you and pour down blessings on you.
As well as the derash (Ta’anis 9a):
״עַשֵּׂר תְּעַשֵּׂר״ — עַשֵּׂר בִּשְׁבִיל שֶׁתִּתְעַשֵּׁר.
“A tithe shall you tithe [te’aser]” (Deuteronomy 14:22) This phrase can be interpreted homiletically: Take a tithe [asser] so that you will become wealthy [titasher], in the merit of the mitzva.
However, this is not so cut and dried. In fact, it is a discussion in Shulkhan Arukh (YD 247:4). This ability to test God’s promise might only apply to tithes, and even in that, since monetary tithing is not Deoraysa (only produce), it also may not apply. Therefore the Chida’s point is well taken.