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