/**
  * Function to process the form
  *
  * @access public
  *
  * @return None
  */
 public function postProcess()
 {
     $config = CRM_Core_Config::singleton();
     // we first reset the confirm page so it accepts new values
     $this->controller->resetPage('Confirm');
     // get the submitted form values.
     $params = $this->controller->exportValues($this->_name);
     if (CRM_Utils_Array::value('priceSetId', $params)) {
         $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Set', $this->_priceSetId, 'is_quick_config');
         $formValue = array();
         if ($is_quick_config) {
             $priceField = new CRM_Price_DAO_Field();
             $priceField->price_set_id = $params['priceSetId'];
             $priceField->orderBy('weight');
             $priceField->find();
             $check = array();
             $otherAmount = FALSE;
             while ($priceField->fetch()) {
                 CRM_Price_BAO_FieldValue::getValues($priceField->id, $values);
                 if ($priceField->name == "membership_amount") {
                     if ($priceFiledID = CRM_Utils_Array::value("price_{$priceField->id}", $params)) {
                         $this->_params['selectMembership'] = $params['selectMembership'] = CRM_Utils_Array::value('membership_type_id', $values[$priceFiledID]);
                         $this->set('selectMembership', CRM_Utils_Array::value('selectMembership', $params));
                         if (CRM_Utils_Array::value('is_separate_payment', $this->_membershipBlock) == 0) {
                             $this->_values['amount'] = CRM_Utils_Array::value('amount', $values[$priceFiledID]);
                         }
                     }
                 }
                 if ($priceField->name == 'contribution_amount') {
                     $priceFiledID = CRM_Utils_Array::value("price_{$priceField->id}", $params);
                     if ($priceFiledID > 0 && !empty($priceFiledID)) {
                         $params['amount'] = $priceFiledID;
                         $this->_values['amount'] = CRM_Utils_Array::value('amount', $values[$priceFiledID]);
                         $this->_values[$priceFiledID]['value'] = CRM_Utils_Array::value('amount', $values[$priceFiledID]);
                         $this->_values[$priceFiledID]['label'] = CRM_Utils_Array::value('label', $values[$priceFiledID]);
                         $this->_values[$priceFiledID]['amount_id'] = CRM_Utils_Array::value('id', $values[$priceFiledID]);
                         $this->_values[$priceFiledID]['weight'] = CRM_Utils_Array::value('weight', $values[$priceFiledID]);
                     }
                 }
                 if ($priceField->name == "other_amount" && ($priceFiledID = CRM_Utils_Array::value("price_{$priceField->id}", $params))) {
                     $params['amount_other'] = $priceFiledID;
                 }
             }
         }
     }
     if ($this->_values['is_pay_later'] && empty($this->_paymentProcessor) && !array_key_exists('hidden_processor', $params) || CRM_Utils_Array::value('payment_processor', $params) == 0) {
         $params['is_pay_later'] = 1;
     } else {
         $params['is_pay_later'] = 0;
     }
     $this->set('is_pay_later', $params['is_pay_later']);
     // assign pay later stuff
     $this->_params['is_pay_later'] = CRM_Utils_Array::value('is_pay_later', $params, FALSE);
     $this->assign('is_pay_later', $params['is_pay_later']);
     if ($params['is_pay_later']) {
         $this->assign('pay_later_text', $this->_values['pay_later_text']);
         $this->assign('pay_later_receipt', $this->_values['pay_later_receipt']);
     }
     //carry campaign from profile.
     if (array_key_exists('contribution_campaign_id', $params)) {
         $params['campaign_id'] = $params['contribution_campaign_id'];
     }
     if (CRM_Utils_Array::value('onbehalfof_id', $params)) {
         $params['organization_id'] = $params['onbehalfof_id'];
     }
     $params['currencyID'] = $config->defaultCurrency;
     $params['amount'] = self::computeAmount($params, $this);
     $params['separate_amount'] = $params['amount'];
     $memFee = NULL;
     if (CRM_Utils_Array::value('selectMembership', $params)) {
         if (!empty($this->_membershipTypeValues)) {
             $membershipTypeValues = $this->_membershipTypeValues[$params['selectMembership']];
         } else {
             $membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, $params['selectMembership']);
         }
         $memFee = $membershipTypeValues['minimum_fee'];
         if (!$params['amount'] && !$this->_separateMembershipPayment) {
             $params['amount'] = $memFee ? $memFee : 0;
         }
     }
     //If the membership & contribution is used in contribution page & not seperate payment
     $fieldId = $memPresent = $membershipLabel = $fieldOption = $proceFieldAmount = $is_quick_config = NULL;
     if ($this->_separateMembershipPayment == 0) {
         $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Set', $this->_priceSetId, 'is_quick_config');
         if ($is_quick_config) {
             foreach ($this->_priceSet['fields'] as $fieldKey => $fieldVal) {
                 if ($fieldVal['name'] == 'membership_amount') {
                     $fieldId = $fieldVal['id'];
                     $fieldOption = $params['price_' . $fieldId];
                     $memPresent = TRUE;
                 } else {
                     if (CRM_Utils_Array::value('price_' . $fieldKey, $params) && $memPresent && ($fieldVal['name'] == 'other_amount' || $fieldVal['name'] == 'contribution_amount')) {
                         $fieldId = $fieldVal['id'];
                         if ($fieldVal['name'] == 'other_amount') {
                             $proceFieldAmount = $this->_submitValues['price_' . $fieldId];
                         } else {
                             $proceFieldAmount = $fieldVal['options'][$this->_submitValues['price_' . $fieldId]]['amount'];
                         }
                         unset($params['price_' . $fieldId]);
                         break;
                     }
                 }
             }
         }
     }
     if (!isset($params['amount_other'])) {
         $this->set('amount_level', CRM_Utils_Array::value('amount_level', $params));
     }
     if ($priceSetId = CRM_Utils_Array::value('priceSetId', $params)) {
         $lineItem = array();
         $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Set', $priceSetId, 'is_quick_config');
         if ($is_quick_config) {
             foreach ($this->_values['fee'] as $key => &$val) {
                 if ($val['name'] == 'other_amount' && $val['html_type'] == 'Text' && array_key_exists('price_' . $key, $params) && $params['price_' . $key] != 0) {
                     foreach ($val['options'] as $optionKey => &$options) {
                         $options['amount'] = CRM_Utils_Array::value('price_' . $key, $params);
                         break;
                     }
                     $params['price_' . $key] = 1;
                     break;
                 }
             }
         }
         CRM_Price_BAO_Set::processAmount($this->_values['fee'], $params, $lineItem[$priceSetId]);
         if ($proceFieldAmount) {
             $lineItem[$params['priceSetId']][$fieldOption]['line_total'] = $proceFieldAmount;
             $lineItem[$params['priceSetId']][$fieldOption]['unit_price'] = $proceFieldAmount;
             if (!$this->_membershipBlock['is_separate_payment']) {
                 $params['amount'] = $proceFieldAmount;
                 //require when separate membership not used
             }
         }
         $this->set('lineItem', $lineItem);
     }
     if ($this->_membershipBlock['is_separate_payment'] && CRM_Utils_Array::value('separate_amount', $params)) {
         $this->set('amount', $params['separate_amount']);
     } else {
         $this->set('amount', $params['amount']);
     }
     // generate and set an invoiceID for this transaction
     $invoiceID = md5(uniqid(rand(), TRUE));
     $this->set('invoiceID', $invoiceID);
     // required only if is_monetary and valid postive amount
     if ($this->_values['is_monetary'] && is_array($this->_paymentProcessor) && ((double) $params['amount'] > 0.0 || $memFee > 0.0)) {
         // default mode is direct
         $this->set('contributeMode', 'direct');
         if ($this->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON) {
             //get the button name
             $buttonName = $this->controller->getButtonName();
             if (in_array($buttonName, array($this->_expressButtonName, $this->_expressButtonName . '_x', $this->_expressButtonName . '_y')) && !CRM_Utils_Array::value('is_pay_later', $params)) {
                 $this->set('contributeMode', 'express');
                 $donateURL = CRM_Utils_System::url('civicrm/contribute', '_qf_Contribute_display=1');
                 $params['cancelURL'] = CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Main_display=1&qfKey={$params['qfKey']}", TRUE, NULL, FALSE);
                 $params['returnURL'] = CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Confirm_display=1&rfp=1&qfKey={$params['qfKey']}", TRUE, NULL, FALSE);
                 $params['invoiceID'] = $invoiceID;
                 //default action is Sale
                 $params['payment_action'] = 'Sale';
                 $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this);
                 $token = $payment->setExpressCheckout($params);
                 if (is_a($token, 'CRM_Core_Error')) {
                     CRM_Core_Error::displaySessionError($token);
                     CRM_Utils_System::redirect($params['cancelURL']);
                 }
                 $this->set('token', $token);
                 $paymentURL = $this->_paymentProcessor['url_site'] . "/cgi-bin/webscr?cmd=_express-checkout&token={$token}";
                 CRM_Utils_System::redirect($paymentURL);
             }
         } elseif ($this->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_NOTIFY) {
             $this->set('contributeMode', 'notify');
         }
     }
     // should we skip the confirm page?
     if (!CRM_Utils_Array::value('is_confirm_enabled', $this->_values)) {
         // build the confirm page
         $confirmForm =& $this->controller->_pages['Confirm'];
         $confirmForm->preProcess();
         $confirmForm->buildQuickForm();
         // the confirmation page is valid
         $data =& $this->controller->container();
         $data['valid']['Confirm'] = 1;
         // confirm the contribution
         $confirmForm->postProcess();
         $qfKey = $this->controller->_key;
         // redirect to thank you page
         CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contribute/transact', "_qf_ThankYou_display=1&qfKey={$qfKey}", TRUE, NULL, FALSE));
     }
 }
 /**
  * Function to set variables up before form is built
  *
  * @return void
  * @access public
  */
 public function preProcess()
 {
     $config = CRM_Core_Config::singleton();
     parent::preProcess();
     // lineItem isn't set until Register postProcess
     $this->_lineItem = $this->get('lineItem');
     $this->_paymentProcessor = $this->get('paymentProcessor');
     if ($this->_contributeMode == 'express') {
         // rfp == redirect from paypal
         $rfp = CRM_Utils_Request::retrieve('rfp', 'Boolean', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'GET');
         if ($rfp) {
             $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this);
             $expressParams = $payment->getExpressCheckoutDetails($this->get('token'));
             $this->_params['payer'] = $expressParams['payer'];
             $this->_params['payer_id'] = $expressParams['payer_id'];
             $this->_params['payer_status'] = $expressParams['payer_status'];
             CRM_Core_Payment_Form::mapParams($this->_bltID, $expressParams, $this->_params, FALSE);
             // fix state and country id if present
             if (!empty($this->_params["billing_state_province_id-{$this->_bltID}"]) && $this->_params["billing_state_province_id-{$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["billing_country_id-{$this->_bltID}"]) {
                 $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]);
             }
             // set a few other parameters for PayPal
             $this->_params['token'] = $this->get('token');
             $this->_params['amount'] = $this->get('amount');
             if (!empty($this->_membershipBlock)) {
                 $this->_params['selectMembership'] = $this->get('selectMembership');
             }
             // we use this here to incorporate any changes made by folks in hooks
             $this->_params['currencyID'] = $config->defaultCurrency;
             $this->_params['payment_action'] = 'Sale';
             // also merge all the other values from the profile fields
             $values = $this->controller->exportValues('Main');
             $skipFields = array('amount', 'amount_other', "billing_street_address-{$this->_bltID}", "billing_city-{$this->_bltID}", "billing_state_province_id-{$this->_bltID}", "billing_postal_code-{$this->_bltID}", "billing_country_id-{$this->_bltID}");
             foreach ($values as $name => $value) {
                 // skip amount field
                 if (!in_array($name, $skipFields)) {
                     $this->_params[$name] = $value;
                 }
             }
             $this->set('getExpressCheckoutDetails', $this->_params);
         } else {
             $this->_params = $this->get('getExpressCheckoutDetails');
         }
     } else {
         $this->_params = $this->controller->exportValues('Main');
         if (!empty($this->_params["billing_state_province_id-{$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["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]);
         }
         if (isset($this->_params['credit_card_exp_date'])) {
             $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params);
             $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params);
         }
         $this->_params['ip_address'] = $_SERVER['REMOTE_ADDR'];
         // hack for safari
         if ($this->_params['ip_address'] == '::1') {
             $this->_params['ip_address'] = '127.0.0.1';
         }
         $this->_params['amount'] = $this->get('amount');
         $this->_useForMember = $this->get('useForMember');
         if (isset($this->_params['amount'])) {
             $priceField = new CRM_Price_DAO_Field();
             $priceField->price_set_id = $this->_params['priceSetId'];
             $priceField->orderBy('weight');
             $priceField->find();
             $contriPriceId = NULL;
             $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_Set', $this->_params['priceSetId'], 'is_quick_config');
             while ($priceField->fetch()) {
                 if ($priceField->name == "contribution_amount") {
                     $contriPriceId = $priceField->id;
                 }
                 if ($isQuickConfig && !empty($this->_params["price_{$priceField->id}"])) {
                     if ($this->_values['fee'][$priceField->id]['html_type'] != 'Text') {
                         $this->_params['amount_level'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_FieldValue', $this->_params["price_{$priceField->id}"], 'label');
                     }
                     if ($priceField->name == "membership_amount") {
                         $this->_params['selectMembership'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_FieldValue', $this->_params["price_{$priceField->id}"], 'membership_type_id');
                     }
                 } else {
                     if (CRM_Utils_Array::value('is_separate_payment', $this->_membershipBlock) && $this->_values['fee'][$priceField->id]['name'] == "other_amount" && CRM_Utils_Array::value("price_{$contriPriceId}", $this->_params) < 1 && !CRM_Utils_Array::value("price_{$priceField->id}", $this->_params)) {
                         $this->_params['amount'] = null;
                     }
                 }
             }
         }
         $this->_params['currencyID'] = $config->defaultCurrency;
         $this->_params['payment_action'] = 'Sale';
     }
     $this->_params['is_pay_later'] = $this->get('is_pay_later');
     $this->assign('is_pay_later', $this->_params['is_pay_later']);
     if ($this->_params['is_pay_later']) {
         $this->assign('pay_later_receipt', $this->_values['pay_later_receipt']);
     }
     // if onbehalf-of-organization
     if (CRM_Utils_Array::value('hidden_onbehalf_profile', $this->_params)) {
         if (CRM_Utils_Array::value('org_option', $this->_params) && CRM_Utils_Array::value('organization_id', $this->_params)) {
             if (CRM_Utils_Array::value('onbehalfof_id', $this->_params)) {
                 $this->_params['organization_id'] = $this->_params['onbehalfof_id'];
             }
         }
         $this->_params['organization_name'] = $this->_params['onbehalf']['organization_name'];
         $addressBlocks = array('street_address', 'city', 'state_province', 'postal_code', 'country', 'supplemental_address_1', 'supplemental_address_2', 'supplemental_address_3', 'postal_code_suffix', 'geo_code_1', 'geo_code_2', 'address_name');
         $blocks = array('email', 'phone', 'im', 'url', 'openid');
         foreach ($this->_params['onbehalf'] as $loc => $value) {
             $field = $typeId = NULL;
             if (strstr($loc, '-')) {
                 list($field, $locType) = explode('-', $loc);
             }
             if (in_array($field, $addressBlocks)) {
                 if ($locType == 'Primary') {
                     require_once 'CRM/Core/BAO/LocationType.php';
                     $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
                     $locType = $defaultLocationType->id;
                 }
                 if ($field == 'country') {
                     $value = CRM_Core_PseudoConstant::countryIsoCode($value);
                 } elseif ($field == 'state_province') {
                     $value = CRM_Core_PseudoConstant::stateProvinceAbbreviation($value);
                 }
                 $isPrimary = 1;
                 if (isset($this->_params['onbehalf_location']['address']) && count($this->_params['onbehalf_location']['address']) > 0) {
                     $isPrimary = 0;
                 }
                 $this->_params['onbehalf_location']['address'][$locType][$field] = $value;
                 if (!CRM_Utils_Array::value('is_primary', $this->_params['onbehalf_location']['address'][$locType])) {
                     $this->_params['onbehalf_location']['address'][$locType]['is_primary'] = $isPrimary;
                 }
                 $this->_params['onbehalf_location']['address'][$locType]['location_type_id'] = $locType;
             } elseif (in_array($field, $blocks)) {
                 if (!$typeId || is_numeric($typeId)) {
                     $blockName = $fieldName = $field;
                     $locationType = 'location_type_id';
                     if ($locType == 'Primary') {
                         $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
                         $locationValue = $defaultLocationType->id;
                     } else {
                         $locationValue = $locType;
                     }
                     $locTypeId = '';
                     if ($field == 'url') {
                         $blockName = 'website';
                         $locationType = 'website_type_id';
                         $locationValue = $this->_params['onbehalf']["{$loc}-website_type_id"];
                     } elseif ($field == 'im') {
                         $fieldName = 'name';
                         $locTypeId = 'provider_id';
                         $typeId = $this->_params['onbehalf']["{$loc}-provider_id"];
                     } elseif ($field == 'phone') {
                         list($field, $locType, $typeId) = explode('-', $loc);
                         $locTypeId = 'phone_type_id';
                     }
                     $isPrimary = 1;
                     if (isset($this->_params['onbehalf_location'][$blockName]) && count($this->_params['onbehalf_location'][$blockName]) > 0) {
                         $isPrimary = 0;
                     }
                     if (!$locTypeId) {
                         $this->_params['onbehalf_location'][$blockName][] = array($fieldName => $value, $locationType => $locationValue, 'is_primary' => $isPrimary);
                     } else {
                         $this->_params['onbehalf_location'][$blockName][] = array($fieldName => $value, $locationType => $locationValue, 'is_primary' => $isPrimary, $locTypeId => $typeId);
                     }
                 }
             } elseif (strstr($loc, 'custom')) {
                 if ($value && isset($this->_params['onbehalf']["{$loc}_id"])) {
                     $value = $this->_params['onbehalf']["{$loc}_id"];
                 }
                 $this->_params['onbehalf_location']["{$loc}"] = $value;
             } else {
                 if ($loc == 'contact_sub_type') {
                     $this->_params['onbehalf_location'][$loc] = $value;
                 } else {
                     $this->_params['onbehalf_location'][$field] = $value;
                 }
             }
         }
     } elseif (CRM_Utils_Array::value('is_for_organization', $this->_values)) {
         // no on behalf of an organization, CRM-5519
         // so reset loc blocks from main params.
         foreach (array('phone', 'email', 'address') as $blk) {
             if (isset($this->_params[$blk])) {
                 unset($this->_params[$blk]);
             }
         }
     }
     // if auto renew checkbox is set, initiate a open-ended recurring membership
     if ((CRM_Utils_Array::value('selectMembership', $this->_params) || CRM_Utils_Array::value('priceSetId', $this->_params)) && CRM_Utils_Array::value('is_recur', $this->_paymentProcessor) && CRM_Utils_Array::value('auto_renew', $this->_params) && !CRM_Utils_Array::value('is_recur', $this->_params) && !CRM_Utils_Array::value('frequency_interval', $this->_params)) {
         $this->_params['is_recur'] = $this->_values['is_recur'] = 1;
         // check if price set is not quick config
         if (CRM_Utils_Array::value('priceSetId', $this->_params) && !$isQuickConfig) {
             list($this->_params['frequency_interval'], $this->_params['frequency_unit']) = CRM_Price_BAO_Set::getRecurDetails($this->_params['priceSetId']);
         } else {
             // FIXME: set interval and unit based on selected membership type
             $this->_params['frequency_interval'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'duration_interval');
             $this->_params['frequency_unit'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_params['selectMembership'], 'duration_unit');
         }
     }
     if ($this->_pcpId) {
         $params = $this->processPcp($this, $this->_params);
         $this->_params = $params;
     }
     $this->_params['invoiceID'] = $this->get('invoiceID');
     //carry campaign from profile.
     if (array_key_exists('contribution_campaign_id', $this->_params)) {
         $this->_params['campaign_id'] = $this->_params['contribution_campaign_id'];
     }
     $this->set('params', $this->_params);
 }