| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * This form records additional payments needed when event/contribution is partially paid. |
| 36 | */ |
| 37 | class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_AbstractEditPayment { |
| 38 | public $_contributeMode = 'direct'; |
| 39 | |
| 40 | /** |
| 41 | * Related component whose financial payment is being processed. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | protected $_component = NULL; |
| 46 | |
| 47 | /** |
| 48 | * Id of the component entity |
| 49 | */ |
| 50 | public $_id = NULL; |
| 51 | |
| 52 | protected $_owed = NULL; |
| 53 | |
| 54 | protected $_refund = NULL; |
| 55 | |
| 56 | protected $_contactId = NULL; |
| 57 | |
| 58 | protected $_contributorDisplayName = NULL; |
| 59 | |
| 60 | protected $_contributorEmail = NULL; |
| 61 | |
| 62 | protected $_toDoNotEmail = NULL; |
| 63 | |
| 64 | protected $_paymentType = NULL; |
| 65 | |
| 66 | protected $_contributionId = NULL; |
| 67 | |
| 68 | protected $fromEmailId = NULL; |
| 69 | |
| 70 | protected $_fromEmails = NULL; |
| 71 | |
| 72 | protected $_view = NULL; |
| 73 | |
| 74 | public $_action = NULL; |
| 75 | |
| 76 | public function preProcess() { |
| 77 | |
| 78 | parent::preProcess(); |
| 79 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE); |
| 80 | $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE); |
| 81 | $this->_component = CRM_Utils_Request::retrieve('component', 'String', $this, TRUE); |
| 82 | $this->_view = CRM_Utils_Request::retrieve('view', 'String', $this, FALSE); |
| 83 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE); |
| 84 | $this->assign('component', $this->_component); |
| 85 | $this->assign('id', $this->_id); |
| 86 | $this->assign('suppressPaymentFormButtons', $this->isBeingCalledFromSelectorContext()); |
| 87 | |
| 88 | if ($this->_view == 'transaction' && ($this->_action & CRM_Core_Action::BROWSE)) { |
| 89 | $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_id, $this->_component, TRUE); |
| 90 | $transactionRows = $paymentInfo['transaction']; |
| 91 | $title = ts('View Payment'); |
| 92 | if ($this->_component == 'event') { |
| 93 | $info = CRM_Event_BAO_Participant::participantDetails($this->_id); |
| 94 | $title .= " - {$info['title']}"; |
| 95 | } |
| 96 | CRM_Utils_System::setTitle($title); |
| 97 | $this->assign('transaction', TRUE); |
| 98 | $this->assign('rows', $transactionRows); |
| 99 | return; |
| 100 | } |
| 101 | $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail(); |
| 102 | |
| 103 | $enitityType = NULL; |
| 104 | $enitityType = 'contribution'; |
| 105 | if ($this->_component == 'event') { |
| 106 | $enitityType = 'participant'; |
| 107 | $this->_contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id, 'contribution_id', 'participant_id'); |
| 108 | $eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'event_id', 'id'); |
| 109 | $this->_fromEmails = CRM_Event_BAO_Event::getFromEmailIds($eventId); |
| 110 | } |
| 111 | else { |
| 112 | $this->_contributionId = $this->_id; |
| 113 | $this->_fromEmails['from_email_id'] = CRM_Core_BAO_Email::getFromEmail(); |
| 114 | } |
| 115 | |
| 116 | $paymentInfo = CRM_Core_BAO_FinancialTrxn::getPartialPaymentWithType($this->_id, $enitityType); |
| 117 | $paymentDetails = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_id, $this->_component, FALSE, TRUE); |
| 118 | |
| 119 | $this->_amtPaid = $paymentDetails['paid']; |
| 120 | $this->_amtTotal = $paymentDetails['total']; |
| 121 | |
| 122 | if (!empty($paymentInfo['refund_due'])) { |
| 123 | $paymentAmt = $this->_refund = $paymentInfo['refund_due']; |
| 124 | $this->_paymentType = 'refund'; |
| 125 | } |
| 126 | elseif (!empty($paymentInfo['amount_owed'])) { |
| 127 | $paymentAmt = $this->_owed = $paymentInfo['amount_owed']; |
| 128 | $this->_paymentType = 'owed'; |
| 129 | } |
| 130 | else { |
| 131 | CRM_Core_Error::fatal(ts('No payment information found for this record')); |
| 132 | } |
| 133 | |
| 134 | if (!empty($this->_mode) && $this->_paymentType == 'refund') { |
| 135 | CRM_Core_Error::fatal(ts('Credit card payment is not for Refund payments use')); |
| 136 | } |
| 137 | |
| 138 | list($this->_contributorDisplayName, $this->_contributorEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactId); |
| 139 | |
| 140 | $this->assignPaymentRelatedVariables(); |
| 141 | |
| 142 | $this->assign('contributionMode', $this->_mode); |
| 143 | $this->assign('contactId', $this->_contactId); |
| 144 | $this->assign('paymentType', $this->_paymentType); |
| 145 | $this->assign('paymentAmt', abs($paymentAmt)); |
| 146 | |
| 147 | $this->setPageTitle($this->_refund ? ts('Refund') : ts('Payment')); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Is this function being called from a datatable selector. |
| 152 | * |
| 153 | * If so we don't want to show the buttons. |
| 154 | */ |
| 155 | protected function isBeingCalledFromSelectorContext() { |
| 156 | return CRM_Utils_Request::retrieve('selector', 'Positive'); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * This virtual function is used to set the default values of |
| 161 | * various form elements |
| 162 | * |
| 163 | * access public |
| 164 | * |
| 165 | * @return array |
| 166 | * reference to the array of default values |
| 167 | */ |
| 168 | /** |
| 169 | * @return array |
| 170 | */ |
| 171 | public function setDefaultValues() { |
| 172 | if ($this->_view == 'transaction' && ($this->_action & CRM_Core_Action::BROWSE)) { |
| 173 | return NULL; |
| 174 | } |
| 175 | $defaults = array(); |
| 176 | if ($this->_mode) { |
| 177 | CRM_Core_Payment_Form::setDefaultValues($this, $this->_contactId); |
| 178 | $defaults = array_merge($defaults, $this->_defaults); |
| 179 | } |
| 180 | |
| 181 | if (empty($defaults['trxn_date'])) { |
| 182 | $defaults['trxn_date'] = date('Y-m-d H:i:s'); |
| 183 | } |
| 184 | |
| 185 | if ($this->_refund) { |
| 186 | $defaults['total_amount'] = abs($this->_refund); |
| 187 | } |
| 188 | |
| 189 | // Set $newCredit variable in template to control whether link to credit card mode is included |
| 190 | $this->assign('newCredit', CRM_Core_Config::isEnabledBackOfficeCreditCardPayments()); |
| 191 | return $defaults; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Build the form object. |
| 196 | */ |
| 197 | public function buildQuickForm() { |
| 198 | if ($this->_view == 'transaction' && ($this->_action & CRM_Core_Action::BROWSE)) { |
| 199 | $this->addButtons(array( |
| 200 | array( |
| 201 | 'type' => 'cancel', |
| 202 | 'name' => ts('Done'), |
| 203 | 'spacing' => ' ', |
| 204 | 'isDefault' => TRUE, |
| 205 | ), |
| 206 | ) |
| 207 | ); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE, CRM_Utils_Request::retrieve('payment_instrument_id', 'Integer')); |
| 212 | $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialTrxn'); |
| 213 | |
| 214 | $this->add('select', 'payment_processor_id', ts('Payment Processor'), $this->_processors, NULL); |
| 215 | $label = ($this->_refund) ? ts('Refund Amount') : ts('Payment Amount'); |
| 216 | $this->addMoney('total_amount', |
| 217 | $label, |
| 218 | FALSE, |
| 219 | $attributes['total_amount'], |
| 220 | TRUE, 'currency', NULL |
| 221 | ); |
| 222 | |
| 223 | $this->add('select', 'payment_instrument_id', |
| 224 | ts('Payment Method'), |
| 225 | array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::paymentInstrument(), |
| 226 | TRUE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);") |
| 227 | ); |
| 228 | |
| 229 | $this->add('text', 'check_number', ts('Check Number'), $attributes['financial_trxn_check_number']); |
| 230 | $this->add('text', 'trxn_id', ts('Transaction ID'), array('class' => 'twelve') + $attributes['trxn_id']); |
| 231 | |
| 232 | //add receipt for offline contribution |
| 233 | $this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?')); |
| 234 | |
| 235 | $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails['from_email_id']); |
| 236 | |
| 237 | $this->add('textarea', 'receipt_text', ts('Confirmation Message')); |
| 238 | |
| 239 | $dateLabel = ($this->_refund) ? ts('Refund Date') : ts('Date Received'); |
| 240 | $this->addField('trxn_date', array('entity' => 'FinancialTrxn', 'label' => $dateLabel, 'context' => 'Contribution'), FALSE, FALSE); |
| 241 | |
| 242 | if ($this->_contactId && $this->_id) { |
| 243 | if ($this->_component == 'event') { |
| 244 | $eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'event_id', 'id'); |
| 245 | $event = CRM_Event_BAO_Event::getEvents(0, $eventId); |
| 246 | $this->assign('eventName', $event[$eventId]); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | $this->assign('displayName', $this->_contributorDisplayName); |
| 251 | $this->assign('component', $this->_component); |
| 252 | $this->assign('email', $this->_contributorEmail); |
| 253 | |
| 254 | $this->add('text', 'fee_amount', ts('Fee Amount'), |
| 255 | $attributes['fee_amount'] |
| 256 | ); |
| 257 | $this->addRule('fee_amount', ts('Please enter a valid monetary value for Fee Amount.'), 'money'); |
| 258 | |
| 259 | $this->add('text', 'net_amount', ts('Net Amount'), |
| 260 | $attributes['net_amount'] |
| 261 | ); |
| 262 | $this->addRule('net_amount', ts('Please enter a valid monetary value for Net Amount.'), 'money'); |
| 263 | |
| 264 | $js = NULL; |
| 265 | if (!$this->_mode) { |
| 266 | $js = array('onclick' => "return verify( );"); |
| 267 | } |
| 268 | |
| 269 | $buttonName = $this->_refund ? 'Record Refund' : 'Record Payment'; |
| 270 | $this->addButtons(array( |
| 271 | array( |
| 272 | 'type' => 'upload', |
| 273 | 'name' => ts('%1', array(1 => $buttonName)), |
| 274 | 'js' => $js, |
| 275 | 'isDefault' => TRUE, |
| 276 | ), |
| 277 | array( |
| 278 | 'type' => 'cancel', |
| 279 | 'name' => ts('Cancel'), |
| 280 | ), |
| 281 | ) |
| 282 | ); |
| 283 | $mailingInfo = Civi::settings()->get('mailing_backend'); |
| 284 | $this->assign('outBound_option', $mailingInfo['outBound_option']); |
| 285 | |
| 286 | $this->addFormRule(array('CRM_Contribute_Form_AdditionalPayment', 'formRule'), $this); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @param $fields |
| 291 | * @param $files |
| 292 | * @param $self |
| 293 | * |
| 294 | * @return array |
| 295 | */ |
| 296 | public static function formRule($fields, $files, $self) { |
| 297 | $errors = array(); |
| 298 | if ($self->_paymentType == 'owed' && $fields['total_amount'] > $self->_owed) { |
| 299 | $errors['total_amount'] = ts('Payment amount cannot be greater than owed amount'); |
| 300 | } |
| 301 | if ($self->_paymentType == 'refund' && $fields['total_amount'] != abs($self->_refund)) { |
| 302 | $errors['total_amount'] = ts('Refund amount must equal refund due amount.'); |
| 303 | } |
| 304 | $netAmt = $fields['total_amount'] - CRM_Utils_Array::value('fee_amount', $fields, 0); |
| 305 | if (!empty($fields['net_amount']) && $netAmt != $fields['net_amount']) { |
| 306 | $errors['net_amount'] = ts('Net amount should be equal to the difference between payment amount and fee amount.'); |
| 307 | } |
| 308 | return $errors; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Process the form submission. |
| 313 | */ |
| 314 | public function postProcess() { |
| 315 | $submittedValues = $this->controller->exportValues($this->_name); |
| 316 | $this->submit($submittedValues); |
| 317 | $childTab = 'contribute'; |
| 318 | if ($this->_component == 'event') { |
| 319 | $childTab = 'participant'; |
| 320 | } |
| 321 | $session = CRM_Core_Session::singleton(); |
| 322 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', |
| 323 | "reset=1&cid={$this->_contactId}&selectedChild={$childTab}" |
| 324 | )); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Process Payments. |
| 329 | * @param array $submittedValues |
| 330 | * |
| 331 | */ |
| 332 | public function submit($submittedValues) { |
| 333 | $this->_params = $submittedValues; |
| 334 | $this->beginPostProcess(); |
| 335 | $participantId = NULL; |
| 336 | if ($this->_component == 'event') { |
| 337 | $participantId = $this->_id; |
| 338 | } |
| 339 | $contributionStatuses = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', |
| 340 | 'contribution_status_id', |
| 341 | array('labelColumn' => 'name') |
| 342 | ); |
| 343 | $contributionStatusID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_contributionId, 'contribution_status_id'); |
| 344 | if ($contributionStatuses[$contributionStatusID] == 'Pending') { |
| 345 | civicrm_api3('Contribution', 'create', |
| 346 | array( |
| 347 | 'id' => $this->_contributionId, |
| 348 | 'contribution_status_id' => array_search('Partially paid', $contributionStatuses), |
| 349 | 'is_pay_later' => 0, |
| 350 | ) |
| 351 | ); |
| 352 | } |
| 353 | |
| 354 | if ($this->_mode) { |
| 355 | // process credit card |
| 356 | $this->assign('contributeMode', 'direct'); |
| 357 | $this->processCreditCard($submittedValues); |
| 358 | $submittedValues = $this->_params; |
| 359 | } |
| 360 | |
| 361 | $defaults = array(); |
| 362 | $contribution = civicrm_api3('Contribution', 'getsingle', array( |
| 363 | 'return' => array("contribution_status_id"), |
| 364 | 'id' => $this->_contributionId, |
| 365 | )); |
| 366 | $contributionStatusId = CRM_Utils_Array::value('contribution_status_id', $contribution); |
| 367 | $result = CRM_Contribute_BAO_Contribution::recordAdditionalPayment($this->_contributionId, $submittedValues, $this->_paymentType, $participantId); |
| 368 | // Fetch the contribution & do proportional line item assignment |
| 369 | $params = array('id' => $this->_contributionId); |
| 370 | $contribution = CRM_Contribute_BAO_Contribution::retrieve($params, $defaults, $params); |
| 371 | CRM_Contribute_BAO_Contribution::addPayments(array($contribution), $contributionStatusId); |
| 372 | |
| 373 | $statusMsg = ts('The payment record has been processed.'); |
| 374 | // send email |
| 375 | if (!empty($result) && !empty($submittedValues['is_email_receipt'])) { |
| 376 | $submittedValues['contact_id'] = $this->_contactId; |
| 377 | $submittedValues['contribution_id'] = $this->_contributionId; |
| 378 | // to get 'from email id' for send receipt |
| 379 | $this->fromEmailId = $submittedValues['from_email_address']; |
| 380 | $sendReceipt = $this->emailReceipt($submittedValues); |
| 381 | if ($sendReceipt) { |
| 382 | $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.'); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success'); |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * @param $submittedValues |
| 391 | */ |
| 392 | public function processCreditCard($submittedValues) { |
| 393 | $config = CRM_Core_Config::singleton(); |
| 394 | $session = CRM_Core_Session::singleton(); |
| 395 | |
| 396 | $unsetParams = array( |
| 397 | 'trxn_id', |
| 398 | 'payment_instrument_id', |
| 399 | 'contribution_status_id', |
| 400 | ); |
| 401 | foreach ($unsetParams as $key) { |
| 402 | if (isset($submittedValues[$key])) { |
| 403 | unset($submittedValues[$key]); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | // Get the required fields value only. |
| 408 | $params = $submittedValues; |
| 409 | |
| 410 | $now = date('YmdHis'); |
| 411 | $fields = array(); |
| 412 | |
| 413 | // we need to retrieve email address |
| 414 | if ($this->_context == 'standalone' && !empty($submittedValues['is_email_receipt'])) { |
| 415 | list($this->userDisplayName, |
| 416 | $this->userEmail |
| 417 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactId); |
| 418 | $this->assign('displayName', $this->userDisplayName); |
| 419 | } |
| 420 | |
| 421 | //set email for primary location. |
| 422 | $fields['email-Primary'] = 1; |
| 423 | $params['email-Primary'] = $this->_contributorEmail; |
| 424 | |
| 425 | // now set the values for the billing location. |
| 426 | foreach ($this->_fields as $name => $dontCare) { |
| 427 | $fields[$name] = 1; |
| 428 | } |
| 429 | |
| 430 | // also add location name to the array |
| 431 | $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); |
| 432 | $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]); |
| 433 | $fields["address_name-{$this->_bltID}"] = 1; |
| 434 | |
| 435 | $ctype = civicrm_api3('Contact', 'getvalue', array( |
| 436 | 'return' => "contact_type", |
| 437 | 'id' => $this->_contactId, |
| 438 | )); |
| 439 | |
| 440 | $nameFields = array('first_name', 'middle_name', 'last_name'); |
| 441 | foreach ($nameFields as $name) { |
| 442 | $fields[$name] = 1; |
| 443 | if (array_key_exists("billing_$name", $params)) { |
| 444 | $params[$name] = $params["billing_{$name}"]; |
| 445 | $params['preserveDBName'] = TRUE; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | if (!empty($params['source'])) { |
| 450 | unset($params['source']); |
| 451 | } |
| 452 | $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, |
| 453 | $this->_contactId, |
| 454 | NULL, NULL, |
| 455 | $ctype |
| 456 | ); |
| 457 | |
| 458 | // Add all the additional payment params we need. |
| 459 | $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}"]); |
| 460 | $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]); |
| 461 | |
| 462 | $this->_params['amount'] = $this->_params['total_amount']; |
| 463 | // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel |
| 464 | // function to get correct amount level consistently. Remove setting of the amount level in |
| 465 | // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest |
| 466 | // to cover all variants. |
| 467 | $this->_params['amount_level'] = 0; |
| 468 | $this->_params['currencyID'] = CRM_Utils_Array::value('currency', |
| 469 | $this->_params, |
| 470 | $config->defaultCurrency |
| 471 | ); |
| 472 | |
| 473 | if (!empty($this->_params['trxn_date'])) { |
| 474 | $this->_params['receive_date'] = $this->_params['trxn_date']; |
| 475 | } |
| 476 | |
| 477 | if (empty($this->_params['invoice_id'])) { |
| 478 | $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); |
| 479 | } |
| 480 | else { |
| 481 | $this->_params['invoiceID'] = $this->_params['invoice_id']; |
| 482 | } |
| 483 | |
| 484 | $this->assignBillingName($params); |
| 485 | $this->assign('address', CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters( |
| 486 | $params, |
| 487 | $this->_bltID |
| 488 | )); |
| 489 | |
| 490 | //Add common data to formatted params |
| 491 | CRM_Contribute_Form_AdditionalInfo::postProcessCommon($params, $this->_params, $this); |
| 492 | // at this point we've created a contact and stored its address etc |
| 493 | // all the payment processors expect the name and address to be in the |
| 494 | // so we copy stuff over to first_name etc. |
| 495 | $paymentParams = $this->_params; |
| 496 | $paymentParams['contactID'] = $this->_contactId; |
| 497 | CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE); |
| 498 | |
| 499 | $paymentParams['contributionPageID'] = NULL; |
| 500 | if (!empty($this->_params['is_email_receipt'])) { |
| 501 | $paymentParams['email'] = $this->_contributorEmail; |
| 502 | $paymentParams['is_email_receipt'] = 1; |
| 503 | } |
| 504 | else { |
| 505 | $paymentParams['is_email_receipt'] = 0; |
| 506 | $this->_params['is_email_receipt'] = 0; |
| 507 | } |
| 508 | if (!empty($this->_params['receive_date'])) { |
| 509 | $paymentParams['receive_date'] = $this->_params['receive_date']; |
| 510 | } |
| 511 | if (!empty($this->_params['receive_date'])) { |
| 512 | $paymentParams['receive_date'] = $this->_params['receive_date']; |
| 513 | } |
| 514 | |
| 515 | $result = NULL; |
| 516 | |
| 517 | if ($paymentParams['amount'] > 0.0) { |
| 518 | try { |
| 519 | // force a reget of the payment processor in case the form changed it, CRM-7179 |
| 520 | $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); |
| 521 | $result = $payment->doPayment($paymentParams); |
| 522 | } |
| 523 | catch (\Civi\Payment\Exception\PaymentProcessorException $e) { |
| 524 | //set the contribution mode. |
| 525 | $urlParams = "action=add&cid={$this->_contactId}&id={$this->_id}&component={$this->_component}"; |
| 526 | if ($this->_mode) { |
| 527 | $urlParams .= "&mode={$this->_mode}"; |
| 528 | } |
| 529 | CRM_Core_Error::displaySessionError($result); |
| 530 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/payment/add', $urlParams)); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | if ($result) { |
| 535 | $this->_params = array_merge($this->_params, $result); |
| 536 | } |
| 537 | |
| 538 | if (empty($this->_params['receive_date'])) { |
| 539 | $this->_params['receive_date'] = $now; |
| 540 | } |
| 541 | |
| 542 | $this->set('params', $this->_params); |
| 543 | |
| 544 | // set source if not set |
| 545 | if (empty($this->_params['source'])) { |
| 546 | $userID = $session->get('userID'); |
| 547 | $userSortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $userID, |
| 548 | 'sort_name' |
| 549 | ); |
| 550 | $this->_params['source'] = ts('Submit Credit Card Payment by: %1', array(1 => $userSortName)); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Function to send email receipt. |
| 556 | * |
| 557 | * @param array $params |
| 558 | * |
| 559 | * @return bool |
| 560 | */ |
| 561 | public function emailReceipt(&$params) { |
| 562 | // email receipt sending |
| 563 | // send message template |
| 564 | if ($this->_component == 'event') { |
| 565 | $eventId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'event_id', 'id'); |
| 566 | |
| 567 | $returnProperties = array('fee_label', 'start_date', 'end_date', 'is_show_location', 'title'); |
| 568 | CRM_Core_DAO::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $eventId, $events, $returnProperties); |
| 569 | $event = $events[$eventId]; |
| 570 | unset($event['start_date']); |
| 571 | unset($event['end_date']); |
| 572 | |
| 573 | $this->assign('event', $event); |
| 574 | $this->assign('isShowLocation', $event['is_show_location']); |
| 575 | if (CRM_Utils_Array::value('is_show_location', $event) == 1) { |
| 576 | $locationParams = array( |
| 577 | 'entity_id' => $eventId, |
| 578 | 'entity_table' => 'civicrm_event', |
| 579 | ); |
| 580 | $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE); |
| 581 | $this->assign('location', $location); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | // assign payment info here |
| 586 | $paymentConfig['confirm_email_text'] = CRM_Utils_Array::value('confirm_email_text', $params); |
| 587 | $this->assign('paymentConfig', $paymentConfig); |
| 588 | $isRefund = ($this->_paymentType == 'refund') ? TRUE : FALSE; |
| 589 | $this->assign('isRefund', $isRefund); |
| 590 | if ($isRefund) { |
| 591 | $this->assign('totalPaid', $this->_amtPaid); |
| 592 | $this->assign('totalAmount', $this->_amtTotal); |
| 593 | $this->assign('refundAmount', $params['total_amount']); |
| 594 | } |
| 595 | else { |
| 596 | $balance = $this->_amtTotal - ($this->_amtPaid + $params['total_amount']); |
| 597 | $paymentsComplete = ($balance == 0) ? 1 : 0; |
| 598 | $this->assign('amountOwed', $balance); |
| 599 | $this->assign('totalAmount', $this->_amtTotal); |
| 600 | $this->assign('paymentAmount', $params['total_amount']); |
| 601 | $this->assign('paymentsComplete', $paymentsComplete); |
| 602 | } |
| 603 | $this->assign('contactDisplayName', $this->_contributorDisplayName); |
| 604 | |
| 605 | // assign trxn details |
| 606 | $this->assign('trxn_id', CRM_Utils_Array::value('trxn_id', $params)); |
| 607 | $this->assign('receive_date', CRM_Utils_Array::value('trxn_date', $params)); |
| 608 | $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); |
| 609 | if (array_key_exists('payment_instrument_id', $params)) { |
| 610 | $this->assign('paidBy', |
| 611 | CRM_Utils_Array::value($params['payment_instrument_id'], |
| 612 | $paymentInstrument |
| 613 | ) |
| 614 | ); |
| 615 | } |
| 616 | $this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params)); |
| 617 | |
| 618 | $sendTemplateParams = array( |
| 619 | 'groupName' => 'msg_tpl_workflow_contribution', |
| 620 | 'valueName' => 'payment_or_refund_notification', |
| 621 | 'contactId' => $this->_contactId, |
| 622 | 'PDFFilename' => ts('notification') . '.pdf', |
| 623 | ); |
| 624 | |
| 625 | // try to send emails only if email id is present |
| 626 | // and the do-not-email option is not checked for that contact |
| 627 | if ($this->_contributorEmail && !$this->_toDoNotEmail) { |
| 628 | if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) { |
| 629 | $receiptFrom = $params['from_email_address']; |
| 630 | } |
| 631 | |
| 632 | $sendTemplateParams['from'] = $receiptFrom; |
| 633 | $sendTemplateParams['toName'] = $this->_contributorDisplayName; |
| 634 | $sendTemplateParams['toEmail'] = $this->_contributorEmail; |
| 635 | } |
| 636 | list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); |
| 637 | return $mailSent; |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Wrapper for unit testing the post process submit function. |
| 642 | * |
| 643 | * @param array $params |
| 644 | * @param string|null $creditCardMode |
| 645 | * @param string $enitityType |
| 646 | * |
| 647 | * @throws \CiviCRM_API3_Exception |
| 648 | */ |
| 649 | public function testSubmit($params, $creditCardMode = NULL, $enitityType = 'contribute') { |
| 650 | $this->_bltID = 5; |
| 651 | // Required because processCreditCard calls set method on this. |
| 652 | $_SERVER['REQUEST_METHOD'] = 'GET'; |
| 653 | $this->controller = new CRM_Core_Controller(); |
| 654 | |
| 655 | $this->assignPaymentRelatedVariables(); |
| 656 | if (!empty($params['contribution_id'])) { |
| 657 | $this->_contributionId = $params['contribution_id']; |
| 658 | |
| 659 | $paymentInfo = CRM_Core_BAO_FinancialTrxn::getPartialPaymentWithType($this->_contributionId, $enitityType); |
| 660 | $paymentDetails = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_contributionId, $enitityType, FALSE, TRUE); |
| 661 | |
| 662 | $this->_amtPaid = $paymentDetails['paid']; |
| 663 | $this->_amtTotal = $paymentDetails['total']; |
| 664 | |
| 665 | if (!empty($paymentInfo['refund_due'])) { |
| 666 | $this->_refund = $paymentInfo['refund_due']; |
| 667 | $this->_paymentType = 'refund'; |
| 668 | } |
| 669 | elseif (!empty($paymentInfo['amount_owed'])) { |
| 670 | $this->_owed = $paymentInfo['amount_owed']; |
| 671 | $this->_paymentType = 'owed'; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | if (!empty($params['contact_id'])) { |
| 676 | $this->_contactId = $params['contact_id']; |
| 677 | } |
| 678 | |
| 679 | if ($creditCardMode) { |
| 680 | $this->_mode = $creditCardMode; |
| 681 | } |
| 682 | |
| 683 | $this->_fields = array(); |
| 684 | $this->submit($params); |
| 685 | } |
| 686 | |
| 687 | } |