Skip to content
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

dev/financial#6 Use template contribution for Contribution.repeattransaction #22487

Conversation

mattwire
Copy link
Contributor

Overview

Calling contribution.repeattransaction without specifying original_contribution_id causes it to try to get the "previous" contribution in Completed status. That doesn't work if the previous contribution is in any other status (eg. Refunded, Failed).
We should be using the generic function to get the template contribution as this allows for us to get the values from the template contribution if available or the latest contribution otherwise.

Before

Contribution.repeattransaction crashes if first contribution is not "Completed". Template contribution is ignored.

After

Template contribution is retrieved and used.

Technical Details

See https://lab.civicrm.org/dev/financial/-/issues/6

Comments

@civibot
Copy link

civibot bot commented Jan 12, 2022

(Standard links)

@civibot civibot bot added the master label Jan 12, 2022
@mattwire mattwire force-pushed the repeattransactiontemplatecontribution branch from 47ea3c5 to cc63dda Compare January 12, 2022 15:10
@mattwire
Copy link
Contributor Author

test this please

]);
$templateContribution = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($params['contribution_recur_id']);
if (empty($templateContribution)) {
throw new CiviCRM_API3_Exception('Contribution.repeattransaction failed to get original_contribution_id for recur with ID: ' . $params['contribution_recur_id']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you change to API_Exception to make it APIv4 friendly too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think within apiv3 this is correct.

API_Exception is a bit of a pain in a way - ideally we could catch 'all CiviCRM extensions' with one catch type but API_Exception doesn't extend any other CiviCRM exception types - I'm guessing that was before it was handed over to CT @colemanw ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@monishdeb This code is within an API3 function so it is unlikely to ever be called by API4? There is quite a bit within the function which would probably be cleaned up before creating a "repeattransaction" for API4 - see eg. #22488 for a start.

$this->assertEquals(7, $contributions['values'][0]['contribution_status_id']);
$this->assertEquals(2, $contributions['values'][1]['contribution_status_id']);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileenmcnaughton @mattwire I am happy with the logic, but I am not sure moving forward, are we relying on APIv4 to do CRUD operations on new unit tests? if it doesn't matter unless the UT captures the fix/change accurately then I am ok here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@monishdeb I think you are saying 'would this be better if we were using apiv4 instead of 3 in the tests.

I guess we are moving that way - but we are not so far down the track I'm alarmed at adding new tests that use apiv3 - in this case, however, I think v4 would be better on the final get because it would make it easy to check contribution_status_id:name rather than the hard-coded '2' & '7'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes contribution_status_id:name is the other reason I felt like APIv4 would be appropriate here.

( I wish there's a emoticon for hi5 )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

five

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileenmcnaughton @monishdeb I agree with preferring to use API4 but I'm not sure I understand fully how we would implement it for the tests? We can set $this->_apiversion = 4; but in this case repeattransaction does not exist for API4 so we would have to change api version during the test which feels a bit messy?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattwire generally I just call the v4 api direct in the tests (unless specifically testing apiv3 or both 3 & 4) - the callApiSuccess helpers are helpful for apiv3 but don't add much to api v4 - so it's really just that last Contribution::get where using apiv4 would make it easy to eliminate the checks on hard-coded status ids (which it the bit that raised the discussion)

@jaapjansma
Copy link
Contributor

@mattwire what is the status of this PR? It is a bit unclear to us whether it needs work or whether it is ready for review. And if it needs to work do you plan to work on this? If not we would advice to close this PR.

@mattwire
Copy link
Contributor Author

@jaapjansma This is quite an important change that affects a few different payment processors (stripe, gocardless etc.). I think that it is only stuck on whether we should start writing tests using api4 or not and only @eileenmcnaughton can answer that - but in my opinion it should not block review/merge of this PR. In my opinion it is ready for review.

@eileenmcnaughton
Copy link
Contributor

I agree using apiv4 is better, but not blockimg

@eileenmcnaughton eileenmcnaughton merged commit f20abb0 into civicrm:master May 23, 2022
@mattwire mattwire deleted the repeattransactiontemplatecontribution branch May 23, 2022 19:34
@mattwire
Copy link
Contributor Author

Thanks @eileenmcnaughton @artfulrobot this should solve one of your workarounds in gocardless (will be in 5.51 I think)

@artfulrobot
Copy link
Contributor

Cheers all. @mattwire it will only solve my problem when GC starts using template contributions! 😆

@mattwire
Copy link
Contributor Author

@artfulrobot Despite the name of this PR it's not actually specific to template contributions and should fix eg. https://lab.civicrm.org/extensions/gocardless/-/blob/main/CRM/Core/Payment/GoCardlessIPN.php#L568

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants