use Civi\Api4\Activity;
use Civi\Api4\ContributionPage;
use Civi\Api4\ContributionRecur;
-use Civi\Api4\Participant;
use Civi\Api4\PaymentProcessor;
/**
*
* This function should only be called from transitioncomponents - it is an interim step in refactoring.
*
- * @param $processContributionObject
* @param $memberships
* @param $contributionId
* @param $membershipStatuses
- * @param $updateResult
* @param $participant
* @param $oldStatus
* @param $pledgePayment
* @param $pledgeID
* @param $pledgePaymentIDs
* @param $contributionStatusId
- *
- * @return array
*/
- protected static function cancel($processContributionObject, $memberships, $contributionId, $membershipStatuses, $updateResult, $participant, $oldStatus, $pledgePayment, $pledgeID, $pledgePaymentIDs, $contributionStatusId) {
+ protected static function cancel($memberships, $contributionId, $membershipStatuses, $participant, $oldStatus, $pledgePayment, $pledgeID, $pledgePaymentIDs, $contributionStatusId) {
// @fixme https://lab.civicrm.org/dev/core/issues/927 Cancelling membership etc is not desirable for all use-cases and we should be able to disable it
- $processContribution = FALSE;
$participantStatuses = CRM_Event_PseudoConstant::participantStatus();
if (is_array($memberships)) {
foreach ($memberships as $membership) {
$membership->status_override_end_date = 'null';
$membership->save();
civicrm_api3('activity', 'create', $activityParam);
-
- $updateResult['updatedComponents']['CiviMember'] = $membership->status_id;
- if ($processContributionObject) {
- $processContribution = TRUE;
- }
}
}
}
if ($participant) {
$updatedStatusId = array_search('Cancelled', $participantStatuses);
CRM_Event_BAO_Participant::updateParticipantStatus($participant->id, $oldStatus, $updatedStatusId, TRUE);
-
- $updateResult['updatedComponents']['CiviEvent'] = $updatedStatusId;
- if ($processContributionObject) {
- $processContribution = TRUE;
- }
}
if ($pledgePayment) {
CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID, $pledgePaymentIDs, $contributionStatusId);
-
- $updateResult['updatedComponents']['CiviPledge'] = $contributionStatusId;
- if ($processContributionObject) {
- $processContribution = TRUE;
- }
}
- return [$updateResult, $processContribution];
}
/**
/**
* Process failed contribution.
*
- * @param $processContributionObject
* @param $memberships
* @param $contributionId
* @param array $membershipStatuses
- * @param array $updateResult
* @param $participant
* @param $pledgePayment
* @param $pledgeID
* @param array $pledgePaymentIDs
* @param $contributionStatusId
*
- * @return array
* @throws \CRM_Core_Exception
*/
- protected static function processFail($processContributionObject, $memberships, $contributionId, array $membershipStatuses, array $updateResult, $participant, $pledgePayment, $pledgeID, array $pledgePaymentIDs, $contributionStatusId): array {
- $processContribution = FALSE;
+ protected static function processFail($memberships, $contributionId, array $membershipStatuses, array $participant, $pledgePayment, $pledgeID, array $pledgePaymentIDs, $contributionStatusId): void {
if (is_array($memberships)) {
foreach ($memberships as $membership) {
$update = TRUE;
$membership->is_override = TRUE;
$membership->status_override_end_date = 'null';
$membership->save();
-
- $updateResult['updatedComponents']['CiviMember'] = $membership->status_id;
- if ($processContributionObject) {
- $processContribution = TRUE;
- }
}
}
}
$participantStatuses = CRM_Event_PseudoConstant::participantStatus();
$updatedStatusId = array_search('Cancelled', $participantStatuses);
CRM_Event_BAO_Participant::updateParticipantStatus($participant->id, $oldStatus, $updatedStatusId, TRUE);
-
- $updateResult['updatedComponents']['CiviEvent'] = $updatedStatusId;
- if ($processContributionObject) {
- $processContribution = TRUE;
- }
}
if ($pledgePayment) {
CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID, $pledgePaymentIDs, $contributionStatusId);
-
- $updateResult['updatedComponents']['CiviPledge'] = $contributionStatusId;
- if ($processContributionObject) {
- $processContribution = TRUE;
- }
}
- return [$updateResult, $processContribution];
}
/**
* This function by-passes hooks - to address this - don't use this function.
*
* @param array $params
- * @param bool $processContributionObject
- *
- * @return array
*
* @throws CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
* For failures use failPayment (preferably exposing by api in the process).
*
*/
- public static function transitionComponents($params, $processContributionObject = FALSE) {
+ public static function transitionComponents($params) {
// get minimum required values.
$contactId = $params['contact_id'] ?? NULL;
$componentId = $params['component_id'] ?? NULL;
array_search('Failed', $contributionStatuses),
])
) {
- return $updateResult;
+ return;
}
if (!$componentName || !$componentId) {
// do check for required ids.
if (empty($componentDetails['membership']) && empty($componentDetails['participant']) && empty($componentDetails['pledge_payment']) || empty($componentDetails['contact_id'])) {
- return $updateResult;
+ return;
}
$input = $ids = [];
$processContribution = FALSE;
if ($contributionStatusId == array_search('Cancelled', $contributionStatuses)) {
// Call interim cancel function - with a goal to cleaning up the signature on it and switching to a tested api Contribution.cancel function.
- list($updateResult, $processContribution) = self::cancel($processContributionObject, $memberships, $contributionId, $membershipStatuses, $updateResult, $participant, $oldStatus, $pledgePayment, $pledgeID, $pledgePaymentIDs, $contributionStatusId);
+ self::cancel($memberships, $contributionId, $membershipStatuses, $participant, $oldStatus, $pledgePayment, $pledgeID, $pledgePaymentIDs, $contributionStatusId);
}
elseif ($contributionStatusId == array_search('Failed', $contributionStatuses)) {
- list($updateResult, $processContribution) = self::processFail($processContributionObject, $memberships, $contributionId, $membershipStatuses, $updateResult, $participant, $pledgePayment, $pledgeID, $pledgePaymentIDs, $contributionStatusId);
+ self::processFail($memberships, $contributionId, $membershipStatuses, $participant, $pledgePayment, $pledgeID, $pledgePaymentIDs, $contributionStatusId);
}
elseif ($contributionStatusId == array_search('Completed', $contributionStatuses)) {
])
) {
// this is case when we already processed contribution object.
- return $updateResult;
+ return;
}
elseif (!$previousContriStatusId &&
!in_array($contributionStatuses[$contribution->contribution_status_id], [
])
) {
// this is case when we are going to process contribution object later.
- return $updateResult;
+ return;
}
if (is_array($memberships)) {
);
}
- $updateResult['updatedComponents']['CiviMember'] = $membership->status_id;
- if ($processContributionObject) {
- $processContribution = TRUE;
- }
-
CRM_Utils_Hook::post('edit', 'Membership', $membership->id, $membership);
}
}
if ($participant) {
$updatedStatusId = array_search('Registered', $participantStatuses);
CRM_Event_BAO_Participant::updateParticipantStatus($participant->id, $oldStatus, $updatedStatusId, TRUE);
-
- $updateResult['updatedComponents']['CiviEvent'] = $updatedStatusId;
- if ($processContributionObject) {
- $processContribution = TRUE;
- }
}
if ($pledgePayment) {
CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeID, $pledgePaymentIDs, $contributionStatusId);
-
- $updateResult['updatedComponents']['CiviPledge'] = $contributionStatusId;
- if ($processContributionObject) {
- $processContribution = TRUE;
- }
}
}
$contribution = CRM_Contribute_BAO_Contribution::create($contributionParams);
}
- return $updateResult;
}
/**
$contribution->total_amount = $contributionParams['total_amount'] = $input['amount'];
}
- if (!empty($contributionParams['contribution_recur_id'])) {
- $recurringContribution = civicrm_api3('ContributionRecur', 'getsingle', [
- 'id' => $contributionParams['contribution_recur_id'],
- ]);
- if (!empty($recurringContribution['campaign_id'])) {
- // CRM-17718 the campaign id on the contribution recur record should get precedence.
- $contributionParams['campaign_id'] = $recurringContribution['campaign_id'];
- }
- if (!empty($recurringContribution['financial_type_id'])) {
- // CRM-17718 the campaign id on the contribution recur record should get precedence.
- $contributionParams['financial_type_id'] = $recurringContribution['financial_type_id'];
- }
+ $recurringContribution = civicrm_api3('ContributionRecur', 'getsingle', [
+ 'id' => $contributionParams['contribution_recur_id'],
+ ]);
+ if (!empty($recurringContribution['financial_type_id'])) {
+ // CRM-17718 the campaign id on the contribution recur record should get precedence.
+ $contributionParams['financial_type_id'] = $recurringContribution['financial_type_id'];
}
$templateContribution = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution(
$contributionParams['contribution_recur_id'],
else {
$contributionParams['financial_type_id'] = $templateContribution['financial_type_id'];
}
- foreach (['contact_id', 'currency', 'source'] as $fieldName) {
- $contributionParams[$fieldName] = $templateContribution[$fieldName];
+ foreach (['contact_id', 'currency', 'source', 'amount_level', 'address_id'] as $fieldName) {
+ if (isset($templateContribution[$fieldName])) {
+ $contributionParams[$fieldName] = $templateContribution[$fieldName];
+ }
+ }
+ if (!empty($recurringContribution['campaign_id'])) {
+ // CRM-17718 the campaign id on the contribution recur record should get precedence.
+ $contributionParams['campaign_id'] = $recurringContribution['campaign_id'];
}
-
- $contributionParams['source'] = $contributionParams['source'] ?: ts('Recurring contribution');
+ if (!isset($contributionParams['campaign_id']) && isset($templateContribution['campaign_id'])) {
+ // Fall back on value from the previous contribution if not passed in as input
+ // or loadable from the recurring contribution.
+ $contributionParams['campaign_id'] = $templateContribution['campaign_id'];
+ }
+ $contributionParams['source'] = $contributionParams['source'] ?? ts('Recurring contribution');
//CRM-18805 -- Contribution page not recorded on recurring transactions, Recurring contribution payments
//do not create CC or BCC emails or profile notifications.
$contributionParams = array_merge([
'contribution_status_id' => $completedContributionStatusID,
- 'source' => self::getRecurringContributionDescription($contribution, $participantID),
], array_intersect_key($input, array_fill_keys($inputContributionWhiteList, 1)
));
return $ids;
}
- /**
- * Get the description (source field) for the recurring contribution.
- *
- * @param CRM_Contribute_BAO_Contribution $contribution
- * @param int|null $participantID
- *
- * @return string
- * @throws \CiviCRM_API3_Exception
- * @throws \API_Exception
- */
- protected static function getRecurringContributionDescription($contribution, $participantID) {
- if (!empty($contribution->source)) {
- return $contribution->source;
- }
- elseif (!empty($contribution->contribution_page_id) && is_numeric($contribution->contribution_page_id)) {
- $contributionPageTitle = civicrm_api3('ContributionPage', 'getvalue', [
- 'id' => $contribution->contribution_page_id,
- 'return' => 'title',
- ]);
- return ts('Online Contribution') . ': ' . $contributionPageTitle;
- }
- elseif ($participantID) {
- $eventTitle = Participant::get(FALSE)
- ->addSelect('event.title')
- ->addWhere('id', '=', (int) $participantID)
- ->execute()->first()['event.title'];
- return ts('Online Event Registration') . ': ' . $eventTitle;
- }
- elseif (!empty($contribution->contribution_recur_id)) {
- return 'recurring contribution';
- }
- return '';
- }
-
/**
* Function use to store line item proportionally in in entity financial trxn table
*
return $contributeSettings[$name] ?? NULL;
}
- /**
- * This function process contribution related objects.
- *
- * @param int $contributionId
- * @param int $statusId
- * @param int|null $previousStatusId
- *
- * @param string $receiveDate
- *
- * @return null|string
- */
- public static function transitionComponentWithReturnMessage($contributionId, $statusId, $previousStatusId = NULL, $receiveDate = NULL) {
- $statusMsg = NULL;
- if (!$contributionId || !$statusId) {
- return $statusMsg;
- }
-
- $params = [
- 'contribution_id' => $contributionId,
- 'contribution_status_id' => $statusId,
- 'previous_contribution_status_id' => $previousStatusId,
- 'receive_date' => $receiveDate,
- ];
-
- $updateResult = CRM_Contribute_BAO_Contribution::transitionComponents($params);
-
- if (!is_array($updateResult) ||
- !($updatedComponents = CRM_Utils_Array::value('updatedComponents', $updateResult)) ||
- !is_array($updatedComponents) ||
- empty($updatedComponents)
- ) {
- return $statusMsg;
- }
-
- // get the user display name.
- $sql = "
- SELECT display_name as displayName
- FROM civicrm_contact
-LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_contact.id )
- WHERE civicrm_contribution.id = {$contributionId}";
- $userDisplayName = CRM_Core_DAO::singleValueQuery($sql);
-
- // get the status message for user.
- foreach ($updatedComponents as $componentName => $updatedStatusId) {
-
- if ($componentName == 'CiviMember') {
- $updatedStatusName = CRM_Utils_Array::value($updatedStatusId,
- CRM_Member_PseudoConstant::membershipStatus()
- );
-
- $statusNameMsgPart = 'updated';
- switch ($updatedStatusName) {
- case 'Cancelled':
- case 'Expired':
- $statusNameMsgPart = $updatedStatusName;
- break;
- }
-
- $statusMsg .= "<br />" . ts("Membership for %1 has been %2.", [
- 1 => $userDisplayName,
- 2 => $statusNameMsgPart,
- ]);
- }
-
- if ($componentName == 'CiviEvent') {
- $updatedStatusName = CRM_Utils_Array::value($updatedStatusId,
- CRM_Event_PseudoConstant::participantStatus()
- );
- if ($updatedStatusName == 'Cancelled') {
- $statusMsg .= "<br />" . ts("Event Registration for %1 has been Cancelled.", [1 => $userDisplayName]);
- }
- elseif ($updatedStatusName == 'Registered') {
- $statusMsg .= "<br />" . ts("Event Registration for %1 has been updated.", [1 => $userDisplayName]);
- }
- }
-
- if ($componentName == 'CiviPledge') {
- $updatedStatusName = CRM_Utils_Array::value($updatedStatusId,
- CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name')
- );
- if ($updatedStatusName == 'Cancelled') {
- $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Cancelled.", [1 => $userDisplayName]);
- }
- elseif ($updatedStatusName == 'Failed') {
- $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been Failed.", [1 => $userDisplayName]);
- }
- elseif ($updatedStatusName == 'Completed') {
- $statusMsg .= "<br />" . ts("Pledge Payment for %1 has been updated.", [1 => $userDisplayName]);
- }
- }
- }
-
- return $statusMsg;
- }
-
/**
* Get the contribution as it is in the database before being updated.
*