Commit | Line | Data |
---|---|---|
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 | */ | |
35 | class CRM_Core_Payment_Form { | |
36 | ||
dc913073 EM |
37 | |
38 | /** | |
9c39fb25 | 39 | * Add payment fields are depending on payment processor |
dc913073 | 40 | * |
3392cb17 | 41 | * @param CRM_Contribute_Form_Contribution| CRM_Contribute_Form_Contribution_Main $form |
459091e1 EM |
42 | * @todo - add other forms specifically to the definition - since we don't have for $form - since we aren't adding the property to |
43 | * CRM_Core_Form (don't suppose we should?) | |
dc913073 EM |
44 | * @param array $processor array of properties including 'object' as loaded from CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors |
45 | */ | |
46 | static public function setPaymentFieldsByProcessor(&$form, $processor) { | |
47 | $form->billingFieldSets = array(); | |
48 | $paymentFields = self::getPaymentFields($processor); | |
dc913073 EM |
49 | $paymentTypeName = self::getPaymentTypeName($processor); |
50 | $paymentTypeLabel = self::getPaymentTypeLabel($processor); | |
51 | //@todo if we switch to iterating through the fieldsets we won't need to assign these directly | |
52 | $form->assign('paymentTypeName', $paymentTypeName); | |
53 | $form->assign('paymentTypeLabel', $paymentTypeLabel); | |
54 | ||
55 | $form->billingFieldSets[$paymentTypeName]['fields'] = $form->_paymentFields = array_intersect_key(self::getPaymentFieldMetadata($processor), array_flip($paymentFields)); | |
56 | $form->billingPane = array($paymentTypeName => $paymentTypeLabel); | |
57 | $form->assign('paymentFields', $paymentFields); | |
58 | if ($processor['billing_mode'] != 4) { | |
59 | //@todo setPaymentFields defines the billing fields - this should be moved to the processor class & renamed getBillingFields | |
459091e1 | 60 | // currently we just add the standard lot unless it's an off-site processor (& then add none) |
dc913073 EM |
61 | //also set the billingFieldSet to hold all the details required to render the fieldset so we can iterate through the fieldset - making |
62 | // it easier to re-order. For not the billingFieldSets param is used to determine whether to show the billing pane | |
63 | CRM_Core_Payment_Form::_setPaymentFields($form); | |
64 | $form->billingFieldSets['billing_name_address-group']['fields'] = array(); | |
65 | } | |
66 | } | |
9c39fb25 | 67 | |
6a488035 | 68 | /** |
9c39fb25 EM |
69 | * add general billing fields |
70 | * @todo set these like processor fields & let payment processors alter them | |
71 | * | |
6a488035 | 72 | * |
77b97be7 EM |
73 | * @param $form |
74 | * | |
6a488035 TO |
75 | * @return void |
76 | * @access protected | |
77 | */ | |
78 | static protected function _setPaymentFields(&$form) { | |
79 | $bltID = $form->_bltID; | |
80 | ||
81 | $form->_paymentFields['billing_first_name'] = array( | |
82 | 'htmlType' => 'text', | |
83 | 'name' => 'billing_first_name', | |
84 | 'title' => ts('Billing First Name'), | |
85 | 'cc_field' => TRUE, | |
86 | 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'), | |
87 | 'is_required' => TRUE, | |
88 | ); | |
89 | ||
90 | $form->_paymentFields['billing_middle_name'] = array( | |
91 | 'htmlType' => 'text', | |
92 | 'name' => 'billing_middle_name', | |
93 | 'title' => ts('Billing Middle Name'), | |
94 | 'cc_field' => TRUE, | |
95 | 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'), | |
96 | 'is_required' => FALSE, | |
97 | ); | |
98 | ||
99 | $form->_paymentFields['billing_last_name'] = array( | |
100 | 'htmlType' => 'text', | |
101 | 'name' => 'billing_last_name', | |
102 | 'title' => ts('Billing Last Name'), | |
103 | 'cc_field' => TRUE, | |
104 | 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'), | |
105 | 'is_required' => TRUE, | |
106 | ); | |
107 | ||
108 | $form->_paymentFields["billing_street_address-{$bltID}"] = array( | |
109 | 'htmlType' => 'text', | |
110 | 'name' => "billing_street_address-{$bltID}", | |
111 | 'title' => ts('Street Address'), | |
112 | 'cc_field' => TRUE, | |
113 | 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'), | |
114 | 'is_required' => TRUE, | |
115 | ); | |
116 | ||
117 | $form->_paymentFields["billing_city-{$bltID}"] = array( | |
118 | 'htmlType' => 'text', | |
119 | 'name' => "billing_city-{$bltID}", | |
120 | 'title' => ts('City'), | |
121 | 'cc_field' => TRUE, | |
122 | 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'), | |
123 | 'is_required' => TRUE, | |
124 | ); | |
125 | ||
126 | $form->_paymentFields["billing_state_province_id-{$bltID}"] = array( | |
c927c151 | 127 | 'htmlType' => 'chainSelect', |
757069de | 128 | 'title' => ts('State/Province'), |
6a488035 | 129 | 'name' => "billing_state_province_id-{$bltID}", |
6a488035 | 130 | 'cc_field' => TRUE, |
bc999cd1 | 131 | 'is_required' => TRUE, |
6a488035 TO |
132 | ); |
133 | ||
134 | $form->_paymentFields["billing_postal_code-{$bltID}"] = array( | |
135 | 'htmlType' => 'text', | |
136 | 'name' => "billing_postal_code-{$bltID}", | |
137 | 'title' => ts('Postal Code'), | |
138 | 'cc_field' => TRUE, | |
139 | 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'), | |
140 | 'is_required' => TRUE, | |
141 | ); | |
142 | ||
143 | $form->_paymentFields["billing_country_id-{$bltID}"] = array( | |
144 | 'htmlType' => 'select', | |
145 | 'name' => "billing_country_id-{$bltID}", | |
146 | 'title' => ts('Country'), | |
147 | 'cc_field' => TRUE, | |
148 | 'attributes' => array( | |
149 | '' => ts('- select -')) + | |
150 | CRM_Core_PseudoConstant::country(), | |
151 | 'is_required' => TRUE, | |
152 | ); | |
92ec1d7f EM |
153 | //CRM-15509 working towards giving control over billing fields to payment processors. For now removing tpl hard-coding |
154 | $smarty = CRM_Core_Smarty::singleton(); | |
155 | $smarty->assign('billingDetailsFields', array( | |
156 | 'billing_first_name', | |
157 | 'billing_middle_name', | |
158 | 'billing_last_name', | |
159 | "billing_street_address-{$bltID}", | |
160 | "billing_city-{$bltID}", | |
161 | "billing_country_id-{$bltID}", | |
95e19ac6 | 162 | "billing_state_province_id-{$bltID}", |
92ec1d7f EM |
163 | "billing_postal_code-{$bltID}", |
164 | )); | |
6a488035 TO |
165 | } |
166 | ||
167 | /** | |
168 | * create all fields needed for a credit card transaction | |
9c39fb25 | 169 | * @deprecated - use the setPaymentFieldsByProcessor which leverages the processor to determine the fields |
c46f87cf | 170 | * @param CRM_Core_Form $form |
dd244018 | 171 | * |
6a488035 TO |
172 | * @return void |
173 | * @access public | |
174 | */ | |
175 | static function setCreditCardFields(&$form) { | |
176 | CRM_Core_Payment_Form::_setPaymentFields($form); | |
177 | ||
178 | $form->_paymentFields['credit_card_number'] = array( | |
179 | 'htmlType' => 'text', | |
180 | 'name' => 'credit_card_number', | |
181 | 'title' => ts('Card Number'), | |
182 | 'cc_field' => TRUE, | |
183 | 'attributes' => array('size' => 20, 'maxlength' => 20, 'autocomplete' => 'off'), | |
184 | 'is_required' => TRUE, | |
185 | ); | |
186 | ||
187 | $form->_paymentFields['cvv2'] = array( | |
188 | 'htmlType' => 'text', | |
189 | 'name' => 'cvv2', | |
190 | 'title' => ts('Security Code'), | |
191 | 'cc_field' => TRUE, | |
192 | 'attributes' => array('size' => 5, 'maxlength' => 10, 'autocomplete' => 'off'), | |
193 | 'is_required' => CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, | |
194 | 'cvv_backoffice_required', | |
195 | NULL | |
196 | ,1 | |
197 | ), | |
198 | ); | |
199 | ||
200 | $form->_paymentFields['credit_card_exp_date'] = array( | |
201 | 'htmlType' => 'date', | |
202 | 'name' => 'credit_card_exp_date', | |
203 | 'title' => ts('Expiration Date'), | |
204 | 'cc_field' => TRUE, | |
205 | 'attributes' => CRM_Core_SelectValues::date('creditCard'), | |
206 | 'is_required' => TRUE, | |
207 | ); | |
208 | ||
209 | $creditCardType = array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::creditCard(); | |
210 | $form->_paymentFields['credit_card_type'] = array( | |
211 | 'htmlType' => 'select', | |
212 | 'name' => 'credit_card_type', | |
213 | 'title' => ts('Card Type'), | |
214 | 'cc_field' => TRUE, | |
215 | 'attributes' => $creditCardType, | |
15c6517d | 216 | 'is_required' => FALSE, |
6a488035 | 217 | ); |
fbfd63ec EM |
218 | //CRM-15509 this is probably a temporary resting place for these form assignments but we are working towards putting this |
219 | // in an option group & having php / payment processors define the billing form rather than the tpl | |
220 | $smarty = CRM_Core_Smarty::singleton(); | |
221 | $smarty->assign('paymentTypeName', 'credit_card'); | |
222 | $smarty->assign('paymentTypeLabel', ts('Credit Card Information')); | |
44b6505d | 223 | $smarty->assign('paymentFields', self::getPaymentFields($form->_paymentProcessor)); |
6a488035 TO |
224 | } |
225 | ||
c46f87cf CW |
226 | /** |
227 | * @param CRM_Core_Form $form | |
92ec1d7f | 228 | * @param bool $useRequired |
dc913073 | 229 | * @param array $paymentFields |
c46f87cf | 230 | */ |
9c39fb25 | 231 | protected static function addCommonFields(&$form, $useRequired, $paymentFields) { |
dc913073 | 232 | foreach ($paymentFields as $name => $field) { |
c46f87cf CW |
233 | if (!empty($field['cc_field'])) { |
234 | if ($field['htmlType'] == 'chainSelect') { | |
235 | $form->addChainSelect($field['name'], array('required' => $useRequired && $field['is_required'])); | |
236 | } | |
237 | else { | |
238 | $form->add($field['htmlType'], | |
239 | $field['name'], | |
240 | $field['title'], | |
241 | $field['attributes'], | |
242 | $useRequired ? $field['is_required'] : FALSE | |
243 | ); | |
244 | } | |
245 | } | |
246 | } | |
247 | } | |
248 | ||
3f3a3ba0 CW |
249 | /** |
250 | * create all fields needed for direct debit transaction | |
9c39fb25 | 251 | * @deprecated - use the setPaymentFieldsByProcessor which leverages the processor to determine the fields |
da6b46f4 EM |
252 | * @param $form |
253 | * | |
6a488035 TO |
254 | * @return void |
255 | * @access public | |
256 | */ | |
257 | static function setDirectDebitFields(&$form) { | |
258 | CRM_Core_Payment_Form::_setPaymentFields($form); | |
259 | ||
260 | $form->_paymentFields['account_holder'] = array( | |
261 | 'htmlType' => 'text', | |
262 | 'name' => 'account_holder', | |
263 | 'title' => ts('Account Holder'), | |
264 | 'cc_field' => TRUE, | |
265 | 'attributes' => array('size' => 20, 'maxlength' => 34, 'autocomplete' => 'on'), | |
266 | 'is_required' => TRUE, | |
267 | ); | |
268 | ||
269 | //e.g. IBAN can have maxlength of 34 digits | |
270 | $form->_paymentFields['bank_account_number'] = array( | |
271 | 'htmlType' => 'text', | |
272 | 'name' => 'bank_account_number', | |
273 | 'title' => ts('Bank Account Number'), | |
274 | 'cc_field' => TRUE, | |
275 | 'attributes' => array('size' => 20, 'maxlength' => 34, 'autocomplete' => 'off'), | |
276 | 'is_required' => TRUE, | |
277 | ); | |
278 | ||
279 | //e.g. SWIFT-BIC can have maxlength of 11 digits | |
280 | $form->_paymentFields['bank_identification_number'] = array( | |
281 | 'htmlType' => 'text', | |
282 | 'name' => 'bank_identification_number', | |
283 | 'title' => ts('Bank Identification Number'), | |
284 | 'cc_field' => TRUE, | |
285 | 'attributes' => array('size' => 20, 'maxlength' => 11, 'autocomplete' => 'off'), | |
286 | 'is_required' => TRUE, | |
287 | ); | |
288 | ||
289 | $form->_paymentFields['bank_name'] = array( | |
290 | 'htmlType' => 'text', | |
291 | 'name' => 'bank_name', | |
292 | 'title' => ts('Bank Name'), | |
293 | 'cc_field' => TRUE, | |
294 | 'attributes' => array('size' => 20, 'maxlength' => 64, 'autocomplete' => 'off'), | |
295 | 'is_required' => TRUE, | |
296 | ); | |
fbfd63ec EM |
297 | //CRM-15509 this is probably a temporary resting place for these form assignments but we are working towards putting this |
298 | // in an option group & having php / payment processors define the billing form rather than the tpl | |
299 | $smarty = CRM_Core_Smarty::singleton(); | |
300 | // replace these payment type names with an option group - moving name & label assumptions out of the tpl is a step towards that | |
301 | $smarty->assign('paymentTypeName', 'direct_debit'); | |
302 | $smarty->assign('paymentTypeLabel', ts('Direct Debit Information')); | |
44b6505d EM |
303 | $smarty->assign('paymentFields', self::getPaymentFields($form->_paymentProcessor)); |
304 | } | |
305 | ||
306 | /** | |
307 | * @param array $paymentProcessor | |
dc913073 EM |
308 | * @todo it will be necessary to set details that affect it - mostly likely take Country as a param. Should we add generic |
309 | * setParams on processor class or just setCountry which we know we need? | |
44b6505d | 310 | * |
dc913073 | 311 | * @return array |
44b6505d EM |
312 | */ |
313 | static function getPaymentFields($paymentProcessor) { | |
44b6505d | 314 | $paymentProcessorObject = CRM_Core_Payment::singleton(($paymentProcessor['is_test'] ? 'test' : 'live'), $paymentProcessor); |
dc913073 | 315 | return $paymentProcessorObject->getPaymentFormFields(); |
44b6505d EM |
316 | } |
317 | ||
318 | /** | |
dc913073 | 319 | * @param array $paymentProcessor |
44b6505d | 320 | * |
dc913073 EM |
321 | * @return array |
322 | */ | |
323 | static function getPaymentFieldMetadata($paymentProcessor) { | |
324 | $paymentProcessorObject = CRM_Core_Payment::singleton(($paymentProcessor['is_test'] ? 'test' : 'live'), $paymentProcessor); | |
325 | return $paymentProcessorObject->getPaymentFormFieldsMetadata(); | |
326 | } | |
327 | ||
328 | /** | |
44b6505d | 329 | * @param array $paymentProcessor |
44b6505d | 330 | * |
dc913073 | 331 | * @return string |
44b6505d | 332 | */ |
dc913073 EM |
333 | static function getPaymentTypeName($paymentProcessor) { |
334 | $paymentProcessorObject = CRM_Core_Payment::singleton(($paymentProcessor['is_test'] ? 'test' : 'live'), $paymentProcessor); | |
335 | return $paymentProcessorObject->getPaymentTypeName(); | |
336 | } | |
44b6505d | 337 | |
dc913073 EM |
338 | /** |
339 | * @param array $paymentProcessor | |
340 | * | |
341 | * @return string | |
342 | */ | |
343 | static function getPaymentTypeLabel($paymentProcessor) { | |
344 | $paymentProcessorObject = CRM_Core_Payment::singleton(($paymentProcessor['is_test'] ? 'test' : 'live'), $paymentProcessor); | |
345 | return ts(($paymentProcessorObject->getPaymentTypeLabel()) . ' Information'); | |
44b6505d EM |
346 | } |
347 | ||
dc913073 | 348 | /** |
cc984198 | 349 | * @param CRM_Contribute_Form_Contribution| CRM_Contribute_Form_Contribution_Main|CRM_Member_Form_Membership $form |
dc913073 EM |
350 | * @param array $processor array of properties including 'object' as loaded from CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors |
351 | * @param bool $isBillingDataOptional | |
352 | * | |
353 | * @return bool | |
354 | */ | |
355 | static function buildPaymentForm($form, $processor, $isBillingDataOptional){ | |
356 | self::setPaymentFieldsByProcessor($form, $processor); | |
357 | self::addCommonFields($form, !$isBillingDataOptional, $form->_paymentFields); | |
358 | self::addRules($form, $form->_paymentFields); | |
359 | self::addPaypalExpressCode($form); | |
360 | return (!empty($form->_paymentFields)); | |
361 | } | |
44b6505d | 362 | |
dc913073 EM |
363 | /** |
364 | * @param CRM_Core_Form $form | |
365 | * @param array $paymentFields array of properties including 'object' as loaded from CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors | |
366 | ||
367 | * @param $paymentFields | |
368 | */ | |
9c39fb25 | 369 | protected static function addRules(&$form, $paymentFields) { |
dc913073 EM |
370 | foreach ($paymentFields as $paymentField => $fieldSpecs) { |
371 | if (!empty($fieldSpecs['rules'])) { | |
372 | foreach ($fieldSpecs['rules'] as $rule) { | |
373 | $form->addRule($paymentField, | |
374 | $rule['rule_message'], | |
375 | $rule['rule_name'], | |
376 | $rule['rule_parameters'] | |
377 | ); | |
378 | } | |
379 | } | |
380 | } | |
6a488035 TO |
381 | } |
382 | ||
383 | /** | |
dc913073 EM |
384 | * billing mode button is basically synonymous with paypal express - this is probably a good example of 'odds & sods' code we |
385 | * 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 | 386 | * |
fd31fa4c | 387 | * @param $form |
dc913073 | 388 | */ |
9c39fb25 | 389 | protected static function addPaypalExpressCode(&$form) { |
dc913073 EM |
390 | if ($form->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON) { |
391 | $form->_expressButtonName = $form->getButtonName('upload', 'express'); | |
392 | $form->assign('expressButtonName', $form->_expressButtonName); | |
393 | $form->add('image', | |
394 | $form->_expressButtonName, | |
395 | $form->_paymentProcessor['url_button'], | |
396 | array('class' => 'crm-form-submit') | |
397 | ); | |
398 | } | |
399 | } | |
400 | /** | |
401 | * Function to add all the credit card fields | |
402 | * @deprecated Use BuildPaymentForm | |
403 | * @param $form | |
fd31fa4c EM |
404 | * @param bool $useRequired |
405 | * | |
15c6517d | 406 | * @return void |
6a488035 TO |
407 | * @access public |
408 | */ | |
409 | static function buildCreditCard(&$form, $useRequired = FALSE) { | |
410 | if ($form->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_FORM) { | |
411 | self::setCreditCardFields($form); | |
dc913073 | 412 | self::addCommonFields($form, $useRequired, $form->_paymentFields); |
6a488035 TO |
413 | |
414 | $form->addRule('cvv2', | |
415 | 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.'), | |
416 | 'integer' | |
417 | ); | |
418 | ||
419 | $form->addRule('credit_card_exp_date', | |
8543f7c1 | 420 | ts('Card expiration date cannot be a past date.'), |
6a488035 TO |
421 | 'currentDate', TRUE |
422 | ); | |
423 | ||
6a488035 TO |
424 | } |
425 | ||
dc913073 | 426 | |
6a488035 TO |
427 | if ($form->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON) { |
428 | $form->_expressButtonName = $form->getButtonName('upload', 'express'); | |
429 | $form->assign('expressButtonName', $form->_expressButtonName); | |
430 | $form->add('image', | |
431 | $form->_expressButtonName, | |
432 | $form->_paymentProcessor['url_button'], | |
97e557d7 | 433 | array('class' => 'crm-form-submit') |
6a488035 TO |
434 | ); |
435 | } | |
436 | } | |
437 | ||
bef9421f CW |
438 | /** |
439 | * The credit card pseudo constant results only the CC label, not the key ID | |
440 | * So we normalize the name to use it as a CSS class. | |
441 | */ | |
442 | static function getCreditCardCSSNames() { | |
443 | $creditCardTypes = array(); | |
444 | foreach (CRM_Contribute_PseudoConstant::creditCard() as $key => $name) { | |
445 | // Replace anything not css-friendly by an underscore | |
446 | // Non-latin names will not like this, but so many things are wrong with | |
447 | // the credit-card type configurations already. | |
448 | $key = str_replace(' ', '', $key); | |
449 | $key = preg_replace('/[^a-zA-Z0-9]/', '_', $key); | |
450 | $key = strtolower($key); | |
451 | $creditCardTypes[$key] = $name; | |
452 | } | |
453 | return $creditCardTypes; | |
454 | } | |
455 | ||
6a488035 TO |
456 | /** |
457 | * Function to add all the direct debit fields | |
9c39fb25 | 458 | * @deprecated use buildPaymentForm |
6a488035 | 459 | * |
da6b46f4 EM |
460 | * @param $form |
461 | * @param bool $useRequired | |
355ba699 | 462 | * @return void |
6a488035 TO |
463 | * @access public |
464 | */ | |
4691b077 | 465 | static function buildDirectDebit(&$form, $useRequired = FALSE) { |
6a488035 TO |
466 | if ($form->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_FORM) { |
467 | self::setDirectDebitFields($form); | |
dc913073 | 468 | self::addCommonFields($form, $useRequired, $form->_paymentFields); |
6a488035 TO |
469 | |
470 | $form->addRule('bank_identification_number', | |
471 | ts('Please enter a valid Bank Identification Number (value must not contain punctuation characters).'), | |
472 | 'nopunctuation' | |
473 | ); | |
474 | ||
475 | $form->addRule('bank_account_number', | |
476 | ts('Please enter a valid Bank Account Number (value must not contain punctuation characters).'), | |
477 | 'nopunctuation' | |
478 | ); | |
479 | } | |
6a488035 TO |
480 | } |
481 | ||
8ae4d0d3 | 482 | /** |
483 | * Function to add address block | |
484 | * | |
485 | * @param $form | |
486 | * @param bool $useRequired | |
487 | * | |
488 | * @return void | |
489 | * @access public | |
490 | */ | |
491 | static function buildAddressBlock(&$form, $useRequired = FALSE) { | |
492 | CRM_Core_Payment_Form::_setPaymentFields($form); | |
493 | foreach ($form->_paymentFields as $name => $field) { | |
494 | if (isset($field['cc_field']) && | |
495 | $field['cc_field'] | |
496 | ) { | |
497 | $form->add($field['htmlType'], | |
498 | $field['name'], | |
499 | $field['title'], | |
500 | $field['attributes'], | |
501 | $useRequired ? $field['is_required'] : FALSE | |
502 | ); | |
503 | } | |
504 | } | |
505 | ||
506 | // also take care of state country widget | |
507 | $stateCountryMap = array( | |
508 | 1 => array( | |
509 | 'country' => "billing_country_id-{$form->_bltID}", | |
510 | 'state_province' => "billing_state_province_id-{$form->_bltID}", | |
511 | ) | |
512 | ); | |
513 | CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap); | |
514 | } | |
515 | ||
7cb3d4f0 CW |
516 | /** |
517 | * Make sure that credit card number and cvv are valid | |
518 | * Called within the scope of a QF formRule function | |
519 | */ | |
520 | static function validateCreditCard($values, &$errors) { | |
521 | if (!empty($values['credit_card_type'])) { | |
522 | if (!empty($values['credit_card_number']) && | |
523 | !CRM_Utils_Rule::creditCardNumber($values['credit_card_number'], $values['credit_card_type']) | |
524 | ) { | |
8543f7c1 | 525 | $errors['credit_card_number'] = ts('Please enter a valid Card Number'); |
7cb3d4f0 CW |
526 | } |
527 | if (!empty($values['cvv2']) && | |
528 | !CRM_Utils_Rule::cvv($values['cvv2'], $values['credit_card_type']) | |
529 | ) { | |
8543f7c1 | 530 | $errors['cvv2'] = ts('Please enter a valid Card Verification Number'); |
7cb3d4f0 CW |
531 | } |
532 | } | |
15c6517d | 533 | elseif (!empty($values['credit_card_number'])) { |
8543f7c1 | 534 | $errors['credit_card_number'] = ts('Please enter a valid Card Number'); |
15c6517d | 535 | } |
7cb3d4f0 CW |
536 | } |
537 | ||
6a488035 TO |
538 | /** |
539 | * function to map address fields | |
540 | * | |
77b97be7 EM |
541 | * @param $id |
542 | * @param $src | |
543 | * @param $dst | |
544 | * @param bool $reverse | |
545 | * | |
6a488035 TO |
546 | * @return void |
547 | * @static | |
548 | */ | |
549 | static function mapParams($id, &$src, &$dst, $reverse = FALSE) { | |
550 | static $map = NULL; | |
551 | if (!$map) { | |
552 | $map = array( | |
553 | 'first_name' => 'billing_first_name', | |
554 | 'middle_name' => 'billing_middle_name', | |
555 | 'last_name' => 'billing_last_name', | |
556 | 'email' => "email-$id", | |
557 | 'street_address' => "billing_street_address-$id", | |
558 | 'supplemental_address_1' => "billing_supplemental_address_1-$id", | |
559 | 'city' => "billing_city-$id", | |
560 | 'state_province' => "billing_state_province-$id", | |
561 | 'postal_code' => "billing_postal_code-$id", | |
562 | 'country' => "billing_country-$id", | |
563 | ); | |
564 | } | |
565 | ||
566 | foreach ($map as $n => $v) { | |
567 | if (!$reverse) { | |
568 | if (isset($src[$n])) { | |
569 | $dst[$v] = $src[$n]; | |
570 | } | |
571 | } | |
572 | else { | |
573 | if (isset($src[$v])) { | |
574 | $dst[$n] = $src[$v]; | |
575 | } | |
576 | } | |
577 | } | |
578 | } | |
579 | ||
580 | /** | |
581 | * function to get the credit card expiration month | |
582 | * The date format for this field should typically be "M Y" (ex: Feb 2011) or "m Y" (02 2011) | |
583 | * See CRM-9017 | |
584 | * | |
2a6da8d7 EM |
585 | * @param $src |
586 | * | |
6a488035 TO |
587 | * @return int |
588 | * @static | |
589 | */ | |
590 | static function getCreditCardExpirationMonth($src) { | |
591 | if ($month = CRM_Utils_Array::value('M', $src['credit_card_exp_date'])) { | |
592 | return $month; | |
593 | } | |
594 | ||
595 | return CRM_Utils_Array::value('m', $src['credit_card_exp_date']); | |
596 | } | |
597 | ||
598 | /** | |
599 | * function to get the credit card expiration year | |
600 | * The date format for this field should typically be "M Y" (ex: Feb 2011) or "m Y" (02 2011) | |
c1cc3e0c | 601 | * This function exists only to make it consistent with getCreditCardExpirationMonth |
6a488035 | 602 | * |
2a6da8d7 EM |
603 | * @param $src |
604 | * | |
6a488035 TO |
605 | * @return int |
606 | * @static | |
607 | */ | |
608 | static function getCreditCardExpirationYear($src) { | |
609 | return CRM_Utils_Array::value('Y', $src['credit_card_exp_date']); | |
610 | } | |
6a488035 | 611 | } |