Merge pull request #17448 from mattwire/api4membershiptype
[civicrm-core.git] / CRM / Core / Payment / Manual.php
CommitLineData
1d1fee72 1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
1d1fee72 5 | |
bc77d7c0
TO
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 |
1d1fee72 9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
1d1fee72 16 */
17class 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.
1d1fee72 23 */
f48e6cf7 24 public function checkConfig() {}
1d1fee72 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'
be2fb01f 49 return [
1d1fee72 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}",
be2fb01f 58 ];
1d1fee72 59 }
60 else {
be2fb01f 61 return [];
1d1fee72 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() {
794d4fc0 71 if (!$this->isBackOffice()) {
be2fb01f 72 return [];
794d4fc0 73 }
74
75 $paymentInstrument = CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', $this->getPaymentInstrumentID());
76 if ($paymentInstrument === 'Credit Card') {
be2fb01f 77 return ['credit_card_type', 'pan_truncation'];
794d4fc0 78 }
79 elseif ($paymentInstrument === 'Check') {
be2fb01f 80 return ['check_number'];
794d4fc0 81 }
be2fb01f 82 return [];
1d1fee72 83 }
794d4fc0 84
1d1fee72 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) {
ac1493e4 120 $this->setResult(CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'));
1d1fee72 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
18135422 136 /**
137 * Set payment instrument id.
138 *
139 * @param int $paymentInstrumentID
140 */
141 public function setPaymentInstrumentID($paymentInstrumentID) {
142 $this->paymentInstrumentID = $paymentInstrumentID;
143 }
144
1d1fee72 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() {
cf7b5c1f 160 return CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', $this->getPaymentInstrumentID());
1d1fee72 161 }
f48e6cf7 162
c9058b9b
MW
163 /**
164 * Are live payments supported - e.g dummy doesn't support this.
165 *
166 * @return bool
167 */
168 protected function supportsLiveMode() {
169 return TRUE;
170 }
171
172 /**
173 * Are test payments supported.
174 *
175 * @return bool
176 */
177 protected function supportsTestMode() {
178 return TRUE;
179 }
180
ecb7ec32 181 /**
182 * Declare that more than one payment can be processed at once.
183 *
184 * @return bool
185 */
186 protected function supportsMultipleConcurrentPayments() {
187 return TRUE;
188 }
189
95974e8e
DG
190 /**
191 * Checks if backoffice recurring edit is allowed
192 *
193 * @return bool
194 */
195 public function supportsEditRecurringContribution() {
196 return TRUE;
197 }
198
5077b04c 199 /**
200 * Does the processor support the user having a choice as to whether to cancel the recurring with the processor?
201 *
202 * If this returns TRUE then there will be an option to send a cancellation request in the cancellation form.
203 *
204 * This would normally be false for processors where CiviCRM maintains the schedule.
205 *
206 * @return bool
207 */
208 protected function supportsCancelRecurringNotifyOptional() {
209 return FALSE;
210 }
211
18135422 212 /**
213 * Are back office payments supported.
214 *
215 * @return bool
216 */
217 protected function supportsBackOffice() {
218 return TRUE;
219 }
220
6d7d7089
AS
221 /**
222 * Does the processor work without an email address?
223 */
224 protected function supportsNoEmailProvided() {
225 return TRUE;
226 }
227
95974e8e 228 /**
3e473c0b 229 * Submit a manual payment.
95974e8e
DG
230 *
231 * @param array $params
232 * Assoc array of input parameters for this transaction.
3e473c0b 233 *
234 * @return array
95974e8e
DG
235 */
236 public function doDirectPayment(&$params) {
237 $statuses = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id');
238 if ($params['is_pay_later']) {
239 $result['payment_status_id'] = array_search('Pending', $statuses);
240 }
241 else {
242 $result['payment_status_id'] = array_search('Completed', $statuses);
243 }
3e473c0b 244 return $result;
95974e8e
DG
245 }
246
cd3bc162 247 /**
248 * Should a receipt be sent out for a pending payment.
249 *
250 * e.g for traditional pay later & ones with a delayed settlement a pending receipt makes sense.
251 */
252 public function isSendReceiptForPending() {
253 return TRUE;
254 }
255
e364c762 256 /**
257 * Get help text information (help, description, etc.) about this payment,
258 * to display to the user.
259 *
260 * @param string $context
261 * Context of the text.
262 * Only explicitly supported contexts are handled without error.
263 * Currently supported:
264 * - contributionPageRecurringHelp (params: is_recur_installments, is_email_receipt)
265 *
266 * @param array $params
267 * Parameters for the field, context specific.
268 *
269 * @return string
270 */
271 public function getText($context, $params) {
272 switch ($context) {
273 case 'contributionPageContinueText':
274 if ($params['amount'] <= 0) {
275 return ts('To complete this transaction, click the <strong>Continue</strong> button below.');
276 }
277 return ts('To complete your contribution, click the <strong>Continue</strong> button below.');
278
279 }
280 }
281
1d1fee72 282}