Skip to content

Commit

Permalink
Update ContributionCancelActions to also handle 'failed'
Browse files Browse the repository at this point in the history
The failed action has no discernable difference from the no-longer-used cancel action. The cancel action had some minor refactoring
done to work through the conclusion that the call to addRecurLineItems was never reached in a meaningful way
but I think we can skip all that & see that fail is not functionally different to cancel & just deprecate the function.

At this point we are very close to simply removing the last core handling for failed & cance & making the extension visible.

The only thing I can see that still needs checking in the 2 functions still to be removed is the handling of
is_override & override_date is tested - which I will do as a follow up
  • Loading branch information
eileenmcnaughton committed Nov 22, 2020
1 parent f72d958 commit ee11162
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CRM/Core/Payment/BaseIPN.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,13 @@ public function loadObjects($input, &$ids, &$objects, $required, $paymentProcess
*
* @param array $objects
*
* @deprecated use the api.
*
* @return bool
* @throws \CiviCRM_API3_Exception|\CRM_Core_Exception
*/
public function failed($objects) {
CRM_Core_Error::deprecatedFunctionWarning('use the api');
$contribution = &$objects['contribution'];
$memberships = [];
if (!empty($objects['membership'])) {
Expand Down
10 changes: 8 additions & 2 deletions CRM/Core/Payment/PayPalIPN.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,10 @@ public function single($input, $ids, $contribution, $recur = FALSE) {
/**
* Main function.
*
* @throws \CRM_Core_Exception
* @return false|void
* @throws \API_Exception
* @throws \CiviCRM_API3_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
public function main() {
try {
Expand Down Expand Up @@ -379,7 +381,11 @@ public function main() {

$status = $input['paymentStatus'];
if ($status === 'Denied' || $status === 'Failed' || $status === 'Voided') {
$this->failed($objects);
Contribution::update(FALSE)->setValues([
'cancel_date' => 'now',
'contribution_status_id:name' => 'Cancelled',
])->addWhere('id', '=', $contributionID)->execute();
Civi::log()->debug("Setting contribution status to Failed");
return;
}
if ($status === 'Pending') {
Expand Down
7 changes: 6 additions & 1 deletion CRM/Core/Payment/PayPalProIPN.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ public function recur($input, $ids, $objects, $first) {
* @param bool $first
*
* @return void
* @throws \API_Exception
*/
public function single($input, $ids, $objects, $recur = FALSE, $first = FALSE) {
$contribution = &$objects['contribution'];
Expand Down Expand Up @@ -322,7 +323,11 @@ public function single($input, $ids, $objects, $recur = FALSE, $first = FALSE) {

$status = $input['paymentStatus'];
if ($status === 'Denied' || $status === 'Failed' || $status === 'Voided') {
$this->failed($objects);
Contribution::update(FALSE)->setValues([
'cancel_date' => 'now',
'contribution_status_id:name' => 'Cancelled',
])->addWhere('id', '=', $contribution->id)->execute();
Civi::log()->debug("Setting contribution status to Failed");
return;
}
if ($status === 'Pending') {
Expand Down
13 changes: 10 additions & 3 deletions ext/contributioncancelactions/contributioncancelactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@
*
* This enacts the following
* - find and cancel any related pending memberships
* - (not yet implemented) find and cancel any related pending participant records
* - (not yet implemented) find any related pledge payment records. Remove the contribution id.
* - (not yet implemented) find and cancel any related pending participant
* records
* - (not yet implemented) find any related pledge payment records. Remove the
* contribution id.
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_post
*
* @throws \CiviCRM_API3_Exception
* @throws \API_Exception
*/
function contributioncancelactions_civicrm_post($op, $objectName, $objectId, $objectRef) {
if ($op === 'edit' && $objectName === 'Contribution') {
if ('Cancelled' === CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $objectRef->contribution_status_id)) {
if (in_array(CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $objectRef->contribution_status_id),
['Cancelled', 'Failed']
)) {
contributioncancelactions_cancel_related_pending_memberships((int) $objectId);
contributioncancelactions_cancel_related_pending_participant_records((int) $objectId);
contributioncancelactions_update_related_pledge((int) $objectId, (int) $objectRef->contribution_status_id);
Expand Down
20 changes: 16 additions & 4 deletions tests/phpunit/CRM/Core/Payment/BaseIPNTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,16 +435,27 @@ public function testThatCancellingEventPaymentWillCancelAllAdditionalPendingPart
$this->assertEquals(2, $cancelledActivatesCount['count']);
}

public function testThatFailedEventPaymentWillCancelAllAdditionalPendingParticipantsAndCreateCancellationActivities() {
/**
* Test that related pending participant records are cancelled.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
public function testThatFailedEventPaymentWillCancelAllAdditionalPendingParticipantsAndCreateCancellationActivities(): void {
$this->_setUpParticipantObjects('Pending from incomplete transaction');
$this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId);
$additionalParticipantId = $this->participantCreate([
'event_id' => $this->_eventId,
'registered_by_id' => $this->_participantId,
'status_id' => 'Pending from incomplete transaction',
]);

$this->IPN->failed($this->objects);
Contribution::update(FALSE)->setValues([
'cancel_date' => 'now',
'contribution_status_id:name' => 'Cancelled',
])->addWhere('id', '=', $this->ids['contribution'])->execute();
Civi::log()->debug("Setting contribution status to Cancelled");

$cancelledParticipantsCount = civicrm_api3('Participant', 'get', [
'sequential' => 1,
Expand Down Expand Up @@ -586,7 +597,7 @@ public function _setUpRecurringContribution() {
*
* @throws \CRM_Core_Exception
*/
public function _setUpParticipantObjects($participantStatus = 'Attended') {
public function _setUpParticipantObjects($participantStatus = 'Attended'): void {
$event = $this->eventCreate(['is_email_confirm' => 1]);

$this->_eventId = $event['id'];
Expand All @@ -605,6 +616,7 @@ public function _setUpParticipantObjects($participantStatus = 'Attended') {
$contribution->id = $this->_contributionId;
$contribution->find();
$this->objects['contribution'] = $contribution;
$this->ids['contribution'] = $contribution->id;
$this->input = [
'component' => 'event',
'total_amount' => 150.00,
Expand Down

0 comments on commit ee11162

Please sign in to comment.