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
35 class CRM_Core_Payment_Form
{
39 * Add payment fields depending on payment processor. 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']
304 * Billing mode button is basically synonymous with paypal express - this is probably a good example of 'odds & sods' code we
305 * 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
309 protected static function addPaypalExpressCode(&$form) {
310 if (empty($form->isBackOffice
)) {
311 if (in_array(CRM_Utils_Array
::value('billing_mode', $form->_paymentProcessor
), array(2, 3))) {
312 $form->_expressButtonName
= $form->getButtonName('upload', 'express');
313 $form->assign('expressButtonName', $form->_expressButtonName
);
315 $form->_expressButtonName
,
316 $form->_paymentProcessor
['url_button'],
317 array('class' => 'crm-form-submit')
324 * Validate the payment instrument values before passing it to the payment processor
325 * We want this to be overrideable by the payment processor, and default to using
326 * this object's validCreditCard for credit cards (implemented as the default in the Payment class).
328 public static function validatePaymentInstrument($payment_processor_id, $values, &$errors, $form) {
329 // ignore if we don't have a payment instrument to validate (e.g. backend payments)
330 if ($payment_processor_id > 0) {
331 $payment = Civi\Payment\System
::singleton()->getById($payment_processor_id);
332 $payment->validatePaymentInstrument($values, $errors);
337 * The credit card pseudo constant results only the CC label, not the key ID
338 * So we normalize the name to use it as a CSS class.
340 public static function getCreditCardCSSNames() {
341 $creditCardTypes = array();
342 foreach (CRM_Contribute_PseudoConstant
::creditCard() as $key => $name) {
343 // Replace anything not css-friendly by an underscore
344 // Non-latin names will not like this, but so many things are wrong with
345 // the credit-card type configurations already.
346 $key = str_replace(' ', '', $key);
347 $key = preg_replace('/[^a-zA-Z0-9]/', '_', $key);
348 $key = strtolower($key);
349 $creditCardTypes[$key] = $name;
351 return $creditCardTypes;
355 * Make sure that credit card number and cvv are valid.
356 * Called within the scope of a QF formRule function
358 * @param array $values
359 * @param array $errors
361 public static function validateCreditCard($values, &$errors) {
362 if (!empty($values['credit_card_type']) ||
!empty($values['credit_card_number'])) {
363 if (!empty($values['credit_card_number']) &&
364 !CRM_Utils_Rule
::creditCardNumber($values['credit_card_number'], $values['credit_card_type'])
366 $errors['credit_card_number'] = ts('Please enter a valid Card Number');
368 if (!empty($values['cvv2']) &&
369 !CRM_Utils_Rule
::cvv($values['cvv2'], $values['credit_card_type'])
371 $errors['cvv2'] = ts('Please enter a valid Card Verification Number');
377 * Map address fields.
382 * @param bool $reverse
384 public static function mapParams($id, $src, &$dst, $reverse = FALSE) {
388 'first_name' => 'billing_first_name',
389 'middle_name' => 'billing_middle_name',
390 'last_name' => 'billing_last_name',
391 'email' => "email-$id",
392 'street_address' => "billing_street_address-$id",
393 'supplemental_address_1' => "billing_supplemental_address_1-$id",
394 'city' => "billing_city-$id",
395 'state_province' => "billing_state_province-$id",
396 'postal_code' => "billing_postal_code-$id",
397 'country' => "billing_country-$id",
398 'contactID' => 'contact_id',
402 foreach ($map as $n => $v) {
404 if (isset($src[$n])) {
409 if (isset($src[$v])) {
417 * Get the credit card expiration month.
418 * The date format for this field should typically be "M Y" (ex: Feb 2011) or "m Y" (02 2011)
425 public static function getCreditCardExpirationMonth($src) {
426 if ($month = CRM_Utils_Array
::value('M', $src['credit_card_exp_date'])) {
430 return CRM_Utils_Array
::value('m', $src['credit_card_exp_date']);
434 * Get the credit card expiration year.
435 * The date format for this field should typically be "M Y" (ex: Feb 2011) or "m Y" (02 2011)
436 * This function exists only to make it consistent with getCreditCardExpirationMonth
442 public static function getCreditCardExpirationYear($src) {
443 return CRM_Utils_Array
::value('Y', $src['credit_card_exp_date']);