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