| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | class CRM_Core_Payment_Form { |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * Add payment fields are depending on payment processor |
| 40 | * |
| 41 | * @param CRM_Contribute_Form_Contribution| CRM_Contribute_Form_Contribution_Main $form |
| 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?) |
| 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); |
| 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 |
| 60 | // currently we just add the standard lot unless it's an off-site processor (& then add none) |
| 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 | } |
| 67 | |
| 68 | /** |
| 69 | * add general billing fields |
| 70 | * @todo set these like processor fields & let payment processors alter them |
| 71 | * |
| 72 | * |
| 73 | * @param $form |
| 74 | * |
| 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( |
| 127 | 'htmlType' => 'chainSelect', |
| 128 | 'title' => ts('State/Province'), |
| 129 | 'name' => "billing_state_province_id-{$bltID}", |
| 130 | 'cc_field' => TRUE, |
| 131 | 'is_required' => TRUE, |
| 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 | ); |
| 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}", |
| 162 | "billing_state_province_id-{$bltID}", |
| 163 | "billing_postal_code-{$bltID}", |
| 164 | )); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * create all fields needed for a credit card transaction |
| 169 | * @deprecated - use the setPaymentFieldsByProcessor which leverages the processor to determine the fields |
| 170 | * @param CRM_Core_Form $form |
| 171 | * |
| 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, |
| 216 | 'is_required' => FALSE, |
| 217 | ); |
| 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')); |
| 223 | $smarty->assign('paymentFields', self::getPaymentFields($form->_paymentProcessor)); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @param CRM_Core_Form $form |
| 228 | * @param bool $useRequired |
| 229 | * @param array $paymentFields |
| 230 | */ |
| 231 | protected static function addCommonFields(&$form, $useRequired, $paymentFields) { |
| 232 | foreach ($paymentFields as $name => $field) { |
| 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 | |
| 249 | /** |
| 250 | * create all fields needed for direct debit transaction |
| 251 | * @deprecated - use the setPaymentFieldsByProcessor which leverages the processor to determine the fields |
| 252 | * @param $form |
| 253 | * |
| 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 | ); |
| 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')); |
| 303 | $smarty->assign('paymentFields', self::getPaymentFields($form->_paymentProcessor)); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * @param array $paymentProcessor |
| 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? |
| 310 | * |
| 311 | * @return array |
| 312 | */ |
| 313 | static function getPaymentFields($paymentProcessor) { |
| 314 | $paymentProcessorObject = CRM_Core_Payment::singleton(($paymentProcessor['is_test'] ? 'test' : 'live'), $paymentProcessor); |
| 315 | return $paymentProcessorObject->getPaymentFormFields(); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * @param array $paymentProcessor |
| 320 | * |
| 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 | /** |
| 329 | * @param array $paymentProcessor |
| 330 | * |
| 331 | * @return string |
| 332 | */ |
| 333 | static function getPaymentTypeName($paymentProcessor) { |
| 334 | $paymentProcessorObject = CRM_Core_Payment::singleton(($paymentProcessor['is_test'] ? 'test' : 'live'), $paymentProcessor); |
| 335 | return $paymentProcessorObject->getPaymentTypeName(); |
| 336 | } |
| 337 | |
| 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'); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * @param CRM_Contribute_Form_Contribution| CRM_Contribute_Form_Contribution_Main|CRM_Member_Form_Membership $form |
| 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 | } |
| 362 | |
| 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 | */ |
| 369 | protected static function addRules(&$form, $paymentFields) { |
| 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 | } |
| 381 | } |
| 382 | |
| 383 | /** |
| 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 |
| 386 | * |
| 387 | * @param $form |
| 388 | */ |
| 389 | protected static function addPaypalExpressCode(&$form) { |
| 390 | if (empty($form->isBackOffice)) { |
| 391 | if ($form->_paymentProcessor['billing_mode'] & |
| 392 | CRM_Core_Payment::BILLING_MODE_BUTTON |
| 393 | ) { |
| 394 | $form->_expressButtonName = $form->getButtonName('upload', 'express'); |
| 395 | $form->assign('expressButtonName', $form->_expressButtonName); |
| 396 | $form->add('image', |
| 397 | $form->_expressButtonName, |
| 398 | $form->_paymentProcessor['url_button'], |
| 399 | array('class' => 'crm-form-submit') |
| 400 | ); |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | /** |
| 405 | * Function to add all the credit card fields |
| 406 | * @deprecated Use BuildPaymentForm |
| 407 | * @param $form |
| 408 | * @param bool $useRequired |
| 409 | * |
| 410 | * @return void |
| 411 | * @access public |
| 412 | */ |
| 413 | static function buildCreditCard(&$form, $useRequired = FALSE) { |
| 414 | if ($form->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_FORM) { |
| 415 | self::setCreditCardFields($form); |
| 416 | self::addCommonFields($form, $useRequired, $form->_paymentFields); |
| 417 | |
| 418 | $form->addRule('cvv2', |
| 419 | 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.'), |
| 420 | 'integer' |
| 421 | ); |
| 422 | |
| 423 | $form->addRule('credit_card_exp_date', |
| 424 | ts('Card expiration date cannot be a past date.'), |
| 425 | 'currentDate', TRUE |
| 426 | ); |
| 427 | |
| 428 | } |
| 429 | |
| 430 | |
| 431 | if ($form->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON) { |
| 432 | $form->_expressButtonName = $form->getButtonName('upload', 'express'); |
| 433 | $form->assign('expressButtonName', $form->_expressButtonName); |
| 434 | $form->add('image', |
| 435 | $form->_expressButtonName, |
| 436 | $form->_paymentProcessor['url_button'], |
| 437 | array('class' => 'crm-form-submit') |
| 438 | ); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * The credit card pseudo constant results only the CC label, not the key ID |
| 444 | * So we normalize the name to use it as a CSS class. |
| 445 | */ |
| 446 | static function getCreditCardCSSNames() { |
| 447 | $creditCardTypes = array(); |
| 448 | foreach (CRM_Contribute_PseudoConstant::creditCard() as $key => $name) { |
| 449 | // Replace anything not css-friendly by an underscore |
| 450 | // Non-latin names will not like this, but so many things are wrong with |
| 451 | // the credit-card type configurations already. |
| 452 | $key = str_replace(' ', '', $key); |
| 453 | $key = preg_replace('/[^a-zA-Z0-9]/', '_', $key); |
| 454 | $key = strtolower($key); |
| 455 | $creditCardTypes[$key] = $name; |
| 456 | } |
| 457 | return $creditCardTypes; |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Function to add all the direct debit fields |
| 462 | * @deprecated use buildPaymentForm |
| 463 | * |
| 464 | * @param $form |
| 465 | * @param bool $useRequired |
| 466 | * @return void |
| 467 | * @access public |
| 468 | */ |
| 469 | static function buildDirectDebit(&$form, $useRequired = FALSE) { |
| 470 | if ($form->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_FORM) { |
| 471 | self::setDirectDebitFields($form); |
| 472 | self::addCommonFields($form, $useRequired, $form->_paymentFields); |
| 473 | |
| 474 | $form->addRule('bank_identification_number', |
| 475 | ts('Please enter a valid Bank Identification Number (value must not contain punctuation characters).'), |
| 476 | 'nopunctuation' |
| 477 | ); |
| 478 | |
| 479 | $form->addRule('bank_account_number', |
| 480 | ts('Please enter a valid Bank Account Number (value must not contain punctuation characters).'), |
| 481 | 'nopunctuation' |
| 482 | ); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Function to add address block |
| 488 | * |
| 489 | * @param $form |
| 490 | * @param bool $useRequired |
| 491 | * |
| 492 | * @return void |
| 493 | * @access public |
| 494 | */ |
| 495 | static function buildAddressBlock(&$form, $useRequired = FALSE) { |
| 496 | CRM_Core_Payment_Form::_setPaymentFields($form); |
| 497 | foreach ($form->_paymentFields as $name => $field) { |
| 498 | if (isset($field['cc_field']) && |
| 499 | $field['cc_field'] |
| 500 | ) { |
| 501 | $form->add($field['htmlType'], |
| 502 | $field['name'], |
| 503 | $field['title'], |
| 504 | $field['attributes'], |
| 505 | $useRequired ? $field['is_required'] : FALSE |
| 506 | ); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | // also take care of state country widget |
| 511 | $stateCountryMap = array( |
| 512 | 1 => array( |
| 513 | 'country' => "billing_country_id-{$form->_bltID}", |
| 514 | 'state_province' => "billing_state_province_id-{$form->_bltID}", |
| 515 | ) |
| 516 | ); |
| 517 | CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Make sure that credit card number and cvv are valid |
| 522 | * Called within the scope of a QF formRule function |
| 523 | */ |
| 524 | static function validateCreditCard($values, &$errors) { |
| 525 | if (!empty($values['credit_card_type'])) { |
| 526 | if (!empty($values['credit_card_number']) && |
| 527 | !CRM_Utils_Rule::creditCardNumber($values['credit_card_number'], $values['credit_card_type']) |
| 528 | ) { |
| 529 | $errors['credit_card_number'] = ts('Please enter a valid Card Number'); |
| 530 | } |
| 531 | if (!empty($values['cvv2']) && |
| 532 | !CRM_Utils_Rule::cvv($values['cvv2'], $values['credit_card_type']) |
| 533 | ) { |
| 534 | $errors['cvv2'] = ts('Please enter a valid Card Verification Number'); |
| 535 | } |
| 536 | } |
| 537 | elseif (!empty($values['credit_card_number'])) { |
| 538 | $errors['credit_card_number'] = ts('Please enter a valid Card Number'); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * function to map address fields |
| 544 | * |
| 545 | * @param $id |
| 546 | * @param $src |
| 547 | * @param $dst |
| 548 | * @param bool $reverse |
| 549 | * |
| 550 | * @return void |
| 551 | * @static |
| 552 | */ |
| 553 | static function mapParams($id, &$src, &$dst, $reverse = FALSE) { |
| 554 | static $map = NULL; |
| 555 | if (!$map) { |
| 556 | $map = array( |
| 557 | 'first_name' => 'billing_first_name', |
| 558 | 'middle_name' => 'billing_middle_name', |
| 559 | 'last_name' => 'billing_last_name', |
| 560 | 'email' => "email-$id", |
| 561 | 'street_address' => "billing_street_address-$id", |
| 562 | 'supplemental_address_1' => "billing_supplemental_address_1-$id", |
| 563 | 'city' => "billing_city-$id", |
| 564 | 'state_province' => "billing_state_province-$id", |
| 565 | 'postal_code' => "billing_postal_code-$id", |
| 566 | 'country' => "billing_country-$id", |
| 567 | ); |
| 568 | } |
| 569 | |
| 570 | foreach ($map as $n => $v) { |
| 571 | if (!$reverse) { |
| 572 | if (isset($src[$n])) { |
| 573 | $dst[$v] = $src[$n]; |
| 574 | } |
| 575 | } |
| 576 | else { |
| 577 | if (isset($src[$v])) { |
| 578 | $dst[$n] = $src[$v]; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * function to get the credit card expiration month |
| 586 | * The date format for this field should typically be "M Y" (ex: Feb 2011) or "m Y" (02 2011) |
| 587 | * See CRM-9017 |
| 588 | * |
| 589 | * @param $src |
| 590 | * |
| 591 | * @return int |
| 592 | * @static |
| 593 | */ |
| 594 | static function getCreditCardExpirationMonth($src) { |
| 595 | if ($month = CRM_Utils_Array::value('M', $src['credit_card_exp_date'])) { |
| 596 | return $month; |
| 597 | } |
| 598 | |
| 599 | return CRM_Utils_Array::value('m', $src['credit_card_exp_date']); |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * function to get the credit card expiration year |
| 604 | * The date format for this field should typically be "M Y" (ex: Feb 2011) or "m Y" (02 2011) |
| 605 | * This function exists only to make it consistent with getCreditCardExpirationMonth |
| 606 | * |
| 607 | * @param $src |
| 608 | * |
| 609 | * @return int |
| 610 | * @static |
| 611 | */ |
| 612 | static function getCreditCardExpirationYear($src) { |
| 613 | return CRM_Utils_Array::value('Y', $src['credit_card_exp_date']); |
| 614 | } |
| 615 | } |