Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
3310ab71 | 29 | * Class for constructing the payment processor block. |
6a488035 TO |
30 | * |
31 | * @package CRM | |
6b83d5bd | 32 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
33 | */ |
34 | class CRM_Core_Payment_Form { | |
35 | ||
dc913073 | 36 | /** |
70d1766d | 37 | * Add payment fields depending on payment processor. |
38 | * | |
39 | * The payment processor can implement the following functions to override the built in fields. | |
dc913073 | 40 | * |
dde5a0ef EM |
41 | * - getPaymentFormFields() |
42 | * - getPaymentFormFieldsMetadata() | |
43 | * (planned - getBillingDetailsFormFields(), getBillingDetailsFormFieldsMetadata() | |
44 | * | |
45 | * Note that this code is written to accommodate the possibility CiviCRM will switch to implementing pay later as a manual processor in future | |
46 | * | |
47 | * @param CRM_Contribute_Form_AbstractEditPayment|CRM_Contribute_Form_Contribution_Main $form | |
6a0b768e TO |
48 | * @param array $processor |
49 | * Array of properties including 'object' as loaded from CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors. | |
1d1fee72 | 50 | * @param int $billing_profile_id |
6a0b768e | 51 | * Display billing fields even for pay later. |
dfc68e82 EM |
52 | * @param bool $isBackOffice |
53 | * Is this a back office function? If so the option to suppress the cvn needs to be evaluated. | |
18135422 | 54 | * @param int $paymentInstrumentID |
55 | * ID of the payment processor. | |
dc913073 | 56 | */ |
518fa0ee | 57 | public static function setPaymentFieldsByProcessor(&$form, $processor, $billing_profile_id = NULL, $isBackOffice = FALSE, $paymentInstrumentID = NULL) { |
be2fb01f | 58 | $form->billingFieldSets = []; |
1d1fee72 | 59 | // Load the pay-later processor |
60 | // @todo load this right up where the other processors are loaded initially. | |
3310ab71 | 61 | if (empty($processor)) { |
1d1fee72 | 62 | $processor = CRM_Financial_BAO_PaymentProcessor::getPayment(0); |
dc913073 | 63 | } |
3310ab71 | 64 | |
1d1fee72 | 65 | $processor['object']->setBillingProfile($billing_profile_id); |
18135422 | 66 | $processor['object']->setBackOffice($isBackOffice); |
67 | $processor['object']->setPaymentInstrumentID($paymentInstrumentID); | |
3310ab71 | 68 | $paymentTypeName = self::getPaymentTypeName($processor); |
3310ab71 | 69 | $form->assign('paymentTypeName', $paymentTypeName); |
c42f1a19 | 70 | $form->assign('paymentTypeLabel', self::getPaymentLabel($processor['object'])); |
18135422 | 71 | $form->assign('isBackOffice', $isBackOffice); |
3310ab71 | 72 | $form->_paymentFields = $form->billingFieldSets[$paymentTypeName]['fields'] = self::getPaymentFieldMetadata($processor); |
73 | $form->_paymentFields = array_merge($form->_paymentFields, self::getBillingAddressMetadata($processor, $form->_bltID)); | |
74 | $form->assign('paymentFields', self::getPaymentFields($processor)); | |
75 | self::setBillingAddressFields($form, $processor); | |
76 | // @todo - this may be obsolete - although potentially it could be used to re-order things in the form. | |
be2fb01f | 77 | $form->billingFieldSets['billing_name_address-group']['fields'] = []; |
dc913073 | 78 | } |
9c39fb25 | 79 | |
6a488035 | 80 | /** |
fe482240 | 81 | * Add general billing fields. |
9c39fb25 | 82 | * |
c490a46a | 83 | * @param CRM_Core_Form $form |
3310ab71 | 84 | * @param CRM_Core_Payment $processor |
6a488035 | 85 | */ |
518fa0ee | 86 | protected static function setBillingAddressFields(&$form, $processor) { |
3310ab71 | 87 | $billingID = $form->_bltID; |
88 | $smarty = CRM_Core_Smarty::singleton(); | |
89 | $smarty->assign('billingDetailsFields', self::getBillingAddressFields($processor, $billingID)); | |
90 | } | |
91 | ||
c46f87cf | 92 | /** |
9d421118 | 93 | * Add the payment fields to the template. |
94 | * | |
95 | * Generally this is the payment processor fields & the billing fields required | |
96 | * for the payment processor. However, this has been complicated by adding | |
97 | * pay later billing fields into this mix | |
98 | * | |
99 | * We now have the situation where the required fields cannot be set as required | |
100 | * on the form level if they are required for the payment processor, as another | |
101 | * processor might be selected and the validation will then be incorrect. | |
102 | * | |
103 | * However, if they are required for pay later we DO set them on the form level, | |
104 | * presumably assuming they will be required whatever happens. | |
105 | * | |
106 | * As a side-note this seems to re-enforce the argument for making pay later | |
107 | * operate as a payment processor rather than as a 'special thing on its own'. | |
108 | * | |
c46f87cf | 109 | * @param CRM_Core_Form $form |
9d421118 | 110 | * Form that the payment fields are to be added to. |
dc913073 | 111 | * @param array $paymentFields |
9d421118 | 112 | * Fields that are to be shown on the payment form. |
c46f87cf | 113 | */ |
c319039f | 114 | protected static function addCommonFields(&$form, $paymentFields) { |
4533376a | 115 | $requiredPaymentFields = $paymentFieldsMetadata = []; |
dc913073 | 116 | foreach ($paymentFields as $name => $field) { |
69e1be3a | 117 | $field['extra'] = isset($field['extra']) ? $field['extra'] : NULL; |
c42f1a19 | 118 | if ($field['htmlType'] == 'chainSelect') { |
be2fb01f | 119 | $form->addChainSelect($field['name'], ['required' => FALSE]); |
c42f1a19 | 120 | } |
121 | else { | |
122 | $form->add($field['htmlType'], | |
123 | $field['name'], | |
124 | $field['title'], | |
125 | $field['attributes'], | |
69e1be3a SL |
126 | FALSE, |
127 | $field['extra'] | |
c42f1a19 | 128 | ); |
c46f87cf | 129 | } |
c319039f | 130 | // This will cause the fields to be marked as required - but it is up to the payment processor to |
131 | // validate it. | |
132 | $requiredPaymentFields[$field['name']] = $field['is_required']; | |
4533376a | 133 | $paymentFieldsMetadata[$field['name']] = $field; |
c46f87cf | 134 | } |
c42f1a19 | 135 | |
4533376a | 136 | $form->assign('paymentFieldsMetadata', $paymentFieldsMetadata); |
9d421118 | 137 | $form->assign('requiredPaymentFields', $requiredPaymentFields); |
c46f87cf CW |
138 | } |
139 | ||
44b6505d | 140 | /** |
3310ab71 | 141 | * Get the payment fields that apply to this processor. |
142 | * | |
44b6505d | 143 | * @param array $paymentProcessor |
3310ab71 | 144 | * |
145 | * @todo sometimes things like the country alter the required fields (e.g direct debit fields). We should possibly | |
146 | * set these before calling getPaymentFormFields (as we identify them). | |
44b6505d | 147 | * |
dc913073 | 148 | * @return array |
44b6505d | 149 | */ |
00be9182 | 150 | public static function getPaymentFields($paymentProcessor) { |
18135422 | 151 | return $paymentProcessor['object']->getPaymentFormFields(); |
44b6505d EM |
152 | } |
153 | ||
154 | /** | |
dc913073 | 155 | * @param array $paymentProcessor |
44b6505d | 156 | * |
dc913073 EM |
157 | * @return array |
158 | */ | |
00be9182 | 159 | public static function getPaymentFieldMetadata($paymentProcessor) { |
18135422 | 160 | return array_intersect_key($paymentProcessor['object']->getPaymentFormFieldsMetadata(), array_flip(self::getPaymentFields($paymentProcessor))); |
3310ab71 | 161 | } |
162 | ||
163 | /** | |
164 | * Get the billing fields that apply to this processor. | |
165 | * | |
166 | * @param array $paymentProcessor | |
167 | * @param int $billingLocationID | |
168 | * ID of billing location type. | |
169 | * | |
170 | * @todo sometimes things like the country alter the required fields (e.g postal code). We should possibly | |
171 | * set these before calling getPaymentFormFields (as we identify them). | |
172 | * | |
173 | * @return array | |
174 | */ | |
175 | public static function getBillingAddressFields($paymentProcessor, $billingLocationID) { | |
1d1fee72 | 176 | return $paymentProcessor['object']->getBillingAddressFields($billingLocationID); |
3310ab71 | 177 | } |
178 | ||
179 | /** | |
180 | * @param array $paymentProcessor | |
181 | * | |
182 | * @param int $billingLocationID | |
183 | * | |
184 | * @return array | |
185 | * @throws \CRM_Core_Exception | |
186 | */ | |
187 | public static function getBillingAddressMetadata($paymentProcessor, $billingLocationID) { | |
188 | $paymentProcessorObject = Civi\Payment\System::singleton()->getByProcessor($paymentProcessor); | |
048d49dc | 189 | return array_intersect_key( |
190 | $paymentProcessorObject->getBillingAddressFieldsMetadata($billingLocationID), | |
e58c1c1a | 191 | array_flip(self::getBillingAddressFields($paymentProcessor, $billingLocationID)) |
048d49dc | 192 | ); |
dc913073 EM |
193 | } |
194 | ||
195 | /** | |
44b6505d | 196 | * @param array $paymentProcessor |
44b6505d | 197 | * |
dc913073 | 198 | * @return string |
44b6505d | 199 | */ |
00be9182 | 200 | public static function getPaymentTypeName($paymentProcessor) { |
1d1fee72 | 201 | return $paymentProcessor['object']->getPaymentTypeName(); |
dc913073 | 202 | } |
44b6505d | 203 | |
dc913073 EM |
204 | /** |
205 | * @param array $paymentProcessor | |
206 | * | |
207 | * @return string | |
208 | */ | |
00be9182 | 209 | public static function getPaymentTypeLabel($paymentProcessor) { |
c42f1a19 | 210 | return ts('%1 Information', [$paymentProcessor->getPaymentTypeLabel()]); |
44b6505d EM |
211 | } |
212 | ||
dc913073 | 213 | /** |
a6513ad5 | 214 | * @param CRM_Contribute_Form_AbstractEditPayment|CRM_Contribute_Form_Contribution_Main|CRM_Core_Payment_ProcessorForm|CRM_Contribute_Form_UpdateBilling $form |
6a0b768e TO |
215 | * @param array $processor |
216 | * Array of properties including 'object' as loaded from CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors. | |
1d1fee72 | 217 | * @param int|string $billing_profile_id |
218 | * Id of a profile to be passed to the processor for the processor to merge with it's required fields. | |
219 | * (currently only implemented by manual/ pay-later processor) | |
dc913073 | 220 | * |
225584c9 EM |
221 | * @param bool $isBackOffice |
222 | * Is this a backoffice form. This could affect the display of the cvn or whether some processors show, | |
223 | * although the distinction is losing it's meaning as front end forms are used for back office and a permission | |
224 | * for the 'enter without cvn' is probably more appropriate. Paypal std does not support another user | |
225 | * entering details but once again the issue is not back office but 'another user'. | |
18135422 | 226 | * @param int $paymentInstrumentID |
227 | * Payment instrument ID. | |
225584c9 | 228 | * |
dc913073 EM |
229 | * @return bool |
230 | */ | |
18135422 | 231 | public static function buildPaymentForm(&$form, $processor, $billing_profile_id, $isBackOffice, $paymentInstrumentID = NULL) { |
dde5a0ef EM |
232 | //if the form has address fields assign to the template so the js can decide what billing fields to show |
233 | $profileAddressFields = $form->get('profileAddressFields'); | |
234 | if (!empty($profileAddressFields)) { | |
235 | $form->assign('profileAddressFields', $profileAddressFields); | |
236 | } | |
237 | ||
287aebfb | 238 | if (!empty($processor['object']) && $processor['object']->buildForm($form)) { |
aefd7f6b | 239 | return NULL; |
dde5a0ef EM |
240 | } |
241 | ||
18135422 | 242 | self::setPaymentFieldsByProcessor($form, $processor, $billing_profile_id, $isBackOffice, $paymentInstrumentID); |
c319039f | 243 | self::addCommonFields($form, $form->_paymentFields); |
dc913073 | 244 | self::addRules($form, $form->_paymentFields); |
dc913073 EM |
245 | return (!empty($form->_paymentFields)); |
246 | } | |
44b6505d | 247 | |
dc913073 EM |
248 | /** |
249 | * @param CRM_Core_Form $form | |
6a0b768e TO |
250 | * @param array $paymentFields |
251 | * Array of properties including 'object' as loaded from CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors. | |
dc913073 EM |
252 | * @param $paymentFields |
253 | */ | |
9c39fb25 | 254 | protected static function addRules(&$form, $paymentFields) { |
dc913073 EM |
255 | foreach ($paymentFields as $paymentField => $fieldSpecs) { |
256 | if (!empty($fieldSpecs['rules'])) { | |
257 | foreach ($fieldSpecs['rules'] as $rule) { | |
258 | $form->addRule($paymentField, | |
259 | $rule['rule_message'], | |
260 | $rule['rule_name'], | |
261 | $rule['rule_parameters'] | |
262 | ); | |
263 | } | |
264 | } | |
265 | } | |
6a488035 TO |
266 | } |
267 | ||
a479fe60 | 268 | /** |
54957108 | 269 | * Validate the payment instrument values before passing it to the payment processor. |
270 | * | |
271 | * We want this to be able to be overridden by the payment processor, and default to using | |
a479fe60 | 272 | * this object's validCreditCard for credit cards (implemented as the default in the Payment class). |
54957108 | 273 | * |
274 | * @param int $payment_processor_id | |
275 | * @param array $values | |
276 | * @param array $errors | |
277 | * @param int $billing_profile_id | |
a479fe60 | 278 | */ |
1d1fee72 | 279 | public static function validatePaymentInstrument($payment_processor_id, $values, &$errors, $billing_profile_id) { |
280 | $payment = Civi\Payment\System::singleton()->getById($payment_processor_id); | |
281 | $payment->setBillingProfile($billing_profile_id); | |
282 | $payment->validatePaymentInstrument($values, $errors); | |
a479fe60 | 283 | } |
284 | ||
70d1766d | 285 | /** |
286 | * Set default values for the form. | |
287 | * | |
288 | * @param CRM_Core_Form $form | |
289 | * @param int $contactID | |
290 | */ | |
291 | public static function setDefaultValues(&$form, $contactID) { | |
292 | $billingDefaults = $form->getProfileDefaults('Billing', $contactID); | |
293 | $form->_defaults = array_merge($form->_defaults, $billingDefaults); | |
294 | ||
295 | // set default country & state from config if no country set | |
296 | // note the effect of this is to set the billing country to default to the site default | |
297 | // country if the person has an address but no country (for anonymous country is set above) | |
298 | // this could have implications if the billing profile is filled but hidden. | |
299 | // this behaviour has been in place for a while but the use of js to hide things has increased | |
300 | if (empty($form->_defaults["billing_country_id-{$form->_bltID}"])) { | |
301 | $form->_defaults["billing_country_id-{$form->_bltID}"] = CRM_Core_Config::singleton()->defaultContactCountry; | |
302 | } | |
303 | if (empty($form->_defaults["billing_state_province_id-{$form->_bltID}"])) { | |
304 | $form->_defaults["billing_state_province_id-{$form->_bltID}"] = CRM_Core_Config::singleton() | |
305 | ->defaultContactStateProvince; | |
306 | } | |
307 | } | |
308 | ||
7cb3d4f0 | 309 | /** |
fe482240 | 310 | * Make sure that credit card number and cvv are valid. |
7cb3d4f0 | 311 | * Called within the scope of a QF formRule function |
431c430b EM |
312 | * |
313 | * @param array $values | |
314 | * @param array $errors | |
06051ca4 | 315 | * @param int $processorID |
7cb3d4f0 | 316 | */ |
06051ca4 | 317 | public static function validateCreditCard($values, &$errors, $processorID = NULL) { |
4d1fd569 | 318 | if (!empty($values['credit_card_type']) || !empty($values['credit_card_number'])) { |
27b252af SL |
319 | if (!empty($values['credit_card_type'])) { |
320 | $processorCards = CRM_Financial_BAO_PaymentProcessor::getCreditCards($processorID); | |
321 | if (!empty($processorCards) && !in_array($values['credit_card_type'], $processorCards)) { | |
322 | $errors['credit_card_type'] = ts('This procesor does not support credit card type ' . $values['credit_card_type']); | |
323 | } | |
324 | } | |
7cb3d4f0 CW |
325 | if (!empty($values['credit_card_number']) && |
326 | !CRM_Utils_Rule::creditCardNumber($values['credit_card_number'], $values['credit_card_type']) | |
327 | ) { | |
8543f7c1 | 328 | $errors['credit_card_number'] = ts('Please enter a valid Card Number'); |
7cb3d4f0 CW |
329 | } |
330 | if (!empty($values['cvv2']) && | |
331 | !CRM_Utils_Rule::cvv($values['cvv2'], $values['credit_card_type']) | |
332 | ) { | |
8543f7c1 | 333 | $errors['cvv2'] = ts('Please enter a valid Card Verification Number'); |
7cb3d4f0 CW |
334 | } |
335 | } | |
336 | } | |
337 | ||
6a488035 | 338 | /** |
fe482240 | 339 | * Map address fields. |
6a488035 | 340 | * |
100fef9d | 341 | * @param int $id |
431c430b EM |
342 | * @param array $src |
343 | * @param array $dst | |
77b97be7 | 344 | * @param bool $reverse |
6a488035 | 345 | */ |
431c430b | 346 | public static function mapParams($id, $src, &$dst, $reverse = FALSE) { |
be2fb01f | 347 | $map = [ |
0b05b9a9 | 348 | 'first_name' => 'billing_first_name', |
349 | 'middle_name' => 'billing_middle_name', | |
350 | 'last_name' => 'billing_last_name', | |
351 | 'email' => "email-$id", | |
352 | 'street_address' => "billing_street_address-$id", | |
353 | 'supplemental_address_1' => "billing_supplemental_address_1-$id", | |
354 | 'city' => "billing_city-$id", | |
355 | 'state_province' => "billing_state_province-$id", | |
356 | 'postal_code' => "billing_postal_code-$id", | |
357 | 'country' => "billing_country-$id", | |
358 | 'contactID' => 'contact_id', | |
be2fb01f | 359 | ]; |
6a488035 TO |
360 | |
361 | foreach ($map as $n => $v) { | |
362 | if (!$reverse) { | |
363 | if (isset($src[$n])) { | |
364 | $dst[$v] = $src[$n]; | |
365 | } | |
366 | } | |
367 | else { | |
368 | if (isset($src[$v])) { | |
369 | $dst[$n] = $src[$v]; | |
370 | } | |
371 | } | |
372 | } | |
3be4a20e BS |
373 | |
374 | //CRM-19469 provide option for returning modified params | |
375 | return $dst; | |
6a488035 TO |
376 | } |
377 | ||
378 | /** | |
fe482240 | 379 | * Get the credit card expiration month. |
6a488035 TO |
380 | * The date format for this field should typically be "M Y" (ex: Feb 2011) or "m Y" (02 2011) |
381 | * See CRM-9017 | |
382 | * | |
2a6da8d7 EM |
383 | * @param $src |
384 | * | |
6a488035 | 385 | * @return int |
6a488035 | 386 | */ |
00be9182 | 387 | public static function getCreditCardExpirationMonth($src) { |
6a488035 TO |
388 | if ($month = CRM_Utils_Array::value('M', $src['credit_card_exp_date'])) { |
389 | return $month; | |
390 | } | |
391 | ||
392 | return CRM_Utils_Array::value('m', $src['credit_card_exp_date']); | |
393 | } | |
394 | ||
395 | /** | |
fe482240 | 396 | * Get the credit card expiration year. |
6a488035 | 397 | * The date format for this field should typically be "M Y" (ex: Feb 2011) or "m Y" (02 2011) |
c1cc3e0c | 398 | * This function exists only to make it consistent with getCreditCardExpirationMonth |
6a488035 | 399 | * |
2a6da8d7 EM |
400 | * @param $src |
401 | * | |
6a488035 | 402 | * @return int |
6a488035 | 403 | */ |
00be9182 | 404 | public static function getCreditCardExpirationYear($src) { |
6a488035 TO |
405 | return CRM_Utils_Array::value('Y', $src['credit_card_exp_date']); |
406 | } | |
96025800 | 407 | |
c42f1a19 | 408 | /** |
409 | * Get the label for the processor. | |
410 | * | |
411 | * We do not use a label if there are no enterable fields. | |
412 | * | |
413 | * @param \CRM_Core_Payment $processor | |
414 | * | |
415 | * @return string | |
416 | */ | |
417 | public static function getPaymentLabel($processor) { | |
418 | $isVisible = FALSE; | |
419 | $paymentTypeLabel = self::getPaymentTypeLabel($processor); | |
420 | foreach (self::getPaymentFieldMetadata(['object' => $processor]) as $paymentField) { | |
421 | if ($paymentField['htmlType'] !== 'hidden') { | |
422 | $isVisible = TRUE; | |
423 | } | |
424 | } | |
425 | return $isVisible ? $paymentTypeLabel : ''; | |
426 | ||
427 | } | |
428 | ||
6a488035 | 429 | } |