3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
35 * This form records additional payments needed when event/contribution is partially paid.
37 class CRM_Contribute_Form_AdditionalPayment
extends CRM_Contribute_Form_AbstractEditPayment
{
38 public $_contributeMode = 'direct';
41 * Related component whose financial payment is being processed.
45 protected $_component = NULL;
48 * Id of the component entity
52 protected $_owed = NULL;
54 protected $_refund = NULL;
57 * @deprecated - use parent $this->contactID
61 protected $_contactId = NULL;
63 protected $_contributorDisplayName = NULL;
65 protected $_contributorEmail = NULL;
67 protected $_toDoNotEmail = NULL;
69 protected $_paymentType = NULL;
71 protected $_contributionId = NULL;
73 protected $fromEmailId = NULL;
75 protected $_fromEmails = NULL;
77 protected $_view = NULL;
79 public $_action = NULL;
81 public function preProcess() {
84 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, TRUE);
85 // @todo don't set this - rely on parent $this->contactID
86 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, TRUE);
87 $this->_component
= CRM_Utils_Request
::retrieve('component', 'String', $this, TRUE);
88 $this->_view
= CRM_Utils_Request
::retrieve('view', 'String', $this, FALSE);
89 $this->assign('component', $this->_component
);
90 $this->assign('id', $this->_id
);
91 $this->assign('suppressPaymentFormButtons', $this->isBeingCalledFromSelectorContext());
93 if ($this->_view
== 'transaction' && ($this->_action
& CRM_Core_Action
::BROWSE
)) {
94 $paymentInfo = CRM_Contribute_BAO_Contribution
::getPaymentInfo($this->_id
, $this->_component
, TRUE);
95 $title = ts('View Payment');
96 if ($this->_component
== 'event') {
97 $info = CRM_Event_BAO_Participant
::participantDetails($this->_id
);
98 $title .= " - {$info['title']}";
100 CRM_Utils_System
::setTitle($title);
101 $this->assign('transaction', TRUE);
102 $this->assign('payments', $paymentInfo['transaction']);
105 $this->_fromEmails
= CRM_Core_BAO_Email
::getFromEmail();
107 $entityType = 'contribution';
108 if ($this->_component
== 'event') {
109 $entityType = 'participant';
110 $this->_contributionId
= CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_ParticipantPayment', $this->_id
, 'contribution_id', 'participant_id');
111 $eventId = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Participant', $this->_id
, 'event_id', 'id');
112 $this->_fromEmails
= CRM_Event_BAO_Event
::getFromEmailIds($eventId);
115 $this->_contributionId
= $this->_id
;
116 $this->_fromEmails
['from_email_id'] = CRM_Core_BAO_Email
::getFromEmail();
119 $paymentInfo = CRM_Core_BAO_FinancialTrxn
::getPartialPaymentWithType($this->_id
, $entityType);
120 $paymentDetails = CRM_Contribute_BAO_Contribution
::getPaymentInfo($this->_id
, $this->_component
, FALSE, TRUE);
122 $this->_amtPaid
= $paymentDetails['paid'];
123 $this->_amtTotal
= $paymentDetails['total'];
125 if (!empty($paymentInfo['refund_due'])) {
126 $paymentAmt = $this->_refund
= $paymentInfo['refund_due'];
127 $this->_paymentType
= 'refund';
129 elseif (!empty($paymentInfo['amount_owed'])) {
130 $paymentAmt = $this->_owed
= $paymentInfo['amount_owed'];
131 $this->_paymentType
= 'owed';
134 CRM_Core_Error
::fatal(ts('No payment information found for this record'));
137 if (!empty($this->_mode
) && $this->_paymentType
== 'refund') {
138 CRM_Core_Error
::fatal(ts('Credit card payment is not for Refund payments use'));
141 list($this->_contributorDisplayName
, $this->_contributorEmail
) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactId
);
143 $this->assign('contributionMode', $this->_mode
);
144 $this->assign('contactId', $this->_contactId
);
145 $this->assign('paymentType', $this->_paymentType
);
146 $this->assign('paymentAmt', abs($paymentAmt));
148 $this->setPageTitle($this->_refund ?
ts('Refund') : ts('Payment'));
152 * Is this function being called from a datatable selector.
154 * If so we don't want to show the buttons.
156 protected function isBeingCalledFromSelectorContext() {
157 return CRM_Utils_Request
::retrieve('selector', 'Positive');
161 * This virtual function is used to set the default values of
162 * various form elements
167 * reference to the array of default values
172 public function setDefaultValues() {
173 if ($this->_view
== 'transaction' && ($this->_action
& CRM_Core_Action
::BROWSE
)) {
178 CRM_Core_Payment_Form
::setDefaultValues($this, $this->_contactId
);
179 $defaults = array_merge($defaults, $this->_defaults
);
182 if (empty($defaults['trxn_date'])) {
183 $defaults['trxn_date'] = date('Y-m-d H:i:s');
186 if ($this->_refund
) {
187 $defaults['total_amount'] = CRM_Utils_Money
::format(abs($this->_refund
), NULL, NULL, TRUE);
189 elseif ($this->_owed
) {
190 $defaults['total_amount'] = number_format($this->_owed
, 2);
193 // Set $newCredit variable in template to control whether link to credit card mode is included
194 $this->assign('newCredit', CRM_Core_Config
::isEnabledBackOfficeCreditCardPayments());
199 * Build the form object.
201 public function buildQuickForm() {
202 if ($this->_view
== 'transaction' && ($this->_action
& CRM_Core_Action
::BROWSE
)) {
203 $this->addButtons(array(
206 'name' => ts('Done'),
207 'spacing' => ' ',
215 CRM_Core_Payment_Form
::buildPaymentForm($this, $this->_paymentProcessor
, FALSE, TRUE, CRM_Utils_Request
::retrieve('payment_instrument_id', 'Integer'));
216 $this->add('select', 'payment_processor_id', ts('Payment Processor'), $this->_processors
, NULL);
218 $attributes = CRM_Core_DAO
::getAttribute('CRM_Financial_DAO_FinancialTrxn');
220 $label = ($this->_refund
) ?
ts('Refund Amount') : ts('Payment Amount');
221 $this->addMoney('total_amount',
224 $attributes['total_amount'],
225 TRUE, 'currency', NULL
228 //add receipt for offline contribution
229 $this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?'));
231 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails
['from_email_id']);
233 $this->add('textarea', 'receipt_text', ts('Confirmation Message'));
235 $dateLabel = ($this->_refund
) ?
ts('Refund Date') : ts('Date Received');
236 $this->addField('trxn_date', array('entity' => 'FinancialTrxn', 'label' => $dateLabel, 'context' => 'Contribution'), FALSE, FALSE);
238 if ($this->_contactId
&& $this->_id
) {
239 if ($this->_component
== 'event') {
240 $eventId = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Participant', $this->_id
, 'event_id', 'id');
241 $event = CRM_Event_BAO_Event
::getEvents(0, $eventId);
242 $this->assign('eventName', $event[$eventId]);
246 $this->assign('displayName', $this->_contributorDisplayName
);
247 $this->assign('component', $this->_component
);
248 $this->assign('email', $this->_contributorEmail
);
251 // render backoffice payment fields only on offline mode
253 $js = array('onclick' => "return verify( );");
255 $this->add('select', 'payment_instrument_id',
256 ts('Payment Method'),
257 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::paymentInstrument(),
259 array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);")
262 $this->add('text', 'check_number', ts('Check Number'), $attributes['financial_trxn_check_number']);
263 $this->add('text', 'trxn_id', ts('Transaction ID'), array('class' => 'twelve') +
$attributes['trxn_id']);
265 $this->add('text', 'fee_amount', ts('Fee Amount'),
266 $attributes['fee_amount']
268 $this->addRule('fee_amount', ts('Please enter a valid monetary value for Fee Amount.'), 'money');
270 $this->add('text', 'net_amount', ts('Net Amount'),
271 $attributes['net_amount']
273 $this->addRule('net_amount', ts('Please enter a valid monetary value for Net Amount.'), 'money');
276 $buttonName = $this->_refund ?
'Record Refund' : 'Record Payment';
277 $this->addButtons(array(
280 'name' => ts('%1', array(1 => $buttonName)),
286 'name' => ts('Cancel'),
290 $mailingInfo = Civi
::settings()->get('mailing_backend');
291 $this->assign('outBound_option', $mailingInfo['outBound_option']);
293 $this->addFormRule(array('CRM_Contribute_Form_AdditionalPayment', 'formRule'), $this);
303 public static function formRule($fields, $files, $self) {
305 if ($self->_paymentType
== 'owed' && $fields['total_amount'] > $self->_owed
) {
306 $errors['total_amount'] = ts('Payment amount cannot be greater than owed amount');
308 if ($self->_paymentType
== 'refund' && $fields['total_amount'] != abs($self->_refund
)) {
309 $errors['total_amount'] = ts('Refund amount must equal refund due amount.');
311 $netAmt = $fields['total_amount'] - CRM_Utils_Array
::value('fee_amount', $fields, 0);
312 if (!empty($fields['net_amount']) && $netAmt != $fields['net_amount']) {
313 $errors['net_amount'] = ts('Net amount should be equal to the difference between payment amount and fee amount.');
315 if ($self->_paymentProcessor
['id'] === 0 && empty($fields['payment_instrument_id'])) {
316 $errors['payment_instrument_id'] = ts('Payment method is a required field');
323 * Process the form submission.
325 public function postProcess() {
326 $submittedValues = $this->controller
->exportValues($this->_name
);
327 $this->submit($submittedValues);
328 $childTab = 'contribute';
329 if ($this->_component
== 'event') {
330 $childTab = 'participant';
332 $session = CRM_Core_Session
::singleton();
333 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view',
334 "reset=1&cid={$this->_contactId}&selectedChild={$childTab}"
340 * @param array $submittedValues
343 public function submit($submittedValues) {
344 $this->_params
= $submittedValues;
345 $this->beginPostProcess();
346 $this->_contributorContactID
= $this->_contactID
;
347 $this->processBillingAddress();
348 $participantId = NULL;
349 if ($this->_component
== 'event') {
350 $participantId = $this->_id
;
352 $contributionStatuses = CRM_Core_PseudoConstant
::get('CRM_Contribute_DAO_Contribution',
353 'contribution_status_id',
354 array('labelColumn' => 'name')
356 $contributionStatusID = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_contributionId
, 'contribution_status_id');
357 if ($contributionStatuses[$contributionStatusID] == 'Pending') {
358 civicrm_api3('Contribution', 'create',
360 'id' => $this->_contributionId
,
361 'contribution_status_id' => array_search('Partially paid', $contributionStatuses),
368 // process credit card
369 $this->assign('contributeMode', 'direct');
370 $this->processCreditCard();
374 $contribution = civicrm_api3('Contribution', 'getsingle', array(
375 'return' => array("contribution_status_id"),
376 'id' => $this->_contributionId
,
378 $contributionStatusId = CRM_Utils_Array
::value('contribution_status_id', $contribution);
379 $result = CRM_Contribute_BAO_Contribution
::recordAdditionalPayment($this->_contributionId
, $this->_params
, $this->_paymentType
, $participantId);
380 // Fetch the contribution & do proportional line item assignment
381 $params = array('id' => $this->_contributionId
);
382 $contribution = CRM_Contribute_BAO_Contribution
::retrieve($params, $defaults, $params);
383 CRM_Contribute_BAO_Contribution
::addPayments(array($contribution), $contributionStatusId);
384 if ($this->_contributionId
&& CRM_Core_Permission
::access('CiviMember')) {
385 $membershipPaymentCount = civicrm_api3('MembershipPayment', 'getCount', array('contribution_id' => $this->_contributionId
));
386 if ($membershipPaymentCount) {
387 $this->ajaxResponse
['updateTabs']['#tab_member'] = CRM_Contact_BAO_Contact
::getCountComponent('membership', $this->_contactID
);
390 if ($this->_contributionId
&& CRM_Core_Permission
::access('CiviEvent')) {
391 $participantPaymentCount = civicrm_api3('ParticipantPayment', 'getCount', array('contribution_id' => $this->_contributionId
));
392 if ($participantPaymentCount) {
393 $this->ajaxResponse
['updateTabs']['#tab_participant'] = CRM_Contact_BAO_Contact
::getCountComponent('participant', $this->_contactID
);
397 $statusMsg = ts('The payment record has been processed.');
399 if (!empty($result) && !empty($this->_params
['is_email_receipt'])) {
400 $this->_params
['contact_id'] = $this->_contactId
;
401 $this->_params
['contribution_id'] = $this->_contributionId
;
402 // to get 'from email id' for send receipt
403 $this->fromEmailId
= $this->_params
['from_email_address'];
404 $sendReceipt = $this->emailReceipt($this->_params
);
406 $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
410 CRM_Core_Session
::setStatus($statusMsg, ts('Saved'), 'success');
413 public function processCreditCard() {
414 $config = CRM_Core_Config
::singleton();
415 $session = CRM_Core_Session
::singleton();
417 $now = date('YmdHis');
420 // we need to retrieve email address
421 if ($this->_context
== 'standalone' && !empty($this->_params
['is_email_receipt'])) {
422 list($this->userDisplayName
,
424 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactId
);
425 $this->assign('displayName', $this->userDisplayName
);
428 $this->formatParamsForPaymentProcessor($this->_params
);
430 $this->_params
['amount'] = $this->_params
['total_amount'];
431 // @todo - stop setting amount level in this function & call the CRM_Price_BAO_PriceSet::getAmountLevel
432 // function to get correct amount level consistently. Remove setting of the amount level in
433 // CRM_Price_BAO_PriceSet::processAmount. Extend the unit tests in CRM_Price_BAO_PriceSetTest
434 // to cover all variants.
435 $this->_params
['amount_level'] = 0;
436 $this->_params
['currencyID'] = CRM_Utils_Array
::value('currency',
438 $config->defaultCurrency
441 if (!empty($this->_params
['trxn_date'])) {
442 $this->_params
['receive_date'] = $this->_params
['trxn_date'];
445 if (empty($this->_params
['receive_date'])) {
446 $this->_params
['receive_date'] = date('YmdHis');
449 if (empty($this->_params
['invoice_id'])) {
450 $this->_params
['invoiceID'] = md5(uniqid(rand(), TRUE));
453 $this->_params
['invoiceID'] = $this->_params
['invoice_id'];
456 $this->assign('address', CRM_Utils_Address
::getFormattedBillingAddressFieldsFromParameters(
461 //Add common data to formatted params
462 $params = $this->_params
;
463 CRM_Contribute_Form_AdditionalInfo
::postProcessCommon($params, $this->_params
, $this);
464 // at this point we've created a contact and stored its address etc
465 // all the payment processors expect the name and address to be in the
466 // so we copy stuff over to first_name etc.
467 $paymentParams = $this->_params
;
468 $paymentParams['contactID'] = $this->_contactId
;
469 CRM_Core_Payment_Form
::mapParams($this->_bltID
, $this->_params
, $paymentParams, TRUE);
471 $paymentParams['contributionPageID'] = NULL;
472 if (!empty($this->_params
['is_email_receipt'])) {
473 $paymentParams['email'] = $this->_contributorEmail
;
474 $paymentParams['is_email_receipt'] = TRUE;
477 $paymentParams['is_email_receipt'] = $this->_params
['is_email_receipt'] = FALSE;
482 if ($paymentParams['amount'] > 0.0) {
484 // force a reget of the payment processor in case the form changed it, CRM-7179
485 $payment = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
486 $result = $payment->doPayment($paymentParams);
488 catch (\Civi\Payment\Exception\PaymentProcessorException
$e) {
489 Civi
::log()->error('Payment processor exception: ' . $e->getMessage());
490 $urlParams = "action=add&cid={$this->_contactId}&id={$this->_contributionId}&component={$this->_component}&mode={$this->_mode}";
491 CRM_Core_Error
::statusBounce(CRM_Utils_System
::url($e->getMessage(), 'civicrm/payment/add', $urlParams));
495 if (!empty($result)) {
496 $this->_params
= array_merge($this->_params
, $result);
499 if (empty($this->_params
['receive_date'])) {
500 $this->_params
['receive_date'] = $now;
503 $this->set('params', $this->_params
);
505 // set source if not set
506 if (empty($this->_params
['source'])) {
507 $userID = $session->get('userID');
508 $userSortName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $userID,
511 $this->_params
['source'] = ts('Submit Credit Card Payment by: %1', array(1 => $userSortName));
516 * Function to send email receipt.
518 * @param array $params
522 public function emailReceipt(&$params) {
523 // email receipt sending
524 // send message template
525 if ($this->_component
== 'event') {
527 // fetch event information from participant ID using API
528 $eventId = civicrm_api3('Participant', 'getvalue', array(
529 'return' => "event_id",
532 $event = civicrm_api3('Event', 'getsingle', array('id' => $eventId));
534 $this->assign('event', $event);
535 $this->assign('isShowLocation', $event['is_show_location']);
536 if (CRM_Utils_Array
::value('is_show_location', $event) == 1) {
537 $locationParams = array(
538 'entity_id' => $eventId,
539 'entity_table' => 'civicrm_event',
541 $location = CRM_Core_BAO_Location
::getValues($locationParams, TRUE);
542 $this->assign('location', $location);
546 // assign payment info here
547 $paymentConfig['confirm_email_text'] = CRM_Utils_Array
::value('confirm_email_text', $params);
548 $this->assign('paymentConfig', $paymentConfig);
550 $this->assign('totalAmount', $this->_amtTotal
);
552 $isRefund = ($this->_paymentType
== 'refund') ?
TRUE : FALSE;
553 $this->assign('isRefund', $isRefund);
555 $this->assign('totalPaid', $this->_amtPaid
);
556 $this->assign('refundAmount', $params['total_amount']);
559 $balance = $this->_amtTotal
- ($this->_amtPaid +
$params['total_amount']);
560 $paymentsComplete = ($balance == 0) ?
1 : 0;
561 $this->assign('amountOwed', $balance);
562 $this->assign('paymentAmount', $params['total_amount']);
563 $this->assign('paymentsComplete', $paymentsComplete);
565 $this->assign('contactDisplayName', $this->_contributorDisplayName
);
567 // assign trxn details
568 $this->assign('trxn_id', CRM_Utils_Array
::value('trxn_id', $params));
569 $this->assign('receive_date', CRM_Utils_Array
::value('trxn_date', $params));
570 $this->assign('paidBy', CRM_Core_PseudoConstant
::getLabel(
571 'CRM_Contribute_BAO_Contribution',
572 'payment_instrument_id',
573 $params['payment_instrument_id']
575 $this->assign('checkNumber', CRM_Utils_Array
::value('check_number', $params));
577 $sendTemplateParams = array(
578 'groupName' => 'msg_tpl_workflow_contribution',
579 'valueName' => 'payment_or_refund_notification',
580 'contactId' => $this->_contactId
,
581 'PDFFilename' => ts('notification') . '.pdf',
584 // try to send emails only if email id is present
585 // and the do-not-email option is not checked for that contact
586 if ($this->_contributorEmail
&& !$this->_toDoNotEmail
) {
587 if (array_key_exists($params['from_email_address'], $this->_fromEmails
['from_email_id'])) {
588 $receiptFrom = $params['from_email_address'];
591 $sendTemplateParams['from'] = $receiptFrom;
592 $sendTemplateParams['toName'] = $this->_contributorDisplayName
;
593 $sendTemplateParams['toEmail'] = $this->_contributorEmail
;
595 list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate
::sendTemplate($sendTemplateParams);
600 * Wrapper for unit testing the post process submit function.
602 * @param array $params
603 * @param string|null $creditCardMode
604 * @param string $entityType
606 * @throws \CiviCRM_API3_Exception
608 public function testSubmit($params, $creditCardMode = NULL, $entityType = 'contribute') {
610 // Required because processCreditCard calls set method on this.
611 $_SERVER['REQUEST_METHOD'] = 'GET';
612 $this->controller
= new CRM_Core_Controller();
614 $this->assignPaymentRelatedVariables();
616 if (!empty($params['contribution_id'])) {
617 $this->_contributionId
= $params['contribution_id'];
619 $paymentInfo = CRM_Core_BAO_FinancialTrxn
::getPartialPaymentWithType($this->_contributionId
, $entityType);
620 $paymentDetails = CRM_Contribute_BAO_Contribution
::getPaymentInfo($this->_contributionId
, $entityType, FALSE, TRUE);
622 $this->_amtPaid
= $paymentDetails['paid'];
623 $this->_amtTotal
= $paymentDetails['total'];
625 if (!empty($paymentInfo['refund_due'])) {
626 $this->_refund
= $paymentInfo['refund_due'];
627 $this->_paymentType
= 'refund';
629 elseif (!empty($paymentInfo['amount_owed'])) {
630 $this->_owed
= $paymentInfo['amount_owed'];
631 $this->_paymentType
= 'owed';
635 if (!empty($params['contact_id'])) {
636 $this->_contactId
= $params['contact_id'];
639 if ($creditCardMode) {
640 $this->_mode
= $creditCardMode;
643 $this->_fields
= array();
644 $this->set('cid', $this->_contactId
);
645 parent
::preProcess();
646 $this->submit($params);