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