Merge pull request #23258 from civicrm/5.49
[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 * Constructor.
28 */
29 public function __construct() {
30 $this->_paymentProcessor = [
31 'payment_type' => 0,
32 'billing_mode' => 0,
33 'id' => 0,
34 'url_recur' => '',
35 'is_recur' => 0,
36 ];
37 }
38
39 /**
40 * Get billing fields required for this processor.
41 *
42 * We apply the existing default of returning fields only for payment processor type 1. Processors can override to
43 * alter.
44 *
45 * @param int $billingLocationID
46 *
47 * @return array
48 */
49 public function getBillingAddressFields($billingLocationID = NULL) {
50 if (!$billingLocationID) {
51 // Note that although the billing id is passed around the forms the idea that it would be anything other than
52 // the result of the function below doesn't seem to have eventuated.
53 // So taking this as a param is possibly something to be removed in favour of the standard default.
54 $billingLocationID = CRM_Core_BAO_LocationType::getBilling();
55 }
56
57 // Only handle pseudo-profile billing for now.
58 if ($this->billingProfile == 'billing') {
59 // @todo - use profile api to retrieve this - either as pseudo-profile or (better) set up billing
60 // as a reserved profile in the DB and (even better) allow the profile to be selected
61 // on the form instead of just 'billing for pay=later bool'
62 return [
63 'first_name' => 'billing_first_name',
64 'middle_name' => 'billing_middle_name',
65 'last_name' => 'billing_last_name',
66 'street_address' => "billing_street_address-{$billingLocationID}",
67 'city' => "billing_city-{$billingLocationID}",
68 'country' => "billing_country_id-{$billingLocationID}",
69 'state_province' => "billing_state_province_id-{$billingLocationID}",
70 'postal_code' => "billing_postal_code-{$billingLocationID}",
71 ];
72 }
73 else {
74 return [];
75 }
76 }
77
78 /**
79 * Get array of fields that should be displayed on the payment form.
80 *
81 * @return array
82 */
83 public function getPaymentFormFields() {
84 if (!$this->isBackOffice()) {
85 return [];
86 }
87
88 $paymentInstrument = CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', $this->getPaymentInstrumentID());
89 if ($paymentInstrument === 'Credit Card') {
90 return ['credit_card_type', 'pan_truncation'];
91 }
92 elseif ($paymentInstrument === 'Check') {
93 return ['check_number'];
94 }
95 return [];
96 }
97
98 /**
99 * Process payment.
100 *
101 * The function ensures an exception is thrown & moves some of this logic out of the form layer and makes the forms
102 * more agnostic.
103 *
104 * @param array $params
105 *
106 * @param string $component
107 *
108 * @return array
109 * Result array
110 *
111 * @throws \Civi\Payment\Exception\PaymentProcessorException
112 */
113 public function doPayment(&$params, $component = 'contribute') {
114 $params['payment_status_id'] = $this->getResult();
115 return $params;
116 }
117
118 /**
119 * Get the result of the payment.
120 *
121 * Usually this will be pending but the calling layer has a chance to set the result.
122 *
123 * This would apply in particular when the form accepts status id.
124 *
125 * Note that currently this payment class is only being used to manage the 'billing block' aspect
126 * of pay later. However, a longer term idea is that by treating 'pay-later' as 'just another processor'
127 * will allow code simplification.
128 *
129 * @return int
130 */
131 protected function getResult() {
132 if (!$this->result) {
133 $this->setResult(CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'));
134 }
135 return $this->result;
136 }
137
138 /**
139 * Set the result to be returned.
140 *
141 * This would be set from outside the function where we want to pass on the status from the form.
142 *
143 * @param int $result
144 */
145 public function setResult($result) {
146 $this->result = $result;
147 }
148
149 /**
150 * Set payment instrument id.
151 *
152 * @param int $paymentInstrumentID
153 */
154 public function setPaymentInstrumentID($paymentInstrumentID) {
155 $this->paymentInstrumentID = $paymentInstrumentID;
156 }
157
158 /**
159 * Get the name of the payment type.
160 *
161 * @return string
162 */
163 public function getPaymentTypeName() {
164 return 'pay-later';
165 }
166
167 /**
168 * Get the name of the payment type.
169 *
170 * @return string
171 */
172 public function getPaymentTypeLabel() {
173 return CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', $this->getPaymentInstrumentID());
174 }
175
176 /**
177 * Are live payments supported - e.g dummy doesn't support this.
178 *
179 * @return bool
180 */
181 protected function supportsLiveMode() {
182 return TRUE;
183 }
184
185 /**
186 * Are test payments supported.
187 *
188 * @return bool
189 */
190 protected function supportsTestMode() {
191 return TRUE;
192 }
193
194 /**
195 * Declare that more than one payment can be processed at once.
196 *
197 * @return bool
198 */
199 protected function supportsMultipleConcurrentPayments() {
200 return TRUE;
201 }
202
203 /**
204 * Checks if backoffice recurring edit is allowed
205 *
206 * @return bool
207 */
208 public function supportsEditRecurringContribution() {
209 return TRUE;
210 }
211
212 /**
213 * Does the processor support the user having a choice as to whether to cancel the recurring with the processor?
214 *
215 * If this returns TRUE then there will be an option to send a cancellation request in the cancellation form.
216 *
217 * This would normally be false for processors where CiviCRM maintains the schedule.
218 *
219 * @return bool
220 */
221 protected function supportsCancelRecurringNotifyOptional() {
222 return FALSE;
223 }
224
225 /**
226 * Are back office payments supported.
227 *
228 * @return bool
229 */
230 protected function supportsBackOffice() {
231 return TRUE;
232 }
233
234 /**
235 * Does the processor work without an email address?
236 */
237 protected function supportsNoEmailProvided() {
238 return TRUE;
239 }
240
241 /**
242 * Should a receipt be sent out for a pending payment.
243 *
244 * e.g for traditional pay later & ones with a delayed settlement a pending receipt makes sense.
245 */
246 public function isSendReceiptForPending() {
247 return TRUE;
248 }
249
250 /**
251 * Get help text information (help, description, etc.) about this payment,
252 * to display to the user.
253 *
254 * @param string $context
255 * Context of the text.
256 * Only explicitly supported contexts are handled without error.
257 * Currently supported:
258 * - contributionPageRecurringHelp (params: is_recur_installments, is_email_receipt)
259 *
260 * @param array $params
261 * Parameters for the field, context specific.
262 *
263 * @return string
264 */
265 public function getText($context, $params) {
266 switch ($context) {
267 case 'contributionPageContinueText':
268 if ($params['amount'] <= 0) {
269 return ts('To complete this transaction, click the <strong>Continue</strong> button below.');
270 }
271 return ts('To complete your contribution, click the <strong>Continue</strong> button below.');
272
273 default:
274 return parent::getText($context, $params);
275 }
276 }
277
278 /**
279 * Does this processor support cancelling recurring contributions through code.
280 *
281 * @return bool
282 */
283 protected function supportsCancelRecurring() {
284 return TRUE;
285 }
286
287 }