Merge pull request #15083 from civicrm/5.17
[civicrm-core.git] / CRM / Admin / Form / PaymentProcessor.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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 $_paymentProcessorDAO;
45
46 /**
47 * @var int
48 * Payment processor Type ID
49 */
50 protected $_paymentProcessorType;
51
52 /**
53 * Get the name of the base entity being edited.
54 *
55 * @return string
56 */
57 public function getDefaultEntity() {
58 return 'PaymentProcessor';
59 }
60
61 public function preProcess() {
62 parent::preProcess();
63
64 if ($this->_id) {
65 $this->_paymentProcessorType = CRM_Utils_Request::retrieve('pp', 'String', $this, FALSE, NULL);
66 if (!$this->_paymentProcessorType) {
67 $this->_paymentProcessorType = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor',
68 $this->_id,
69 'payment_processor_type_id'
70 );
71 }
72 $this->set('pp', $this->_paymentProcessorType);
73 }
74 else {
75 $this->_paymentProcessorType = CRM_Utils_Request::retrieve('pp', 'String', $this, TRUE, NULL);
76 }
77
78 $this->assign('ppType', $this->_paymentProcessorType);
79 $ppTypeName = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType',
80 $this->_paymentProcessorType,
81 'name'
82 );
83 $this->assign('ppTypeName', $ppTypeName);
84
85 $this->_paymentProcessorDAO = new CRM_Financial_DAO_PaymentProcessorType();
86 $this->_paymentProcessorDAO->id = $this->_paymentProcessorType;
87
88 $this->_paymentProcessorDAO->find(TRUE);
89
90 if ($this->_id) {
91 $refreshURL = CRM_Utils_System::url('civicrm/admin/paymentProcessor',
92 "reset=1&action=update&id={$this->_id}",
93 FALSE, NULL, FALSE
94 );
95 }
96 else {
97 $refreshURL = CRM_Utils_System::url('civicrm/admin/paymentProcessor',
98 "reset=1&action=add",
99 FALSE, NULL, FALSE
100 );
101 }
102
103 //CRM-4129
104 $destination = CRM_Utils_Request::retrieve('civicrmDestination', 'String', $this);
105 if ($destination) {
106 $destination = urlencode($destination);
107 $refreshURL .= "&civicrmDestination=$destination";
108 }
109
110 $this->refreshURL = $refreshURL;
111 $this->assign('refreshURL', $refreshURL);
112
113 $this->assign('is_recur', $this->_paymentProcessorDAO->is_recur);
114
115 $this->_fields = [
116 [
117 'name' => 'user_name',
118 'label' => $this->_paymentProcessorDAO->user_name_label,
119 ],
120 [
121 'name' => 'password',
122 'label' => $this->_paymentProcessorDAO->password_label,
123 ],
124 [
125 'name' => 'signature',
126 'label' => $this->_paymentProcessorDAO->signature_label,
127 ],
128 [
129 'name' => 'subject',
130 'label' => $this->_paymentProcessorDAO->subject_label,
131 ],
132 [
133 'name' => 'url_site',
134 'label' => ts('Site URL'),
135 'rule' => 'url',
136 'msg' => ts('Enter a valid URL'),
137 ],
138 ];
139
140 if ($this->_paymentProcessorDAO->is_recur) {
141 $this->_fields[] = [
142 'name' => 'url_recur',
143 'label' => ts('Recurring Payments URL'),
144 'rule' => 'url',
145 'msg' => ts('Enter a valid URL'),
146 ];
147 }
148
149 if (!empty($this->_paymentProcessorDAO->url_button_default)) {
150 $this->_fields[] = [
151 'name' => 'url_button',
152 'label' => ts('Button URL'),
153 'rule' => 'url',
154 'msg' => ts('Enter a valid URL'),
155 ];
156 }
157
158 if (!empty($this->_paymentProcessorDAO->url_api_default)) {
159 $this->_fields[] = [
160 'name' => 'url_api',
161 'label' => ts('API URL'),
162 'rule' => 'url',
163 'msg' => ts('Enter a valid URL'),
164 ];
165 }
166 }
167
168 /**
169 * Build the form object.
170 *
171 * @param bool $check
172 */
173 public function buildQuickForm($check = FALSE) {
174 parent::buildQuickForm();
175
176 if ($this->_action & CRM_Core_Action::DELETE) {
177 return;
178 }
179
180 $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_PaymentProcessor');
181
182 $this->add('text', 'name', ts('Name'),
183 $attributes['name'], TRUE
184 );
185
186 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', [
187 'CRM_Financial_DAO_PaymentProcessor',
188 $this->_id,
189 'name',
190 CRM_Core_Config::domainID(),
191 ]);
192
193 $this->add('text', 'description', ts('Description'),
194 $attributes['description']
195 );
196
197 $this->add('select',
198 'payment_processor_type_id',
199 ts('Payment Processor Type'),
200 CRM_Financial_BAO_PaymentProcessor::buildOptions('payment_processor_type_id'),
201 TRUE,
202 ['onchange' => "reload(true)"]
203 );
204
205 // Financial Account of account type asset CRM-11515
206 $accountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' ");
207 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount(NULL, key($accountType));
208 if ($fcount = count($financialAccount)) {
209 $this->assign('financialAccount', $fcount);
210 }
211 $this->add('select', 'financial_account_id', ts('Financial Account'),
212 ['' => ts('- select -')] + $financialAccount,
213 TRUE
214 );
215 $this->addSelect('payment_instrument_id',
216 [
217 'entity' => 'contribution',
218 'label' => ts('Payment Method'),
219 'placeholder' => NULL,
220 ]
221 );
222
223 // is this processor active ?
224 $this->add('checkbox', 'is_active', ts('Is this Payment Processor active?'));
225 $this->add('checkbox', 'is_default', ts('Is this Payment Processor the default?'));
226 $creditCardTypes = CRM_Contribute_PseudoConstant::creditCard();
227 $this->addCheckBox('accept_credit_cards', ts('Accepted Credit Card Type(s)'),
228 $creditCardTypes, NULL, NULL, NULL, NULL, '&nbsp;&nbsp;&nbsp;');
229 foreach ($this->_fields as $field) {
230 if (empty($field['label'])) {
231 continue;
232 }
233
234 $this->addField($field['name'], ['label' => $field['label']]);
235
236 $fieldSpec = civicrm_api3($this->getDefaultEntity(), 'getfield', [
237 'name' => $field['name'],
238 'action' => 'create',
239 ]);
240 $this->add($fieldSpec['values']['html']['type'], "test_{$field['name']}",
241 $field['label'], $attributes[$field['name']]
242 );
243 if (!empty($field['rule'])) {
244 $this->addRule($field['name'], $field['msg'], $field['rule']);
245 $this->addRule("test_{$field['name']}", $field['msg'], $field['rule']);
246 }
247 }
248
249 $this->addFormRule(['CRM_Admin_Form_PaymentProcessor', 'formRule']);
250 }
251
252 /**
253 * @param $fields
254 *
255 * @return array|bool
256 */
257 public static function formRule($fields) {
258
259 // make sure that at least one of live or test is present
260 // and we have at least name and url_site
261 // would be good to make this processor specific
262 $errors = [];
263
264 if (!(self::checkSection($fields, $errors) ||
265 self::checkSection($fields, $errors, 'test')
266 )
267 ) {
268 $errors['_qf_default'] = ts('You must have at least the test or live section filled');
269 }
270
271 if (!empty($errors)) {
272 return $errors;
273 }
274
275 return empty($errors) ? TRUE : $errors;
276 }
277
278 /**
279 * @param $fields
280 * @param $errors
281 * @param null $section
282 *
283 * @return bool
284 */
285 public static function checkSection(&$fields, &$errors, $section = NULL) {
286 $names = ['user_name'];
287
288 $present = FALSE;
289 $allPresent = TRUE;
290 foreach ($names as $name) {
291 if ($section) {
292 $name = "{$section}_$name";
293 }
294 if (!empty($fields[$name])) {
295 $present = TRUE;
296 }
297 else {
298 $allPresent = FALSE;
299 }
300 }
301
302 if ($present) {
303 if (!$allPresent) {
304 $errors['_qf_default'] = ts('You must have at least the user_name specified');
305 }
306 }
307 return $present;
308 }
309
310 /**
311 * @return array
312 */
313 public function setDefaultValues() {
314 $defaults = [];
315
316 if (!$this->_id) {
317 $defaults['is_active'] = $defaults['is_default'] = 1;
318 $defaults['url_site'] = $this->_paymentProcessorDAO->url_site_default;
319 $defaults['url_api'] = $this->_paymentProcessorDAO->url_api_default;
320 $defaults['url_recur'] = $this->_paymentProcessorDAO->url_recur_default;
321 $defaults['url_button'] = $this->_paymentProcessorDAO->url_button_default;
322 $defaults['test_url_site'] = $this->_paymentProcessorDAO->url_site_test_default;
323 $defaults['test_url_api'] = $this->_paymentProcessorDAO->url_api_test_default;
324 $defaults['test_url_recur'] = $this->_paymentProcessorDAO->url_recur_test_default;
325 $defaults['test_url_button'] = $this->_paymentProcessorDAO->url_button_test_default;
326 $defaults['payment_instrument_id'] = $this->_paymentProcessorDAO->payment_instrument_id;
327 // When user changes payment processor type, it is passed in via $this->_ppType so update defaults array.
328 if ($this->_paymentProcessorType) {
329 $defaults['payment_processor_type_id'] = $this->_paymentProcessorType;
330 }
331 $defaults['financial_account_id'] = CRM_Financial_BAO_PaymentProcessor::getDefaultFinancialAccountID();
332 return $defaults;
333 }
334 $domainID = CRM_Core_Config::domainID();
335
336 $dao = new CRM_Financial_DAO_PaymentProcessor();
337 $dao->id = $this->_id;
338 $dao->domain_id = $domainID;
339 if (!$dao->find(TRUE)) {
340 return $defaults;
341 }
342
343 CRM_Core_DAO::storeValues($dao, $defaults);
344 // If payment processor ID does not exist, $paymentProcessorName will be FALSE
345 $paymentProcessorName = CRM_Core_PseudoConstant::getName('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', $this->_paymentProcessorType);
346 if ($this->_paymentProcessorType && $paymentProcessorName) {
347 // When user changes payment processor type, it is passed in via $this->_ppType so update defaults array.
348 $defaults['payment_processor_type_id'] = $this->_paymentProcessorType;
349 }
350 else {
351 CRM_Core_Session::setStatus('Payment Processor Type (ID=' . $this->_paymentProcessorType . ') not found. Did you disable the payment processor extension?', 'Missing Payment Processor', 'alert');
352 }
353
354 $cards = json_decode(CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor',
355 $this->_id,
356 'accepted_credit_cards'
357 ), TRUE);
358 $acceptedCards = [];
359 if (!empty($cards)) {
360 foreach ($cards as $card => $val) {
361 $acceptedCards[$card] = 1;
362 }
363 }
364 $defaults['accept_credit_cards'] = $acceptedCards;
365 unset($defaults['accepted_credit_cards']);
366 // now get testID
367 $testDAO = new CRM_Financial_DAO_PaymentProcessor();
368 $testDAO->name = $dao->name;
369 $testDAO->is_test = 1;
370 $testDAO->domain_id = $domainID;
371 if ($testDAO->find(TRUE)) {
372 $this->_testID = $testDAO->id;
373
374 foreach ($this->_fields as $field) {
375 $testName = "test_{$field['name']}";
376 $defaults[$testName] = $testDAO->{$field['name']};
377 }
378 }
379 $defaults['financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($dao->id, NULL, 'civicrm_payment_processor');
380
381 return $defaults;
382 }
383
384 /**
385 * Process the form submission.
386 */
387 public function postProcess() {
388
389 if ($this->_action & CRM_Core_Action::DELETE) {
390 CRM_Financial_BAO_PaymentProcessor::del($this->_id);
391 CRM_Core_Session::setStatus("", ts('Payment Processor Deleted.'), "success");
392 return NULL;
393 }
394
395 $values = $this->controller->exportValues($this->_name);
396 $domainID = CRM_Core_Config::domainID();
397
398 if (!empty($values['is_default'])) {
399 $query = "UPDATE civicrm_payment_processor SET is_default = 0 WHERE domain_id = $domainID";
400 CRM_Core_DAO::executeQuery($query);
401 }
402
403 if ($this->_paymentProcessorType !== $values['payment_processor_type_id']) {
404 // If we changed the payment processor type, need to update the object as well
405 $this->_paymentProcessorType = $values['payment_processor_type_id'];
406 $this->_paymentProcessorDAO = new CRM_Financial_DAO_PaymentProcessorType();
407 $this->_paymentProcessorDAO->id = $values['payment_processor_type_id'];
408 $this->_paymentProcessorDAO->find(TRUE);
409 }
410 $this->updatePaymentProcessor($values, $domainID, FALSE);
411 $this->updatePaymentProcessor($values, $domainID, TRUE);
412
413 $processor = civicrm_api3('payment_processor', 'getsingle', ['name' => $values['name'], 'is_test' => 0]);
414 $errors = Civi\Payment\System::singleton()->checkProcessorConfig($processor);
415 if ($errors) {
416 CRM_Core_Session::setStatus($errors, 'Payment processor configuration invalid', 'error');
417 Civi::log()->error('Payment processor configuration invalid: ' . $errors);
418 CRM_Core_Session::singleton()->pushUserContext($this->refreshURL);
419 }
420 else {
421 CRM_Core_Session::setStatus(ts('Payment processor %1 has been saved.', [1 => "<em>{$values['name']}</em>"]), ts('Saved'), 'success');
422 }
423 }
424
425 /**
426 * Save a payment processor.
427 *
428 * @param array $values
429 * @param int $domainID
430 * @param bool $test
431 */
432 public function updatePaymentProcessor(&$values, $domainID, $test) {
433 if ($test) {
434 foreach (['user_name', 'password', 'signature', 'url_site', 'url_recur', 'url_api', 'url_button', 'subject'] as $field) {
435 $values[$field] = empty($values["test_{$field}"]) ? CRM_Utils_Array::value($field, $values) : $values["test_{$field}"];
436 }
437 }
438 if (!empty($values['accept_credit_cards'])) {
439 $creditCards = [];
440 $accptedCards = array_keys($values['accept_credit_cards']);
441 $creditCardTypes = CRM_Contribute_PseudoConstant::creditCard();
442 foreach ($creditCardTypes as $type => $val) {
443 if (in_array($type, $accptedCards)) {
444 $creditCards[$type] = $creditCardTypes[$type];
445 }
446 }
447 $creditCards = json_encode($creditCards);
448 }
449 else {
450 $creditCards = "NULL";
451 }
452 $params = array_merge([
453 'id' => $test ? $this->_testID : $this->_id,
454 'domain_id' => $domainID,
455 'is_test' => $test,
456 'is_active' => 0,
457 'is_default' => 0,
458 'is_recur' => $this->_paymentProcessorDAO->is_recur,
459 'billing_mode' => $this->_paymentProcessorDAO->billing_mode,
460 'class_name' => $this->_paymentProcessorDAO->class_name,
461 'payment_type' => $this->_paymentProcessorDAO->payment_type,
462 'payment_instrument_id' => $this->_paymentProcessorDAO->payment_instrument_id,
463 'financial_account_id' => $values['financial_account_id'],
464 'accepted_credit_cards' => $creditCards,
465 ], $values);
466
467 civicrm_api3('PaymentProcessor', 'create', $params);
468 }
469
470 }