Skip to content

Commit

Permalink
Merge pull request #19 from aghstrategies/partiallypaid
Browse files Browse the repository at this point in the history
AGH #18534 handle payments completing pay later contributions
  • Loading branch information
alifrumin authored Oct 29, 2019
2 parents 8494131 + 85f1f87 commit 62722e0
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 40 deletions.
109 changes: 73 additions & 36 deletions eventmembershipsignup.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,78 @@ function eventmembershipsignup_civicrm_post($op, $objectName, $objectId, &$objec
// }
}

/**
* Check if the change in the contribution status should trigger a change to the participant status
* @param int $contributionStatusBeforeId Contribution Status id before this change
* @param int $contributionStatusGoingToId Contribution Status id going to
* @param int $participantStatusIdNow Participant Status Id
* @param int $participantId Participant Id
*/
function eventmembershipsignup_updateparticipantstatus($contributionStatusBeforeId, $contributionStatusGoingToId, $participantStatusIdNow, $participantId) {

// Get status names from ids
$contributionStatusBefore = CRM_Contribute_PseudoConstant::contributionStatus($contributionStatusBeforeId, 'name');
$contributionStatusGoingTo = CRM_Contribute_PseudoConstant::contributionStatus($contributionStatusGoingToId, 'name');
$participantStatus = CRM_Event_PseudoConstant::participantStatus($participantStatusIdNow, NULL, "name");
$participantStatusOptions = CRM_Event_PseudoConstant::participantStatus();

// We will only update participant ids if the contribution status is changing AND the participant status is listed here
if ($contributionStatusBefore != $contributionStatusGoingTo && in_array($participantStatus, [
'Pending from pay later',
'Pending from incomplete transaction',
'Pending in cart',
'Partially paid',
'Registered',
])) {

$updatedStatusId = NULL;

switch ($contributionStatusGoingTo) {
// IF the contribution is being updated to one of these statuses the participant status should be the same as the contribution status
case 'Pending refund':
case 'Partially paid':
case 'Cancelled':
$updatedStatusId = array_search($contributionStatusGoingTo, $participantStatusOptions);
break;

// IF the contribution going to Completed the participant status should be Registered
case 'Completed':
$updatedStatusId = array_search('Registered', $participantStatusOptions);
break;

// IF the contribution status is going to one of these pending statuses the participant status should be set to pending as well.
case 'Pending':
case 'In Progress':
$updatedStatusId = array_search('Pending from pay later', $participantStatusOptions);
break;

// IF the contribution status is going to one of these statuses do not update the participant status at all
case 'Refunded':
case 'Overdue':
case 'Chargeback':
case 'Failed':
break;
}
// If the updated status has been set update the participant record
if ($updatedStatusId) {
CRM_Event_BAO_Participant::updateParticipantStatus($participantId, $participantStatusIdNow, $updatedStatusId, TRUE);
}
}
}

/**
* Implements hook_civicrm_pre().
*/
function eventmembershipsignup_civicrm_pre($op, $objectName, $id, &$params) {
if ($op == 'edit' && $objectName == 'Contribution') {
if (empty($params['prevContribution']->is_pay_later)
|| CRM_Utils_Array::value('is_pay_later', $params, 1)
|| empty($params['participant_id'])) {
return;
}

$contribStatusAPI = civicrm_api3('Contribution', 'getoptions', array(
'field' => "contribution_status_id",
'context' => "validate",
));

$participantStatusAPI = civicrm_api3('Participant', 'getoptions', array(
'field' => "participant_status_id",
'context' => "validate",
));
// Get all Event Registrations added by the extension

// FIXME: for now, no updating of memberships, just events. The reason?
// This:
// https://github.com/civicrm/civicrm-core/blob/4.7.15/CRM/Contribute/BAO/Contribution.php#L1810
// and the following 110 lines. For now, add-on memberships are not held in
// pending status so there is no need to activate them when pay-later is
// resolved.
$sql = <<<HERESQL
SELECT p.id as participant_id, p.status_id as participant_status_id
FROM civicrm_contribution c
Expand All @@ -122,27 +173,13 @@ function eventmembershipsignup_civicrm_pre($op, $objectName, $id, &$params) {
HERESQL;
$dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($id, 'Integer')));

// FIXME: for now, no updating of memberships, just events. The reason?
// This:
// https://github.com/civicrm/civicrm-core/blob/4.7.15/CRM/Contribute/BAO/Contribution.php#L1810
// and the following 110 lines. For now, add-on memberships are not held in
// pending status so there is no need to activate them when pay-later is
// resolved.
switch (CRM_Utils_Array::value($params['contribution_status_id'], $contribStatusAPI['values'])) {
case 'Cancelled':
case 'Failed':
$updatedStatusId = array_search('Cancelled', $participantStatusAPI['values']);
while ($dao->fetch()) {
CRM_Event_BAO_Participant::updateParticipantStatus($dao->participant_id, $dao->participant_status_id, $updatedStatusId, TRUE);
}
break;

case 'Completed':
$updatedStatusId = array_search('Registered', $participantStatusAPI['values']);
while ($dao->fetch()) {
CRM_Event_BAO_Participant::updateParticipantStatus($dao->participant_id, $dao->participant_status_id, $updatedStatusId, TRUE);
}
break;
while ($dao->fetch()) {
eventmembershipsignup_updateparticipantstatus(
$params['prevContribution']->contribution_status_id,
$params['contribution_status_id'],
$dao->participant_status_id,
$dao->participant_id
);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
<email>[email protected]</email>
</maintainer>
<releaseDate>2017-12-12</releaseDate>
<version>2.3</version>
<version>2.4</version>
<develStage>stable</develStage>
<compatibility>
<ver>4.6</ver>
<ver>4.7</ver>
<ver>5.13</ver>
<ver>5.18</ver>
</compatibility>
<urls>
<url desc="documentation">https://aghstrategies.com/content/event-additional-signup-extension</url>
Expand Down
30 changes: 30 additions & 0 deletions tests/eventmembershipsignup.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Scenario: Completing pay-later payments for an event with an additional event signup
Given Event Additional Signup is installed
And two events are set up
And the first event has online registration enabled with a price set that has a field with an option where the second event is selected for an additional signup
And a contact has registered for the first event with Pay Later, causing a pending contribution and two pending participant records
When an admin user clicks Record Payment on the contribution
And the payment is equal to the total amount due
Then the contribution should have the status Completed
And the participant record for the second event should have the status Registered

Scenario: Making partial payments for an event with an additional event signup
Given Event Additional Signup is installed
And two events are set up
And the first event has online registration enabled with a price set that has a field with an option where the second event is selected for an additional signup
And a contact has registered for the first event with Pay Later, causing a pending contribution and two pending participant records
When an admin user clicks Record Payment on the contribution
And the payment is less than the total amount due
Then the contribution should have the status Partially Paid
And the participant record for the second event should have the status Partially Paid

Scenario: Completing partial payments for an event with an additional event signup
Given Event Additional Signup is installed
And two events are set up
And the first event has online registration enabled with a price set that has a field with an option where the second event is selected for an additional signup
And a contact has registered for the first event with Pay Later, causing a pending contribution and two pending participant records
And a partial payment has been made, with the status of the contribution record and the two participant records being Partially Paid
When an admin user clicks Record Payment on the contribution
And the payment is equal to the remaining amount due
Then the contribution should have the status Completed
And the participant record for the second event should have the status Registered

0 comments on commit 62722e0

Please sign in to comment.