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 * Is pay later enabled on this form?
28 protected $isPayLater = FALSE;
31 * The label for the pay later pseudoprocessor option.
35 protected $payLaterLabel;
38 * Is this a back office form
42 public $isBackOffice = FALSE;
45 * The payment mode that we are in ("live" or "test")
46 * This should be protected and retrieved via getPaymentMode() but it's accessed all over the place so we have to leave it public for now.
55 public function isPayLater() {
56 return $this->isPayLater
;
60 * @param bool $isPayLater
62 public function setIsPayLater($isPayLater) {
63 $this->isPayLater
= $isPayLater;
69 public function getIsBackOffice() {
70 return $this->isBackOffice
;
74 * Get the payment mode ('live' or 'test')
78 public function getPaymentMode() {
83 * Set the payment mode ('live' or 'test')
85 public function setPaymentMode() {
86 $this->_mode
= ($this->_action
=== CRM_Core_Action
::PREVIEW
) ?
'test' : 'live';
92 public function getPayLaterLabel(): string {
93 if ($this->payLaterLabel
) {
94 return $this->payLaterLabel
;
96 return $this->get('payLaterLabel') ??
'';
100 * @param string $payLaterLabel
102 public function setPayLaterLabel(string $payLaterLabel) {
103 $this->set('payLaterLabel', $payLaterLabel);
104 $this->payLaterLabel
= $payLaterLabel;
108 * Alter line items for template.
110 * This is an early cut of what will ideally eventually be a hooklike call to the
111 * CRM_Invoicing_Utils class with a potential end goal of moving this handling to an extension.
113 * @param $tplLineItems
115 protected function alterLineItemsForTemplate(&$tplLineItems) {
116 if (!CRM_Invoicing_Utils
::isInvoicingEnabled()) {
119 // @todo this should really be the first time we are determining
120 // the tax rates - we can calculate them from the financial_type_id
121 // & amount here so we didn't need a deeper function to semi-get
122 // them but not be able to 'format them right' because they are
123 // potentially being used for 'something else'.
124 // @todo invoicing code - please feel the hate. Also move this 'hook-like-bit'
125 // to the CRM_Invoicing_Utils class.
126 foreach ($tplLineItems as $key => $value) {
127 foreach ($value as $k => $v) {
128 if (isset($v['tax_rate']) && $v['tax_rate'] != '') {
129 // These only need assigning once, but code is more readable with them here
130 $this->assign('getTaxDetails', TRUE);
131 $this->assign('taxTerm', CRM_Invoicing_Utils
::getTaxTerm());
132 // Cast to float to display without trailing zero decimals
133 $tplLineItems[$key][$k]['tax_rate'] = (float) $v['tax_rate'];
140 * Assign line items to the template.
142 * @param $tplLineItems
144 protected function assignLineItemsToTemplate($tplLineItems) {
145 // @todo this should be a hook that invoicing code hooks into rather than a call to it.
146 $this->alterLineItemsForTemplate($tplLineItems);
147 $this->assign('lineItem', $tplLineItems);
151 * Get the configured processors, including the pay later processor.
155 protected function getProcessors(): array {
157 if (!empty($this->_paymentProcessors
)) {
158 foreach ($this->_paymentProcessors
as $key => $processor) {
159 $pps[$key] = $this->getPaymentProcessorTitle($processor);
162 if ($this->getPayLaterLabel()) {
163 $pps[0] = $this->getPayLaterLabel();
169 * Get the title of the payment processor to display to the user
170 * Note: There is an identical function in CRM_Core_Payment
172 * @param array $processor
176 protected function getPaymentProcessorTitle($processor) {
177 return $processor['title'] ??
$processor['name'];
181 * Adds in either a set of radio buttons or hidden fields to contain the payment processors on a front end form
183 protected function addPaymentProcessorFieldsToForm() {
184 $paymentProcessors = $this->getProcessors();
186 foreach ($paymentProcessors as $ppKey => $ppval) {
188 $optAttributes[$ppKey]['class'] = 'payment_processor_' . strtolower($this->_paymentProcessors
[$ppKey]['payment_processor_type']);
191 $optAttributes[$ppKey]['class'] = 'payment_processor_paylater';
194 if (count($paymentProcessors) > 1) {
195 $this->addRadio('payment_processor_id', ts('Payment Method'), $paymentProcessors,
196 NULL, " ", FALSE, $optAttributes
199 elseif (!empty($paymentProcessors)) {
200 $ppKeys = array_keys($paymentProcessors);
201 $currentPP = array_pop($ppKeys);
202 $this->addElement('hidden', 'payment_processor_id', $currentPP);