Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Core_Payment_Form { | |
36 | ||
dc913073 EM |
37 | |
38 | /** | |
dde5a0ef | 39 | * Add payment fields depending on payment processor. 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. | |
50 | * @param bool $forceBillingFieldsForPayLater | |
51 | * Display billing fields even for pay later. | |
dc913073 | 52 | */ |
dde5a0ef | 53 | static public function setPaymentFieldsByProcessor(&$form, $processor, $forceBillingFieldsForPayLater = FALSE) { |
dc913073 | 54 | $form->billingFieldSets = array(); |
dde5a0ef EM |
55 | if ($processor != NULL) { |
56 | // ie it is pay later | |
57 | $paymentFields = self::getPaymentFields($processor); | |
58 | $paymentTypeName = self::getPaymentTypeName($processor); | |
59 | $paymentTypeLabel = self::getPaymentTypeLabel($processor); | |
60 | //@todo if we switch to iterating through $form->billingFieldSets we won't need to assign these directly | |
61 | $form->assign('paymentTypeName', $paymentTypeName); | |
62 | $form->assign('paymentTypeLabel', $paymentTypeLabel); | |
63 | ||
64 | $form->billingFieldSets[$paymentTypeName]['fields'] = $form->_paymentFields = array_intersect_key(self::getPaymentFieldMetadata($processor), array_flip($paymentFields)); | |
65 | $form->billingPane = array($paymentTypeName => $paymentTypeLabel); | |
66 | $form->assign('paymentFields', $paymentFields); | |
67 | } | |
68 | ||
69 | // @todo - replace this section with one similar to above per discussion - probably use a manual processor shell class to stand in for that capability | |
70 | //return without adding billing fields if billing_mode = 4 (@todo - more the ability to set that to the payment processor) | |
71 | // or payment processor is NULL (pay later) | |
353ffa53 | 72 | if (($processor == NULL && !$forceBillingFieldsForPayLater) || CRM_Utils_Array::value('billing_mode', $processor) == 4) { |
dde5a0ef | 73 | return; |
dc913073 | 74 | } |
dde5a0ef EM |
75 | //@todo setPaymentFields defines the billing fields - this should be moved to the processor class & renamed getBillingFields |
76 | // potentially pay later would also be a payment processor | |
77 | //also set the billingFieldSet to hold all the details required to render the fieldset so we can iterate through the fieldset - making | |
78 | // it easier to re-order in hooks etc. The billingFieldSets param is used to determine whether to show the billing pane | |
79 | CRM_Core_Payment_Form::setBillingDetailsFields($form); | |
80 | $form->billingFieldSets['billing_name_address-group']['fields'] = array(); | |
dc913073 | 81 | } |
9c39fb25 | 82 | |
6a488035 | 83 | /** |
fe482240 | 84 | * Add general billing fields. |
9c39fb25 EM |
85 | * @todo set these like processor fields & let payment processors alter them |
86 | * | |
c490a46a | 87 | * @param CRM_Core_Form $form |
77b97be7 | 88 | * |
6a488035 | 89 | * @return void |
6a488035 | 90 | */ |
dde5a0ef | 91 | static protected function setBillingDetailsFields(&$form) { |
6a488035 TO |
92 | $bltID = $form->_bltID; |
93 | ||
94 | $form->_paymentFields['billing_first_name'] = array( | |
95 | 'htmlType' => 'text', | |
96 | 'name' => 'billing_first_name', | |
97 | 'title' => ts('Billing First Name'), | |
98 | 'cc_field' => TRUE, | |
99 | 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'), | |
100 | 'is_required' => TRUE, | |
101 | ); | |
102 | ||
103 | $form->_paymentFields['billing_middle_name'] = array( | |
104 | 'htmlType' => 'text', | |
105 | 'name' => 'billing_middle_name', | |
106 | 'title' => ts('Billing Middle Name'), | |
107 | 'cc_field' => TRUE, | |
108 | 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'), | |
109 | 'is_required' => FALSE, | |
110 | ); | |
111 | ||
112 | $form->_paymentFields['billing_last_name'] = array( | |
113 | 'htmlType' => 'text', | |
114 | 'name' => 'billing_last_name', | |
115 | 'title' => ts('Billing Last Name'), | |
116 | 'cc_field' => TRUE, | |
117 | 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'), | |
118 | 'is_required' => TRUE, | |
119 | ); | |
120 | ||
121 | $form->_paymentFields["billing_street_address-{$bltID}"] = array( | |
122 | 'htmlType' => 'text', | |
123 | 'name' => "billing_street_address-{$bltID}", | |
124 | 'title' => ts('Street Address'), | |
125 | 'cc_field' => TRUE, | |
126 | 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'), | |
127 | 'is_required' => TRUE, | |
128 | ); | |
129 | ||
130 | $form->_paymentFields["billing_city-{$bltID}"] = array( | |
131 | 'htmlType' => 'text', | |
132 | 'name' => "billing_city-{$bltID}", | |
133 | 'title' => ts('City'), | |
134 | 'cc_field' => TRUE, | |
135 | 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'), | |
136 | 'is_required' => TRUE, | |
137 | ); | |
138 | ||
139 | $form->_paymentFields["billing_state_province_id-{$bltID}"] = array( | |
c927c151 | 140 | 'htmlType' => 'chainSelect', |
757069de | 141 | 'title' => ts('State/Province'), |
6a488035 | 142 | 'name' => "billing_state_province_id-{$bltID}", |
6a488035 | 143 | 'cc_field' => TRUE, |
bc999cd1 | 144 | 'is_required' => TRUE, |
6a488035 TO |
145 | ); |
146 | ||
147 | $form->_paymentFields["billing_postal_code-{$bltID}"] = array( | |
148 | 'htmlType' => 'text', | |
149 | 'name' => "billing_postal_code-{$bltID}", | |
150 | 'title' => ts('Postal Code'), | |
151 | 'cc_field' => TRUE, | |
152 | 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'), | |
153 | 'is_required' => TRUE, | |
154 | ); | |
155 | ||
156 | $form->_paymentFields["billing_country_id-{$bltID}"] = array( | |
157 | 'htmlType' => 'select', | |
158 | 'name' => "billing_country_id-{$bltID}", | |
159 | 'title' => ts('Country'), | |
160 | 'cc_field' => TRUE, | |
161 | 'attributes' => array( | |
acb1052e WA |
162 | '' => ts('- select -'), |
163 | ) + | |
164 | CRM_Core_PseudoConstant::country(), | |
6a488035 TO |
165 | 'is_required' => TRUE, |
166 | ); | |
92ec1d7f EM |
167 | //CRM-15509 working towards giving control over billing fields to payment processors. For now removing tpl hard-coding |
168 | $smarty = CRM_Core_Smarty::singleton(); | |
169 | $smarty->assign('billingDetailsFields', array( | |
170 | 'billing_first_name', | |
171 | 'billing_middle_name', | |
172 | 'billing_last_name', | |
173 | "billing_street_address-{$bltID}", | |
174 | "billing_city-{$bltID}", | |
175 | "billing_country_id-{$bltID}", | |
95e19ac6 | 176 | "billing_state_province_id-{$bltID}", |
92ec1d7f EM |
177 | "billing_postal_code-{$bltID}", |
178 | )); | |
6a488035 TO |
179 | } |
180 | ||
c46f87cf CW |
181 | /** |
182 | * @param CRM_Core_Form $form | |
92ec1d7f | 183 | * @param bool $useRequired |
dc913073 | 184 | * @param array $paymentFields |
c46f87cf | 185 | */ |
9c39fb25 | 186 | protected static function addCommonFields(&$form, $useRequired, $paymentFields) { |
dc913073 | 187 | foreach ($paymentFields as $name => $field) { |
c46f87cf CW |
188 | if (!empty($field['cc_field'])) { |
189 | if ($field['htmlType'] == 'chainSelect') { | |
190 | $form->addChainSelect($field['name'], array('required' => $useRequired && $field['is_required'])); | |
191 | } | |
192 | else { | |
193 | $form->add($field['htmlType'], | |
194 | $field['name'], | |
195 | $field['title'], | |
196 | $field['attributes'], | |
197 | $useRequired ? $field['is_required'] : FALSE | |
198 | ); | |
199 | } | |
200 | } | |
201 | } | |
202 | } | |
203 | ||
44b6505d EM |
204 | /** |
205 | * @param array $paymentProcessor | |
dc913073 EM |
206 | * @todo it will be necessary to set details that affect it - mostly likely take Country as a param. Should we add generic |
207 | * setParams on processor class or just setCountry which we know we need? | |
44b6505d | 208 | * |
dc913073 | 209 | * @return array |
44b6505d | 210 | */ |
00be9182 | 211 | public static function getPaymentFields($paymentProcessor) { |
44b6505d | 212 | $paymentProcessorObject = CRM_Core_Payment::singleton(($paymentProcessor['is_test'] ? 'test' : 'live'), $paymentProcessor); |
dc913073 | 213 | return $paymentProcessorObject->getPaymentFormFields(); |
44b6505d EM |
214 | } |
215 | ||
216 | /** | |
dc913073 | 217 | * @param array $paymentProcessor |
44b6505d | 218 | * |
dc913073 EM |
219 | * @return array |
220 | */ | |
00be9182 | 221 | public static function getPaymentFieldMetadata($paymentProcessor) { |
dc913073 EM |
222 | $paymentProcessorObject = CRM_Core_Payment::singleton(($paymentProcessor['is_test'] ? 'test' : 'live'), $paymentProcessor); |
223 | return $paymentProcessorObject->getPaymentFormFieldsMetadata(); | |
224 | } | |
225 | ||
226 | /** | |
44b6505d | 227 | * @param array $paymentProcessor |
44b6505d | 228 | * |
dc913073 | 229 | * @return string |
44b6505d | 230 | */ |
00be9182 | 231 | public static function getPaymentTypeName($paymentProcessor) { |
dc913073 EM |
232 | $paymentProcessorObject = CRM_Core_Payment::singleton(($paymentProcessor['is_test'] ? 'test' : 'live'), $paymentProcessor); |
233 | return $paymentProcessorObject->getPaymentTypeName(); | |
234 | } | |
44b6505d | 235 | |
dc913073 EM |
236 | /** |
237 | * @param array $paymentProcessor | |
238 | * | |
239 | * @return string | |
240 | */ | |
00be9182 | 241 | public static function getPaymentTypeLabel($paymentProcessor) { |
dc913073 EM |
242 | $paymentProcessorObject = CRM_Core_Payment::singleton(($paymentProcessor['is_test'] ? 'test' : 'live'), $paymentProcessor); |
243 | return ts(($paymentProcessorObject->getPaymentTypeLabel()) . ' Information'); | |
44b6505d EM |
244 | } |
245 | ||
dc913073 | 246 | /** |
a6513ad5 | 247 | * @param CRM_Contribute_Form_AbstractEditPayment|CRM_Contribute_Form_Contribution_Main|CRM_Core_Payment_ProcessorForm|CRM_Contribute_Form_UpdateBilling $form |
6a0b768e TO |
248 | * @param array $processor |
249 | * Array of properties including 'object' as loaded from CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors. | |
250 | * @param bool $isBillingDataOptional | |
251 | * This manifests for 'NULL' (pay later) payment processor as the addition of billing fields to the form and. | |
dde5a0ef EM |
252 | * for payment processors that gather payment data on site as rendering the fields as not being required. (not entirely sure why but this |
253 | * is implemented for back office forms) | |
dc913073 EM |
254 | * |
255 | * @return bool | |
256 | */ | |
9b873358 | 257 | public static function buildPaymentForm($form, $processor, $isBillingDataOptional) { |
dde5a0ef EM |
258 | //if the form has address fields assign to the template so the js can decide what billing fields to show |
259 | $profileAddressFields = $form->get('profileAddressFields'); | |
260 | if (!empty($profileAddressFields)) { | |
261 | $form->assign('profileAddressFields', $profileAddressFields); | |
262 | } | |
263 | ||
264 | // $processor->buildForm appears to be an undocumented (possibly unused) option for payment processors | |
265 | // which was previously available only in some form flows | |
266 | if (!empty($form->_paymentProcessor) && !empty($form->_paymentProcessor['object']) && $form->_paymentProcessor['object']->isSupported('buildForm')) { | |
267 | $form->_paymentProcessor['object']->buildForm($form); | |
acb1052e | 268 | return NULL; |
dde5a0ef EM |
269 | } |
270 | ||
271 | self::setPaymentFieldsByProcessor($form, $processor, empty($isBillingDataOptional)); | |
dc913073 EM |
272 | self::addCommonFields($form, !$isBillingDataOptional, $form->_paymentFields); |
273 | self::addRules($form, $form->_paymentFields); | |
274 | self::addPaypalExpressCode($form); | |
275 | return (!empty($form->_paymentFields)); | |
276 | } | |
44b6505d | 277 | |
dc913073 EM |
278 | /** |
279 | * @param CRM_Core_Form $form | |
6a0b768e TO |
280 | * @param array $paymentFields |
281 | * Array of properties including 'object' as loaded from CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors. | |
dc913073 EM |
282 | * @param $paymentFields |
283 | */ | |
9c39fb25 | 284 | protected static function addRules(&$form, $paymentFields) { |
dc913073 EM |
285 | foreach ($paymentFields as $paymentField => $fieldSpecs) { |
286 | if (!empty($fieldSpecs['rules'])) { | |
287 | foreach ($fieldSpecs['rules'] as $rule) { | |
288 | $form->addRule($paymentField, | |
289 | $rule['rule_message'], | |
290 | $rule['rule_name'], | |
291 | $rule['rule_parameters'] | |
292 | ); | |
293 | } | |
294 | } | |
295 | } | |
6a488035 TO |
296 | } |
297 | ||
298 | /** | |
100fef9d | 299 | * Billing mode button is basically synonymous with paypal express - this is probably a good example of 'odds & sods' code we |
dc913073 | 300 | * need to find a way for the payment processor to assign. A tricky aspect is that the payment processor may need to set the order |
6a488035 | 301 | * |
fd31fa4c | 302 | * @param $form |
dc913073 | 303 | */ |
9c39fb25 | 304 | protected static function addPaypalExpressCode(&$form) { |
0be0b79d | 305 | if (empty($form->isBackOffice)) { |
2b3fa30d | 306 | if (CRM_Utils_Array::value('billing_mode', $form->_paymentProcessor) == 3 |
0be0b79d EM |
307 | ) { |
308 | $form->_expressButtonName = $form->getButtonName('upload', 'express'); | |
309 | $form->assign('expressButtonName', $form->_expressButtonName); | |
310 | $form->add('image', | |
311 | $form->_expressButtonName, | |
312 | $form->_paymentProcessor['url_button'], | |
313 | array('class' => 'crm-form-submit') | |
314 | ); | |
315 | } | |
dc913073 EM |
316 | } |
317 | } | |
6a488035 | 318 | |
a479fe60 | 319 | /** |
320 | * Validate the payment instrument values before passing it to the payment processor | |
321 | * We want this to be overrideable by the payment processor, and default to using | |
322 | * this object's validCreditCard for credit cards (implemented as the default in the Payment class). | |
323 | */ | |
324 | public static function validatePaymentInstrument($payment_processor_id, $values, &$errors, $form) { | |
325 | // ignore if we don't have a payment instrument to validate (e.g. backend payments) | |
326 | if ($payment_processor_id > 0) { | |
d1bd65af | 327 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($payment_processor_id, 'live'); |
a479fe60 | 328 | $payment = CRM_Core_Payment::singleton('live', $paymentProcessor, $form); |
329 | $payment->validatePaymentInstrument($values, $errors); | |
330 | } | |
331 | } | |
332 | ||
bef9421f CW |
333 | /** |
334 | * The credit card pseudo constant results only the CC label, not the key ID | |
335 | * So we normalize the name to use it as a CSS class. | |
336 | */ | |
00be9182 | 337 | public static function getCreditCardCSSNames() { |
bef9421f CW |
338 | $creditCardTypes = array(); |
339 | foreach (CRM_Contribute_PseudoConstant::creditCard() as $key => $name) { | |
340 | // Replace anything not css-friendly by an underscore | |
341 | // Non-latin names will not like this, but so many things are wrong with | |
342 | // the credit-card type configurations already. | |
343 | $key = str_replace(' ', '', $key); | |
344 | $key = preg_replace('/[^a-zA-Z0-9]/', '_', $key); | |
345 | $key = strtolower($key); | |
346 | $creditCardTypes[$key] = $name; | |
347 | } | |
348 | return $creditCardTypes; | |
349 | } | |
350 | ||
7cb3d4f0 | 351 | /** |
fe482240 | 352 | * Make sure that credit card number and cvv are valid. |
7cb3d4f0 CW |
353 | * Called within the scope of a QF formRule function |
354 | */ | |
00be9182 | 355 | public static function validateCreditCard($values, &$errors) { |
7cb3d4f0 CW |
356 | if (!empty($values['credit_card_type'])) { |
357 | if (!empty($values['credit_card_number']) && | |
358 | !CRM_Utils_Rule::creditCardNumber($values['credit_card_number'], $values['credit_card_type']) | |
359 | ) { | |
8543f7c1 | 360 | $errors['credit_card_number'] = ts('Please enter a valid Card Number'); |
7cb3d4f0 CW |
361 | } |
362 | if (!empty($values['cvv2']) && | |
363 | !CRM_Utils_Rule::cvv($values['cvv2'], $values['credit_card_type']) | |
364 | ) { | |
8543f7c1 | 365 | $errors['cvv2'] = ts('Please enter a valid Card Verification Number'); |
7cb3d4f0 CW |
366 | } |
367 | } | |
15c6517d | 368 | elseif (!empty($values['credit_card_number'])) { |
8543f7c1 | 369 | $errors['credit_card_number'] = ts('Please enter a valid Card Number'); |
15c6517d | 370 | } |
7cb3d4f0 CW |
371 | } |
372 | ||
6a488035 | 373 | /** |
fe482240 | 374 | * Map address fields. |
6a488035 | 375 | * |
100fef9d | 376 | * @param int $id |
77b97be7 EM |
377 | * @param $src |
378 | * @param $dst | |
379 | * @param bool $reverse | |
380 | * | |
6a488035 | 381 | * @return void |
6a488035 | 382 | */ |
00be9182 | 383 | public static function mapParams($id, &$src, &$dst, $reverse = FALSE) { |
6a488035 TO |
384 | static $map = NULL; |
385 | if (!$map) { | |
386 | $map = array( | |
387 | 'first_name' => 'billing_first_name', | |
388 | 'middle_name' => 'billing_middle_name', | |
389 | 'last_name' => 'billing_last_name', | |
390 | 'email' => "email-$id", | |
391 | 'street_address' => "billing_street_address-$id", | |
392 | 'supplemental_address_1' => "billing_supplemental_address_1-$id", | |
393 | 'city' => "billing_city-$id", | |
394 | 'state_province' => "billing_state_province-$id", | |
395 | 'postal_code' => "billing_postal_code-$id", | |
396 | 'country' => "billing_country-$id", | |
397 | ); | |
398 | } | |
399 | ||
400 | foreach ($map as $n => $v) { | |
401 | if (!$reverse) { | |
402 | if (isset($src[$n])) { | |
403 | $dst[$v] = $src[$n]; | |
404 | } | |
405 | } | |
406 | else { | |
407 | if (isset($src[$v])) { | |
408 | $dst[$n] = $src[$v]; | |
409 | } | |
410 | } | |
411 | } | |
412 | } | |
413 | ||
414 | /** | |
fe482240 | 415 | * Get the credit card expiration month. |
6a488035 TO |
416 | * The date format for this field should typically be "M Y" (ex: Feb 2011) or "m Y" (02 2011) |
417 | * See CRM-9017 | |
418 | * | |
2a6da8d7 EM |
419 | * @param $src |
420 | * | |
6a488035 | 421 | * @return int |
6a488035 | 422 | */ |
00be9182 | 423 | public static function getCreditCardExpirationMonth($src) { |
6a488035 TO |
424 | if ($month = CRM_Utils_Array::value('M', $src['credit_card_exp_date'])) { |
425 | return $month; | |
426 | } | |
427 | ||
428 | return CRM_Utils_Array::value('m', $src['credit_card_exp_date']); | |
429 | } | |
430 | ||
431 | /** | |
fe482240 | 432 | * Get the credit card expiration year. |
6a488035 | 433 | * The date format for this field should typically be "M Y" (ex: Feb 2011) or "m Y" (02 2011) |
c1cc3e0c | 434 | * This function exists only to make it consistent with getCreditCardExpirationMonth |
6a488035 | 435 | * |
2a6da8d7 EM |
436 | * @param $src |
437 | * | |
6a488035 | 438 | * @return int |
6a488035 | 439 | */ |
00be9182 | 440 | public static function getCreditCardExpirationYear($src) { |
6a488035 TO |
441 | return CRM_Utils_Array::value('Y', $src['credit_card_exp_date']); |
442 | } | |
96025800 | 443 | |
6a488035 | 444 | } |