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