From 3b8e6c3f1374137e889c3e1dbe499cb2d500a524 Mon Sep 17 00:00:00 2001 From: eileenmcnaugton Date: Thu, 27 Aug 2015 02:02:31 +1200 Subject: [PATCH] CRM-17089 fix remaining billing field enotices by consolidation with Contribute form --- CRM/Contribute/Form/AbstractEditPayment.php | 75 +++++++++++++++++++++ CRM/Contribute/Form/Contribution.php | 55 ++------------- CRM/Member/Form.php | 47 ------------- CRM/Member/Form/Membership.php | 14 +--- CRM/Member/Form/MembershipRenewal.php | 34 ++++++---- 5 files changed, 104 insertions(+), 121 deletions(-) diff --git a/CRM/Contribute/Form/AbstractEditPayment.php b/CRM/Contribute/Form/AbstractEditPayment.php index 096c07da83..912f2eaebe 100644 --- a/CRM/Contribute/Form/AbstractEditPayment.php +++ b/CRM/Contribute/Form/AbstractEditPayment.php @@ -633,4 +633,79 @@ LEFT JOIN civicrm_contribution on (civicrm_contribution.contact_id = civicrm_co } } + /** + * Begin post processing. + * + * This function aims to start to bring together common postProcessing functions. + * + * Eventually these are also shared with the front end forms & may need to be moved to where they can also + * access this function. + */ + protected function beginPostProcess() { + if (in_array('credit_card_exp_date', array_keys($this->_params))) { + $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params); + $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params); + } + + $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); + } + + + /** + * Add the billing address to the contact who paid. + * + * Note that this function works based on the presence or otherwise of billing fields & can be called regardless of + * whether they are 'expected' (due to assumptions about the payment processor type or the setting to collect billing + * for pay later. + */ + protected function processBillingAddress() { + $fields = array(); + + $fields['email-Primary'] = 1; + $this->_params['email-5'] = $this->_params['email-Primary'] = $this->_contributorEmail; + // now set the values for the billing location. + foreach (array_keys($this->_fields) as $name) { + $fields[$name] = 1; + } + + // also add location name to the array + $this->_params["address_name-{$this->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $this->_params) . ' ' . CRM_Utils_Array::value('billing_middle_name', $this->_params) . ' ' . CRM_Utils_Array::value('billing_last_name', $this->_params); + $this->_params["address_name-{$this->_bltID}"] = trim($this->_params["address_name-{$this->_bltID}"]); + + $fields["address_name-{$this->_bltID}"] = 1; + + //ensure we don't over-write the payer's email with the member's email + if ($this->_contributorContactID == $this->_contactID) { + $fields["email-{$this->_bltID}"] = 1; + } + + list($hasBillingField, $addressParams) = CRM_Contribute_BAO_Contribution::getPaymentProcessorReadyAddressParams($this->_params, $this->_bltID); + $nameFields = array('first_name', 'middle_name', 'last_name'); + + foreach ($nameFields as $name) { + $fields[$name] = 1; + if (array_key_exists("billing_$name", $this->_params)) { + $this->_params[$name] = $this->_params["billing_{$name}"]; + $this->_params['preserveDBName'] = TRUE; + } + } + + if ($hasBillingField) { + $addressParams = array_merge($this->_params, $addressParams); + //here we are setting up the billing contact - if different from the member they are already created + // but they will get billing details assigned + CRM_Contact_BAO_Contact::createProfileContact($addressParams, $fields, + $this->_contributorContactID, NULL, NULL, + CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID, 'contact_type') + ); + } + // Add additional parameters that the payment processors are used to receiving. + if (!empty($this->_params["billing_state_province_id-{$this->_bltID}"])) { + $this->_params["state_province-{$this->_bltID}"] = $this->_params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($this->_params["billing_state_province_id-{$this->_bltID}"]); + } + if (!empty($this->_params["billing_country_id-{$this->_bltID}"])) { + $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]); + } + } + } diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index 3ef4825c9f..a883db6a7c 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -1056,13 +1056,13 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP $submittedValues['source'] = ts('Submit Credit Card Payment by: %1', array(1 => $userSortName)); } - $params = $this->_params = $submittedValues; + $params = $submittedValues; + $this->_params = array_merge($this->_params, $submittedValues); // Mapping requiring documentation. $this->_params['payment_processor'] = $submittedValues['payment_processor_id']; $now = date('YmdHis'); - $fields = array(); // we need to retrieve email address if ($this->_context == 'standalone' && !empty($submittedValues['is_email_receipt'])) { @@ -1072,56 +1072,13 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP $this->assign('displayName', $this->userDisplayName); } - // Set email for primary location. - $fields['email-Primary'] = 1; - $params['email-Primary'] = $this->userEmail; - - // now set the values for the billing location. - foreach (array_keys($this->_fields) as $name) { - $fields[$name] = 1; - } - - // also add location name to the array - $params["address_name-{$this->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $params) . ' ' . CRM_Utils_Array::value('billing_middle_name', $params) . ' ' . CRM_Utils_Array::value('billing_last_name', $params); - - $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]); - $fields["address_name-{$this->_bltID}"] = 1; - - $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; - } - } - + $this->_contributorEmail = $this->userEmail; + $this->_contributorContactID = $contactID; + $this->processBillingAddress(); if (!empty($params['source'])) { unset($params['source']); } - CRM_Contact_BAO_Contact::createProfileContact($params, $fields, - $contactID, - NULL, NULL, - CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', - $contactID, - 'contact_type' - ) - ); - - // add all the additional payment params we need - if (!empty($this->_params["billing_state_province_id-{$this->_bltID}"])) { - $this->_params["state_province-{$this->_bltID}"] = $this->_params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($this->_params["billing_state_province_id-{$this->_bltID}"]); - } - if (!empty($this->_params["billing_country_id-{$this->_bltID}"])) { - $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]); - } - - if (in_array('credit_card_exp_date', array_keys($this->_params))) { - $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params); - $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params); - } - $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); $this->_params['amount'] = $this->_params['total_amount']; $this->_params['amount_level'] = 0; $this->_params['description'] = ts("Contribution submitted by a staff person using contributor's credit card"); @@ -1416,6 +1373,8 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP protected function submit($submittedValues, $action, $pledgePaymentID) { $softParams = $softIDs = array(); $pId = $contribution = $isRelatedId = FALSE; + $this->_params = $submittedValues; + $this->beginPostProcess(); if (!empty($submittedValues['price_set_id']) && $action & CRM_Core_Action::UPDATE) { $line = CRM_Price_BAO_LineItem::getLineItems($this->_id, 'contribution'); diff --git a/CRM/Member/Form.php b/CRM/Member/Form.php index 282b4a3a8b..8d7a2e1f33 100644 --- a/CRM/Member/Form.php +++ b/CRM/Member/Form.php @@ -436,53 +436,6 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment { return $formValues; } - /** - * Add the billing address to the contact who paid. - */ - protected function processBillingAddress() { - $fields = array(); - - // set email for primary location. - $fields['email-Primary'] = 1; - $this->_params['email-5'] = $this->_params['email-Primary'] = $this->_contributorEmail; - // now set the values for the billing location. - foreach (array_keys($this->_fields) as $name) { - $fields[$name] = 1; - } - - // also add location name to the array - $this->_params["address_name-{$this->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $this->_params) . ' ' . CRM_Utils_Array::value('billing_middle_name', $this->_params) . ' ' . CRM_Utils_Array::value('billing_last_name', $this->_params); - $this->_params["address_name-{$this->_bltID}"] = trim($this->_params["address_name-{$this->_bltID}"]); - - $fields["address_name-{$this->_bltID}"] = 1; - - //ensure we don't over-write the payer's email with the member's email - if ($this->_contributorContactID == $this->_contactID) { - $fields["email-{$this->_bltID}"] = 1; - } - - list($hasBillingField, $addressParams) = CRM_Contribute_BAO_Contribution::getPaymentProcessorReadyAddressParams($this->_params, $this->_bltID); - $nameFields = array('first_name', 'middle_name', 'last_name'); - - foreach ($nameFields as $name) { - $fields[$name] = 1; - if (array_key_exists("billing_$name", $this->_params)) { - $this->_params[$name] = $this->_params["billing_{$name}"]; - $this->_params['preserveDBName'] = TRUE; - } - } - - if ($hasBillingField) { - $addressParams = array_merge($this->_params, $addressParams); - //here we are setting up the billing contact - if different from the member they are already created - // but they will get billing details assigned - CRM_Contact_BAO_Contact::createProfileContact($addressParams, $fields, - $this->_contributorContactID, NULL, NULL, - CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID, 'contact_type') - ); - } - } - /** * Assign billing name to the template. */ diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index 9cc6afff2e..3551e3bd74 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -370,19 +370,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { $this->assign('is_pay_later', TRUE); } if ($this->_mode) { - // set default country from config if no country set - $config = CRM_Core_Config::singleton(); - if (empty($defaults["billing_country_id-{$this->_bltID}"])) { - $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry; - } - - if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) { - $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince; - } - - $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID); - $defaults = array_merge($defaults, $billingDefaults); - + $defaults = $this->getBillingDefaults($defaults); // hack to simplify credit card entry for testing // $defaults['credit_card_type'] = 'Visa'; // $defaults['credit_card_number'] = '4807731747657838'; diff --git a/CRM/Member/Form/MembershipRenewal.php b/CRM/Member/Form/MembershipRenewal.php index 178dd1df61..6c12749aff 100644 --- a/CRM/Member/Form/MembershipRenewal.php +++ b/CRM/Member/Form/MembershipRenewal.php @@ -232,19 +232,7 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { $this->assign('member_is_test', CRM_Utils_Array::value('member_is_test', $defaults)); if ($this->_mode) { - // set default country from config if no country set - $config = CRM_Core_Config::singleton(); - if (empty($defaults["billing_country_id-{$this->_bltID}"])) { - $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry; - } - - if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) { - $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince; - } - - $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID); - $defaults = array_merge($defaults, $billingDefaults); - + $defaults = $this->getBillingDefaults($defaults); } return $defaults; } @@ -743,4 +731,24 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { } } + /** + * @param $defaults + * + * @return array + */ + protected function getBillingDefaults($defaults) { + // set default country from config if no country set + $config = CRM_Core_Config::singleton(); + if (empty($defaults["billing_country_id-{$this->_bltID}"])) { + $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry; + } + + if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) { + $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince; + } + + $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID); + return array_merge($defaults, $billingDefaults); + } + } -- 2.25.1