Ejemplo n.º 1
0
 /**
  * Post form submission handling.
  *
  * This is also called from the test suite.
  *
  * @param int $contactID
  *
  * @return array
  */
 protected function processFormSubmission($contactID)
 {
     $isPayLater = $this->_params['is_pay_later'];
     if (!isset($this->_params['payment_processor_id'])) {
         // If there is no processor we are using the pay-later manual pseudo-processor.
         // (note it might make sense to make this a row in the processor table in the db).
         $this->_params['payment_processor_id'] = 0;
     }
     if (isset($this->_params['payment_processor_id']) && $this->_params['payment_processor_id'] == 0) {
         $this->_params['is_pay_later'] = $isPayLater = TRUE;
     }
     // add a description field at the very beginning
     $this->_params['description'] = ts('Online Contribution') . ': ' . ($this->_pcpInfo['title'] ? $this->_pcpInfo['title'] : $this->_values['title']);
     $this->_params['accountingCode'] = CRM_Utils_Array::value('accountingCode', $this->_values);
     // fix currency ID
     $this->_params['currencyID'] = CRM_Core_Config::singleton()->defaultCurrency;
     //carry payment processor id.
     if (CRM_Utils_Array::value('id', $this->_paymentProcessor)) {
         $this->_params['payment_processor_id'] = $this->_paymentProcessor['id'];
     }
     $premiumParams = $membershipParams = $params = $this->_params;
     if (!empty($params['image_URL'])) {
         CRM_Contact_BAO_Contact::processImageParams($params);
     }
     $fields = array('email-Primary' => 1);
     // get the add to groups
     $addToGroups = array();
     // now set the values for the billing location.
     foreach ($this->_fields as $name => $value) {
         $fields[$name] = 1;
         // get the add to groups for uf fields
         if (!empty($value['add_to_group_id'])) {
             $addToGroups[$value['add_to_group_id']] = $value['add_to_group_id'];
         }
     }
     $fields = $this->formatParamsForPaymentProcessor($fields);
     // billing email address
     $fields["email-{$this->_bltID}"] = 1;
     // if onbehalf-of-organization contribution, take out
     // 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->_values['onbehalf_profile_id'])) {
         $behalfOrganization = array();
         $orgFields = array('organization_name', 'organization_id', 'org_option');
         foreach ($orgFields as $fld) {
             if (array_key_exists($fld, $params)) {
                 $behalfOrganization[$fld] = $params[$fld];
                 unset($params[$fld]);
             }
         }
         if (is_array($params['onbehalf']) && !empty($params['onbehalf'])) {
             foreach ($params['onbehalf'] as $fld => $values) {
                 if (strstr($fld, 'custom_')) {
                     $behalfOrganization[$fld] = $values;
                 } elseif (!strstr($fld, '-')) {
                     if (in_array($fld, array('contribution_campaign_id', 'member_campaign_id'))) {
                         $fld = 'campaign_id';
                     } else {
                         $behalfOrganization[$fld] = $values;
                     }
                     $this->_params[$fld] = $values;
                 }
             }
         }
         if (array_key_exists('onbehalf_location', $params) && is_array($params['onbehalf_location'])) {
             foreach ($params['onbehalf_location'] as $block => $vals) {
                 //fix for custom data (of type checkbox, multi-select)
                 if (substr($block, 0, 7) == 'custom_') {
                     continue;
                 }
                 // fix the index of block elements
                 if (is_array($vals)) {
                     foreach ($vals as $key => $val) {
                         //dont adjust the index of address block as
                         //it's index is WRT to location type
                         $newKey = $block == 'address' ? $key : ++$key;
                         $behalfOrganization[$block][$newKey] = $val;
                     }
                 }
             }
             unset($params['onbehalf_location']);
         }
         if (!empty($params['onbehalf[image_URL]'])) {
             $behalfOrganization['image_URL'] = $params['onbehalf[image_URL]'];
         }
     }
     // check for profile double opt-in and get groups to be subscribed
     $subscribeGroupIds = CRM_Core_BAO_UFGroup::getDoubleOptInGroupIds($params, $contactID);
     // since we are directly adding contact to group lets unset it from mailing
     if (!empty($addToGroups)) {
         foreach ($addToGroups as $groupId) {
             if (isset($subscribeGroupIds[$groupId])) {
                 unset($subscribeGroupIds[$groupId]);
             }
         }
     }
     foreach ($addToGroups as $k) {
         if (array_key_exists($k, $subscribeGroupIds)) {
             unset($addToGroups[$k]);
         }
     }
     if (empty($contactID)) {
         $dupeParams = $params;
         if (!empty($dupeParams['onbehalf'])) {
             unset($dupeParams['onbehalf']);
         }
         if (!empty($dupeParams['honor'])) {
             unset($dupeParams['honor']);
         }
         $dedupeParams = CRM_Dedupe_Finder::formatParams($dupeParams, 'Individual');
         $dedupeParams['check_permission'] = FALSE;
         $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual');
         // if we find more than one contact, use the first one
         $contactID = CRM_Utils_Array::value(0, $ids);
         // Fetch default greeting id's if creating a contact
         if (!$contactID) {
             foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
                 if (!isset($params[$greeting])) {
                     $params[$greeting] = CRM_Contact_BAO_Contact_Utils::defaultGreeting('Individual', $greeting);
                 }
             }
         }
         $contactType = NULL;
     } else {
         $contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'contact_type');
     }
     $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $contactID, $addToGroups, NULL, $contactType, TRUE);
     // Make the contact ID associated with the contribution available at the Class level.
     // Also make available to the session.
     //@todo consider handling this in $this->getContactID();
     $this->set('contactID', $contactID);
     $this->_contactID = $contactID;
     //get email primary first if exist
     $subscriptionEmail = array('email' => CRM_Utils_Array::value('email-Primary', $params));
     if (!$subscriptionEmail['email']) {
         $subscriptionEmail['email'] = CRM_Utils_Array::value("email-{$this->_bltID}", $params);
     }
     // subscribing contact to groups
     if (!empty($subscribeGroupIds) && $subscriptionEmail['email']) {
         CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($subscribeGroupIds, $subscriptionEmail, $contactID);
     }
     // If onbehalf-of-organization contribution / signup, add organization
     // and it's location.
     if (isset($this->_values['onbehalf_profile_id']) && isset($behalfOrganization['organization_name'])) {
         $ufFields = array();
         foreach ($this->_fields['onbehalf'] as $name => $value) {
             $ufFields[$name] = 1;
         }
         self::processOnBehalfOrganization($behalfOrganization, $contactID, $this->_values, $this->_params, $ufFields);
     } elseif (!empty($this->_membershipContactID) && $contactID != $this->_membershipContactID) {
         // this is an onbehalf renew case for inherited membership. For e.g a permissioned member of household,
         // store current user id as related contact for later use for mailing / activity..
         $this->_values['related_contact'] = $contactID;
         $this->_params['related_contact'] = $contactID;
         // swap contact like we do for on-behalf-org case, so parent/primary membership is affected
         $contactID = $this->_membershipContactID;
     }
     // lets store the contactID in the session
     // for things like tell a friend
     $session = CRM_Core_Session::singleton();
     if (!$session->get('userID')) {
         $session->set('transaction.userID', $contactID);
     } else {
         $session->set('transaction.userID', NULL);
     }
     $this->_useForMember = $this->get('useForMember');
     // store the fact that this is a membership and membership type is selected
     if (!empty($membershipParams['selectMembership']) && $membershipParams['selectMembership'] != 'no_thanks' || $this->_useForMember) {
         if (!$this->_useForMember) {
             $this->assign('membership_assign', TRUE);
             $this->set('membershipTypeID', $this->_params['selectMembership']);
         }
         if ($this->_action & CRM_Core_Action::PREVIEW) {
             $membershipParams['is_test'] = 1;
         }
         if ($this->_params['is_pay_later']) {
             $membershipParams['is_pay_later'] = 1;
         }
         //inherit campaign from contribution page.
         if (!array_key_exists('campaign_id', $membershipParams)) {
             $membershipParams['campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->_values);
         }
         CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $membershipParams, TRUE);
         $this->doMembershipProcessing($contactID, $membershipParams, $premiumParams, $isPayLater);
     } else {
         // at this point we've created a contact and stored its address etc
         // all the payment processors expect the name and address to be in the
         // so we copy stuff over to first_name etc.
         $paymentParams = $this->_params;
         if (!empty($paymentParams['onbehalf']) && is_array($paymentParams['onbehalf'])) {
             foreach ($paymentParams['onbehalf'] as $key => $value) {
                 if (strstr($key, 'custom_')) {
                     $this->_params[$key] = $value;
                 }
             }
         }
         $result = CRM_Contribute_BAO_Contribution_Utils::processConfirm($this, $paymentParams, $contactID, $this->wrangleFinancialTypeID($this->_values['financial_type_id']), 'contribution', $this->_mode == 'test' ? 1 : 0, CRM_Utils_Array::value('is_recur', $paymentParams));
         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;
     }
 }
Ejemplo n.º 2
0
 /**
  * Process the form
  *
  * @return void
  * @access public
  */
 public function postProcess()
 {
     $config = CRM_Core_Config::singleton();
     $contactID = $this->getContactID();
     // add a description field at the very beginning
     $this->_params['description'] = ts('Online Contribution') . ': ' . ($this->_pcpInfo['title'] ? $this->_pcpInfo['title'] : $this->_values['title']);
     // also add accounting code
     $this->_params['accountingCode'] = CRM_Utils_Array::value('accountingCode', $this->_values);
     // fix currency ID
     $this->_params['currencyID'] = $config->defaultCurrency;
     $premiumParams = $membershipParams = $tempParams = $params = $this->_params;
     //carry payment processor id.
     if ($paymentProcessorId = CRM_Utils_Array::value('id', $this->_paymentProcessor)) {
         $this->_params['payment_processor_id'] = $paymentProcessorId;
         foreach (array('premiumParams', 'membershipParams', 'tempParams', 'params') as $p) {
             ${$p}['payment_processor_id'] = $paymentProcessorId;
         }
     }
     $fields = array();
     if (!empty($params['image_URL'])) {
         CRM_Contact_BAO_Contact::processImageParams($params);
     }
     // set email for primary location.
     $fields['email-Primary'] = 1;
     // get the add to groups
     $addToGroups = array();
     // now set the values for the billing location.
     foreach ($this->_fields as $name => $value) {
         $fields[$name] = 1;
         // get the add to groups for uf fields
         if (!empty($value['add_to_group_id'])) {
             $addToGroups[$value['add_to_group_id']] = $value['add_to_group_id'];
         }
     }
     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;
             }
         }
     }
     // billing email address
     $fields["email-{$this->_bltID}"] = 1;
     //unset the billing parameters if it is pay later mode
     //to avoid creation of billing location
     if ($params['is_pay_later']) {
         $billingFields = array('billing_first_name', 'billing_middle_name', 'billing_last_name', "billing_street_address-{$this->_bltID}", "billing_city-{$this->_bltID}", "billing_state_province-{$this->_bltID}", "billing_state_province_id-{$this->_bltID}", "billing_postal_code-{$this->_bltID}", "billing_country-{$this->_bltID}", "billing_country_id-{$this->_bltID}");
         foreach ($billingFields as $value) {
             unset($params[$value]);
             unset($fields[$value]);
         }
     }
     // if onbehalf-of-organization contribution, take out
     // 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'])) {
         $behalfOrganization = array();
         $orgFields = array('organization_name', 'organization_id', 'org_option');
         foreach ($orgFields as $fld) {
             if (array_key_exists($fld, $params)) {
                 $behalfOrganization[$fld] = $params[$fld];
                 unset($params[$fld]);
             }
         }
         if (is_array($params['onbehalf']) && !empty($params['onbehalf'])) {
             foreach ($params['onbehalf'] as $fld => $values) {
                 if (strstr($fld, 'custom_')) {
                     $behalfOrganization[$fld] = $values;
                 } elseif (!strstr($fld, '-')) {
                     if (in_array($fld, array('contribution_campaign_id', 'member_campaign_id'))) {
                         $fld = 'campaign_id';
                     } else {
                         $behalfOrganization[$fld] = $values;
                     }
                     $this->_params[$fld] = $values;
                 }
             }
         }
         if (array_key_exists('onbehalf_location', $params) && is_array($params['onbehalf_location'])) {
             foreach ($params['onbehalf_location'] as $block => $vals) {
                 //fix for custom data (of type checkbox, multi-select)
                 if (substr($block, 0, 7) == 'custom_') {
                     continue;
                 }
                 // fix the index of block elements
                 if (is_array($vals)) {
                     foreach ($vals as $key => $val) {
                         //dont adjust the index of address block as
                         //it's index is WRT to location type
                         $newKey = $block == 'address' ? $key : ++$key;
                         $behalfOrganization[$block][$newKey] = $val;
                     }
                 }
             }
             unset($params['onbehalf_location']);
         }
         if (!empty($params['onbehalf[image_URL]'])) {
             $behalfOrganization['image_URL'] = $params['onbehalf[image_URL]'];
         }
     }
     // check for profile double opt-in and get groups to be subscribed
     $subscribeGroupIds = CRM_Core_BAO_UFGroup::getDoubleOptInGroupIds($params, $contactID);
     // since we are directly adding contact to group lets unset it from mailing
     if (!empty($addToGroups)) {
         foreach ($addToGroups as $groupId) {
             if (isset($subscribeGroupIds[$groupId])) {
                 unset($subscribeGroupIds[$groupId]);
             }
         }
     }
     foreach ($addToGroups as $k) {
         if (array_key_exists($k, $subscribeGroupIds)) {
             unset($addToGroups[$k]);
         }
     }
     if (empty($contactID)) {
         $dupeParams = $params;
         if (!empty($dupeParams['onbehalf'])) {
             unset($dupeParams['onbehalf']);
         }
         $dedupeParams = CRM_Dedupe_Finder::formatParams($dupeParams, 'Individual');
         $dedupeParams['check_permission'] = FALSE;
         $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual');
         // if we find more than one contact, use the first one
         $contact_id = CRM_Utils_Array::value(0, $ids);
         // Fetch default greeting id's if creating a contact
         if (!$contact_id) {
             foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
                 if (!isset($params[$greeting])) {
                     $params[$greeting] = CRM_Contact_BAO_Contact_Utils::defaultGreeting('Individual', $greeting);
                 }
             }
         }
         $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $contact_id, $addToGroups, NULL, NULL, TRUE);
     } else {
         $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'contact_type');
         $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $contactID, $addToGroups, NULL, $ctype, TRUE);
     }
     // Make the contact ID associated with the contribution available at the Class level.
     // Also make available to the session.
     //@todo consider handling this in $this->getContactID();
     $this->set('contactID', $contactID);
     $this->_contactID = $contactID;
     //get email primary first if exist
     $subscribtionEmail = array('email' => CRM_Utils_Array::value('email-Primary', $params));
     if (!$subscribtionEmail['email']) {
         $subscribtionEmail['email'] = CRM_Utils_Array::value("email-{$this->_bltID}", $params);
     }
     // subscribing contact to groups
     if (!empty($subscribeGroupIds) && $subscribtionEmail['email']) {
         CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($subscribeGroupIds, $subscribtionEmail, $contactID);
     }
     // If onbehalf-of-organization contribution / signup, add organization
     // and it's location.
     if (isset($params['hidden_onbehalf_profile']) && isset($behalfOrganization['organization_name'])) {
         $ufFields = array();
         foreach ($this->_fields['onbehalf'] as $name => $value) {
             $ufFields[$name] = 1;
         }
         self::processOnBehalfOrganization($behalfOrganization, $contactID, $this->_values, $this->_params, $ufFields);
     } else {
         if (!empty($this->_membershipContactID) && $contactID != $this->_membershipContactID) {
             // this is an onbehalf renew case for inherited membership. For e.g a permissioned member of household,
             // store current user id as related contact for later use for mailing / activity..
             $this->_values['related_contact'] = $contactID;
             $this->_params['related_contact'] = $contactID;
             // swap contact like we do for on-behalf-org case, so parent/primary membership is affected
             $contactID = $this->_membershipContactID;
         }
     }
     // lets store the contactID in the session
     // for things like tell a friend
     $session = CRM_Core_Session::singleton();
     if (!$session->get('userID')) {
         $session->set('transaction.userID', $contactID);
     } else {
         $session->set('transaction.userID', NULL);
     }
     $this->_useForMember = $this->get('useForMember');
     // store the fact that this is a membership and membership type is selected
     $processMembership = FALSE;
     if (!empty($membershipParams['selectMembership']) && $membershipParams['selectMembership'] != 'no_thanks' || $this->_useForMember) {
         $processMembership = TRUE;
         if (!$this->_useForMember) {
             $this->assign('membership_assign', TRUE);
             $this->set('membershipTypeID', $this->_params['selectMembership']);
         }
         if ($this->_action & CRM_Core_Action::PREVIEW) {
             $membershipParams['is_test'] = 1;
         }
         if ($this->_params['is_pay_later']) {
             $membershipParams['is_pay_later'] = 1;
         }
     }
     if ($processMembership) {
         CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $membershipParams, TRUE);
         // added new parameter for cms user contact id, needed to distinguish behaviour for on behalf of sign-ups
         if (isset($this->_params['related_contact'])) {
             $membershipParams['cms_contactID'] = $this->_params['related_contact'];
         } else {
             $membershipParams['cms_contactID'] = $contactID;
         }
         //inherit campaign from contirb page.
         if (!array_key_exists('campaign_id', $membershipParams)) {
             $membershipParams['campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->_values);
         }
         if (!empty($membershipParams['onbehalf']) && is_array($membershipParams['onbehalf']) && !empty($membershipParams['onbehalf']['member_campaign_id'])) {
             $this->_params['campaign_id'] = $membershipParams['onbehalf']['member_campaign_id'];
         }
         $customFieldsFormatted = $fieldTypes = array();
         if (!empty($membershipParams['onbehalf']) && is_array($membershipParams['onbehalf'])) {
             foreach ($membershipParams['onbehalf'] as $key => $value) {
                 if (strstr($key, 'custom_')) {
                     $customFieldId = explode('_', $key);
                     CRM_Core_BAO_CustomField::formatCustomField($customFieldId[1], $customFieldsFormatted, $value, 'Membership', NULL, $contactID);
                 }
             }
             $fieldTypes = array('Contact', 'Organization', 'Membership');
         }
         $priceFieldIds = $this->get('memberPriceFieldIDS');
         if (!empty($priceFieldIds)) {
             $contributionTypeID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceFieldIds['id'], 'financial_type_id');
             unset($priceFieldIds['id']);
             $membershipTypeIds = array();
             $membershipTypeTerms = array();
             foreach ($priceFieldIds as $priceFieldId) {
                 if ($id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) {
                     $membershipTypeIds[] = $id;
                     $term = 1;
                     if ($term = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_num_terms')) {
                         $membershipTypeTerms[$id] = $term > 1 ? $term : 1;
                     } else {
                         $membershipTypeTerms[$id] = 1;
                     }
                 }
             }
             $membershipParams['selectMembership'] = $membershipTypeIds;
             $membershipParams['financial_type_id'] = $contributionTypeID;
             $membershipParams['types_terms'] = $membershipTypeTerms;
         }
         if (!empty($membershipParams['selectMembership'])) {
             // CRM-12233
             if ($this->_separateMembershipPayment && $this->_values['amount_block_is_active']) {
                 foreach ($this->_values['fee'] as $key => $feeValues) {
                     if ($feeValues['name'] == 'membership_amount') {
                         $fieldId = $this->_params['price_' . $key];
                         $this->_memLineItem[$this->_priceSetId][$fieldId] = $this->_lineItem[$this->_priceSetId][$fieldId];
                         unset($this->_lineItem[$this->_priceSetId][$fieldId]);
                         break;
                     }
                 }
             }
             CRM_Member_BAO_Membership::postProcessMembership($membershipParams, $contactID, $this, $premiumParams, $customFieldsFormatted, $fieldTypes);
         }
     } else {
         // at this point we've created a contact and stored its address etc
         // all the payment processors expect the name and address to be in the
         // so we copy stuff over to first_name etc.
         $paymentParams = $this->_params;
         $contributionTypeId = $this->_values['financial_type_id'];
         $fieldTypes = array();
         if (!empty($paymentParams['onbehalf']) && is_array($paymentParams['onbehalf'])) {
             foreach ($paymentParams['onbehalf'] as $key => $value) {
                 if (strstr($key, 'custom_')) {
                     $this->_params[$key] = $value;
                 }
             }
             $fieldTypes = array('Contact', 'Organization', 'Contribution');
         }
         CRM_Contribute_BAO_Contribution_Utils::processConfirm($this, $paymentParams, $premiumParams, $contactID, $contributionTypeId, 'contribution', $fieldTypes);
     }
 }
Ejemplo n.º 3
0
 /**
  * Process the Memberships
  *
  * @param array  $membershipParams array of membership fields
  * @param int    $contactID        contact id 
  * @param object $form             form object  
  *
  * @return void
  * @access public
  */
 public function postProcessMembership($membershipParams, $contactID, &$form, &$premiumParams)
 {
     $tempParams = $membershipParams;
     $paymentDone = false;
     $result = null;
     $isTest = CRM_Utils_Array::value('is_test', $membershipParams, false);
     $form->assign('membership_assign', true);
     $form->set('membershipTypeID', $membershipParams['selectMembership']);
     require_once 'CRM/Member/BAO/MembershipType.php';
     require_once 'CRM/Member/BAO/Membership.php';
     $membershipTypeID = $membershipParams['selectMembership'];
     $membershipDetails = self::buildMembershipTypeValues($form, $membershipTypeID);
     $form->assign('membership_name', $membershipDetails['name']);
     $minimumFee = CRM_Utils_Array::value('minimum_fee', $membershipDetails);
     $contributionTypeId = null;
     if ($form->_values['amount_block_is_active']) {
         $contributionTypeId = $form->_values['contribution_type_id'];
     } else {
         $paymentDone = true;
         $params['amount'] = $minimumFee;
         $contributionTypeId = $membershipDetails['contribution_type_id'];
     }
     //amount must be greater than zero for
     //adding contribution record  to contribution table.
     //this condition is arises when separate membership payment is
     //enable and contribution amount is not selected. fix for CRM-3010
     require_once 'CRM/Contribute/BAO/Contribution/Utils.php';
     if ($form->_amount > 0.0) {
         $result = CRM_Contribute_BAO_Contribution_Utils::processConfirm($form, $membershipParams, $premiumParams, $contactID, $contributionTypeId, 'membership');
     } else {
         // create the CMS contact here since we normally do this under processConfirm
         CRM_Contribute_BAO_Contribution_Utils::createCMSUser($membershipParams, $membershipParams['cms_contactID'], 'email-' . $form->_bltID);
     }
     $errors = array();
     if (is_a($result[1], 'CRM_Core_Error')) {
         $errors[1] = CRM_Core_Error::getMessages($result[1]);
     } else {
         $contribution[1] = $result[1];
     }
     $memBlockDetails = CRM_Member_BAO_Membership::getMembershipBlock($form->_id);
     if ($memBlockDetails['is_separate_payment'] && !$paymentDone) {
         require_once 'CRM/Contribute/DAO/ContributionType.php';
         $contributionType = new CRM_Contribute_DAO_ContributionType();
         $contributionType->id = $membershipDetails['contribution_type_id'];
         if (!$contributionType->find(true)) {
             CRM_Core_Error::fatal("Could not find a system table");
         }
         $tempParams['amount'] = $minimumFee;
         $invoiceID = md5(uniqid(rand(), true));
         $tempParams['invoiceID'] = $invoiceID;
         //we don't allow recurring membership.CRM-3781.
         if (CRM_Utils_Array::value('is_recur', $tempParams)) {
             $tempParams['is_recur'] = 0;
         }
         $result = null;
         if ($form->_values['is_monetary'] && !$form->_params['is_pay_later']) {
             require_once 'CRM/Core/Payment.php';
             $payment =& CRM_Core_Payment::singleton($form->_mode, $form->_paymentProcessor, $form);
             if ($form->_contributeMode == 'express') {
                 $result =& $payment->doExpressCheckout($tempParams);
             } else {
                 $result =& $payment->doDirectPayment($tempParams);
             }
         }
         if (is_a($result, 'CRM_Core_Error')) {
             $errors[2] = CRM_Core_Error::getMessages($result);
         } else {
             //assign receive date when separate membership payment
             //and contribution amount not selected.
             if ($form->_amount == 0) {
                 $now = date('YmdHis');
                 $form->_params['receive_date'] = $now;
                 $receiveDate = CRM_Utils_Date::mysqlToIso($now);
                 $form->set('params', $form->_params);
                 $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 dont need to create the user twice, so lets disable cms_create_account
             // irrespective of the value, CRM-2888
             $tempParams['cms_create_account'] = 0;
             $pending = $form->_params['is_pay_later'] ? true : false;
             //set this variable as we are not creating pledge for
             //separate membership payment contribution.
             //so for differentiating membership contributon from
             //main contribution.
             $form->_params['separate_membership_payment'] = 1;
             $contribution[2] = CRM_Contribute_Form_Contribution_Confirm::processContribution($form, $tempParams, $result, $contactID, $contributionType, false, $pending);
         }
     }
     $index = $memBlockDetails['is_separate_payment'] ? 2 : 1;
     if (!CRM_Utils_Array::value($index, $errors)) {
         $membership = self::renewMembership($contactID, $membershipTypeID, $isTest, $form, null, CRM_Utils_Array::value('cms_contactID', $membershipParams));
         if (isset($contribution[$index])) {
             //insert payment record
             require_once 'CRM/Member/DAO/MembershipPayment.php';
             $dao = new CRM_Member_DAO_MembershipPayment();
             $dao->membership_id = $membership->id;
             $dao->contribution_id = $contribution[$index]->id;
             //Fixed for avoiding duplicate entry error when user goes
             //back and forward during payment mode is notify
             if (!$dao->find(true)) {
                 CRM_Utils_Hook::pre('create', 'MembershipPayment', null, $dao);
                 $dao->save();
                 CRM_Utils_Hook::post('create', 'MembershipPayment', $dao->id, $dao);
             }
         }
     }
     require_once 'CRM/Core/BAO/CustomValueTable.php';
     CRM_Core_BAO_CustomValueTable::postProcess($form->_params, CRM_Core_DAO::$_nullArray, 'civicrm_membership', $membership->id, 'Membership');
     if (!empty($errors)) {
         foreach ($errors as $error) {
             if (is_string($error)) {
                 $message[] = $error;
             }
         }
         $message = ts("Payment Processor Error message") . ": " . implode('<br/>', $message);
         $session = CRM_Core_Session::singleton();
         $session->setStatus($message);
         CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Main_display=true&qfKey={$form->_params['qfKey']}"));
     }
     $form->_params['membershipID'] = $membership->id;
     if ($form->_contributeMode == 'notify') {
         if ($form->_values['is_monetary'] && $form->_amount > 0.0 && !$form->_params['is_pay_later']) {
             // this does not return
             require_once 'CRM/Core/Payment.php';
             $payment =& CRM_Core_Payment::singleton($form->_mode, $form->_paymentProcessor, $form);
             $payment->doTransferCheckout($form->_params, 'contribute');
         }
     }
     $form->_values['membership_id'] = $membership->id;
     if (isset($contribution[$index]->id)) {
         $form->_values['contribution_id'] = $contribution[$index]->id;
     }
     // Do not send an email if Recurring transaction is done via Direct Mode
     // Email will we sent when the IPN is received.
     if ($form->_params['is_recur'] && $form->_contributeMode == 'direct') {
         return true;
     }
     //finally send an email receipt
     require_once "CRM/Contribute/BAO/ContributionPage.php";
     CRM_Contribute_BAO_ContributionPage::sendMail($contactID, $form->_values, $isTest);
 }
Ejemplo n.º 4
0
 /**
  * Process the Memberships.
  *
  * @param array $membershipParams
  *   Array of membership fields.
  * @param int $contactID
  *   Contact id.
  * @param CRM_Contribute_Form_Contribution_Confirm $form
  *   Confirmation form object.
  *
  * @param array $premiumParams
  * @param null $customFieldsFormatted
  * @param null $includeFieldTypes
  *
  * @param array $membershipDetails
  *
  * @param array $membershipTypeIDs
  *
  * @param bool $isPaidMembership
  * @param array $membershipID
  *
  * @param $isProcessSeparateMembershipTransaction
  *
  * @param int $defaultContributionTypeID
  * @param array $membershipLineItems
  *   Line items specific to membership payment that is separate to contribution.
  * @param $isPayLater
  *
  * @throws \CRM_Core_Exception
  */
 public static function postProcessMembership($membershipParams, $contactID, &$form, $premiumParams, $customFieldsFormatted = NULL, $includeFieldTypes = NULL, $membershipDetails, $membershipTypeIDs, $isPaidMembership, $membershipID, $isProcessSeparateMembershipTransaction, $defaultContributionTypeID, $membershipLineItems, $isPayLater)
 {
     $result = $membershipContribution = NULL;
     $isTest = CRM_Utils_Array::value('is_test', $membershipParams, FALSE);
     $errors = $createdMemberships = array();
     //@todo move this into the calling function & pass in the correct financialTypeID
     if (isset($paymentParams['financial_type'])) {
         $financialTypeID = $paymentParams['financial_type'];
     } else {
         $financialTypeID = $defaultContributionTypeID;
     }
     if (CRM_Utils_Array::value('membership_source', $form->_params)) {
         $membershipParams['contribution_source'] = $form->_params['membership_source'];
     }
     if ($isPaidMembership) {
         $result = CRM_Contribute_BAO_Contribution_Utils::processConfirm($form, $membershipParams, $premiumParams, $contactID, $financialTypeID, 'membership', array(), $isTest, $isPayLater);
         if (is_a($result[1], 'CRM_Core_Error')) {
             $errors[1] = CRM_Core_Error::getMessages($result[1]);
         } elseif (!empty($result[1])) {
             // Save the contribution ID so that I can be used in email receipts
             // For example, if you need to generate a tax receipt for the donation only.
             $form->_values['contribution_other_id'] = $result[1]->id;
             //note that this will be over-written if we are using a separate membership transaction. Otherwise there is only one
             $membershipContribution = $result[1];
         }
     }
     if ($isProcessSeparateMembershipTransaction) {
         try {
             $lineItems = $form->_lineItem = $membershipLineItems;
             if (empty($form->_params['auto_renew']) && !empty($membershipParams['is_recur'])) {
                 unset($membershipParams['is_recur']);
             }
             $membershipContribution = self::processSecondaryFinancialTransaction($contactID, $form, $membershipParams, $isTest, $membershipLineItems, CRM_Utils_Array::value('minimum_fee', $membershipDetails, 0), CRM_Utils_Array::value('financial_type_id', $membershipDetails));
         } catch (CRM_Core_Exception $e) {
             $errors[2] = $e->getMessage();
             $membershipContribution = NULL;
         }
     }
     $membership = NULL;
     if (!empty($membershipContribution) && !is_a($membershipContribution, 'CRM_Core_Error')) {
         $membershipContributionID = $membershipContribution->id;
     }
     //@todo - why is this nested so deep? it seems like it could be just set on the calling function on the form layer
     if (isset($membershipParams['onbehalf']) && !empty($membershipParams['onbehalf']['member_campaign_id'])) {
         $form->_params['campaign_id'] = $membershipParams['onbehalf']['member_campaign_id'];
     }
     //@todo it should no longer be possible for it to get to this point & membership to not be an array
     if (is_array($membershipTypeIDs) && !empty($membershipContributionID)) {
         $typesTerms = CRM_Utils_Array::value('types_terms', $membershipParams, array());
         foreach ($membershipTypeIDs as $memType) {
             $numTerms = CRM_Utils_Array::value($memType, $typesTerms, 1);
             $createdMemberships[$memType] = self::createOrRenewMembership($membershipParams, $contactID, $customFieldsFormatted, $membershipID, $memType, $isTest, $numTerms, $membershipContribution, $form);
         }
         if ($form->_priceSetId && !empty($form->_useForMember) && !empty($form->_lineItem)) {
             foreach ($form->_lineItem[$form->_priceSetId] as &$priceFieldOp) {
                 if (!empty($priceFieldOp['membership_type_id']) && isset($createdMemberships[$priceFieldOp['membership_type_id']])) {
                     $membershipOb = $createdMemberships[$priceFieldOp['membership_type_id']];
                     $priceFieldOp['start_date'] = $membershipOb->start_date ? CRM_Utils_Date::customFormat($membershipOb->start_date, '%B %E%f, %Y') : '-';
                     $priceFieldOp['end_date'] = $membershipOb->end_date ? CRM_Utils_Date::customFormat($membershipOb->end_date, '%B %E%f, %Y') : '-';
                 } else {
                     $priceFieldOp['start_date'] = $priceFieldOp['end_date'] = 'N/A';
                 }
             }
             $form->_values['lineItem'] = $form->_lineItem;
             $form->assign('lineItem', $form->_lineItem);
         }
     }
     if (!empty($errors)) {
         $message = self::compileErrorMessage($errors);
         throw new CRM_Core_Exception($message);
     }
     $form->_params['createdMembershipIDs'] = array();
     // CRM-7851 - Moved after processing Payment Errors
     //@todo - the reasoning for this being here seems a little outdated
     foreach ($createdMemberships as $createdMembership) {
         CRM_Core_BAO_CustomValueTable::postProcess($form->_params, CRM_Core_DAO::$_nullArray, 'civicrm_membership', $createdMembership->id, 'Membership');
         $form->_params['createdMembershipIDs'][] = $createdMembership->id;
     }
     if (count($createdMemberships) == 1) {
         //presumably this is only relevant for exactly 1 membership
         $form->_params['membershipID'] = $createdMembership->id;
     }
     //CRM-15232: Check if membership is created and on the basis of it use
     //membership reciept template to send payment reciept
     if (count($createdMemberships)) {
         $form->_values['isMembership'] = TRUE;
     }
     if ($form->_contributeMode == 'notify') {
         if ($form->_values['is_monetary'] && $form->_amount > 0.0 && !$form->_params['is_pay_later']) {
             // call postProcess hook before leaving
             $form->postProcessHook();
             // this does not return
             $payment = CRM_Core_Payment::singleton($form->_mode, $form->_paymentProcessor, $form);
             $payment->doTransferCheckout($form->_params, 'contribute');
         }
     }
     if (isset($membershipContributionID)) {
         $form->_values['contribution_id'] = $membershipContributionID;
     }
     // Refer to CRM-16737. Payment processors 'should' return payment_status_id
     // to denote the outcome of the transaction.
     //
     // In 4.7 trxn_id will no longer denote the outcome & all processor transactions must return an array
     // containing payment_status_id.
     // In 4.6 support (such as there was) for other ways of denoting payment outcome is retained but the use
     // of payment_status_id is strongly encouraged.
     if (!empty($form->_params['is_recur']) && $form->_contributeMode == 'direct') {
         if (!empty($membershipContribution->trxn_id) && !isset($membershipContribution->payment_status_id) || !empty($membershipContribution->payment_status_id) && $membershipContribution->payment_status_id == 1) {
             try {
                 civicrm_api3('contribution', 'completetransaction', array('id' => $membershipContribution->id, 'trxn_id' => $membershipContribution->trxn_id));
             } catch (CiviCRM_API3_Exception $e) {
                 // if for any reason it is already completed this will fail - e.g extensions hacking around core not completing transactions prior to CRM-15296
                 // so let's be gentle here
                 CRM_Core_Error::debug_log_message('contribution ' . $membershipContribution->id . ' not completed with trxn_id ' . $membershipContribution->trxn_id . ' and message ' . $e->getMessage());
             }
         }
         // Do not send an email if Recurring transaction is done via Direct Mode
         // Email will we sent when the IPN is received.
         return;
     }
     //finally send an email receipt
     CRM_Contribute_BAO_ContributionPage::sendMail($contactID, $form->_values, $isTest, FALSE, $includeFieldTypes);
 }
Ejemplo n.º 5
0
 /**
  * Process the Memberships
  *
  * @param array  $membershipParams array of membership fields
  * @param int    $contactID        contact id
  * @param object $form             form object
  *
  * @return void
  * @access public
  */
 public static function postProcessMembership($membershipParams, $contactID, &$form, &$premiumParams, $customFieldsFormatted = NULL, $includeFieldTypes = NULL)
 {
     $tempParams = $membershipParams;
     $paymentDone = FALSE;
     $result = NULL;
     $isTest = CRM_Utils_Array::value('is_test', $membershipParams, FALSE);
     $form->assign('membership_assign', TRUE);
     $form->set('membershipTypeID', $membershipParams['selectMembership']);
     $singleMembershipTypeID = $membershipTypeID = $membershipParams['selectMembership'];
     if (is_array($membershipTypeID) && count($membershipTypeID) == 1) {
         $singleMembershipTypeID = $membershipTypeID[0];
     }
     $membershipDetails = self::buildMembershipTypeValues($form, $singleMembershipTypeID);
     $form->assign('membership_name', CRM_Utils_Array::value('name', $membershipDetails));
     $minimumFee = CRM_Utils_Array::value('minimum_fee', $membershipDetails);
     $contributionTypeId = NULL;
     if ($form->_values['amount_block_is_active']) {
         $contributionTypeId = $form->_values['financial_type_id'];
     } else {
         $paymentDone = TRUE;
         $params['amount'] = $minimumFee;
         $contributionTypeId = CRM_Utils_Array::value('financial_type_id', $membershipDetails);
         if (!$contributionTypeId) {
             $contributionTypeId = CRM_Utils_Array::value('financial_type_id', $membershipParams);
         }
     }
     //amount must be greater than zero for
     //adding contribution record  to contribution table.
     //this condition arises when separate membership payment is
     //enabled and contribution amount is not selected. fix for CRM-3010
     if ($form->_amount > 0.0 && $membershipParams['amount']) {
         $result = CRM_Contribute_BAO_Contribution_Utils::processConfirm($form, $membershipParams, $premiumParams, $contactID, $contributionTypeId, 'membership');
     } else {
         // we need to explicitly create a CMS user in case of free memberships
         // since the below has already been done under processConfirm for paid memberships
         CRM_Contribute_BAO_Contribution_Utils::createCMSUser($membershipParams, $membershipParams['cms_contactID'], 'email-' . $form->_bltID);
     }
     $errors = array();
     if (is_a($result[1], 'CRM_Core_Error')) {
         $errors[1] = CRM_Core_Error::getMessages($result[1]);
     } elseif (CRM_Utils_Array::value(1, $result)) {
         // Save the contribution ID so that I can be used in email receipts
         // For example, if you need to generate a tax receipt for the donation only.
         $form->_values['contribution_other_id'] = $result[1]->id;
         $contribution[1] = $result[1];
     }
     $memBlockDetails = CRM_Member_BAO_Membership::getMembershipBlock($form->_id);
     if (CRM_Utils_Array::value('is_separate_payment', $memBlockDetails) && !$paymentDone) {
         $form->_lineItem = $form->_memLineItem;
         $contributionType = new CRM_Financial_DAO_FinancialType();
         $contributionType->id = CRM_Utils_Array::value('financial_type_id', $membershipDetails);
         if (!$contributionType->find(TRUE)) {
             CRM_Core_Error::fatal(ts("Could not find a system table"));
         }
         $tempParams['amount'] = $minimumFee;
         $invoiceID = md5(uniqid(rand(), TRUE));
         $tempParams['invoiceID'] = $invoiceID;
         //we don't allow recurring membership.CRM-3781.
         if (CRM_Utils_Array::value('is_recur', $tempParams)) {
             $tempParams['is_recur'] = 0;
         }
         $result = NULL;
         if ($form->_values['is_monetary'] && !$form->_params['is_pay_later'] && $minimumFee > 0.0) {
             $payment = CRM_Core_Payment::singleton($form->_mode, $form->_paymentProcessor, $form);
             if ($form->_contributeMode == 'express') {
                 $result =& $payment->doExpressCheckout($tempParams);
             } else {
                 $result =& $payment->doDirectPayment($tempParams);
             }
         }
         if (is_a($result, 'CRM_Core_Error')) {
             $errors[2] = CRM_Core_Error::getMessages($result);
         } else {
             //assign receive date when separate membership payment
             //and contribution amount not selected.
             if ($form->_amount == 0) {
                 $now = date('YmdHis');
                 $form->_params['receive_date'] = $now;
                 $receiveDate = CRM_Utils_Date::mysqlToIso($now);
                 $form->set('params', $form->_params);
                 $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 dont need to create the user twice, so lets disable cms_create_account
             // irrespective of the value, CRM-2888
             $tempParams['cms_create_account'] = 0;
             $pending = $form->_params['is_pay_later'] ? CRM_Utils_Array::value('minimum_fee', $membershipDetails, 0) > 0.0 ? TRUE : FALSE : FALSE;
             //set this variable as we are not creating pledge for
             //separate membership payment contribution.
             //so for differentiating membership contributon from
             //main contribution.
             $form->_params['separate_membership_payment'] = 1;
             $contribution[2] = CRM_Contribute_Form_Contribution_Confirm::processContribution($form, $tempParams, $result, $contactID, $contributionType, TRUE, $pending);
         }
     }
     $index = CRM_Utils_Array::value('is_separate_payment', $memBlockDetails) ? 2 : 1;
     if (!CRM_Utils_Array::value($index, $errors)) {
         if (isset($membershipParams['onbehalf']) && CRM_Utils_Array::value('member_campaign_id', $membershipParams['onbehalf'])) {
             $form->_params['campaign_id'] = $membershipParams['onbehalf']['member_campaign_id'];
         }
         if (is_array($membershipTypeID)) {
             $typesTerms = CRM_Utils_Array::value('types_terms', $membershipParams, array());
             $createdMemberships = array();
             foreach ($membershipTypeID as $memType) {
                 $numTerms = CRM_Utils_Array::value($memType, $typesTerms, 1);
                 $membership = self::renewMembership($contactID, $memType, $isTest, $form, NULL, CRM_Utils_Array::value('cms_contactID', $membershipParams), $customFieldsFormatted, CRM_Utils_Array::value($memType, $typesTerms, 1));
                 $createdMemberships[$memType] = $membership;
                 if (isset($contribution[$index])) {
                     //insert payment record
                     $dao = new CRM_Member_DAO_MembershipPayment();
                     $dao->membership_id = $membership->id;
                     $dao->contribution_id = $contribution[$index]->id;
                     //Fixed for avoiding duplicate entry error when user goes
                     //back and forward during payment mode is notify
                     if (!$dao->find(TRUE)) {
                         CRM_Utils_Hook::pre('create', 'MembershipPayment', NULL, $dao);
                         $dao->save();
                         CRM_Utils_Hook::post('create', 'MembershipPayment', $dao->id, $dao);
                     }
                 }
             }
             if ($form->_priceSetId && !empty($form->_useForMember) && !empty($form->_lineItem)) {
                 foreach ($form->_lineItem[$form->_priceSetId] as &$priceFieldOp) {
                     if (CRM_Utils_Array::value('membership_type_id', $priceFieldOp) && isset($createdMemberships[$priceFieldOp['membership_type_id']])) {
                         $membershipOb = $createdMemberships[$priceFieldOp['membership_type_id']];
                         $priceFieldOp['start_date'] = $membershipOb->start_date ? CRM_Utils_Date::customFormat($membershipOb->start_date, '%d%f %b, %Y') : '-';
                         $priceFieldOp['end_date'] = $membershipOb->end_date ? CRM_Utils_Date::customFormat($membershipOb->end_date, '%d%f %b, %Y') : '-';
                     } else {
                         $priceFieldOp['start_date'] = $priceFieldOp['end_date'] = 'N/A';
                     }
                 }
                 $form->_values['lineItem'] = $form->_lineItem;
                 $form->assign('lineItem', $form->_lineItem);
             }
         } else {
             $membership = self::renewMembership($contactID, $membershipTypeID, $isTest, $form, NULL, CRM_Utils_Array::value('cms_contactID', $membershipParams), $customFieldsFormatted, CRM_Utils_Array::value('types_terms', $membershipParams, 1), CRM_Utils_Array::value('contribution_status_id', $result) == 2 ? TRUE : FALSE);
             if (isset($contribution[$index])) {
                 //insert payment record
                 $dao = new CRM_Member_DAO_MembershipPayment();
                 $dao->membership_id = $membership->id;
                 $dao->contribution_id = $contribution[$index]->id;
                 //Fixed for avoiding duplicate entry error when user goes
                 //back and forward during payment mode is notify
                 if (!$dao->find(TRUE)) {
                     CRM_Utils_Hook::pre('create', 'MembershipPayment', NULL, $dao);
                     $dao->save();
                     CRM_Utils_Hook::post('create', 'MembershipPayment', $dao->id, $dao);
                 }
             }
         }
     }
     if (!empty($errors)) {
         foreach ($errors as $error) {
             if (is_string($error)) {
                 $message[] = $error;
             }
         }
         $message = ts('Payment Processor Error message') . ': ' . implode('<br/>', $message);
         throw new CRM_Core_Exception($message);
     }
     // CRM-7851
     CRM_Core_BAO_CustomValueTable::postProcess($form->_params, CRM_Core_DAO::$_nullArray, 'civicrm_membership', $membership->id, 'Membership');
     $form->_params['membershipID'] = $membership->id;
     if ($form->_contributeMode == 'notify') {
         if ($form->_values['is_monetary'] && $form->_amount > 0.0 && !$form->_params['is_pay_later']) {
             // call postprocess hook before leaving
             $form->postProcessHook();
             // this does not return
             $payment = CRM_Core_Payment::singleton($form->_mode, $form->_paymentProcessor, $form);
             $payment->doTransferCheckout($form->_params, 'contribute');
         }
     }
     $form->_values['membership_id'] = $membership->id;
     if (isset($contribution[$index]->id)) {
         $form->_values['contribution_id'] = $contribution[$index]->id;
     }
     // Do not send an email if Recurring transaction is done via Direct Mode
     // Email will we sent when the IPN is received.
     if (CRM_Utils_Array::value('is_recur', $form->_params) && $form->_contributeMode == 'direct') {
         return TRUE;
     }
     //finally send an email receipt
     CRM_Contribute_BAO_ContributionPage::sendMail($contactID, $form->_values, $isTest, FALSE, $includeFieldTypes);
 }
Ejemplo n.º 6
0
 /**
  * Process the form
  *
  * @return void
  * @access public
  */
 public function postProcess()
 {
     $config =& CRM_Core_Config::singleton();
     require_once "CRM/Contact/BAO/Contact.php";
     $contactID = $this->_userID;
     // add a description field at the very beginning
     $this->_params['description'] = ts('Online Contribution') . ': ' . ($this->_pcpInfo['title'] ? $this->_pcpInfo['title'] : $this->_values['title']);
     // also add accounting code
     $this->_params['accountingCode'] = CRM_Utils_Array::value('accountingCode', $this->_values);
     // fix currency ID
     $this->_params['currencyID'] = $config->defaultCurrency;
     $premiumParams = $membershipParams = $tempParams = $params = $this->_params;
     $now = date('YmdHis');
     $fields = array();
     // set email for primary location.
     $fields["email-Primary"] = 1;
     // don't create primary email address, just add it to billing location
     //$params["email-Primary"] = $params["email-{$this->_bltID}"];
     // get the add to groups
     $addToGroups = array();
     // now set the values for the billing location.
     foreach ($this->_fields as $name => $value) {
         $fields[$name] = 1;
         // get the add to groups for uf fields
         if (CRM_Utils_Array::value('add_to_group_id', $value)) {
             $addToGroups[$value['add_to_group_id']] = $value['add_to_group_id'];
         }
     }
     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;
             }
         }
     }
     // billing email address
     $fields["email-{$this->_bltID}"] = 1;
     //unset the billing parameters if it is pay later mode
     //to avoid creation of billing location
     if ($params['is_pay_later']) {
         $billingFields = array("billing_first_name", "billing_middle_name", "billing_last_name", "billing_street_address-{$this->_bltID}", "billing_city-{$this->_bltID}", "billing_state_province-{$this->_bltID}", "billing_state_province_id-{$this->_bltID}", "billing_postal_code-{$this->_bltID}", "billing_country-{$this->_bltID}", "billing_country_id-{$this->_bltID}");
         foreach ($billingFields as $value) {
             unset($params[$value]);
             unset($fields[$value]);
         }
     }
     // if onbehalf-of-organization contribution, take out
     // organization params in a separate variable, to make sure
     // normal behavior is continued. And use that variable to
     // process on-behalf-of functionality.
     if (CRM_Utils_Array::value('is_for_organization', $this->_values)) {
         $behalfOrganization = array();
         foreach (array('organization_name', 'organization_id', 'org_option') as $fld) {
             if (array_key_exists($fld, $params)) {
                 $behalfOrganization[$fld] = $params[$fld];
                 unset($params[$fld]);
             }
         }
         if (array_key_exists('onbehalf_location', $params) && is_array($params['onbehalf_location'])) {
             foreach ($params['onbehalf_location'] as $block => $vals) {
                 $behalfOrganization[$block] = $vals;
             }
             unset($params['onbehalf_location']);
         }
     }
     if (!isset($contactID)) {
         require_once 'CRM/Dedupe/Finder.php';
         $dedupeParams = CRM_Dedupe_Finder::formatParams($params, 'Individual');
         $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual');
         // if we find more than one contact, use the first one
         $contact_id = CRM_Utils_Array::value(0, $ids);
         $contactID =& CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $contact_id, $addToGroups);
         $this->set('contactID', $contactID);
     } else {
         $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'contact_type');
         $contactID =& CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $contactID, $addToGroups, null, $ctype);
     }
     // If onbehalf-of-organization contribution / signup, add organization
     // and it's location.
     if (isset($params['is_for_organization']) && isset($behalfOrganization['organization_name'])) {
         self::processOnBehalfOrganization($behalfOrganization, $contactID, $this->_values, $this->_params);
     }
     // lets store the contactID in the session
     // for things like tell a friend
     $session =& CRM_Core_Session::singleton();
     if (!$session->get('userID')) {
         $session->set('transaction.userID', $contactID);
     } else {
         $session->set('transaction.userID', null);
     }
     // store the fact that this is a membership and membership type is selected
     $processMembership = false;
     if (CRM_Utils_Array::value('selectMembership', $membershipParams) && $membershipParams['selectMembership'] != 'no_thanks') {
         $processMembership = true;
         $this->assign('membership_assign', true);
         $this->set('membershipTypeID', $this->_params['selectMembership']);
         if ($this->_action & CRM_Core_Action::PREVIEW) {
             $membershipParams['is_test'] = 1;
         }
         if ($this->_params['is_pay_later']) {
             $membershipParams['is_pay_later'] = 1;
         }
     }
     if ($processMembership) {
         require_once 'CRM/Core/Payment/Form.php';
         CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $membershipParams, true);
         // added new parameter for cms user contact id, needed to distinguish behaviour for on behalf of sign-ups
         if (isset($this->_params['related_contact'])) {
             $membershipParams['cms_contactID'] = $this->_params['related_contact'];
         } else {
             $membershipParams['cms_contactID'] = $contactID;
         }
         require_once 'CRM/Member/BAO/Membership.php';
         CRM_Member_BAO_Membership::postProcessMembership($membershipParams, $contactID, $this, $premiumParams);
     } else {
         // at this point we've created a contact and stored its address etc
         // all the payment processors expect the name and address to be in the
         // so we copy stuff over to first_name etc.
         $paymentParams = $this->_params;
         $contributionTypeId = $this->_values['contribution_type_id'];
         require_once "CRM/Contribute/BAO/Contribution/Utils.php";
         CRM_Contribute_BAO_Contribution_Utils::processConfirm($this, $paymentParams, $premiumParams, $contactID, $contributionTypeId, 'contribution');
     }
 }