INFRA-132 - Remove white space after an opening "(" or before a closing ")"
[civicrm-core.git] / CRM / Admin / Form / PaymentProcessor.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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: PaymentProcessor.php 9702 2007-05-29 23:57:16Z lobo $
33 *
34 */
35
36 /**
37 * This class generates form components for Payment Processor
38 *
39 */
40 class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
41 protected $_id = NULL;
42
43 protected $_testID = NULL;
44
45 protected $_fields = NULL;
46
47 protected $_ppDAO;
48
49 public function preProcess() {
50 if (!CRM_Core_Permission::check('administer payment processors')) {
51 CRM_Core_Error::statusBounce('The \'administer payment processors\' permission is required to add or edit a payment processor.');
52 }
53 parent::preProcess();
54
55 CRM_Utils_System::setTitle(ts('Settings - Payment Processor'));
56
57 // get the payment processor meta information
58
59 if ($this->_id) {
60 $this->_ppType = CRM_Utils_Request::retrieve('pp', 'String', $this, FALSE, NULL);
61 if (!$this->_ppType) {
62 $this->_ppType = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor',
63 $this->_id,
64 'payment_processor_type_id'
65 );
66 }
67 $this->set('pp', $this->_ppType);
68 }
69 else {
70 $this->_ppType = CRM_Utils_Request::retrieve('pp', 'String', $this, TRUE, NULL);
71 }
72
73 $this->assign('ppType', $this->_ppType);
74 $ppTypeName = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType',
75 $this->_ppType,
76 'name'
77 );
78 $this->assign('ppTypeName', $ppTypeName);
79
80 $this->_ppDAO = new CRM_Financial_DAO_PaymentProcessorType();
81 $this->_ppDAO->id = $this->_ppType;
82
83 if (!$this->_ppDAO->find(TRUE)) {
84 CRM_Core_Error::fatal(ts('Could not find payment processor meta information'));
85 }
86
87 if ($this->_id) {
88 $refreshURL = CRM_Utils_System::url('civicrm/admin/paymentProcessor',
89 "reset=1&action=update&id={$this->_id}",
90 FALSE, NULL, FALSE
91 );
92 }
93 else {
94 $refreshURL = CRM_Utils_System::url('civicrm/admin/paymentProcessor',
95 "reset=1&action=add",
96 FALSE, NULL, FALSE
97 );
98 }
99
100 //CRM-4129
101 $destination = CRM_Utils_Request::retrieve('civicrmDestination', 'String', $this);
102 if ($destination) {
103 $destination = urlencode($destination);
104 $refreshURL .= "&civicrmDestination=$destination";
105 }
106
107 $this->assign('refreshURL', $refreshURL);
108
109 $this->assign('is_recur', $this->_ppDAO->is_recur);
110
111 $this->_fields = array(
112 array(
113 'name' => 'user_name',
114 'label' => $this->_ppDAO->user_name_label,
115 ),
116 array(
117 'name' => 'password',
118 'label' => $this->_ppDAO->password_label,
119 ),
120 array(
121 'name' => 'signature',
122 'label' => $this->_ppDAO->signature_label,
123 ),
124 array(
125 'name' => 'subject',
126 'label' => $this->_ppDAO->subject_label,
127 ),
128 array(
129 'name' => 'url_site',
130 'label' => ts('Site URL'),
131 'rule' => 'url',
132 'msg' => ts('Enter a valid URL'),
133 ),
134 );
135
136 if ($this->_ppDAO->is_recur) {
137 $this->_fields[] = array(
138 'name' => 'url_recur',
139 'label' => ts('Recurring Payments URL'),
140 'rule' => 'url',
141 'msg' => ts('Enter a valid URL'),
142 );
143 }
144
145 if (!empty($this->_ppDAO->url_button_default)) {
146 $this->_fields[] = array(
147 'name' => 'url_button',
148 'label' => ts('Button URL'),
149 'rule' => 'url',
150 'msg' => ts('Enter a valid URL'),
151 );
152 }
153
154 if (!empty($this->_ppDAO->url_api_default)) {
155 $this->_fields[] = array(
156 'name' => 'url_api',
157 'label' => ts('API URL'),
158 'rule' => 'url',
159 'msg' => ts('Enter a valid URL'),
160 );
161 }
162 }
163
164 /**
165 * Build the form object
166 *
167 * @param bool $check
168 *
169 * @return void
170 */
171 public function buildQuickForm($check = FALSE) {
172 parent::buildQuickForm();
173
174 if ($this->_action & CRM_Core_Action::DELETE) {
175 return;
176 }
177
178 $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_PaymentProcessor');
179
180 $this->add('text', 'name', ts('Name'),
181 $attributes['name'], TRUE
182 );
183
184 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array('CRM_Financial_DAO_PaymentProcessor', $this->_id));
185
186 $this->add('text', 'description', ts('Description'),
187 $attributes['description']
188 );
189
190 $types = CRM_Core_PseudoConstant::paymentProcessorType();
191 $this->add('select', 'payment_processor_type_id', ts('Payment Processor Type'), $types, TRUE,
192 array('onchange' => "reload(true)")
193 );
194
195 // Financial Account of account type asset CRM-11515
196 $accountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' ");
197 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount(NULL, key($accountType));
198 if ($fcount = count($financialAccount)) {
199 $this->assign('financialAccount', $fcount);
200 }
201 $this->add('select', 'financial_account_id', ts('Financial Account'),
202 array('' => ts('- select -')) + $financialAccount,
203 TRUE
204 );
205 // is this processor active ?
206 $this->add('checkbox', 'is_active', ts('Is this Payment Processor active?'));
207 $this->add('checkbox', 'is_default', ts('Is this Payment Processor the default?'));
208
209 foreach ($this->_fields as $field) {
210 if (empty($field['label'])) {
211 continue;
212 }
213
214 $this->add('text', $field['name'],
215 $field['label'], $attributes[$field['name']]
216 );
217 $this->add('text', "test_{$field['name']}",
218 $field['label'], $attributes[$field['name']]
219 );
220 if (!empty($field['rule'])) {
221 $this->addRule($field['name'], $field['msg'], $field['rule']);
222 $this->addRule("test_{$field['name']}", $field['msg'], $field['rule']);
223 }
224 }
225
226 $this->addFormRule(array('CRM_Admin_Form_PaymentProcessor', 'formRule'));
227 }
228
229 /**
230 * @param $fields
231 *
232 * @return array|bool
233 */
234 public static function formRule($fields) {
235
236 // make sure that at least one of live or test is present
237 // and we have at least name and url_site
238 // would be good to make this processor specific
239 $errors = array();
240
241 if (!(self::checkSection($fields, $errors) ||
242 self::checkSection($fields, $errors, 'test')
243 )) {
244 $errors['_qf_default'] = ts('You must have at least the test or live section filled');
245 }
246
247 if (!empty($errors)) {
248 return $errors;
249 }
250
251 return empty($errors) ? TRUE : $errors;
252 }
253
254 /**
255 * @param $fields
256 * @param $errors
257 * @param null $section
258 *
259 * @return bool
260 */
261 public static function checkSection(&$fields, &$errors, $section = NULL) {
262 $names = array('user_name');
263
264 $present = FALSE;
265 $allPresent = TRUE;
266 foreach ($names as $name) {
267 if ($section) {
268 $name = "{$section}_$name";
269 }
270 if (!empty($fields[$name])) {
271 $present = TRUE;
272 }
273 else {
274 $allPresent = FALSE;
275 }
276 }
277
278 if ($present) {
279 if (!$allPresent) {
280 $errors['_qf_default'] = ts('You must have at least the user_name specified');
281 }
282 }
283 return $present;
284 }
285
286 /**
287 * @return array
288 */
289 public function setDefaultValues() {
290 $defaults = array();
291 if ($this->_ppType) {
292 $defaults['payment_processor_type_id'] = $this->_ppType;
293 }
294 if (!$this->_id) {
295 $defaults['is_active'] = $defaults['is_default'] = 1;
296 $defaults['url_site'] = $this->_ppDAO->url_site_default;
297 $defaults['url_api'] = $this->_ppDAO->url_api_default;
298 $defaults['url_recur'] = $this->_ppDAO->url_recur_default;
299 $defaults['url_button'] = $this->_ppDAO->url_button_default;
300 $defaults['test_url_site'] = $this->_ppDAO->url_site_test_default;
301 $defaults['test_url_api'] = $this->_ppDAO->url_api_test_default;
302 $defaults['test_url_recur'] = $this->_ppDAO->url_recur_test_default;
303 $defaults['test_url_button'] = $this->_ppDAO->url_button_test_default;
304 return $defaults;
305 }
306 $domainID = CRM_Core_Config::domainID();
307
308 $dao = new CRM_Financial_DAO_PaymentProcessor();
309 $dao->id = $this->_id;
310 $dao->domain_id = $domainID;
311 if (!$dao->find(TRUE)) {
312 return $defaults;
313 }
314
315 CRM_Core_DAO::storeValues($dao, $defaults);
316
317 // now get testID
318 $testDAO = new CRM_Financial_DAO_PaymentProcessor();
319 $testDAO->name = $dao->name;
320 $testDAO->is_test = 1;
321 $testDAO->domain_id = $domainID;
322 if ($testDAO->find(TRUE)) {
323 $this->_testID = $testDAO->id;
324
325 foreach ($this->_fields as $field) {
326 $testName = "test_{$field['name']}";
327 $defaults[$testName] = $testDAO->{$field['name']};
328 }
329 }
330 $defaults['financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getFinancialAccount($dao->id, 'civicrm_payment_processor', 'financial_account_id');
331
332 return $defaults;
333 }
334
335 /**
336 * Process the form submission
337 *
338 *
339 * @return Void
340 */
341 public function postProcess() {
342 CRM_Utils_System::flushCache('CRM_Financial_DAO_PaymentProcessor');
343
344 if ($this->_action & CRM_Core_Action::DELETE) {
345 CRM_Financial_BAO_PaymentProcessor::del($this->_id);
346 CRM_Core_Session::setStatus("", ts('Payment Processor Deleted.'), "success");
347 return;
348 }
349
350 $values = $this->controller->exportValues($this->_name);
351 $domainID = CRM_Core_Config::domainID();
352
353 if (!empty($values['is_default'])) {
354 $query = "UPDATE civicrm_payment_processor SET is_default = 0 WHERE domain_id = $domainID";
355 CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
356 }
357
358 $this->updatePaymentProcessor($values, $domainID, FALSE);
359 $this->updatePaymentProcessor($values, $domainID, TRUE);
360 CRM_Core_Session::setStatus(ts('Payment processor %1 has been saved.', array(1 => "<em>{$values['name']}</em>")), ts('Saved'), 'success');
361 }
362
363 /**
364 * Save a payment processor
365 *
366 * @param $values
367 * @param int $domainID
368 * @param $test
369 *
370 * @return Void
371 */
372 public function updatePaymentProcessor(&$values, $domainID, $test) {
373 $dao = new CRM_Financial_DAO_PaymentProcessor();
374
375 $dao->id = $test ? $this->_testID : $this->_id;
376 $dao->domain_id = $domainID;
377 $dao->is_test = $test;
378 $dao->is_default = CRM_Utils_Array::value('is_default', $values, 0);
379
380 $dao->is_active = CRM_Utils_Array::value('is_active', $values, 0);
381
382 $dao->name = $values['name'];
383 $dao->description = $values['description'];
384 $dao->payment_processor_type_id = $values['payment_processor_type_id'];
385
386 foreach ($this->_fields as $field) {
387 $fieldName = $test ? "test_{$field['name']}" : $field['name'];
388 $dao->{$field['name']} = trim(CRM_Utils_Array::value($fieldName, $values));
389 if (empty($dao->{$field['name']})) {
390 $dao->{$field['name']} = 'null';
391 }
392 }
393
394 // also copy meta fields from the info DAO
395 $dao->is_recur = $this->_ppDAO->is_recur;
396 $dao->billing_mode = $this->_ppDAO->billing_mode;
397 $dao->class_name = $this->_ppDAO->class_name;
398 $dao->payment_type = $this->_ppDAO->payment_type;
399
400 $dao->save();
401
402 //CRM-11515
403
404 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
405 $params = array(
406 'entity_table' => 'civicrm_payment_processor',
407 'entity_id' => $dao->id,
408 'account_relationship' => $relationTypeId,
409 'financial_account_id' => $values['financial_account_id']
410 );
411 CRM_Financial_BAO_FinancialTypeAccount::add($params);
412 }
413 }