Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
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 | |
1d2a585a | 46 | /** |
b30c9316 | 47 | * Get the name of the base entity being edited. |
1d2a585a | 48 | * |
49 | * @return string | |
1d2a585a | 50 | */ |
51 | public function getDefaultEntity() { | |
52 | return 'PaymentProcessor'; | |
53 | } | |
54 | ||
00be9182 | 55 | public function preProcess() { |
6a488035 TO |
56 | parent::preProcess(); |
57 | ||
6a488035 TO |
58 | if ($this->_id) { |
59 | $this->_ppType = CRM_Utils_Request::retrieve('pp', 'String', $this, FALSE, NULL); | |
60 | if (!$this->_ppType) { | |
481a74f4 | 61 | $this->_ppType = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor', |
6a488035 TO |
62 | $this->_id, |
63 | 'payment_processor_type_id' | |
64 | ); | |
65 | } | |
66 | $this->set('pp', $this->_ppType); | |
67 | } | |
68 | else { | |
69 | $this->_ppType = CRM_Utils_Request::retrieve('pp', 'String', $this, TRUE, NULL); | |
70 | } | |
71 | ||
72 | $this->assign('ppType', $this->_ppType); | |
481a74f4 | 73 | $ppTypeName = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', |
6a488035 TO |
74 | $this->_ppType, |
75 | 'name' | |
76 | ); | |
481a74f4 | 77 | $this->assign('ppTypeName', $ppTypeName); |
6a488035 | 78 | |
481a74f4 | 79 | $this->_ppDAO = new CRM_Financial_DAO_PaymentProcessorType(); |
6a488035 TO |
80 | $this->_ppDAO->id = $this->_ppType; |
81 | ||
1d2a585a | 82 | $this->_ppDAO->find(TRUE); |
6a488035 TO |
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, |
35b63106 DS |
182 | 'name', |
183 | CRM_Core_Config::domainID(), | |
353ffa53 | 184 | )); |
6a488035 TO |
185 | |
186 | $this->add('text', 'description', ts('Description'), | |
187 | $attributes['description'] | |
188 | ); | |
189 | ||
190 | $types = CRM_Core_PseudoConstant::paymentProcessorType(); | |
481a74f4 | 191 | $this->add('select', 'payment_processor_type_id', ts('Payment Processor Type'), $types, TRUE, |
8ef12e64 | 192 | array('onchange' => "reload(true)") |
6a488035 TO |
193 | ); |
194 | ||
195 | // Financial Account of account type asset CRM-11515 | |
f743a6eb | 196 | $accountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' "); |
6a488035 TO |
197 | $financialAccount = CRM_Contribute_PseudoConstant::financialAccount(NULL, key($accountType)); |
198 | if ($fcount = count($financialAccount)) { | |
199 | $this->assign('financialAccount', $fcount); | |
200 | } | |
8ef12e64 | 201 | $this->add('select', 'financial_account_id', ts('Financial Account'), |
6a488035 | 202 | array('' => ts('- select -')) + $financialAccount, |
02fc859b | 203 | TRUE |
6a488035 | 204 | ); |
b7e7f943 | 205 | $this->addSelect('payment_instrument_id', |
206 | array( | |
207 | 'entity' => 'contribution', | |
208 | 'label' => ts('Payment Method'), | |
209 | 'placeholder' => NULL, | |
210 | ) | |
211 | ); | |
212 | ||
6a488035 TO |
213 | // is this processor active ? |
214 | $this->add('checkbox', 'is_active', ts('Is this Payment Processor active?')); | |
215 | $this->add('checkbox', 'is_default', ts('Is this Payment Processor the default?')); | |
cb5962bd | 216 | $creditCardTypes = CRM_Contribute_PseudoConstant::creditCard(); |
660ef5f6 | 217 | $this->addCheckBox('accept_credit_cards', ts('Accepted Credit Card Type(s)'), |
cb5962bd | 218 | $creditCardTypes, NULL, NULL, NULL, NULL, ' '); |
6a488035 TO |
219 | foreach ($this->_fields as $field) { |
220 | if (empty($field['label'])) { | |
221 | continue; | |
222 | } | |
223 | ||
1d2a585a | 224 | $this->addField($field['name'], array('label' => $field['label'])); |
225 | ||
226 | $fieldSpec = civicrm_api3($this->getDefaultEntity(), 'getfield', array( | |
227 | 'name' => $field['name'], | |
228 | 'action' => 'create', | |
229 | )); | |
230 | $this->add($fieldSpec['values']['html']['type'], "test_{$field['name']}", | |
6a488035 TO |
231 | $field['label'], $attributes[$field['name']] |
232 | ); | |
a7488080 | 233 | if (!empty($field['rule'])) { |
6a488035 TO |
234 | $this->addRule($field['name'], $field['msg'], $field['rule']); |
235 | $this->addRule("test_{$field['name']}", $field['msg'], $field['rule']); | |
236 | } | |
237 | } | |
238 | ||
239 | $this->addFormRule(array('CRM_Admin_Form_PaymentProcessor', 'formRule')); | |
240 | } | |
241 | ||
e0ef6999 EM |
242 | /** |
243 | * @param $fields | |
244 | * | |
245 | * @return array|bool | |
246 | */ | |
00be9182 | 247 | public static function formRule($fields) { |
6a488035 TO |
248 | |
249 | // make sure that at least one of live or test is present | |
250 | // and we have at least name and url_site | |
251 | // would be good to make this processor specific | |
252 | $errors = array(); | |
253 | ||
254 | if (!(self::checkSection($fields, $errors) || | |
353ffa53 TO |
255 | self::checkSection($fields, $errors, 'test') |
256 | ) | |
257 | ) { | |
6a488035 TO |
258 | $errors['_qf_default'] = ts('You must have at least the test or live section filled'); |
259 | } | |
260 | ||
261 | if (!empty($errors)) { | |
262 | return $errors; | |
263 | } | |
264 | ||
265 | return empty($errors) ? TRUE : $errors; | |
266 | } | |
267 | ||
e0ef6999 EM |
268 | /** |
269 | * @param $fields | |
270 | * @param $errors | |
271 | * @param null $section | |
272 | * | |
273 | * @return bool | |
274 | */ | |
00be9182 | 275 | public static function checkSection(&$fields, &$errors, $section = NULL) { |
6a488035 TO |
276 | $names = array('user_name'); |
277 | ||
278 | $present = FALSE; | |
279 | $allPresent = TRUE; | |
280 | foreach ($names as $name) { | |
281 | if ($section) { | |
282 | $name = "{$section}_$name"; | |
283 | } | |
284 | if (!empty($fields[$name])) { | |
285 | $present = TRUE; | |
286 | } | |
287 | else { | |
288 | $allPresent = FALSE; | |
289 | } | |
290 | } | |
291 | ||
292 | if ($present) { | |
293 | if (!$allPresent) { | |
294 | $errors['_qf_default'] = ts('You must have at least the user_name specified'); | |
295 | } | |
296 | } | |
297 | return $present; | |
298 | } | |
299 | ||
e0ef6999 EM |
300 | /** |
301 | * @return array | |
302 | */ | |
00be9182 | 303 | public function setDefaultValues() { |
6a488035 | 304 | $defaults = array(); |
6d5de837 | 305 | |
6a488035 TO |
306 | if (!$this->_id) { |
307 | $defaults['is_active'] = $defaults['is_default'] = 1; | |
308 | $defaults['url_site'] = $this->_ppDAO->url_site_default; | |
309 | $defaults['url_api'] = $this->_ppDAO->url_api_default; | |
310 | $defaults['url_recur'] = $this->_ppDAO->url_recur_default; | |
311 | $defaults['url_button'] = $this->_ppDAO->url_button_default; | |
312 | $defaults['test_url_site'] = $this->_ppDAO->url_site_test_default; | |
313 | $defaults['test_url_api'] = $this->_ppDAO->url_api_test_default; | |
314 | $defaults['test_url_recur'] = $this->_ppDAO->url_recur_test_default; | |
315 | $defaults['test_url_button'] = $this->_ppDAO->url_button_test_default; | |
b7e7f943 | 316 | $defaults['payment_instrument_id'] = $this->_ppDAO->payment_instrument_id; |
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 | return $defaults; |
322 | } | |
323 | $domainID = CRM_Core_Config::domainID(); | |
324 | ||
481a74f4 | 325 | $dao = new CRM_Financial_DAO_PaymentProcessor(); |
353ffa53 | 326 | $dao->id = $this->_id; |
6a488035 TO |
327 | $dao->domain_id = $domainID; |
328 | if (!$dao->find(TRUE)) { | |
329 | return $defaults; | |
330 | } | |
331 | ||
332 | CRM_Core_DAO::storeValues($dao, $defaults); | |
6d5de837 DG |
333 | // When user changes payment processor type, it is passed in via $this->_ppType so update defaults array. |
334 | if ($this->_ppType) { | |
335 | $defaults['payment_processor_type_id'] = $this->_ppType; | |
336 | } | |
c294dbbc | 337 | $cards = json_decode(CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor', |
cb5962bd SL |
338 | $this->_id, |
339 | 'accepted_credit_cards' | |
340 | ), TRUE); | |
c294dbbc | 341 | $acceptedCards = array(); |
27b252af SL |
342 | if (!empty($cards)) { |
343 | foreach ($cards as $card => $val) { | |
344 | $acceptedCards[$card] = 1; | |
345 | } | |
c294dbbc SL |
346 | } |
347 | $defaults['accept_credit_cards'] = $acceptedCards; | |
cb5962bd | 348 | unset($defaults['accepted_credit_cards']); |
6a488035 TO |
349 | // now get testID |
350 | $testDAO = new CRM_Financial_DAO_PaymentProcessor(); | |
353ffa53 TO |
351 | $testDAO->name = $dao->name; |
352 | $testDAO->is_test = 1; | |
6a488035 TO |
353 | $testDAO->domain_id = $domainID; |
354 | if ($testDAO->find(TRUE)) { | |
355 | $this->_testID = $testDAO->id; | |
356 | ||
357 | foreach ($this->_fields as $field) { | |
358 | $testName = "test_{$field['name']}"; | |
359 | $defaults[$testName] = $testDAO->{$field['name']}; | |
360 | } | |
361 | } | |
74afdc40 | 362 | $defaults['financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($dao->id, NULL, 'civicrm_payment_processor'); |
6a488035 TO |
363 | |
364 | return $defaults; | |
365 | } | |
366 | ||
367 | /** | |
eceb18cc | 368 | * Process the form submission. |
6a488035 TO |
369 | */ |
370 | public function postProcess() { | |
6a488035 TO |
371 | |
372 | if ($this->_action & CRM_Core_Action::DELETE) { | |
373 | CRM_Financial_BAO_PaymentProcessor::del($this->_id); | |
374 | CRM_Core_Session::setStatus("", ts('Payment Processor Deleted.'), "success"); | |
28a04ea9 | 375 | return NULL; |
6a488035 TO |
376 | } |
377 | ||
378 | $values = $this->controller->exportValues($this->_name); | |
379 | $domainID = CRM_Core_Config::domainID(); | |
380 | ||
a7488080 | 381 | if (!empty($values['is_default'])) { |
6a488035 | 382 | $query = "UPDATE civicrm_payment_processor SET is_default = 0 WHERE domain_id = $domainID"; |
33621c4f | 383 | CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
384 | } |
385 | ||
386 | $this->updatePaymentProcessor($values, $domainID, FALSE); | |
387 | $this->updatePaymentProcessor($values, $domainID, TRUE); | |
388 | CRM_Core_Session::setStatus(ts('Payment processor %1 has been saved.', array(1 => "<em>{$values['name']}</em>")), ts('Saved'), 'success'); | |
389 | } | |
390 | ||
391 | /** | |
eceb18cc | 392 | * Save a payment processor. |
6a488035 | 393 | * |
ce064e4f | 394 | * @param array $values |
100fef9d | 395 | * @param int $domainID |
ce064e4f | 396 | * @param bool $test |
6a488035 | 397 | */ |
00be9182 | 398 | public function updatePaymentProcessor(&$values, $domainID, $test) { |
be725500 | 399 | if ($test) { |
421dade4 | 400 | foreach (array('user_name', 'password', 'signature', 'url_site', 'url_recur', 'url_api', 'url_button', 'subject') as $field) { |
be725500 | 401 | $values[$field] = empty($values["test_{$field}"]) ? CRM_Utils_Array::value($field, $values) : $values["test_{$field}"]; |
402 | } | |
403 | } | |
c294dbbc SL |
404 | if (!empty($values['accept_credit_cards'])) { |
405 | $creditCards = array(); | |
406 | $accptedCards = array_keys($values['accept_credit_cards']); | |
407 | $creditCardTypes = CRM_Contribute_PseudoConstant::creditCard(); | |
408 | foreach ($creditCardTypes as $type => $val) { | |
409 | if (in_array($type, $accptedCards)) { | |
410 | $creditCards[$type] = $creditCardTypes[$type]; | |
411 | } | |
412 | } | |
413 | $creditCards = json_encode($creditCards); | |
414 | } | |
415 | else { | |
416 | $creditCards = "NULL"; | |
417 | } | |
b7e7f943 | 418 | $params = array_merge(array( |
419 | 'id' => $test ? $this->_testID : $this->_id, | |
420 | 'domain_id' => $domainID, | |
421 | 'is_test' => $test, | |
422 | 'is_active' => 0, | |
423 | 'is_default' => 0, | |
424 | 'is_recur' => $this->_ppDAO->is_recur, | |
425 | 'billing_mode' => $this->_ppDAO->billing_mode, | |
426 | 'class_name' => $this->_ppDAO->class_name, | |
427 | 'payment_type' => $this->_ppDAO->payment_type, | |
428 | 'payment_instrument_id' => $this->_ppDAO->payment_instrument_id, | |
21dfd5f5 | 429 | 'financial_account_id' => $values['financial_account_id'], |
cb5962bd | 430 | 'accepted_credit_cards' => $creditCards, |
b7e7f943 | 431 | ), $values); |
432 | ||
433 | civicrm_api3('PaymentProcessor', 'create', $params); | |
6a488035 | 434 | } |
96025800 | 435 | |
6a488035 | 436 | } |