Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
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 | 202 | ); |
b7e7f943 | 203 | $this->addSelect('payment_instrument_id', |
204 | array( | |
205 | 'entity' => 'contribution', | |
206 | 'label' => ts('Payment Method'), | |
207 | 'placeholder' => NULL, | |
208 | ) | |
209 | ); | |
210 | ||
6a488035 TO |
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?')); | |
cb5962bd | 214 | $creditCardTypes = CRM_Contribute_PseudoConstant::creditCard(); |
660ef5f6 | 215 | $this->addCheckBox('accept_credit_cards', ts('Accepted Credit Card Type(s)'), |
cb5962bd | 216 | $creditCardTypes, NULL, NULL, NULL, NULL, ' '); |
6a488035 TO |
217 | foreach ($this->_fields as $field) { |
218 | if (empty($field['label'])) { | |
219 | continue; | |
220 | } | |
221 | ||
222 | $this->add('text', $field['name'], | |
223 | $field['label'], $attributes[$field['name']] | |
224 | ); | |
225 | $this->add('text', "test_{$field['name']}", | |
226 | $field['label'], $attributes[$field['name']] | |
227 | ); | |
a7488080 | 228 | if (!empty($field['rule'])) { |
6a488035 TO |
229 | $this->addRule($field['name'], $field['msg'], $field['rule']); |
230 | $this->addRule("test_{$field['name']}", $field['msg'], $field['rule']); | |
231 | } | |
232 | } | |
233 | ||
234 | $this->addFormRule(array('CRM_Admin_Form_PaymentProcessor', 'formRule')); | |
235 | } | |
236 | ||
e0ef6999 EM |
237 | /** |
238 | * @param $fields | |
239 | * | |
240 | * @return array|bool | |
241 | */ | |
00be9182 | 242 | public static function formRule($fields) { |
6a488035 TO |
243 | |
244 | // make sure that at least one of live or test is present | |
245 | // and we have at least name and url_site | |
246 | // would be good to make this processor specific | |
247 | $errors = array(); | |
248 | ||
249 | if (!(self::checkSection($fields, $errors) || | |
353ffa53 TO |
250 | self::checkSection($fields, $errors, 'test') |
251 | ) | |
252 | ) { | |
6a488035 TO |
253 | $errors['_qf_default'] = ts('You must have at least the test or live section filled'); |
254 | } | |
255 | ||
256 | if (!empty($errors)) { | |
257 | return $errors; | |
258 | } | |
259 | ||
260 | return empty($errors) ? TRUE : $errors; | |
261 | } | |
262 | ||
e0ef6999 EM |
263 | /** |
264 | * @param $fields | |
265 | * @param $errors | |
266 | * @param null $section | |
267 | * | |
268 | * @return bool | |
269 | */ | |
00be9182 | 270 | public static function checkSection(&$fields, &$errors, $section = NULL) { |
6a488035 TO |
271 | $names = array('user_name'); |
272 | ||
273 | $present = FALSE; | |
274 | $allPresent = TRUE; | |
275 | foreach ($names as $name) { | |
276 | if ($section) { | |
277 | $name = "{$section}_$name"; | |
278 | } | |
279 | if (!empty($fields[$name])) { | |
280 | $present = TRUE; | |
281 | } | |
282 | else { | |
283 | $allPresent = FALSE; | |
284 | } | |
285 | } | |
286 | ||
287 | if ($present) { | |
288 | if (!$allPresent) { | |
289 | $errors['_qf_default'] = ts('You must have at least the user_name specified'); | |
290 | } | |
291 | } | |
292 | return $present; | |
293 | } | |
294 | ||
e0ef6999 EM |
295 | /** |
296 | * @return array | |
297 | */ | |
00be9182 | 298 | public function setDefaultValues() { |
6a488035 | 299 | $defaults = array(); |
6d5de837 | 300 | |
6a488035 TO |
301 | if (!$this->_id) { |
302 | $defaults['is_active'] = $defaults['is_default'] = 1; | |
303 | $defaults['url_site'] = $this->_ppDAO->url_site_default; | |
304 | $defaults['url_api'] = $this->_ppDAO->url_api_default; | |
305 | $defaults['url_recur'] = $this->_ppDAO->url_recur_default; | |
306 | $defaults['url_button'] = $this->_ppDAO->url_button_default; | |
307 | $defaults['test_url_site'] = $this->_ppDAO->url_site_test_default; | |
308 | $defaults['test_url_api'] = $this->_ppDAO->url_api_test_default; | |
309 | $defaults['test_url_recur'] = $this->_ppDAO->url_recur_test_default; | |
310 | $defaults['test_url_button'] = $this->_ppDAO->url_button_test_default; | |
b7e7f943 | 311 | $defaults['payment_instrument_id'] = $this->_ppDAO->payment_instrument_id; |
6d5de837 DG |
312 | // When user changes payment processor type, it is passed in via $this->_ppType so update defaults array. |
313 | if ($this->_ppType) { | |
314 | $defaults['payment_processor_type_id'] = $this->_ppType; | |
315 | } | |
6a488035 TO |
316 | return $defaults; |
317 | } | |
318 | $domainID = CRM_Core_Config::domainID(); | |
319 | ||
481a74f4 | 320 | $dao = new CRM_Financial_DAO_PaymentProcessor(); |
353ffa53 | 321 | $dao->id = $this->_id; |
6a488035 TO |
322 | $dao->domain_id = $domainID; |
323 | if (!$dao->find(TRUE)) { | |
324 | return $defaults; | |
325 | } | |
326 | ||
327 | CRM_Core_DAO::storeValues($dao, $defaults); | |
6d5de837 DG |
328 | // When user changes payment processor type, it is passed in via $this->_ppType so update defaults array. |
329 | if ($this->_ppType) { | |
330 | $defaults['payment_processor_type_id'] = $this->_ppType; | |
331 | } | |
c294dbbc | 332 | $cards = json_decode(CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor', |
cb5962bd SL |
333 | $this->_id, |
334 | 'accepted_credit_cards' | |
335 | ), TRUE); | |
c294dbbc | 336 | $acceptedCards = array(); |
27b252af SL |
337 | if (!empty($cards)) { |
338 | foreach ($cards as $card => $val) { | |
339 | $acceptedCards[$card] = 1; | |
340 | } | |
c294dbbc SL |
341 | } |
342 | $defaults['accept_credit_cards'] = $acceptedCards; | |
cb5962bd | 343 | unset($defaults['accepted_credit_cards']); |
6a488035 TO |
344 | // now get testID |
345 | $testDAO = new CRM_Financial_DAO_PaymentProcessor(); | |
353ffa53 TO |
346 | $testDAO->name = $dao->name; |
347 | $testDAO->is_test = 1; | |
6a488035 TO |
348 | $testDAO->domain_id = $domainID; |
349 | if ($testDAO->find(TRUE)) { | |
350 | $this->_testID = $testDAO->id; | |
351 | ||
352 | foreach ($this->_fields as $field) { | |
353 | $testName = "test_{$field['name']}"; | |
354 | $defaults[$testName] = $testDAO->{$field['name']}; | |
355 | } | |
356 | } | |
357 | $defaults['financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getFinancialAccount($dao->id, 'civicrm_payment_processor', 'financial_account_id'); | |
358 | ||
359 | return $defaults; | |
360 | } | |
361 | ||
362 | /** | |
eceb18cc | 363 | * Process the form submission. |
6a488035 TO |
364 | */ |
365 | public function postProcess() { | |
6a488035 TO |
366 | |
367 | if ($this->_action & CRM_Core_Action::DELETE) { | |
368 | CRM_Financial_BAO_PaymentProcessor::del($this->_id); | |
369 | CRM_Core_Session::setStatus("", ts('Payment Processor Deleted.'), "success"); | |
28a04ea9 | 370 | return NULL; |
6a488035 TO |
371 | } |
372 | ||
373 | $values = $this->controller->exportValues($this->_name); | |
374 | $domainID = CRM_Core_Config::domainID(); | |
375 | ||
a7488080 | 376 | if (!empty($values['is_default'])) { |
6a488035 | 377 | $query = "UPDATE civicrm_payment_processor SET is_default = 0 WHERE domain_id = $domainID"; |
33621c4f | 378 | CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
379 | } |
380 | ||
381 | $this->updatePaymentProcessor($values, $domainID, FALSE); | |
382 | $this->updatePaymentProcessor($values, $domainID, TRUE); | |
383 | CRM_Core_Session::setStatus(ts('Payment processor %1 has been saved.', array(1 => "<em>{$values['name']}</em>")), ts('Saved'), 'success'); | |
384 | } | |
385 | ||
386 | /** | |
eceb18cc | 387 | * Save a payment processor. |
6a488035 | 388 | * |
ce064e4f | 389 | * @param array $values |
100fef9d | 390 | * @param int $domainID |
ce064e4f | 391 | * @param bool $test |
6a488035 | 392 | */ |
00be9182 | 393 | public function updatePaymentProcessor(&$values, $domainID, $test) { |
be725500 | 394 | if ($test) { |
421dade4 | 395 | foreach (array('user_name', 'password', 'signature', 'url_site', 'url_recur', 'url_api', 'url_button', 'subject') as $field) { |
be725500 | 396 | $values[$field] = empty($values["test_{$field}"]) ? CRM_Utils_Array::value($field, $values) : $values["test_{$field}"]; |
397 | } | |
398 | } | |
c294dbbc SL |
399 | if (!empty($values['accept_credit_cards'])) { |
400 | $creditCards = array(); | |
401 | $accptedCards = array_keys($values['accept_credit_cards']); | |
402 | $creditCardTypes = CRM_Contribute_PseudoConstant::creditCard(); | |
403 | foreach ($creditCardTypes as $type => $val) { | |
404 | if (in_array($type, $accptedCards)) { | |
405 | $creditCards[$type] = $creditCardTypes[$type]; | |
406 | } | |
407 | } | |
408 | $creditCards = json_encode($creditCards); | |
409 | } | |
410 | else { | |
411 | $creditCards = "NULL"; | |
412 | } | |
b7e7f943 | 413 | $params = array_merge(array( |
414 | 'id' => $test ? $this->_testID : $this->_id, | |
415 | 'domain_id' => $domainID, | |
416 | 'is_test' => $test, | |
417 | 'is_active' => 0, | |
418 | 'is_default' => 0, | |
419 | 'is_recur' => $this->_ppDAO->is_recur, | |
420 | 'billing_mode' => $this->_ppDAO->billing_mode, | |
421 | 'class_name' => $this->_ppDAO->class_name, | |
422 | 'payment_type' => $this->_ppDAO->payment_type, | |
423 | 'payment_instrument_id' => $this->_ppDAO->payment_instrument_id, | |
21dfd5f5 | 424 | 'financial_account_id' => $values['financial_account_id'], |
cb5962bd | 425 | 'accepted_credit_cards' => $creditCards, |
b7e7f943 | 426 | ), $values); |
427 | ||
428 | civicrm_api3('PaymentProcessor', 'create', $params); | |
6a488035 | 429 | } |
96025800 | 430 | |
6a488035 | 431 | } |