3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
33 class CRM_Core_Payment_Form
{
37 * Add payment fields depending on payment processor.
39 * The payment processor can implement the following functions to override the built in fields.
41 * - getPaymentFormFields()
42 * - getPaymentFormFieldsMetadata()
43 * (planned - getBillingDetailsFormFields(), getBillingDetailsFormFieldsMetadata()
45 * Note that this code is written to accommodate the possibility CiviCRM will switch to implementing pay later as a manual processor in future
47 * @param CRM_Contribute_Form_AbstractEditPayment|CRM_Contribute_Form_Contribution_Main $form
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.
52 * @param bool $isBackOffice
53 * Is this a back office function? If so the option to suppress the cvn needs to be evaluated.
55 static public function setPaymentFieldsByProcessor(&$form, $processor, $forceBillingFieldsForPayLater = FALSE, $isBackOffice = FALSE) {
56 $form->billingFieldSets
= array();
57 if ($processor != NULL) {
59 $paymentFields = self
::getPaymentFields($processor);
60 $paymentTypeName = self
::getPaymentTypeName($processor);
61 $paymentTypeLabel = self
::getPaymentTypeLabel($processor);
62 //@todo if we switch to iterating through $form->billingFieldSets we won't need to assign these directly
63 $form->assign('paymentTypeName', $paymentTypeName);
64 $form->assign('paymentTypeLabel', $paymentTypeLabel);
66 $form->billingFieldSets
[$paymentTypeName]['fields'] = $form->_paymentFields
= array_intersect_key(self
::getPaymentFieldMetadata($processor), array_flip($paymentFields));
67 $form->assign('paymentFields', $paymentFields);
70 // @todo - replace this section with one similar to above per discussion - probably use a manual processor shell class to stand in for that capability
71 //return without adding billing fields if billing_mode = 4 (@todo - more the ability to set that to the payment processor)
72 // or payment processor is NULL (pay later)
73 if (($processor == NULL && !$forceBillingFieldsForPayLater) || CRM_Utils_Array
::value('billing_mode', $processor) == 4) {
76 //@todo setPaymentFields defines the billing fields - this should be moved to the processor class & renamed getBillingFields
77 // potentially pay later would also be a payment processor
78 //also set the billingFieldSet to hold all the details required to render the fieldset so we can iterate through the fieldset - making
79 // it easier to re-order in hooks etc. The billingFieldSets param is used to determine whether to show the billing pane
80 CRM_Core_Payment_Form
::setBillingDetailsFields($form);
81 $form->billingFieldSets
['billing_name_address-group']['fields'] = array();
85 * Add general billing fields.
86 * @todo set these like processor fields & let payment processors alter them
88 * @param CRM_Core_Form $form
92 static protected function setBillingDetailsFields(&$form) {
93 $bltID = $form->_bltID
;
95 $form->_paymentFields
['billing_first_name'] = array(
97 'name' => 'billing_first_name',
98 'title' => ts('Billing First Name'),
100 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'),
101 'is_required' => TRUE,
104 $form->_paymentFields
['billing_middle_name'] = array(
105 'htmlType' => 'text',
106 'name' => 'billing_middle_name',
107 'title' => ts('Billing Middle Name'),
109 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'),
110 'is_required' => FALSE,
113 $form->_paymentFields
['billing_last_name'] = array(
114 'htmlType' => 'text',
115 'name' => 'billing_last_name',
116 'title' => ts('Billing Last Name'),
118 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'),
119 'is_required' => TRUE,
122 $form->_paymentFields
["billing_street_address-{$bltID}"] = array(
123 'htmlType' => 'text',
124 'name' => "billing_street_address-{$bltID}",
125 'title' => ts('Street Address'),
127 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'),
128 'is_required' => TRUE,
131 $form->_paymentFields
["billing_city-{$bltID}"] = array(
132 'htmlType' => 'text',
133 'name' => "billing_city-{$bltID}",
134 'title' => ts('City'),
136 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'),
137 'is_required' => TRUE,
140 $form->_paymentFields
["billing_state_province_id-{$bltID}"] = array(
141 'htmlType' => 'chainSelect',
142 'title' => ts('State/Province'),
143 'name' => "billing_state_province_id-{$bltID}",
145 'is_required' => TRUE,
148 $form->_paymentFields
["billing_postal_code-{$bltID}"] = array(
149 'htmlType' => 'text',
150 'name' => "billing_postal_code-{$bltID}",
151 'title' => ts('Postal Code'),
153 'attributes' => array('size' => 30, 'maxlength' => 60, 'autocomplete' => 'off'),
154 'is_required' => TRUE,
157 $form->_paymentFields
["billing_country_id-{$bltID}"] = array(
158 'htmlType' => 'select',
159 'name' => "billing_country_id-{$bltID}",
160 'title' => ts('Country'),
162 'attributes' => array(
163 '' => ts('- select -'),
165 CRM_Core_PseudoConstant
::country(),
166 'is_required' => TRUE,
168 //CRM-15509 working towards giving control over billing fields to payment processors. For now removing tpl hard-coding
169 $smarty = CRM_Core_Smarty
::singleton();
170 $smarty->assign('billingDetailsFields', array(
171 'billing_first_name',
172 'billing_middle_name',
174 "billing_street_address-{$bltID}",
175 "billing_city-{$bltID}",
176 "billing_country_id-{$bltID}",
177 "billing_state_province_id-{$bltID}",
178 "billing_postal_code-{$bltID}",
183 * @param CRM_Core_Form $form
184 * @param bool $useRequired
185 * @param array $paymentFields
187 protected static function addCommonFields(&$form, $useRequired, $paymentFields) {
188 foreach ($paymentFields as $name => $field) {
189 if (!empty($field['cc_field'])) {
190 if ($field['htmlType'] == 'chainSelect') {
191 $form->addChainSelect($field['name'], array('required' => $useRequired && $field['is_required']));
194 $form->add($field['htmlType'],
197 $field['attributes'],
198 $useRequired ?
$field['is_required'] : FALSE
206 * @param array $paymentProcessor
207 * @todo it will be necessary to set details that affect it - mostly likely take Country as a param. Should we add generic
208 * setParams on processor class or just setCountry which we know we need?
212 public static function getPaymentFields($paymentProcessor) {
213 $paymentProcessorObject = Civi\Payment\System
::singleton()->getByProcessor($paymentProcessor);
214 return $paymentProcessorObject->getPaymentFormFields();
218 * @param array $paymentProcessor
222 public static function getPaymentFieldMetadata($paymentProcessor) {
223 $paymentProcessorObject = Civi\Payment\System
::singleton()->getByProcessor($paymentProcessor);
224 return $paymentProcessorObject->getPaymentFormFieldsMetadata();
228 * @param array $paymentProcessor
232 public static function getPaymentTypeName($paymentProcessor) {
233 $paymentProcessorObject = Civi\Payment\System
::singleton()->getByProcessor($paymentProcessor);
234 return $paymentProcessorObject->getPaymentTypeName();
238 * @param array $paymentProcessor
242 public static function getPaymentTypeLabel($paymentProcessor) {
243 $paymentProcessorObject = Civi\Payment\System
::singleton()->getByProcessor($paymentProcessor);
244 return ts(($paymentProcessorObject->getPaymentTypeLabel()) . ' Information');
248 * @param CRM_Contribute_Form_AbstractEditPayment|CRM_Contribute_Form_Contribution_Main|CRM_Core_Payment_ProcessorForm|CRM_Contribute_Form_UpdateBilling $form
249 * @param array $processor
250 * Array of properties including 'object' as loaded from CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors.
251 * @param bool $isBillingDataOptional
252 * This manifests for 'NULL' (pay later) payment processor as the addition of billing fields to the form and.
253 * for payment processors that gather payment data on site as rendering the fields as not being required. (not entirely sure why but this
254 * is implemented for back office forms)
256 * @param bool $isBackOffice
257 * Is this a backoffice form. This could affect the display of the cvn or whether some processors show,
258 * although the distinction is losing it's meaning as front end forms are used for back office and a permission
259 * for the 'enter without cvn' is probably more appropriate. Paypal std does not support another user
260 * entering details but once again the issue is not back office but 'another user'.
264 public static function buildPaymentForm(&$form, $processor, $isBillingDataOptional, $isBackOffice) {
265 //if the form has address fields assign to the template so the js can decide what billing fields to show
266 $profileAddressFields = $form->get('profileAddressFields');
267 if (!empty($profileAddressFields)) {
268 $form->assign('profileAddressFields', $profileAddressFields);
271 if (!empty($processor['object']) && $processor['object']->buildForm($form)) {
275 self
::setPaymentFieldsByProcessor($form, $processor, empty($isBillingDataOptional), $isBackOffice);
276 self
::addCommonFields($form, !$isBillingDataOptional, $form->_paymentFields
);
277 self
::addRules($form, $form->_paymentFields
);
278 return (!empty($form->_paymentFields
));
282 * @param CRM_Core_Form $form
283 * @param array $paymentFields
284 * Array of properties including 'object' as loaded from CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors.
285 * @param $paymentFields
287 protected static function addRules(&$form, $paymentFields) {
288 foreach ($paymentFields as $paymentField => $fieldSpecs) {
289 if (!empty($fieldSpecs['rules'])) {
290 foreach ($fieldSpecs['rules'] as $rule) {
291 $form->addRule($paymentField,
292 $rule['rule_message'],
294 $rule['rule_parameters']
302 * Validate the payment instrument values before passing it to the payment processor
303 * We want this to be overrideable by the payment processor, and default to using
304 * this object's validCreditCard for credit cards (implemented as the default in the Payment class).
306 public static function validatePaymentInstrument($payment_processor_id, $values, &$errors, $form) {
307 // ignore if we don't have a payment instrument to validate (e.g. backend payments)
308 if ($payment_processor_id > 0) {
309 $payment = Civi\Payment\System
::singleton()->getById($payment_processor_id);
310 $payment->validatePaymentInstrument($values, $errors);
315 * The credit card pseudo constant results only the CC label, not the key ID
316 * So we normalize the name to use it as a CSS class.
318 public static function getCreditCardCSSNames() {
319 $creditCardTypes = array();
320 foreach (CRM_Contribute_PseudoConstant
::creditCard() as $key => $name) {
321 // Replace anything not css-friendly by an underscore
322 // Non-latin names will not like this, but so many things are wrong with
323 // the credit-card type configurations already.
324 $key = str_replace(' ', '', $key);
325 $key = preg_replace('/[^a-zA-Z0-9]/', '_', $key);
326 $key = strtolower($key);
327 $creditCardTypes[$key] = $name;
329 return $creditCardTypes;
333 * Set default values for the form.
335 * @param CRM_Core_Form $form
336 * @param int $contactID
338 public static function setDefaultValues(&$form, $contactID) {
339 $billingDefaults = $form->getProfileDefaults('Billing', $contactID);
340 $form->_defaults
= array_merge($form->_defaults
, $billingDefaults);
342 // set default country & state from config if no country set
343 // note the effect of this is to set the billing country to default to the site default
344 // country if the person has an address but no country (for anonymous country is set above)
345 // this could have implications if the billing profile is filled but hidden.
346 // this behaviour has been in place for a while but the use of js to hide things has increased
347 if (empty($form->_defaults
["billing_country_id-{$form->_bltID}"])) {
348 $form->_defaults
["billing_country_id-{$form->_bltID}"] = CRM_Core_Config
::singleton()->defaultContactCountry
;
350 if (empty($form->_defaults
["billing_state_province_id-{$form->_bltID}"])) {
351 $form->_defaults
["billing_state_province_id-{$form->_bltID}"] = CRM_Core_Config
::singleton()
352 ->defaultContactStateProvince
;
357 * Make sure that credit card number and cvv are valid.
358 * Called within the scope of a QF formRule function
360 * @param array $values
361 * @param array $errors
363 public static function validateCreditCard($values, &$errors) {
364 if (!empty($values['credit_card_type']) ||
!empty($values['credit_card_number'])) {
365 if (!empty($values['credit_card_number']) &&
366 !CRM_Utils_Rule
::creditCardNumber($values['credit_card_number'], $values['credit_card_type'])
368 $errors['credit_card_number'] = ts('Please enter a valid Card Number');
370 if (!empty($values['cvv2']) &&
371 !CRM_Utils_Rule
::cvv($values['cvv2'], $values['credit_card_type'])
373 $errors['cvv2'] = ts('Please enter a valid Card Verification Number');
379 * Map address fields.
384 * @param bool $reverse
386 public static function mapParams($id, $src, &$dst, $reverse = FALSE) {
390 'first_name' => 'billing_first_name',
391 'middle_name' => 'billing_middle_name',
392 'last_name' => 'billing_last_name',
393 'email' => "email-$id",
394 'street_address' => "billing_street_address-$id",
395 'supplemental_address_1' => "billing_supplemental_address_1-$id",
396 'city' => "billing_city-$id",
397 'state_province' => "billing_state_province-$id",
398 'postal_code' => "billing_postal_code-$id",
399 'country' => "billing_country-$id",
400 'contactID' => 'contact_id',
404 foreach ($map as $n => $v) {
406 if (isset($src[$n])) {
411 if (isset($src[$v])) {
419 * 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)
427 public static function getCreditCardExpirationMonth($src) {
428 if ($month = CRM_Utils_Array
::value('M', $src['credit_card_exp_date'])) {
432 return CRM_Utils_Array
::value('m', $src['credit_card_exp_date']);
436 * Get the credit card expiration year.
437 * The date format for this field should typically be "M Y" (ex: Feb 2011) or "m Y" (02 2011)
438 * This function exists only to make it consistent with getCreditCardExpirationMonth
444 public static function getCreditCardExpirationYear($src) {
445 return CRM_Utils_Array
::value('Y', $src['credit_card_exp_date']);