From ab5f98045ccfff065ab0709761b644afa0ce9c60 Mon Sep 17 00:00:00 2001 From: Nishant Bhorodia Date: Thu, 3 Dec 2020 18:47:00 +0530 Subject: [PATCH] Issue#521: Send notification after successful contribution payment --- CRM/Contribute/BAO/Contribution.php | 48 +++++++++++----- CRM/Contribute/BAO/ContributionSoft.php | 4 -- CRM/Contribute/Form/Contribution/Confirm.php | 59 +++++++++++--------- 3 files changed, 69 insertions(+), 42 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 2f8338320603..fa5dffa56d77 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -517,24 +517,18 @@ public static function create(&$params) { ])->execute()->first(); $campaignParams = isset($params['campaign_id']) ? ['campaign_id' => ($params['campaign_id'] ?? NULL)] : []; - $activityParams = array_merge([ + Activity::save(FALSE)->addRecord(array_merge([ 'activity_type_id:name' => 'Contribution', 'source_record_id' => $contribution->id, + 'source_contact_id' => CRM_Core_Session::getLoggedInContactID() ?: $contribution->contact_id, + 'target_contact_id' => CRM_Core_Session::getLoggedInContactID() ? [$contribution->contact_id] : [], 'activity_date_time' => $contribution->receive_date, 'is_test' => (bool) $contribution->is_test, 'status_id:name' => $isCompleted ? 'Completed' : 'Scheduled', 'skipRecentView' => TRUE, 'subject' => CRM_Activity_BAO_Activity::getActivitySubject($contribution), 'id' => $existingActivity['id'] ?? NULL, - ], $campaignParams); - if (!$activityParams['id']) { - // Don't set target contacts on update as these will have been - // correctly created and we risk overwriting them with - // 'best guess' params. - $activityParams['source_contact_id'] = (int) ($params['source_contact_id'] ?? (CRM_Core_Session::getLoggedInContactID() ?: $contribution->contact_id)); - $activityParams['target_contact_id'] = ($activityParams['source_contact_id'] === (int) $contribution->contact_id) ? [] : [$contribution->contact_id]; - } - Activity::save(FALSE)->addRecord($activityParams)->execute(); + ], $campaignParams))->execute(); } // do not add to recent items for import, CRM-4399 @@ -2442,7 +2436,7 @@ protected static function repeatTransaction(&$contribution, $input, $contributio else { $contributionParams['financial_type_id'] = $templateContribution['financial_type_id']; } - foreach (['contact_id', 'currency', 'source', 'amount_level', 'address_id', 'on_behalf', 'source_contact_id', 'tax_amount'] as $fieldName) { + foreach (['contact_id', 'currency', 'source', 'amount_level', 'address_id'] as $fieldName) { if (isset($templateContribution[$fieldName])) { $contributionParams[$fieldName] = $templateContribution[$fieldName]; } @@ -2465,6 +2459,9 @@ protected static function repeatTransaction(&$contribution, $input, $contributio if (isset($contribution->contribution_page_id) && is_numeric($contribution->contribution_page_id)) { $contributionParams['contribution_page_id'] = $contribution->contribution_page_id; } + if (!empty($contribution->tax_amount)) { + $contributionParams['tax_amount'] = $contribution->tax_amount; + } $createContribution = civicrm_api3('Contribution', 'create', $contributionParams); $contribution->id = $createContribution['id']; @@ -4257,21 +4254,46 @@ public static function completeOrder($input, $ids, $contribution, $isPostPayment $contribution->contribution_status_id = $contributionParams['contribution_status_id']; + CRM_Core_Error::debug_log_message('Contribution record updated successfully'); + $contributionSoft = civicrm_api4('ContributionSoft', 'get', [ + 'where' => [ + ['contribution_id', '=', $contributionID], + ], + 'checkPermissions' => FALSE, + ]); + if (!empty($contributionSoft)) { + //Send notification to owner for PCP + if ($contributionSoft->column('pcp_id')) { + CRM_Contribute_Form_Contribution_Confirm::pcpNotifyOwner($contribution, $contributionSoft[0]); + } + } $transaction->commit(); - \Civi::log()->info("Contribution {$contributionParams['id']} updated successfully"); // @todo - check if Contribution::create does this, test, remove. CRM_Contribute_BAO_ContributionRecur::updateRecurLinkedPledge($contributionID, $recurringContributionID, $contributionParams['contribution_status_id'], $input['amount']); + // create an activity record + // @todo - check if Contribution::create does this, test, remove. + if ($input['component'] == 'contribute') { + //CRM-4027 + $targetContactID = NULL; + if ($contributionContactID) { + $targetContactID = $contribution->contact_id; + $contribution->contact_id = $contributionContactID; + } + CRM_Activity_BAO_Activity::addActivity($contribution, 'Contribution', $targetContactID); + } + if (self::isEmailReceipt($input, $contribution->contribution_page_id, $recurringContributionID)) { civicrm_api3('Contribution', 'sendconfirmation', [ 'id' => $contributionID, 'payment_processor_id' => $paymentProcessorId, ]); - \Civi::log()->info("Contribution {$contributionParams['id']} Receipt sent"); + CRM_Core_Error::debug_log_message("Receipt sent"); } + CRM_Core_Error::debug_log_message("Success: Database updated"); return $contributionResult; } diff --git a/CRM/Contribute/BAO/ContributionSoft.php b/CRM/Contribute/BAO/ContributionSoft.php index 030b9e8dfce0..953c7ddfc9c3 100644 --- a/CRM/Contribute/BAO/ContributionSoft.php +++ b/CRM/Contribute/BAO/ContributionSoft.php @@ -607,10 +607,6 @@ protected static function processPCP($pcp, $contribution) { $softParams['pcp_personal_note'] = $pcp['pcp_personal_note'] ?? NULL; $softParams['soft_credit_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', 'pcp'); $contributionSoft = self::add($softParams); - //Send notification to owner for PCP - if ($contributionSoft->pcp_id && empty($pcpId)) { - CRM_Contribute_Form_Contribution_Confirm::pcpNotifyOwner($contribution, (array) $contributionSoft); - } } //Delete PCP against this contribution and create new on submitted PCP information elseif ($pcpId) { diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index e69795941453..d757ebcd591d 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -1000,6 +1000,13 @@ public static function processFormContribution( CRM_Core_BAO_Note::add($noteParams, []); } + if (isset($params['related_contact'])) { + $contactID = $params['related_contact']; + } + elseif (isset($params['cms_contactID'])) { + $contactID = $params['cms_contactID']; + } + //create contribution activity w/ individual and target //activity w/ organisation contact id when onbelf, CRM-4027 $actParams = []; @@ -1018,6 +1025,12 @@ public static function processFormContribution( } $transaction->commit(); + // CRM-13074 - create the CMSUser after the transaction is completed as it + // is not appropriate to delete a valid contribution if a user create problem occurs + CRM_Contribute_BAO_Contribution_Utils::createCMSUser($params, + $contactID, + 'email-' . $billingLocationID + ); return $contribution; } @@ -1096,7 +1109,10 @@ public static function processRecurringContribution(&$form, &$params, $contactID } CRM_Utils_System::redirect(CRM_Utils_System::url($urlString, $urlParams)); } - $form->_params['contributionRecurID'] = $recurring->id; + // Only set contribution recur ID for contributions since offline membership recur payments are handled somewhere else. + if (!is_a($form, "CRM_Member_Form_Membership")) { + $form->_params['contributionRecurID'] = $recurring->id; + } return $recurring->id; } @@ -1225,12 +1241,9 @@ public static function processOnBehalfOrganization(&$behalfOrganization, &$conta * * @param object $contribution * @param array $contributionSoft - * Contribution object. - * - * @throws \API_Exception - * @throws \CRM_Core_Exception + * Contribution array. */ - public static function pcpNotifyOwner($contribution, array $contributionSoft) { + public static function pcpNotifyOwner($contribution, $contributionSoft) { $params = ['id' => $contributionSoft['pcp_id']]; CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $params, $pcpInfo); $ownerNotifyID = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCPBlock', $pcpInfo['pcp_block_id'], 'owner_notify_id'); @@ -1291,7 +1304,7 @@ public static function pcpNotifyOwner($contribution, array $contributionSoft) { * * @return array */ - public static function processPcp(&$page, $params): array { + public static function processPcp(&$page, $params) { $params['pcp_made_through_id'] = $page->_pcpId; $page->assign('pcpBlock', TRUE); if (!empty($params['pcp_display_in_roll']) && empty($params['pcp_roll_nickname'])) { @@ -1327,7 +1340,7 @@ public static function processPcp(&$page, $params): array { * Line items specifically relating to memberships. */ protected function processMembership($membershipParams, $contactID, $customFieldsFormatted, $fieldTypes, $premiumParams, - $membershipLineItems): void { + $membershipLineItems) { $membershipTypeIDs = (array) $membershipParams['selectMembership']; $membershipTypes = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, $membershipTypeIDs); @@ -1429,7 +1442,8 @@ protected function postProcessMembership( CRM_Price_BAO_LineItem::getLineItemArray($membershipParams); } - $paymentResult = $form->processConfirm( + $paymentResult = self::processConfirm( + $form, $membershipParams, $contactID, $financialTypeID, @@ -1713,6 +1727,10 @@ protected function processSecondaryFinancialTransaction($contactID, &$form, $tem $form->set('membership_amount', $minimumFee); $form->assign('membership_amount', $minimumFee); + // we don't need to create the user twice, so lets disable cms_create_account + // irrespective of the value, CRM-2888 + $tempParams['cms_create_account'] = 0; + //set this variable as we are not creating pledge for //separate membership payment contribution. //so for differentiating membership contribution from @@ -2283,7 +2301,7 @@ protected function processFormSubmission($contactID) { } } - $result = $this->processConfirm($paymentParams, + $result = self::processConfirm($this, $paymentParams, $contactID, $this->wrangleFinancialTypeID($this->_values['financial_type_id']), ($this->_mode == 'test') ? 1 : 0, @@ -2504,6 +2522,8 @@ protected static function isPaymentTransaction($form) { /** * Process payment after confirmation. * + * @param CRM_Core_Form $form + * Form object. * @param array $paymentParams * Array with payment related key. * value pairs @@ -2519,14 +2539,14 @@ protected static function isPaymentTransaction($form) { * @return array * associated array */ - public function processConfirm( + public static function processConfirm( + &$form, &$paymentParams, $contactID, $financialTypeID, $isTest, $isRecur ): array { - $form = $this; CRM_Core_Payment_Form::mapParams($form->_bltID, $form->_params, $paymentParams, TRUE); $isPaymentTransaction = self::isPaymentTransaction($form); @@ -2540,7 +2560,8 @@ public function processConfirm( // add some financial type details to the params list // if folks need to use it - $paymentParams['financial_type_id'] = $paymentParams['financialTypeID'] = $financialType->id; + //CRM-15297 deprecate contributionTypeID + $paymentParams['financial_type_id'] = $paymentParams['financialTypeID'] = $paymentParams['contributionTypeID'] = $financialType->id; //CRM-15297 - contributionType is obsolete - pass financial type as well so people can deprecate it $paymentParams['financialType_name'] = $paymentParams['contributionType_name'] = $form->_params['contributionType_name'] = $financialType->name; //CRM-11456 @@ -2614,18 +2635,6 @@ public function processConfirm( $form->_bltID, $isRecur ); - // CRM-13074 - create the CMSUser after the transaction is completed as it - // is not appropriate to delete a valid contribution if a user create problem occurs - if (isset($params['related_contact'])) { - $contactID = $params['related_contact']; - } - elseif (isset($params['cms_contactID'])) { - $contactID = $params['cms_contactID']; - } - CRM_Contribute_BAO_Contribution_Utils::createCMSUser($params, - $contactID, - 'email-' . $form->_bltID - ); $paymentParams['item_name'] = $form->_params['description'];