Skip to content

Commit

Permalink
Restrict Order API to pending statuses for participant
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Aug 24, 2020
1 parent 0c5decd commit 6994bf4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
22 changes: 22 additions & 0 deletions CRM/Event/BAO/ParticipantStatusType.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,28 @@ public static function setIsActive($id, $isActive) {
return CRM_Core_DAO::setFieldValue('CRM_Event_BAO_ParticipantStatusType', $id, 'is_active', $isActive);
}

/**
* Checks if status_id (id or string (eg. 5 or "Pending from pay later") is allowed for class
*
* @param int|string $status_id
* @param string $class
*
* @return bool
*/
public static function getIsValidStatusForClass($status_id, $class = 'Pending') {
$classParticipantStatuses = civicrm_api3('ParticipantStatusType', 'get', [
'class' => $class,
'is_active' => 1,
])['values'];
foreach ($classParticipantStatuses as $id => $detail) {
$allowedParticipantStatuses[$id] = $detail['name'];
}
if (in_array($status_id,$allowedParticipantStatuses) || array_key_exists($status_id, $allowedParticipantStatuses)) {
return TRUE;
}
return FALSE;
}

/**
* @param array $params
*
Expand Down
7 changes: 6 additions & 1 deletion api/v3/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ function civicrm_api3_order_create($params) {
if ($entityParams) {
switch ($entity) {
case 'participant':
$entityParams['status_id'] = $entityParams['participant_status_id'] ?? 'Pending from incomplete transaction';
if (isset($entityParams['participant_status_id'])
&& (!CRM_Event_BAO_ParticipantStatusType::getIsValidStatusForClass($entityParams['participant_status_id'], 'Pending'))) {
throw new CiviCRM_API3_Exception('Creating a participant via the Order API with a non "pending" status is not supported');
}
$entityParams['participant_status_id'] = $entityParams['participant_status_id'] ?? 'Pending from incomplete transaction';
$entityParams['status_id'] = $entityParams['participant_status_id'];
break;

case 'membership':
Expand Down

0 comments on commit 6994bf4

Please sign in to comment.