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