X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FContribute%2FForm%2FContribution%2FConfirm.php;h=b367b2f4f556e4cd8cb1e3fb8c774e1e8be58b69;hb=bddc8a28fc2bb767b15fab2143df6426b6cc5ee4;hp=33888d22e5a953a7e7b99e214c52c69fea95f8af;hpb=2de8bcf21e424dd6ddf0080fdc02ab2d01a1c222;p=civicrm-core.git diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index 33888d22e5..b367b2f4f5 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -228,16 +228,9 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $this->assign('pay_later_receipt', $this->_values['pay_later_receipt']); } // if onbehalf-of-organization - if (!empty($this->_params['hidden_onbehalf_profile'])) { + if (!empty($this->_values['onbehalf_profile_id']) && !empty($this->_params['onbehalf']['organization_name'])) { // CRM-15182 - if (empty($this->_params['org_option']) && empty($this->_params['organization_id'])) { - if (!empty($this->_params['onbehalfof_id'])) { - $this->_params['organization_id'] = $this->_params['onbehalfof_id']; - } - else { - $this->_params['organization_id'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_params['onbehalf']['organization_name'], 'id', 'display_name'); - } - } + $this->_params['organization_id'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_params['onbehalf']['organization_name'], 'id', 'display_name'); $this->_params['organization_name'] = $this->_params['onbehalf']['organization_name']; $addressBlocks = array( @@ -403,17 +396,16 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $params = $this->_params; // make sure we have values for it - if ($this->_honor_block_is_active && !empty($params['soft_credit_type_id'])) { + if (!empty($this->_values['honoree_profile_id']) && !empty($params['soft_credit_type_id'])) { $honorName = NULL; $softCreditTypes = CRM_Core_OptionGroup::values("soft_credit_type", FALSE); - $this->assign('honor_block_is_active', $this->_honor_block_is_active); $this->assign('soft_credit_type', $softCreditTypes[$params['soft_credit_type_id']]); - CRM_Contribute_BAO_ContributionSoft::formatHonoreeProfileFields($this, $params['honor'], $params['honoree_profile_id']); + CRM_Contribute_BAO_ContributionSoft::formatHonoreeProfileFields($this, $params['honor']); $fieldTypes = array('Contact'); - $fieldTypes[] = CRM_Core_BAO_UFGroup::getContactType($params['honoree_profile_id']); - $this->buildCustom($params['honoree_profile_id'], 'honoreeProfileFields', TRUE, 'honor', $fieldTypes); + $fieldTypes[] = CRM_Core_BAO_UFGroup::getContactType($this->_values['honoree_profile_id']); + $this->buildCustom($this->_values['honoree_profile_id'], 'honoreeProfileFields', TRUE, 'honor', $fieldTypes); } $this->assign('receiptFromEmail', CRM_Utils_Array::value('receipt_from_email', $this->_values)); $amount_block_is_active = $this->get('amount_block_is_active'); @@ -468,15 +460,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $this->buildCustom($this->_values['custom_pre_id'], 'customPre', TRUE); $this->buildCustom($this->_values['custom_post_id'], 'customPost', TRUE); - if (!empty($params['hidden_onbehalf_profile'])) { - $ufJoinParams = array( - 'module' => 'onBehalf', - 'entity_table' => 'civicrm_contribution_page', - 'entity_id' => $this->_id, - ); - $OnBehalfProfile = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); - $profileId = $OnBehalfProfile[0]; - + if (!empty($this->_values['onbehalf_profile_id']) && !empty($params['onbehalf'])) { $fieldTypes = array('Contact', 'Organization'); $contactSubType = CRM_Contact_BAO_ContactType::subTypes('Organization'); $fieldTypes = array_merge($fieldTypes, $contactSubType); @@ -487,7 +471,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $fieldTypes = array_merge($fieldTypes, array('Contribution')); } - $this->buildCustom($profileId, 'onbehalfProfile', TRUE, 'onbehalf', $fieldTypes); + $this->buildCustom($this->_values['onbehalf_profile_id'], 'onbehalfProfile', TRUE, 'onbehalf', $fieldTypes); } $this->_separateMembershipPayment = $this->get('separateMembershipPayment'); @@ -810,8 +794,6 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr * - thankyou_date (not all forms will set this) * * @param CRM_Financial_DAO_FinancialType $financialType - * @param bool $pending - * The intention is this should always be TRUE and we are refactoring towards any completion happening elsewhere. * @param bool $online * Is the form a front end form? If so set a bunch of unpredictable things that should be passed in from the form. * @@ -827,7 +809,6 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $result, $contributionParams, $financialType, - $pending, $online, $billingLocationID ) { @@ -835,13 +816,10 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $contactID = $contributionParams['contact_id']; $isEmailReceipt = !empty($form->_values['is_email_receipt']); - // How do these vary from params? These are currently passed to - // - custom data function.... - $formParams = $form->_params; - $isSeparateMembershipPayment = empty($formParams['separate_membership_payment']) ? FALSE : TRUE; - $pledgeID = empty($formParams['pledge_id']) ? NULL : $formParams['pledge_id']; + $isSeparateMembershipPayment = empty($params['separate_membership_payment']) ? FALSE : TRUE; + $pledgeID = empty($params['pledge_id']) ? NULL : $params['pledge_id']; if (!$isSeparateMembershipPayment && !empty($form->_values['pledge_block_id']) && - (!empty($formParams['is_pledge']) || $pledgeID)) { + (!empty($params['is_pledge']) || $pledgeID)) { $isPledge = TRUE; } else { @@ -871,28 +849,9 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $receiptDate = $now; } - // Prepare soft contribution due to pcp or Submit Credit / Debit Card Contribution by admin. - if (!empty($params['pcp_made_through_id']) || !empty($params['soft_credit_to'])) { - // if its due to pcp - if (!empty($params['pcp_made_through_id'])) { - $contributionParams['soft_credit_to'] = CRM_Core_DAO::getFieldValue( - 'CRM_PCP_DAO_PCP', - $params['pcp_made_through_id'], - 'contact_id' - ); - // Pass these details onto with the contribution to make them - // available at hook_post_process, CRM-8908 - // @todo - obsolete? - $params['soft_credit_to'] = $contributionParams['soft_credit_to']; - } - else { - $contributionParams['soft_credit_to'] = CRM_Utils_Array::value('soft_credit_to', $params); - } - } - if (isset($params['amount'])) { $contributionParams = array_merge(self::getContributionParams( - $params, $financialType->id, $nonDeductibleAmount, $pending, + $params, $financialType->id, $nonDeductibleAmount, TRUE, $result, $receiptDate, $recurringContributionID), $contributionParams ); @@ -929,18 +888,18 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $form->_contributionID = $contribution->id; } - //CRM-13981, processing honor contact into soft-credit contribution - CRM_Contact_Form_ProfileContact::postProcess($form); + // process soft credit / pcp params first + CRM_Contribute_BAO_ContributionSoft::formatSoftCreditParams($params, $form); - // process soft credit / pcp pages - CRM_Contribute_Form_Contribution_Confirm::processPcpSoft($params, $contribution); + //CRM-13981, processing honor contact into soft-credit contribution + CRM_Contribute_BAO_ContributionSoft::processSoftContribution($params, $contribution); //handle pledge stuff. if ($isPledge) { if ($pledgeID) { //when user doing pledge payments. //update the schedule when payment(s) are made - foreach ($form->_params['pledge_amount'] as $paymentId => $dontCare) { + foreach ($params['pledge_amount'] as $paymentId => $dontCare) { $scheduledAmount = CRM_Core_DAO::getFieldValue( 'CRM_Pledge_DAO_PledgePayment', $paymentId, @@ -1013,7 +972,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr } if ($online && $contribution) { - CRM_Core_BAO_CustomValueTable::postProcess($form->_params, + CRM_Core_BAO_CustomValueTable::postProcess($params, 'civicrm_contribution', $contribution->id, 'Contribution' @@ -1183,29 +1142,29 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr * Array of fields from the onbehalf profile relevant to the organization. */ public static function processOnBehalfOrganization(&$behalfOrganization, &$contactID, &$values, &$params, $fields = NULL) { - $isCurrentEmployer = FALSE; + $isNotCurrentEmployer = FALSE; $dupeIDs = array(); $orgID = NULL; - if (!empty($behalfOrganization['organization_id']) && empty($behalfOrganization['org_option'])) { + if (!empty($behalfOrganization['organization_id'])) { $orgID = $behalfOrganization['organization_id']; unset($behalfOrganization['organization_id']); - $isCurrentEmployer = TRUE; + } + // create employer relationship with $contactID only when new organization is there + // else retain the existing relationship + else { + // get the Employee relationship type id + $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b'); + + // keep relationship params ready + $relParams['relationship_type_id'] = $relTypeId . '_a_b'; + $relParams['is_permission_a_b'] = 1; + $relParams['is_active'] = 1; + $isNotCurrentEmployer = TRUE; } // formalities for creating / editing organization. $behalfOrganization['contact_type'] = 'Organization'; - // get the relationship type id - $relType = new CRM_Contact_DAO_RelationshipType(); - $relType->name_a_b = 'Employee of'; - $relType->find(TRUE); - $relTypeId = $relType->id; - - // keep relationship params ready - $relParams['relationship_type_id'] = $relTypeId . '_a_b'; - $relParams['is_permission_a_b'] = 1; - $relParams['is_active'] = 1; - if (!$orgID) { // check if matching organization contact exists $dedupeParams = CRM_Dedupe_Finder::formatParams($behalfOrganization, 'Organization'); @@ -1236,9 +1195,11 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr NULL, NULL, 'Organization' ); // create relationship - $relParams['contact_check'][$orgID] = 1; - $cid = array('contact' => $contactID); - CRM_Contact_BAO_Relationship::legacyCreateMultiple($relParams, $cid); + if ($isNotCurrentEmployer) { + $relParams['contact_check'][$orgID] = 1; + $cid = array('contact' => $contactID); + CRM_Contact_BAO_Relationship::legacyCreateMultiple($relParams, $cid); + } // if multiple match - send a duplicate alert if ($dupeIDs && (count($dupeIDs) > 1)) { @@ -1255,18 +1216,16 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr // required for mailing/template display ..etc $values['related_contact'] = $contactID; - // required for IPN - $params['related_contact'] = $contactID; //make this employee of relationship as current //employer / employee relationship, CRM-3532 - if ($isCurrentEmployer && + if ($isNotCurrentEmployer && ($orgID != CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'employer_id')) ) { - $isCurrentEmployer = FALSE; + $isNotCurrentEmployer = FALSE; } - if (!$isCurrentEmployer && $orgID) { + if (!$isNotCurrentEmployer && $orgID) { //build current employer params $currentEmpParams[$contactID] = $orgID; CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams); @@ -1278,47 +1237,6 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr } } - /** - * Function used to save pcp / soft credit entry. - * - * This is used by contribution and also event pcps - * - * @param array $params - * @param object $contribution - * Contribution object. - */ - public static function processPcpSoft(&$params, &$contribution) { - // Add soft contribution due to pcp or Submit Credit / Debit Card Contribution by admin. - if (!empty($params['soft_credit_to'])) { - $contributionSoftParams = array(); - foreach (array( - 'pcp_display_in_roll', - 'pcp_roll_nickname', - 'pcp_personal_note', - 'amount', - ) as $val) { - if (!empty($params[$val])) { - $contributionSoftParams[$val] = $params[$val]; - } - } - - $contributionSoftParams['contact_id'] = $params['soft_credit_to']; - // add contribution id - $contributionSoftParams['contribution_id'] = $contribution->id; - // add pcp id - $contributionSoftParams['pcp_id'] = $params['pcp_made_through_id']; - - $contributionSoftParams['soft_credit_type_id'] = CRM_Core_OptionGroup::getValue('soft_credit_type', 'pcp', 'name'); - - $contributionSoft = CRM_Contribute_BAO_ContributionSoft::add($contributionSoftParams); - - //Send notification to owner for PCP - if ($contributionSoft->id && $contributionSoft->pcp_id) { - CRM_Contribute_Form_Contribution_Confirm::pcpNotifyOwner($contribution, $contributionSoft); - } - } - } - /** * Function used to send notification mail to pcp owner. * @@ -1662,24 +1580,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr } $payment = Civi\Payment\System::singleton()->getByProcessor($form->_paymentProcessor); $paymentActionResult = $payment->doPayment($form->_params, 'contribute'); - - if (CRM_Utils_Array::value('payment_status_id', $paymentActionResult) == 1) { - // Refer to CRM-16737. Payment processors 'should' return payment_status_id - // to denote the outcome of the transaction. - try { - civicrm_api3('contribution', 'completetransaction', array( - 'id' => $paymentResult['contribution']->id, - 'trxn_id' => CRM_Utils_Array::value('trxn_id', $paymentActionResult, $paymentResult['contribution']->trxn_id), - 'is_transactional' => FALSE, - 'payment_processor_id' => $form->_paymentProcessor['id'], - )); - } - catch (CiviCRM_API3_Exception $e) { - if ($e->getErrorCode() != 'contribution_completed') { - throw new CRM_Core_Exception('Failed to update contribution in database'); - } - } - } + $this->completeTransaction($paymentActionResult, $paymentResult['contribution']->id); // Do not send an email if Recurring transaction is done via Direct Mode // Email will we sent when the IPN is received. return; @@ -1732,19 +1633,6 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $tempParams['amount'] = $minimumFee; $tempParams['invoiceID'] = md5(uniqid(rand(), TRUE)); - $result = NULL; - if ($form->_values['is_monetary'] && !$form->_params['is_pay_later'] && $minimumFee > 0.0) { - // At the moment our tests are calling this form in a way that leaves 'object' empty. For - // now we compensate here. - if (empty($form->_paymentProcessor['object'])) { - $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); - } - else { - $payment = $form->_paymentProcessor['object']; - } - $result = $payment->doPayment($tempParams, 'contribute'); - } - //assign receive date when separate membership payment //and contribution amount not selected. if ($form->_amount == 0) { @@ -1755,22 +1643,13 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $form->assign('receive_date', $receiveDate); } - $form->set('membership_trx_id', $result['trxn_id']); $form->set('membership_amount', $minimumFee); - - $form->assign('membership_trx_id', $result['trxn_id']); $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; - //CRM-16165, scenarios are - // 1) If contribution is_pay_later and if contribution amount is > 0.0 we set pending = TRUE, vice-versa FALSE - // 2) If not pay later but auto-renewal membership is chosen then pending = TRUE as it later triggers - // pending recurring contribution, vice-versa FALSE - $pending = $form->_params['is_pay_later'] ? (($minimumFee > 0.0) ? TRUE : FALSE) : (!empty($form->_params['auto_renew']) ? TRUE : FALSE); - //set this variable as we are not creating pledge for //separate membership payment contribution. //so for differentiating membership contribution from @@ -1788,19 +1667,33 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $isMonetary = !empty($form->_values['is_monetary']); if ($isMonetary) { if (empty($paymentParams['is_pay_later'])) { - // @todo look up payment_instrument_id on payment processor table. - $contributionParams['payment_instrument_id'] = 1; + $contributionParams['payment_instrument_id'] = $form->_paymentProcessor['payment_instrument_id']; } } $membershipContribution = CRM_Contribute_Form_Contribution_Confirm::processFormContribution($form, $tempParams, - $result, + $tempParams, $contributionParams, $financialType, - $pending, TRUE, $form->_bltID ); + + if ($form->_values['is_monetary'] && !$form->_params['is_pay_later'] && $minimumFee > 0.0) { + // At the moment our tests are calling this form in a way that leaves 'object' empty. For + // now we compensate here. + if (empty($form->_paymentProcessor['object'])) { + $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); + } + else { + $payment = $form->_paymentProcessor['object']; + } + $result = $payment->doPayment($tempParams, 'contribute'); + $form->set('membership_trx_id', $result['trxn_id']); + $form->assign('membership_trx_id', $result['trxn_id']); + $this->completeTransaction($result, $membershipContribution->id); + } + return $membershipContribution; } @@ -2029,22 +1922,16 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr } } - if (!array_key_exists('first_name', $fields)) { - $nameFields = array('first_name', 'middle_name', 'last_name'); - foreach ($nameFields as $name) { - $fields[$name] = 1; - if (array_key_exists("billing_$name", $params)) { - $params[$name] = $params["billing_{$name}"]; - $params['preserveDBName'] = TRUE; - } - } - } + $fields = $this->formatParamsForPaymentProcessor($fields); // billing email address $fields["email-{$this->_bltID}"] = 1; //unset the billing parameters if it is pay later mode //to avoid creation of billing location + // @todo - note that elsewhere we don't unset these - we simply make + // a sensible decision about including them when building the form + // and if they are submitted we handle them. Check out abstractEditPaymentForm. if ($isPayLater && !$this->_isBillingAddressRequiredForPayLater) { $billingFields = array( 'billing_first_name', @@ -2069,7 +1956,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr // organization params in a separate variable, to make sure // normal behavior is continued. And use that variable to // process on-behalf-of functionality. - if (!empty($this->_params['hidden_onbehalf_profile'])) { + if (!empty($this->_values['onbehalf_profile_id'])) { $behalfOrganization = array(); $orgFields = array('organization_name', 'organization_id', 'org_option'); foreach ($orgFields as $fld) { @@ -2197,7 +2084,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr // If onbehalf-of-organization contribution / signup, add organization // and it's location. - if (isset($params['hidden_onbehalf_profile']) && isset($behalfOrganization['organization_name'])) { + if (isset($this->_values['onbehalf_profile_id']) && isset($behalfOrganization['organization_name'])) { $ufFields = array(); foreach ($this->_fields['onbehalf'] as $name => $value) { $ufFields[$name] = 1; @@ -2276,27 +2163,13 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr ($this->_mode == 'test') ? 1 : 0 ); - if (!empty($result['is_payment_failure'])) { - return $result; - } - // @todo move premium processing to complete transaction if it truly is an 'after' action. - $this->postProcessPremium($premiumParams, $result['contribution']); - if (CRM_Utils_Array::value('payment_status_id', $result) == 1) { - try { - civicrm_api3('contribution', 'completetransaction', array( - 'id' => $result['contribution']->id, - 'trxn_id' => CRM_Utils_Array::value('trxn_id', $result), - 'payment_processor_id' => $this->_paymentProcessor['id'], - 'is_transactional' => FALSE, - ) - ); - } - catch (CiviCRM_API3_Exception $e) { - if ($e->getErrorCode() != 'contribution_completed') { - throw new CRM_Core_Exception('Failed to update contribution in database'); - } - } - + if (empty($result['is_payment_failure'])) { + // @todo move premium processing to complete transaction if it truly is an 'after' action. + $this->postProcessPremium($premiumParams, $result['contribution']); + } + if (!empty($result['contribution'])) { + // Not quite sure why it would be empty at this stage but tests show it can be ... at least in tests. + $this->completeTransaction($result, $result['contribution']->id); } return $result; } @@ -2404,4 +2277,36 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr } } + /** + * Complete transaction if payment has been processed. + * + * Check the result for a success outcome & if paid then complete the transaction. + * + * Completing will trigger update of related entities and emails. + * + * @param array $result + * @param int $contributionID + * + * @throws \CRM_Core_Exception + */ + protected function completeTransaction($result, $contributionID) { + if (CRM_Utils_Array::value('payment_status_id', $result) == 1) { + try { + civicrm_api3('contribution', 'completetransaction', array( + 'id' => $contributionID, + 'trxn_id' => CRM_Utils_Array::value('trxn_id', $result), + 'payment_processor_id' => $this->_paymentProcessor['id'], + 'is_transactional' => FALSE, + 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result), + ) + ); + } + catch (CiviCRM_API3_Exception $e) { + if ($e->getErrorCode() != 'contribution_completed') { + throw new CRM_Core_Exception('Failed to update contribution in database'); + } + } + } + } + }