3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class holds functionality shared between various front end forms.
21 trait CRM_Financial_Form_FrontEndPaymentFormTrait
{
24 * The label for the pay later pseudoprocessor option.
28 protected $payLaterLabel;
33 public function getPayLaterLabel(): string {
34 if ($this->payLaterLabel
) {
35 return $this->payLaterLabel
;
37 return $this->get('payLaterLabel') ??
'';
41 * @param string $payLaterLabel
43 public function setPayLaterLabel(string $payLaterLabel) {
44 $this->set('payLaterLabel', $payLaterLabel);
45 $this->payLaterLabel
= $payLaterLabel;
49 * Alter line items for template.
51 * This is an early cut of what will ideally eventually be a hooklike call to the
52 * CRM_Invoicing_Utils class with a potential end goal of moving this handling to an extension.
54 * @param $tplLineItems
56 protected function alterLineItemsForTemplate(&$tplLineItems) {
57 if (!CRM_Invoicing_Utils
::isInvoicingEnabled()) {
60 // @todo this should really be the first time we are determining
61 // the tax rates - we can calculate them from the financial_type_id
62 // & amount here so we didn't need a deeper function to semi-get
63 // them but not be able to 'format them right' because they are
64 // potentially being used for 'something else'.
65 // @todo invoicing code - please feel the hate. Also move this 'hook-like-bit'
66 // to the CRM_Invoicing_Utils class.
67 foreach ($tplLineItems as $key => $value) {
68 foreach ($value as $k => $v) {
69 if (isset($v['tax_rate']) && $v['tax_rate'] != '') {
70 // These only need assigning once, but code is more readable with them here
71 $this->assign('getTaxDetails', TRUE);
72 $this->assign('taxTerm', CRM_Invoicing_Utils
::getTaxTerm());
73 // Cast to float to display without trailing zero decimals
74 $tplLineItems[$key][$k]['tax_rate'] = (float) $v['tax_rate'];
81 * Assign line items to the template.
83 * @param $tplLineItems
85 protected function assignLineItemsToTemplate($tplLineItems) {
86 // @todo this should be a hook that invoicing code hooks into rather than a call to it.
87 $this->alterLineItemsForTemplate($tplLineItems);
88 $this->assign('lineItem', $tplLineItems);
92 * Get the configured processors, including the pay later processor.
96 protected function getProcessors(): array {
98 if (!empty($this->_paymentProcessors
)) {
99 foreach ($this->_paymentProcessors
as $key => $processor) {
100 $pps[$key] = $this->getPaymentProcessorTitle($processor);
103 if ($this->getPayLaterLabel()) {
104 $pps[0] = $this->getPayLaterLabel();
110 * Get the title of the payment processor to display to the user
111 * Note: There is an identical function in CRM_Core_Payment
113 * @param array $processor
117 protected function getPaymentProcessorTitle($processor) {
118 return $processor['title'] ??
$processor['name'];
122 * Adds in either a set of radio buttons or hidden fields to contain the payment processors on a front end form
124 protected function addPaymentProcessorFieldsToForm() {
125 $paymentProcessors = $this->getProcessors();
127 foreach ($paymentProcessors as $ppKey => $ppval) {
129 $optAttributes[$ppKey]['class'] = 'payment_processor_' . strtolower($this->_paymentProcessors
[$ppKey]['payment_processor_type']);
132 $optAttributes[$ppKey]['class'] = 'payment_processor_paylater';
135 if (count($paymentProcessors) > 1) {
136 $this->addRadio('payment_processor_id', ts('Payment Method'), $paymentProcessors,
137 NULL, " ", FALSE, $optAttributes
140 elseif (!empty($paymentProcessors)) {
141 $ppKeys = array_keys($paymentProcessors);
142 $currentPP = array_pop($ppKeys);
143 $this->addElement('hidden', 'payment_processor_id', $currentPP);