-
-
Notifications
You must be signed in to change notification settings - Fork 825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix bug saving a new recurring contribution to the database #29005
Conversation
I experienced a bug in CiviCRM 5.69.2 with the Stripe plugin from mjw. Adding a new recurring contribution (for an annual membership), CiviCRM reports the error: 2024-01-12 04:49:44-0500 [error] CRM_Contribute_Form_Contribution_Confirm::completeTransaction CRM_Core_Exception: contribution_status_id is not a valid integer and the error message "Failed to update contribution in database". I tracked this down to the code patched here in contributionRecur, which attempts to store "In Progress" as the contribution_status_id, which is indeed not a valid integer. It looks like everywhere else in the code, CiviCRM is going to do a PseudoConstant lookup to store the relevant ID. I've modified the code here to match the convention elsewhere. The patch appears to work on my own install of CiviCRM. I don't have a CiviCRM account yet. I was planning to report it on the bug tracker, but my account hasn't been approved yet. Anyway, I can report it there once I have an account. Or not, if you just merge this patch. Thanks for all the work maintaining this software!
🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷 Introduction for new contributors...
Quick links for reviewers...
|
Can one of the admins verify this patch? |
1 similar comment
Can one of the admins verify this patch? |
@civicrm-builder add to whitelist |
Thank you for this patch @patricklam. For background, I think what's probably going on here is that some payment processors are switching from Civi's APIv3 over to APIv4. The two APIs have a crucial difference wrt these option value lookups: if you pass a string to APIv3 it will helpfully assume you meant to pass an id and convert it for you. APIv4 is also capable of doing those lookups but in order to reduce ambiguity it doesn't make assumptions - you have to explicitly tell it what to lookup by passing a key like Anyway, I think this is a good fix because it will work for both versions of the API. Since this appears to be a regression I think this PR should be agains the |
@@ -940,15 +940,15 @@ public static function updateOnNewPayment($recurringContributionID, $paymentStat | |||
|
|||
if ($paymentStatus == 'Completed' | |||
&& CRM_Contribute_PseudoConstant::contributionStatus($existing['contribution_status_id'], 'name') == 'Pending') { | |||
$params['contribution_status_id'] = 'In Progress'; | |||
$params['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'In Progress'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say the tests are failing because contributionrecur has different statuses than contribution. But also, the fact that the tests were passing before means the api3 feature to use the name is still working. Is it possible you've disabled or are somehow missing the contribution_recur statuses in your db? If you go to admin - system settings - option groups do you have one for contribution_recur_status
- does it have choices?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@demeritcowboy huh, my db was missing the contribution_recur_status options. (I ran into another problem where it wouldn't show me any types for adding option values; maybe there is another missing option value, but added it directly to the db). I've added contribution_recur_status now.
@colemanw I tried to rebase my PR but didn't quite seem to manage to do it. I'd thought that I had done it, but apparently not. I have created a rebased commit on 5.70 at 61cec1b but I don't know how to update this PR to point to that commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it sounds like there is no code bug and this PR is about futureproofing now, it could just be against master.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, maybe my assumption was incorrect and there's no bug here at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, the stripe plugin is using v3 almost everywhere that I saw. It was definitely throwing a type error trying to put "In Progress" as the contribution_status_id, but my db was also missing that option value. I stopped looking after this, but I don't know where "In Progress" would get looked up as a contribution_recur_status option value. I guess it does somewhere.
I do see that the BAO Contribution code does explicitly go through CRM_Core_PseudoConstant though; looks like ContributionRecur is older code that didn't also get updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It happens at
Lines 2378 to 2382 in 3cab30b
$newValue = array_search((string) $value, $options); | |
if ($newValue !== FALSE) { | |
$value = $newValue; | |
return; | |
} |
Here's two options:
- Leave it as-is since the code is ok and the error seems related to missing config.
- Use getKey but using the right BAO (ContributionRecur), but it's unnecessary because of the above - api3 will convert by itself.
By the way using $params['contribution_status_id:name'] = "In Progress"
doesn't seem to work with api3 in a quick test. It gets ignored and returns all the records.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see the lookup code, thanks.
I'll leave that up to the maintainers; it's not my codebase. The patch below does use the ContributionRecur BAO and it does work for me.
In the interest of getting you your first PR credit I think we could merge it if it passes tests. Can you do a separate PR to add yourself to this file https://github.com/civicrm/civicrm-core/blob/master/contributor-key.yml for release notes credit? |
Normally we ask to "squash" commits that aren't logically separate, but we've spent enough on this. FYI for future: https://docs.civicrm.org/dev/en/latest/tools/git/#rebasing-interactive |
Got it. Thanks!
…On Tue, Jan 16, 2024, 3:02 PM demeritcowboy ***@***.***> wrote:
Normally we ask to "squash" commits that aren't logically separate, but
we've spent enough on this. FYI for future:
https://docs.civicrm.org/dev/en/latest/tools/git/#rebasing-interactive
—
Reply to this email directly, view it on GitHub
<#29005 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAOKE5TWDAZXZGSZTZSMZI3YOXNT3AVCNFSM6AAAAABBZZ22AWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOJSHE3DGMBVHA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
I experienced a bug in CiviCRM 5.69.2 with the Stripe plugin from mjw. Adding a new recurring contribution (for an annual membership), CiviCRM reports the error:
2024-01-12 04:49:44-0500 [error] CRM_Contribute_Form_Contribution_Confirm::completeTransaction CRM_Core_Exception: contribution_status_id is not a valid integer
and the error message "Failed to update contribution in database".
I tracked this down to the code patched here in contributionRecur, which attempts to store "In Progress" as the contribution_status_id, which is indeed not a valid integer. It looks like everywhere else in the code, CiviCRM is going to do a PseudoConstant lookup to store the relevant ID. I've modified the code here to match the convention elsewhere.
The patch appears to work on my own install of CiviCRM.
I don't have a CiviCRM account yet. I was planning to report it on the bug tracker, but my account hasn't been approved yet. Anyway, I can report it there once I have an account. Or not, if you just merge this patch. Thanks for all the work maintaining this software!
Overview
It looks like stale code dealing with recurring contributions is trying to put invalid values into the database, and getting rejected.
Before
Adding a new recurring contribution (for an annual membership), CiviCRM reports the error:
2024-01-12 04:49:44-0500 [error] CRM_Contribute_Form_Contribution_Confirm::completeTransaction CRM_Core_Exception: contribution_status_id is not a valid integer
and the error message "Failed to update contribution in database".
After
Adding a new recurring contribution appears to work.