CRM-17728 enotices on payment processor form
[civicrm-core.git] / CRM / Admin / Form / PaymentProcessor.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33
34 /**
35 * This class generates form components for Payment Processor.
36 */
37 class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
38 protected $_id = NULL;
39
40 protected $_testID = NULL;
41
42 protected $_fields = NULL;
43
44 protected $_ppDAO;
45
46 public function preProcess() {
47 if (!CRM_Core_Permission::check('administer payment processors')) {
48 CRM_Core_Error::statusBounce('The \'administer payment processors\' permission is required to add or edit a payment processor.');
49 }
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 * Build the form object.
163 *
164 * @param bool $check
165 */
166 public function buildQuickForm($check = FALSE) {
167 parent::buildQuickForm();
168
169 if ($this->_action & CRM_Core_Action::DELETE) {
170 return;
171 }
172
173 $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_PaymentProcessor');
174
175 $this->add('text', 'name', ts('Name'),
176 $attributes['name'], TRUE
177 );
178
179 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array(
180 'CRM_Financial_DAO_PaymentProcessor',
181 $this->_id,
182 ));
183
184 $this->add('text', 'description', ts('Description'),
185 $attributes['description']
186 );
187
188 $types = CRM_Core_PseudoConstant::paymentProcessorType();
189 $this->add('select', 'payment_processor_type_id', ts('Payment Processor Type'), $types, TRUE,
190 array('onchange' => "reload(true)")
191 );
192
193 // Financial Account of account type asset CRM-11515
194 $accountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' ");
195 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount(NULL, key($accountType));
196 if ($fcount = count($financialAccount)) {
197 $this->assign('financialAccount', $fcount);
198 }
199 $this->add('select', 'financial_account_id', ts('Financial Account'),
200 array('' => ts('- select -')) + $financialAccount,
201 TRUE
202 );
203 $this->addSelect('payment_instrument_id',
204 array(
205 'entity' => 'contribution',
206 'label' => ts('Payment Method'),
207 'placeholder' => NULL,
208 )
209 );
210
211 // is this processor active ?
212 $this->add('checkbox', 'is_active', ts('Is this Payment Processor active?'));
213 $this->add('checkbox', 'is_default', ts('Is this Payment Processor the default?'));
214
215 foreach ($this->_fields as $field) {
216 if (empty($field['label'])) {
217 continue;
218 }
219
220 $this->add('text', $field['name'],
221 $field['label'], $attributes[$field['name']]
222 );
223 $this->add('text', "test_{$field['name']}",
224 $field['label'], $attributes[$field['name']]
225 );
226 if (!empty($field['rule'])) {
227 $this->addRule($field['name'], $field['msg'], $field['rule']);
228 $this->addRule("test_{$field['name']}", $field['msg'], $field['rule']);
229 }
230 }
231
232 $this->addFormRule(array('CRM_Admin_Form_PaymentProcessor', 'formRule'));
233 }
234
235 /**
236 * @param $fields
237 *
238 * @return array|bool
239 */
240 public static function formRule($fields) {
241
242 // make sure that at least one of live or test is present
243 // and we have at least name and url_site
244 // would be good to make this processor specific
245 $errors = array();
246
247 if (!(self::checkSection($fields, $errors) ||
248 self::checkSection($fields, $errors, 'test')
249 )
250 ) {
251 $errors['_qf_default'] = ts('You must have at least the test or live section filled');
252 }
253
254 if (!empty($errors)) {
255 return $errors;
256 }
257
258 return empty($errors) ? TRUE : $errors;
259 }
260
261 /**
262 * @param $fields
263 * @param $errors
264 * @param null $section
265 *
266 * @return bool
267 */
268 public static function checkSection(&$fields, &$errors, $section = NULL) {
269 $names = array('user_name');
270
271 $present = FALSE;
272 $allPresent = TRUE;
273 foreach ($names as $name) {
274 if ($section) {
275 $name = "{$section}_$name";
276 }
277 if (!empty($fields[$name])) {
278 $present = TRUE;
279 }
280 else {
281 $allPresent = FALSE;
282 }
283 }
284
285 if ($present) {
286 if (!$allPresent) {
287 $errors['_qf_default'] = ts('You must have at least the user_name specified');
288 }
289 }
290 return $present;
291 }
292
293 /**
294 * @return array
295 */
296 public function setDefaultValues() {
297 $defaults = array();
298
299 if (!$this->_id) {
300 $defaults['is_active'] = $defaults['is_default'] = 1;
301 $defaults['url_site'] = $this->_ppDAO->url_site_default;
302 $defaults['url_api'] = $this->_ppDAO->url_api_default;
303 $defaults['url_recur'] = $this->_ppDAO->url_recur_default;
304 $defaults['url_button'] = $this->_ppDAO->url_button_default;
305 $defaults['test_url_site'] = $this->_ppDAO->url_site_test_default;
306 $defaults['test_url_api'] = $this->_ppDAO->url_api_test_default;
307 $defaults['test_url_recur'] = $this->_ppDAO->url_recur_test_default;
308 $defaults['test_url_button'] = $this->_ppDAO->url_button_test_default;
309 $defaults['payment_instrument_id'] = $this->_ppDAO->payment_instrument_id;
310 // When user changes payment processor type, it is passed in via $this->_ppType so update defaults array.
311 if ($this->_ppType) {
312 $defaults['payment_processor_type_id'] = $this->_ppType;
313 }
314 return $defaults;
315 }
316 $domainID = CRM_Core_Config::domainID();
317
318 $dao = new CRM_Financial_DAO_PaymentProcessor();
319 $dao->id = $this->_id;
320 $dao->domain_id = $domainID;
321 if (!$dao->find(TRUE)) {
322 return $defaults;
323 }
324
325 CRM_Core_DAO::storeValues($dao, $defaults);
326 // When user changes payment processor type, it is passed in via $this->_ppType so update defaults array.
327 if ($this->_ppType) {
328 $defaults['payment_processor_type_id'] = $this->_ppType;
329 }
330
331 // now get testID
332 $testDAO = new CRM_Financial_DAO_PaymentProcessor();
333 $testDAO->name = $dao->name;
334 $testDAO->is_test = 1;
335 $testDAO->domain_id = $domainID;
336 if ($testDAO->find(TRUE)) {
337 $this->_testID = $testDAO->id;
338
339 foreach ($this->_fields as $field) {
340 $testName = "test_{$field['name']}";
341 $defaults[$testName] = $testDAO->{$field['name']};
342 }
343 }
344 $defaults['financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getFinancialAccount($dao->id, 'civicrm_payment_processor', 'financial_account_id');
345
346 return $defaults;
347 }
348
349 /**
350 * Process the form submission.
351 */
352 public function postProcess() {
353
354 if ($this->_action & CRM_Core_Action::DELETE) {
355 CRM_Financial_BAO_PaymentProcessor::del($this->_id);
356 CRM_Core_Session::setStatus("", ts('Payment Processor Deleted.'), "success");
357 return NULL;
358 }
359
360 $values = $this->controller->exportValues($this->_name);
361 $domainID = CRM_Core_Config::domainID();
362
363 if (!empty($values['is_default'])) {
364 $query = "UPDATE civicrm_payment_processor SET is_default = 0 WHERE domain_id = $domainID";
365 CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
366 }
367
368 $this->updatePaymentProcessor($values, $domainID, FALSE);
369 $this->updatePaymentProcessor($values, $domainID, TRUE);
370 CRM_Core_Session::setStatus(ts('Payment processor %1 has been saved.', array(1 => "<em>{$values['name']}</em>")), ts('Saved'), 'success');
371 }
372
373 /**
374 * Save a payment processor.
375 *
376 * @param array $values
377 * @param int $domainID
378 * @param bool $test
379 */
380 public function updatePaymentProcessor(&$values, $domainID, $test) {
381 $params = array_merge(array(
382 'id' => $test ? $this->_testID : $this->_id,
383 'domain_id' => $domainID,
384 'is_test' => $test,
385 'is_active' => 0,
386 'is_default' => 0,
387 'is_recur' => $this->_ppDAO->is_recur,
388 'billing_mode' => $this->_ppDAO->billing_mode,
389 'class_name' => $this->_ppDAO->class_name,
390 'payment_type' => $this->_ppDAO->payment_type,
391 'payment_instrument_id' => $this->_ppDAO->payment_instrument_id,
392 'financial_account_id' => $values['financial_account_id'],
393 ), $values);
394
395 civicrm_api3('PaymentProcessor', 'create', $params);
396 }
397
398 }