Minor cleanup
[civicrm-core.git] / CRM / Core / Payment / Form.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Core_Payment_Form {
36
37 /**
38 * Add payment fields are depending on payment type
39 *
40 * @param int $type eg CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT
41 * @param CRM_Core_Form $form
42 */
43 static public function setPaymentFieldsByType($type, &$form) {
44 if ($type & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT) {
45 CRM_Core_Payment_Form::setDirectDebitFields($form);
46 }
47 else {
48 CRM_Core_Payment_Form::setCreditCardFields($form);
49 }
50 }
51
52 /**
53 * create all common fields needed for a credit card or direct debit transaction
54 *
77b97be7
EM
55 * @param $form
56 *
6a488035
TO
57 * @return void
58 * @access protected
59 */
60 static protected function _setPaymentFields(&$form) {
61 $bltID = $form->_bltID;
62
63 $form->_paymentFields['billing_first_name'] = array(
64 'htmlType' => 'text',
65 'name' => 'billing_first_name',
66 'title' => ts('Billing First Name'),
67 'cc_field' => TRUE,
68 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'),
69 'is_required' => TRUE,
70 );
71
72 $form->_paymentFields['billing_middle_name'] = array(
73 'htmlType' => 'text',
74 'name' => 'billing_middle_name',
75 'title' => ts('Billing Middle Name'),
76 'cc_field' => TRUE,
77 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'),
78 'is_required' => FALSE,
79 );
80
81 $form->_paymentFields['billing_last_name'] = array(
82 'htmlType' => 'text',
83 'name' => 'billing_last_name',
84 'title' => ts('Billing Last Name'),
85 'cc_field' => TRUE,
86 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'),
87 'is_required' => TRUE,
88 );
89
90 $form->_paymentFields["billing_street_address-{$bltID}"] = array(
91 'htmlType' => 'text',
92 'name' => "billing_street_address-{$bltID}",
93 'title' => ts('Street Address'),
94 'cc_field' => TRUE,
95 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'),
96 'is_required' => TRUE,
97 );
98
99 $form->_paymentFields["billing_city-{$bltID}"] = array(
100 'htmlType' => 'text',
101 'name' => "billing_city-{$bltID}",
102 'title' => ts('City'),
103 'cc_field' => TRUE,
104 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'),
105 'is_required' => TRUE,
106 );
107
108 $form->_paymentFields["billing_state_province_id-{$bltID}"] = array(
c927c151 109 'htmlType' => 'chainSelect',
757069de 110 'title' => ts('State/Province'),
6a488035 111 'name' => "billing_state_province_id-{$bltID}",
6a488035 112 'cc_field' => TRUE,
bc999cd1 113 'is_required' => TRUE,
6a488035
TO
114 );
115
116 $form->_paymentFields["billing_postal_code-{$bltID}"] = array(
117 'htmlType' => 'text',
118 'name' => "billing_postal_code-{$bltID}",
119 'title' => ts('Postal Code'),
120 'cc_field' => TRUE,
121 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'),
122 'is_required' => TRUE,
123 );
124
125 $form->_paymentFields["billing_country_id-{$bltID}"] = array(
126 'htmlType' => 'select',
127 'name' => "billing_country_id-{$bltID}",
128 'title' => ts('Country'),
129 'cc_field' => TRUE,
130 'attributes' => array(
131 '' => ts('- select -')) +
132 CRM_Core_PseudoConstant::country(),
133 'is_required' => TRUE,
134 );
135 }
136
137 /**
138 * create all fields needed for a credit card transaction
139 *
dd244018
EM
140 * @param $form
141 *
6a488035
TO
142 * @return void
143 * @access public
144 */
145 static function setCreditCardFields(&$form) {
146 CRM_Core_Payment_Form::_setPaymentFields($form);
147
148 $form->_paymentFields['credit_card_number'] = array(
149 'htmlType' => 'text',
150 'name' => 'credit_card_number',
151 'title' => ts('Card Number'),
152 'cc_field' => TRUE,
153 'attributes' => array('size' => 20, 'maxlength' => 20, 'autocomplete' => 'off'),
154 'is_required' => TRUE,
155 );
156
157 $form->_paymentFields['cvv2'] = array(
158 'htmlType' => 'text',
159 'name' => 'cvv2',
160 'title' => ts('Security Code'),
161 'cc_field' => TRUE,
162 'attributes' => array('size' => 5, 'maxlength' => 10, 'autocomplete' => 'off'),
163 'is_required' => CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME,
164 'cvv_backoffice_required',
165 NULL
166 ,1
167 ),
168 );
169
170 $form->_paymentFields['credit_card_exp_date'] = array(
171 'htmlType' => 'date',
172 'name' => 'credit_card_exp_date',
173 'title' => ts('Expiration Date'),
174 'cc_field' => TRUE,
175 'attributes' => CRM_Core_SelectValues::date('creditCard'),
176 'is_required' => TRUE,
177 );
178
179 $creditCardType = array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::creditCard();
180 $form->_paymentFields['credit_card_type'] = array(
181 'htmlType' => 'select',
182 'name' => 'credit_card_type',
183 'title' => ts('Card Type'),
184 'cc_field' => TRUE,
185 'attributes' => $creditCardType,
15c6517d 186 'is_required' => FALSE,
6a488035
TO
187 );
188 }
189
3f3a3ba0
CW
190 /**
191 * create all fields needed for direct debit transaction
6a488035 192 *
da6b46f4
EM
193 * @param $form
194 *
6a488035
TO
195 * @return void
196 * @access public
197 */
198 static function setDirectDebitFields(&$form) {
199 CRM_Core_Payment_Form::_setPaymentFields($form);
200
201 $form->_paymentFields['account_holder'] = array(
202 'htmlType' => 'text',
203 'name' => 'account_holder',
204 'title' => ts('Account Holder'),
205 'cc_field' => TRUE,
206 'attributes' => array('size' => 20, 'maxlength' => 34, 'autocomplete' => 'on'),
207 'is_required' => TRUE,
208 );
209
210 //e.g. IBAN can have maxlength of 34 digits
211 $form->_paymentFields['bank_account_number'] = array(
212 'htmlType' => 'text',
213 'name' => 'bank_account_number',
214 'title' => ts('Bank Account Number'),
215 'cc_field' => TRUE,
216 'attributes' => array('size' => 20, 'maxlength' => 34, 'autocomplete' => 'off'),
217 'is_required' => TRUE,
218 );
219
220 //e.g. SWIFT-BIC can have maxlength of 11 digits
221 $form->_paymentFields['bank_identification_number'] = array(
222 'htmlType' => 'text',
223 'name' => 'bank_identification_number',
224 'title' => ts('Bank Identification Number'),
225 'cc_field' => TRUE,
226 'attributes' => array('size' => 20, 'maxlength' => 11, 'autocomplete' => 'off'),
227 'is_required' => TRUE,
228 );
229
230 $form->_paymentFields['bank_name'] = array(
231 'htmlType' => 'text',
232 'name' => 'bank_name',
233 'title' => ts('Bank Name'),
234 'cc_field' => TRUE,
235 'attributes' => array('size' => 20, 'maxlength' => 64, 'autocomplete' => 'off'),
236 'is_required' => TRUE,
237 );
238 }
239
240 /**
241 * Function to add all the credit card fields
242 *
fd31fa4c
EM
243 * @param $form
244 * @param bool $useRequired
245 *
15c6517d 246 * @return void
6a488035
TO
247 * @access public
248 */
249 static function buildCreditCard(&$form, $useRequired = FALSE) {
250 if ($form->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_FORM) {
251 self::setCreditCardFields($form);
252 foreach ($form->_paymentFields as $name => $field) {
c927c151
CW
253 if (!empty($field['cc_field'])) {
254 if ($field['htmlType'] == 'chainSelect') {
255 $form->addChainSelect($field['name'], array('required' => $useRequired && $field['is_required']));
256 }
257 else {
258 $form->add($field['htmlType'],
259 $field['name'],
260 $field['title'],
261 $field['attributes'],
262 $useRequired ? $field['is_required'] : FALSE
263 );
264 }
6a488035
TO
265 }
266 }
267
268 $form->addRule('cvv2',
269 ts('Please enter a valid value for your card security code. This is usually the last 3-4 digits on the card\'s signature panel.'),
270 'integer'
271 );
272
273 $form->addRule('credit_card_exp_date',
8543f7c1 274 ts('Card expiration date cannot be a past date.'),
6a488035
TO
275 'currentDate', TRUE
276 );
277
6a488035
TO
278 }
279
280 if ($form->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON) {
281 $form->_expressButtonName = $form->getButtonName('upload', 'express');
282 $form->assign('expressButtonName', $form->_expressButtonName);
283 $form->add('image',
284 $form->_expressButtonName,
285 $form->_paymentProcessor['url_button'],
97e557d7 286 array('class' => 'crm-form-submit')
6a488035
TO
287 );
288 }
289 }
290
bef9421f
CW
291 /**
292 * The credit card pseudo constant results only the CC label, not the key ID
293 * So we normalize the name to use it as a CSS class.
294 */
295 static function getCreditCardCSSNames() {
296 $creditCardTypes = array();
297 foreach (CRM_Contribute_PseudoConstant::creditCard() as $key => $name) {
298 // Replace anything not css-friendly by an underscore
299 // Non-latin names will not like this, but so many things are wrong with
300 // the credit-card type configurations already.
301 $key = str_replace(' ', '', $key);
302 $key = preg_replace('/[^a-zA-Z0-9]/', '_', $key);
303 $key = strtolower($key);
304 $creditCardTypes[$key] = $name;
305 }
306 return $creditCardTypes;
307 }
308
6a488035
TO
309 /**
310 * Function to add all the direct debit fields
311 *
da6b46f4
EM
312 * @param $form
313 * @param bool $useRequired
355ba699 314 * @return void
6a488035
TO
315 * @access public
316 */
4691b077 317 static function buildDirectDebit(&$form, $useRequired = FALSE) {
6a488035
TO
318 if ($form->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_FORM) {
319 self::setDirectDebitFields($form);
320 foreach ($form->_paymentFields as $name => $field) {
321 if (isset($field['cc_field']) &&
322 $field['cc_field']
323 ) {
324 $form->add($field['htmlType'],
325 $field['name'],
326 $field['title'],
327 $field['attributes'],
328 $useRequired ? $field['is_required'] : FALSE
329 );
330 }
331 }
332
333 $form->addRule('bank_identification_number',
334 ts('Please enter a valid Bank Identification Number (value must not contain punctuation characters).'),
335 'nopunctuation'
336 );
337
338 $form->addRule('bank_account_number',
339 ts('Please enter a valid Bank Account Number (value must not contain punctuation characters).'),
340 'nopunctuation'
341 );
342 }
343
344 if ($form->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON) {
345 $form->_expressButtonName = $form->getButtonName($form->buttonType(), 'express');
346 $form->add('image',
347 $form->_expressButtonName,
348 $form->_paymentProcessor['url_button'],
97e557d7 349 array('class' => 'crm-form-submit')
6a488035
TO
350 );
351 }
352 }
353
7cb3d4f0
CW
354 /**
355 * Make sure that credit card number and cvv are valid
356 * Called within the scope of a QF formRule function
357 */
358 static function validateCreditCard($values, &$errors) {
359 if (!empty($values['credit_card_type'])) {
360 if (!empty($values['credit_card_number']) &&
361 !CRM_Utils_Rule::creditCardNumber($values['credit_card_number'], $values['credit_card_type'])
362 ) {
8543f7c1 363 $errors['credit_card_number'] = ts('Please enter a valid Card Number');
7cb3d4f0
CW
364 }
365 if (!empty($values['cvv2']) &&
366 !CRM_Utils_Rule::cvv($values['cvv2'], $values['credit_card_type'])
367 ) {
8543f7c1 368 $errors['cvv2'] = ts('Please enter a valid Card Verification Number');
7cb3d4f0
CW
369 }
370 }
15c6517d 371 elseif (!empty($values['credit_card_number'])) {
8543f7c1 372 $errors['credit_card_number'] = ts('Please enter a valid Card Number');
15c6517d 373 }
7cb3d4f0
CW
374 }
375
6a488035
TO
376 /**
377 * function to map address fields
378 *
77b97be7
EM
379 * @param $id
380 * @param $src
381 * @param $dst
382 * @param bool $reverse
383 *
6a488035
TO
384 * @return void
385 * @static
386 */
387 static function mapParams($id, &$src, &$dst, $reverse = FALSE) {
388 static $map = NULL;
389 if (!$map) {
390 $map = array(
391 'first_name' => 'billing_first_name',
392 'middle_name' => 'billing_middle_name',
393 'last_name' => 'billing_last_name',
394 'email' => "email-$id",
395 'street_address' => "billing_street_address-$id",
396 'supplemental_address_1' => "billing_supplemental_address_1-$id",
397 'city' => "billing_city-$id",
398 'state_province' => "billing_state_province-$id",
399 'postal_code' => "billing_postal_code-$id",
400 'country' => "billing_country-$id",
401 );
402 }
403
404 foreach ($map as $n => $v) {
405 if (!$reverse) {
406 if (isset($src[$n])) {
407 $dst[$v] = $src[$n];
408 }
409 }
410 else {
411 if (isset($src[$v])) {
412 $dst[$n] = $src[$v];
413 }
414 }
415 }
416 }
417
418 /**
419 * function to get the credit card expiration month
420 * The date format for this field should typically be "M Y" (ex: Feb 2011) or "m Y" (02 2011)
421 * See CRM-9017
422 *
2a6da8d7
EM
423 * @param $src
424 *
6a488035
TO
425 * @return int
426 * @static
427 */
428 static function getCreditCardExpirationMonth($src) {
429 if ($month = CRM_Utils_Array::value('M', $src['credit_card_exp_date'])) {
430 return $month;
431 }
432
433 return CRM_Utils_Array::value('m', $src['credit_card_exp_date']);
434 }
435
436 /**
437 * function to get the credit card expiration year
438 * The date format for this field should typically be "M Y" (ex: Feb 2011) or "m Y" (02 2011)
439 * This function exists only to make it consistant with getCreditCardExpirationMonth
440 *
2a6da8d7
EM
441 * @param $src
442 *
6a488035
TO
443 * @return int
444 * @static
445 */
446 static function getCreditCardExpirationYear($src) {
447 return CRM_Utils_Array::value('Y', $src['credit_card_exp_date']);
448 }
449
6a488035
TO
450}
451