Fix unreleased regression where processors using getPaymentDescription get a fatal
[civicrm-core.git] / CRM / Core / Payment / Manual.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Core_Payment_Manual extends CRM_Core_Payment {
18
19 protected $result;
20
21 /**
22 * This function checks to see if we have the right config values.
23 */
24 public function checkConfig() {}
25
26 /**
27 * Get billing fields required for this processor.
28 *
29 * We apply the existing default of returning fields only for payment processor type 1. Processors can override to
30 * alter.
31 *
32 * @param int $billingLocationID
33 *
34 * @return array
35 */
36 public function getBillingAddressFields($billingLocationID = NULL) {
37 if (!$billingLocationID) {
38 // Note that although the billing id is passed around the forms the idea that it would be anything other than
39 // the result of the function below doesn't seem to have eventuated.
40 // So taking this as a param is possibly something to be removed in favour of the standard default.
41 $billingLocationID = CRM_Core_BAO_LocationType::getBilling();
42 }
43
44 // Only handle pseudo-profile billing for now.
45 if ($this->billingProfile == 'billing') {
46 // @todo - use profile api to retrieve this - either as pseudo-profile or (better) set up billing
47 // as a reserved profile in the DB and (even better) allow the profile to be selected
48 // on the form instead of just 'billing for pay=later bool'
49 return [
50 'first_name' => 'billing_first_name',
51 'middle_name' => 'billing_middle_name',
52 'last_name' => 'billing_last_name',
53 'street_address' => "billing_street_address-{$billingLocationID}",
54 'city' => "billing_city-{$billingLocationID}",
55 'country' => "billing_country_id-{$billingLocationID}",
56 'state_province' => "billing_state_province_id-{$billingLocationID}",
57 'postal_code' => "billing_postal_code-{$billingLocationID}",
58 ];
59 }
60 else {
61 return [];
62 }
63 }
64
65 /**
66 * Get array of fields that should be displayed on the payment form.
67 *
68 * @return array
69 */
70 public function getPaymentFormFields() {
71 if (!$this->isBackOffice()) {
72 return [];
73 }
74
75 $paymentInstrument = CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', $this->getPaymentInstrumentID());
76 if ($paymentInstrument === 'Credit Card') {
77 return ['credit_card_type', 'pan_truncation'];
78 }
79 elseif ($paymentInstrument === 'Check') {
80 return ['check_number'];
81 }
82 return [];
83 }
84
85 /**
86 * Process payment.
87 *
88 * The function ensures an exception is thrown & moves some of this logic out of the form layer and makes the forms
89 * more agnostic.
90 *
91 * @param array $params
92 *
93 * @param string $component
94 *
95 * @return array
96 * Result array
97 *
98 * @throws \Civi\Payment\Exception\PaymentProcessorException
99 */
100 public function doPayment(&$params, $component = 'contribute') {
101 $params['payment_status_id'] = $this->getResult();
102 return $params;
103 }
104
105 /**
106 * Get the result of the payment.
107 *
108 * Usually this will be pending but the calling layer has a chance to set the result.
109 *
110 * This would apply in particular when the form accepts status id.
111 *
112 * Note that currently this payment class is only being used to manage the 'billing block' aspect
113 * of pay later. However, a longer term idea is that by treating 'pay-later' as 'just another processor'
114 * will allow code simplification.
115 *
116 * @return int
117 */
118 protected function getResult() {
119 if (!$this->result) {
120 $this->setResult(CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'status_id', 'Pending'));
121 }
122 return $this->result;
123 }
124
125 /**
126 * Set the result to be returned.
127 *
128 * This would be set from outside the function where we want to pass on the status from the form.
129 *
130 * @param int $result
131 */
132 public function setResult($result) {
133 $this->result = $result;
134 }
135
136 /**
137 * Set payment instrument id.
138 *
139 * @param int $paymentInstrumentID
140 */
141 public function setPaymentInstrumentID($paymentInstrumentID) {
142 $this->paymentInstrumentID = $paymentInstrumentID;
143 }
144
145 /**
146 * Get the name of the payment type.
147 *
148 * @return string
149 */
150 public function getPaymentTypeName() {
151 return 'pay-later';
152 }
153
154 /**
155 * Get the name of the payment type.
156 *
157 * @return string
158 */
159 public function getPaymentTypeLabel() {
160 return CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', $this->getPaymentInstrumentID());
161 }
162
163 /**
164 * Declare that more than one payment can be processed at once.
165 *
166 * @return bool
167 */
168 protected function supportsMultipleConcurrentPayments() {
169 return TRUE;
170 }
171
172 /**
173 * Checks if backoffice recurring edit is allowed
174 *
175 * @return bool
176 */
177 public function supportsEditRecurringContribution() {
178 return TRUE;
179 }
180
181 /**
182 * Are back office payments supported.
183 *
184 * @return bool
185 */
186 protected function supportsBackOffice() {
187 return TRUE;
188 }
189
190 /**
191 * Submit a manual payment.
192 *
193 * @param array $params
194 * Assoc array of input parameters for this transaction.
195 *
196 * @return array
197 */
198 public function doDirectPayment(&$params) {
199 $statuses = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id');
200 if ($params['is_pay_later']) {
201 $result['payment_status_id'] = array_search('Pending', $statuses);
202 }
203 else {
204 $result['payment_status_id'] = array_search('Completed', $statuses);
205 }
206 return $result;
207 }
208
209 /**
210 * Should a receipt be sent out for a pending payment.
211 *
212 * e.g for traditional pay later & ones with a delayed settlement a pending receipt makes sense.
213 */
214 public function isSendReceiptForPending() {
215 return TRUE;
216 }
217
218 /**
219 * Get help text information (help, description, etc.) about this payment,
220 * to display to the user.
221 *
222 * @param string $context
223 * Context of the text.
224 * Only explicitly supported contexts are handled without error.
225 * Currently supported:
226 * - contributionPageRecurringHelp (params: is_recur_installments, is_email_receipt)
227 *
228 * @param array $params
229 * Parameters for the field, context specific.
230 *
231 * @return string
232 */
233 public function getText($context, $params) {
234 switch ($context) {
235 case 'contributionPageContinueText':
236 if ($params['amount'] <= 0) {
237 return ts('To complete this transaction, click the <strong>Continue</strong> button below.');
238 }
239 return ts('To complete your contribution, click the <strong>Continue</strong> button below.');
240
241 }
242 }
243
244 }