The Torah tells us that when Yaakov Avinu awoke following the dream in which he was spoken to by Hashem, who promised to be with him and protect him, he took a vow, saying that if Hashem will indeed be with him, protect him, provide him with food and clothing, and enable him to return home in peace, he will dedicate one tenth of his possessions (Ma'aser) to Hashem (בראשית כ"ח:כ'-כ"ב). The simple explanation of Yaakov's words, as presented by Rashi (לפסוק כ"ב שם בד"ה והאבן-הראשון), is that Yaakov pledged to do a Mitzvah, specifically, to set aside a tenth of all that he has for Hashem, "in exchange" for Hashem granting him what he needed at that time. Later in the Torah, after things appear to have indeed worked out as Yaakov had wanted, we find that he keeps his word, as noted by Rashi (שם ל"ב:י"ד בד"ה הבא), and sets aside a tithe of his belongings to Hashem; Ibn Ezra elsewhere (שם ל"ה:י"ד בד"ה ויסך) similarly records the fulfillment of Yaakov's vow. The Midrash in Bereishis Rabbah (פרשה ע' סימן א') states that Yaakov here was setting an example for future generations by showing that it is proper for one to take a vow to do a Mitzvah at a time of danger, and thereby merit Hashem's assistance. Although it seems form the Gemara in Nedarim (דף י:) that one should avoid taking such vows, at least in certain cases, it is clear form Tosafos in Chulin (דף ב: בסוף ד"ה אבל), referring to the Posuk in our Parsha (שם כ"ח:כ') that it is permissible, and indeed proper, to take such vows during a time of danger.
The implication of all this is that one is permitted to ask Hashem for some kind of reward or "payment" for a Mitzvah which one plans to do. The difficulty is that according to the Mishnah in Pirkei Avos (פרק א' משנה ג'), one is not supposed to serve Hashem in anticipation or expectation of receiving a reward; Rashi (שם בד"ה פרס) explains that this means that one should not say that he will fulfill a Mitzvah so that Hashem will provide him with what he wants, but one should rather serve Hashem out of love alone, and the Rambam (פרק י' מהל' תשובה הלכה א',ד') rules accordingly. Moreover, the Torah later (דברים ו':ט"ז) states explicitly that one is not permitted to "test" Hashem, the Semag (מצות לא תעשה ד') explains, as does the Semak (סימן י"ח), that this means that one may not say that he will perform a Mitzvah in order to receive a reward, and the Ramban, in his commentary on that Posuk (שם), concurs, adding that one may not say that he will observe the Torah only if all is well.
It is worth noting that the Rambam, both in his Sefer HaMitzvos (מצות לא תעשה ס"ד) and in his Mishneh Torah (פרק י' מהל' יסודי התורה הלכה ה'), understands this prohibition to be addressing an entirely different issue, and he seems to make no mention of a prohibition to test Hashem in terms of a reward for fulfilling a Mitzvah; this also seems to be the position of Rabbeinu Saadyah Gaon in his Sefer HaMitzvos (מצות לא תעשה קכ"ז, ועיין בביאורו של הגרי"פ פערלא שם). Nevertheless, the Sefer HaChinuch (מצוה תכ"ד), while presenting the prohibition as understood by the Rambam (שם ושם), incorporates into it as well the notion that one may not do a Mitzvah and look to see if Hashem will reward him; it is also the understanding of the Sefer Yereyim (סימן ז'), and of Rabbeinu Yonah in his Shaarei Teshuvah ( ל'שער ג' סימן), and of others that one may not say that he will do a Mitzvah and "test" Hashem to make sure he will be rewarded. One may ask, therefore, how Yaakov Avinu was able to commit himself to dedicate one tenth of his possessions to Hashem only on the condition, apparently, that Hashem would reward him with the assistance he desired; more practically, one may ask whether one today may commit himself to do a Mitzvah only, or even primarily, for the purpose of receiving a reward.
The Gemara in Taanis (דף ט.) discusses this prohibition to test Hashem, with the clear understanding that it precludes one from doing a Mitzvah with the expectation of a reward, which is the position codified by the Tur (יורה דעה סימן רמ"ז), and concludes (שם) that the Mitzvah of separating Ma'aser, that is, taking the appropriate tithes from the produce of one's fields, is the one exception to the rule. Based on a Posuk in Malachi (ג':י') which states that Hashem himself asks people to give the appropriate Ma'aser gifts and to thereby test Him to see if He will indeed reward them for this, the Gemara (שם) indicates that one may indeed test Hashem and fulfill the Mitzvah of Ma'aser in anticipation of a great reward. The Gemara (שם) in fact derives from a Posuk elsewhere in the Torah (דברים י"ב:כ"ב) that one who properly fulfills the Mitzvah of Ma'aser will be rewarded by becoming rich, an idea found as well in the Midrash Tanchuma there in Parshas Re'eih (אות י"ח).
This idea that one can become wealthy by fulfilling a certain Mitzvah is extended to the Mitzvah of Tzedakah by the Midrash (Shochar Tov) on Mishlei (פרשה י"א פסוק כ"ד) which states that one who distributes his money to Tzedakah will increase his own wealth thereby. The Gemara in Gittin (דף ז.-ז:) states similarly that one can avoid poverty by giving Tzedakah, and that one who gives Tzedakah will in general be saved from terrible suffering; the Midrash in VaYikra Rabbah (פרשה ל"ד סימן י') likewise states that one who gives Tzedakah to a poor person will be rewarded by Hashem, while one who fails to do so will be punished. The Gemara in Kesubos (דף ס"ו:-ס"ז.) records how a certain very wealthy man lost is entire fortune because he failed to fulfill the Mitzvah of Tzedakah properly; although he did give Tzedakah, he did so, according to one view, solely for his own honor. The Maharsha (חדושי אגדות לדף ס"ז. שם בד"ה איבעית) notes that to do a Mitzvah for personal honor is a most terrible course of behavior; Rav Elchanan Wasserman (קובץ שעורים למס' כתובות אות רכ"ד) explains that the problem is when one does the Mitzvah solely for his own honor and not at all for the sake of the Mitzvah. It is thus clear form the above that when it comes to the Mitzvah of Tzedakah, one can fulfill the Mitzvah and indeed anticipate a great reward, although, apparently, the reward should not be one's sole motivation for fulfilling that Mitzvah.
The Gemara in Pesachim (דף ח.-ח:) states that one who pledges money to Tzedakah in order to receive a reward, such as that his child should live or that he should merit entering Olam HaBo, is considered a complete Tzaddik. Rashi (בעמוד ב' שם בד"ה הרי) explains that such a person is not to be viewed as behaving improperly, but rather that he is fulfilling a Mitzvah of Hashem with the intent of also receiving personal benefit; it is noteworthy that Rabbeinu Chananel, in his commentary on the Gemara in Rosh HaShanah (דף ד.) where this statement also appears, has a slightly different version of the text which does not imply that this individual is a complete Tzaddik (צדיק גמור), but that the Mitzvah of Tzedakah is properly fulfilled (צדקה גמורה). In any case, it is clear that one may pledge to give Tzedakah and openly request a reward from Hashem.
Tosafos, both in Pesachim (שם בד"ה שיזכה) and in Rosh HaShanah (שם בד"ה בשביל), questions how this can be permissible in light of the aforementioned Mishnah in Pirkei Avos (שם) which seems to prohibit fulfilling a Mitzvah for the sake of receiving a reward. Tosafos in Pesachim (שם) responds that the prohibition is only upon a person who, if he does not receive his desired reward, will regret the Mitzvah which he performed, but one who will not react that way may indeed request a reward and remain a complete Tzaddik; Tosafos in Rosh HaShanah (שם) offers a different response, but according to the Tosafos Yom Tov, in his commentary on that Mishnah in Pirkei Avos (שם בד"ה אל), the ultimate intent appears to be the same there too. This idea is also hinted at by Rashi in Rosh HaShanah (שם בד"ה כאן), and in Bava Basra (דף י: בד"ה כאן), where this issue is also discussed, and is presented as well by Tosafos in Avodah Zarah (דף י"ט. בד"ה על). The Beis HaLeivi, in his commentary on the Torah (לפ' תרומה בד"ה והנה), adds that one who truly believes in Hashem regardless of any reward he may receive may request a reward because that too is an appropriate intent behind fulfilling a Mitzvah, precisely because it is the desire of Hashem to reward one whose faith in Him is unwavering regardless of what happens in his life. It should be noted that it appears form the Ramo (יו"ד סימן ר"כ סעיף ט"ו) that one violates the prohibition against testing Hashem only if he specifies that he will not fulfill the Mitzvah if he is not rewarded; the Vilna Gaon (ביאור הגר"א שם ס"ק כ') applies this idea to answer our question from the Mishnah in Pirkei Avos (שם). It is clear form the above sources, then, that one may request a reward for performing the Mitzvah of Tzedakah.
The Tur (שם סימן רמ"ז) writes that when one has mercy on a poor person, Hashem has mercy upon him, as alluded to by the Gemara in Shabbos (דף קנ"א:), and the Shulchan Aruch (שם סעיף ג') rules accordingly; the Tur (שם) then notes that while it is in general forbidden to test Hashem, as explained above, one is permitted to do so in connection with the Mitzvah of Tzedakah for which a reward may be requested. The Beis Yosef, however (שם בד"ה ומ"ש ואמרו), suggests that this permission to test Hashem applies only to the Mitzvah of Ma'aser, the giving of agricultural tithes, which is the case discussed in the aforementioned Gemara in Taanis (שם), and not to all forms of Tzedakah; the Derishah (שם אות א') discusses this point as well, but he explains why other forms of Tzedakah are included. Rabbeinu Yehudah HaChassid, in his Sefer Chassidim (סימן קמ"ד), also understands that the Ma'aser referred to is the one tenth of one's possessions which are given to the poor, and this is the ruling as well of the Ramo (יו"ד שם סעיף ד'), who says that one may request a reward for all forms of Tzedakah, although he does quote the other view. The Pischei Teshuvah (שם ס"ק ב') quotes Rav Yaakov Emden (שו"ת שאילת יעב"ץ חלק א' סוף סימן ג') as well as the Shaloh (מס' מגילה, ענין צדקה ומעשר בד"ה יש מפזר), who is also cited there in the Shulchan Aruch in the Gilyon Maharsha (שם בד"ה ואסור), as holding that the permission to test Hashem applies only to the Mitzvah of taking Ma'aser from one's produce, and not to other forms of Tzedakah; the Chofetz Chaim, in his Sefer Ahavas Chessed (חלק ב', ענין מעשר כספים פרק י"ח בהג"ה בד"ה וכן), argues in favor of the view that other forms of Tzedakah are included in the permission to test Hashem, and he brings several proofs.
It would appear that the practice today is to accept the opinion that one may indeed request a reward for fulfilling the Mitzvah of Tzedakah; many people give Tzedakah "in exchange" for a Beracha for the family or a Refuah Sheleimah for a sick friend or relative when a Mi SheBeirach is recited in Shul. In this connection, it has been reported in the name of HaRav Yosef Dov Soloveitchik that the phrase "בשכר זה," "as a reward for this (Mitzvah)," which appears in the standard text of the Mi SheBeirach, ought to be recited only if the person is indeed pledging to give Tzedakah, and not if he simply had an Aliyah and requests a Mi SheBeirach, because only for the Mitzvah of Tzedakah may one ask for a reward. In conclusion, it should be noted that according to the Gemara later in Taanis (דף י:), as explained by Rashi (שם בד"ה על החולה) and the Rosh (שם פרק א' סימן ו'), if one pledged to fast for the sake of a Refuah Sheleimah for one who is ill, and the patient then dies, he must still honor that pledge. The Ramo (יו"ד סימן
ר"כ סעיף ט"ו) rules accordingly, and extends this as well to one who pledged to give Tzedakah; one must thus always give the Tzedakah even if he did not receive the desired reward, unless he specifically stated that his pledge was conditional, which one should not do. Among others, the Netziv (שו"ת משיב דבר סימן מ"ה) affirms this ruling in a Teshuvah which discusses several issues relevant to this topic.