fixes to comment blocks
[civicrm-core.git] / CRM / Admin / Form / PaymentProcessor.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
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 */
40class 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;
8ef12e64 48
00be9182 49 public function preProcess() {
22e263ad 50 if (!CRM_Core_Permission::check('administer payment processors')) {
8f816b2a 51 CRM_Core_Error::statusBounce('The \'administer payment processors\' permission is required to add or edit a payment processor.');
e16ce4cd 52 }
6a488035
TO
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) {
481a74f4 62 $this->_ppType = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor',
6a488035
TO
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);
481a74f4 74 $ppTypeName = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType',
6a488035
TO
75 $this->_ppType,
76 'name'
77 );
481a74f4 78 $this->assign('ppTypeName', $ppTypeName);
6a488035 79
481a74f4 80 $this->_ppDAO = new CRM_Financial_DAO_PaymentProcessorType();
6a488035
TO
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 /**
eceb18cc 165 * Build the form object.
6a488035 166 *
fd31fa4c
EM
167 * @param bool $check
168 *
355ba699 169 * @return void
6a488035
TO
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
353ffa53
TO
184 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array(
185 'CRM_Financial_DAO_PaymentProcessor',
28a04ea9 186 $this->_id,
353ffa53 187 ));
6a488035
TO
188
189 $this->add('text', 'description', ts('Description'),
190 $attributes['description']
191 );
192
193 $types = CRM_Core_PseudoConstant::paymentProcessorType();
481a74f4 194 $this->add('select', 'payment_processor_type_id', ts('Payment Processor Type'), $types, TRUE,
8ef12e64 195 array('onchange' => "reload(true)")
6a488035
TO
196 );
197
198 // Financial Account of account type asset CRM-11515
f743a6eb 199 $accountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' ");
6a488035
TO
200 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount(NULL, key($accountType));
201 if ($fcount = count($financialAccount)) {
202 $this->assign('financialAccount', $fcount);
203 }
8ef12e64 204 $this->add('select', 'financial_account_id', ts('Financial Account'),
6a488035 205 array('' => ts('- select -')) + $financialAccount,
02fc859b 206 TRUE
6a488035
TO
207 );
208 // is this processor active ?
209 $this->add('checkbox', 'is_active', ts('Is this Payment Processor active?'));
210 $this->add('checkbox', 'is_default', ts('Is this Payment Processor the default?'));
211
6a488035
TO
212 foreach ($this->_fields as $field) {
213 if (empty($field['label'])) {
214 continue;
215 }
216
217 $this->add('text', $field['name'],
218 $field['label'], $attributes[$field['name']]
219 );
220 $this->add('text', "test_{$field['name']}",
221 $field['label'], $attributes[$field['name']]
222 );
a7488080 223 if (!empty($field['rule'])) {
6a488035
TO
224 $this->addRule($field['name'], $field['msg'], $field['rule']);
225 $this->addRule("test_{$field['name']}", $field['msg'], $field['rule']);
226 }
227 }
228
229 $this->addFormRule(array('CRM_Admin_Form_PaymentProcessor', 'formRule'));
230 }
231
e0ef6999
EM
232 /**
233 * @param $fields
234 *
235 * @return array|bool
236 */
00be9182 237 public static function formRule($fields) {
6a488035
TO
238
239 // make sure that at least one of live or test is present
240 // and we have at least name and url_site
241 // would be good to make this processor specific
242 $errors = array();
243
244 if (!(self::checkSection($fields, $errors) ||
353ffa53
TO
245 self::checkSection($fields, $errors, 'test')
246 )
247 ) {
6a488035
TO
248 $errors['_qf_default'] = ts('You must have at least the test or live section filled');
249 }
250
251 if (!empty($errors)) {
252 return $errors;
253 }
254
255 return empty($errors) ? TRUE : $errors;
256 }
257
e0ef6999
EM
258 /**
259 * @param $fields
260 * @param $errors
261 * @param null $section
262 *
263 * @return bool
264 */
00be9182 265 public static function checkSection(&$fields, &$errors, $section = NULL) {
6a488035
TO
266 $names = array('user_name');
267
268 $present = FALSE;
269 $allPresent = TRUE;
270 foreach ($names as $name) {
271 if ($section) {
272 $name = "{$section}_$name";
273 }
274 if (!empty($fields[$name])) {
275 $present = TRUE;
276 }
277 else {
278 $allPresent = FALSE;
279 }
280 }
281
282 if ($present) {
283 if (!$allPresent) {
284 $errors['_qf_default'] = ts('You must have at least the user_name specified');
285 }
286 }
287 return $present;
288 }
289
e0ef6999
EM
290 /**
291 * @return array
292 */
00be9182 293 public function setDefaultValues() {
6a488035
TO
294 $defaults = array();
295 if ($this->_ppType) {
02fc859b 296 $defaults['payment_processor_type_id'] = $this->_ppType;
6a488035
TO
297 }
298 if (!$this->_id) {
299 $defaults['is_active'] = $defaults['is_default'] = 1;
300 $defaults['url_site'] = $this->_ppDAO->url_site_default;
301 $defaults['url_api'] = $this->_ppDAO->url_api_default;
302 $defaults['url_recur'] = $this->_ppDAO->url_recur_default;
303 $defaults['url_button'] = $this->_ppDAO->url_button_default;
304 $defaults['test_url_site'] = $this->_ppDAO->url_site_test_default;
305 $defaults['test_url_api'] = $this->_ppDAO->url_api_test_default;
306 $defaults['test_url_recur'] = $this->_ppDAO->url_recur_test_default;
307 $defaults['test_url_button'] = $this->_ppDAO->url_button_test_default;
308 return $defaults;
309 }
310 $domainID = CRM_Core_Config::domainID();
311
481a74f4 312 $dao = new CRM_Financial_DAO_PaymentProcessor();
353ffa53 313 $dao->id = $this->_id;
6a488035
TO
314 $dao->domain_id = $domainID;
315 if (!$dao->find(TRUE)) {
316 return $defaults;
317 }
318
319 CRM_Core_DAO::storeValues($dao, $defaults);
320
321 // now get testID
322 $testDAO = new CRM_Financial_DAO_PaymentProcessor();
353ffa53
TO
323 $testDAO->name = $dao->name;
324 $testDAO->is_test = 1;
6a488035
TO
325 $testDAO->domain_id = $domainID;
326 if ($testDAO->find(TRUE)) {
327 $this->_testID = $testDAO->id;
328
329 foreach ($this->_fields as $field) {
330 $testName = "test_{$field['name']}";
331 $defaults[$testName] = $testDAO->{$field['name']};
332 }
333 }
334 $defaults['financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getFinancialAccount($dao->id, 'civicrm_payment_processor', 'financial_account_id');
335
336 return $defaults;
337 }
338
339 /**
eceb18cc 340 * Process the form submission.
6a488035 341 *
6a488035
TO
342 *
343 * @return Void
344 */
345 public function postProcess() {
481a74f4 346 CRM_Utils_System::flushCache('CRM_Financial_DAO_PaymentProcessor');
6a488035
TO
347
348 if ($this->_action & CRM_Core_Action::DELETE) {
349 CRM_Financial_BAO_PaymentProcessor::del($this->_id);
350 CRM_Core_Session::setStatus("", ts('Payment Processor Deleted.'), "success");
28a04ea9 351 return NULL;
6a488035
TO
352 }
353
354 $values = $this->controller->exportValues($this->_name);
355 $domainID = CRM_Core_Config::domainID();
356
a7488080 357 if (!empty($values['is_default'])) {
6a488035
TO
358 $query = "UPDATE civicrm_payment_processor SET is_default = 0 WHERE domain_id = $domainID";
359 CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
360 }
361
362 $this->updatePaymentProcessor($values, $domainID, FALSE);
363 $this->updatePaymentProcessor($values, $domainID, TRUE);
364 CRM_Core_Session::setStatus(ts('Payment processor %1 has been saved.', array(1 => "<em>{$values['name']}</em>")), ts('Saved'), 'success');
365 }
366
367 /**
eceb18cc 368 * Save a payment processor.
6a488035 369 *
77b97be7 370 * @param $values
100fef9d 371 * @param int $domainID
77b97be7
EM
372 * @param $test
373 *
28a04ea9 374 * @return void
6a488035 375 */
00be9182 376 public function updatePaymentProcessor(&$values, $domainID, $test) {
481a74f4 377 $dao = new CRM_Financial_DAO_PaymentProcessor();
6a488035 378
353ffa53
TO
379 $dao->id = $test ? $this->_testID : $this->_id;
380 $dao->domain_id = $domainID;
381 $dao->is_test = $test;
6a488035
TO
382 $dao->is_default = CRM_Utils_Array::value('is_default', $values, 0);
383
384 $dao->is_active = CRM_Utils_Array::value('is_active', $values, 0);
385
386 $dao->name = $values['name'];
387 $dao->description = $values['description'];
388 $dao->payment_processor_type_id = $values['payment_processor_type_id'];
389
390 foreach ($this->_fields as $field) {
391 $fieldName = $test ? "test_{$field['name']}" : $field['name'];
392 $dao->{$field['name']} = trim(CRM_Utils_Array::value($fieldName, $values));
393 if (empty($dao->{$field['name']})) {
394 $dao->{$field['name']} = 'null';
395 }
396 }
397
398 // also copy meta fields from the info DAO
353ffa53 399 $dao->is_recur = $this->_ppDAO->is_recur;
6a488035 400 $dao->billing_mode = $this->_ppDAO->billing_mode;
353ffa53 401 $dao->class_name = $this->_ppDAO->class_name;
6a488035
TO
402 $dao->payment_type = $this->_ppDAO->payment_type;
403
404 $dao->save();
8ef12e64 405
6a488035 406 //CRM-11515
8ef12e64 407
f743a6eb 408 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
6a488035
TO
409 $params = array(
410 'entity_table' => 'civicrm_payment_processor',
411 'entity_id' => $dao->id,
412 'account_relationship' => $relationTypeId,
21dfd5f5 413 'financial_account_id' => $values['financial_account_id'],
6a488035
TO
414 );
415 CRM_Financial_BAO_FinancialTypeAccount::add($params);
416 }
96025800 417
6a488035 418}