Note field in contribution profile exposes unrelated note to donor
Overview
If a donor makes a recurring contribution and the profile defined for the contribution page contains a note field, then the receipt for each subsequent payment sent to the donor may display a note from the donor's profile unrelated to any note recorded when the original contribution was made.
Similarly, if a donor makes a recurring contribution and specifies an honoree, and the honoree profile contains a note field, then the receipt for each subsequent payment sent to the donor may display a note from the honoree's profile unrelated to that contribution.
The same problem occurs on a non-recurring contribution, and on the original contribution of a recurring contribution, if either the main profile or the honoree profile contains a note field and that field is left blank. Even if the note field is not left blank, it is possible for the wrong note to be displayed as the displayed note is simply the last note returned from a query of notes associated with the contact, with no relevant sorting criteria.
The particular note displayed is thus indeterminate in all of these cases, but in my testing it has been the last note recorded for the contact (donor or honoree, respectively).
Reproduction steps
Create the Contacts
- Create a contact who will be the donor. Add a note: "Existing donor note".
- Create a contact who will be the honoree. Add a note: "Existing honoree note".
Create the Contribution Page
- Create a contribution page, check the "Honoree Section Enabled" option.
- Edit the Honoree Profile and add an Individual Note field.
- On the Profiles tab, select a profile which contains a note field.
- On the Receipts tab, check the "Enable Email Receipt to Contributor" option, and complete the required fields in that section.
- Save the contribution form.
Make the Contribution
- Go to the online contribution page (test mode).
- Specify the above created donor as the donor.
- Specifying the above created honoree as the honoree.
- Leave blank the note fields in the main profile and the honoree profile.
Current behaviour
The email received by the donor indicates:
"Note(s): Existing donor note" and
"Note(s): Existing honoree note"
Expected behaviour
The email received by the donor should indicate:
"Note(s):
"Note(s):
i.e. two blank note fields
Background Information
This is similar to event#10 (moved) which affected the note displayed in events confirmation emails. The fix there was simple -- remove the note field from the email if the submitted note was blank. I suspect that solution, in the case of a non-blank note field submission, unfortunately relies on the same indeterminate query results which generally returns, but is not guaranteed to return, the most recent note for the contact..
In the case of contribution receipts, the problem is compounded by recurring contributions. In this case, even if the indeterminate query happens to return the most recent note, that is not necessarily the note related to the contribution which was made some time in the past.
The best solution, I believe, would involve adding a field to the notes table which ties each note to a particular contribution or event registration, or adding a table analogous to civicrm_uf_join to provide that kind of linkage.
A "poor man's" solution is to simply prevent notes fields from being displayed in contribution receipts. This could be done by adding unset($fields['note']);
and unset($profileFields['note']);
, respectively, prior to the calls to getValues in the two Relevant Code functions shown below.
Relevant Code
-
Values from the main profile are retrieved with a call to CRM_Core_BAO_UFGroup::getValues in CRM/Contribute/BAO/ContributionPage.php::getProfileNameAndFields
-
Values from the honoree profile are retrieved with a call to CRM_Core_BAO_UFGroup::getValues in CRM/Contribute/BAO/ContributionSoft.php::formatHonoreeProfileFields
Environment information
- CiviCRM: 5.20.0
- PHP: 7.2.25
- CMS: Drupal 7.30
- Database: MariaDB 10.0
- Payment Processor: Paypal Standard