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