3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 * This class generates form components for Payment Processor.
37 class CRM_Admin_Form_PaymentProcessor
extends CRM_Admin_Form
{
38 protected $_id = NULL;
40 protected $_testID = NULL;
42 protected $_fields = NULL;
46 public function preProcess() {
47 if (!CRM_Core_Permission
::check('administer payment processors')) {
48 CRM_Core_Error
::statusBounce('The \'administer payment processors\' permission is required to add or edit a payment processor.');
52 CRM_Utils_System
::setTitle(ts('Settings - Payment Processor'));
54 // get the payment processor meta information
57 $this->_ppType
= CRM_Utils_Request
::retrieve('pp', 'String', $this, FALSE, NULL);
58 if (!$this->_ppType
) {
59 $this->_ppType
= CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_PaymentProcessor',
61 'payment_processor_type_id'
64 $this->set('pp', $this->_ppType
);
67 $this->_ppType
= CRM_Utils_Request
::retrieve('pp', 'String', $this, TRUE, NULL);
70 $this->assign('ppType', $this->_ppType
);
71 $ppTypeName = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_PaymentProcessorType',
75 $this->assign('ppTypeName', $ppTypeName);
77 $this->_ppDAO
= new CRM_Financial_DAO_PaymentProcessorType();
78 $this->_ppDAO
->id
= $this->_ppType
;
80 if (!$this->_ppDAO
->find(TRUE)) {
81 CRM_Core_Error
::fatal(ts('Could not find payment processor meta information'));
85 $refreshURL = CRM_Utils_System
::url('civicrm/admin/paymentProcessor',
86 "reset=1&action=update&id={$this->_id}",
91 $refreshURL = CRM_Utils_System
::url('civicrm/admin/paymentProcessor',
98 $destination = CRM_Utils_Request
::retrieve('civicrmDestination', 'String', $this);
100 $destination = urlencode($destination);
101 $refreshURL .= "&civicrmDestination=$destination";
104 $this->assign('refreshURL', $refreshURL);
106 $this->assign('is_recur', $this->_ppDAO
->is_recur
);
108 $this->_fields
= array(
110 'name' => 'user_name',
111 'label' => $this->_ppDAO
->user_name_label
,
114 'name' => 'password',
115 'label' => $this->_ppDAO
->password_label
,
118 'name' => 'signature',
119 'label' => $this->_ppDAO
->signature_label
,
123 'label' => $this->_ppDAO
->subject_label
,
126 'name' => 'url_site',
127 'label' => ts('Site URL'),
129 'msg' => ts('Enter a valid URL'),
133 if ($this->_ppDAO
->is_recur
) {
134 $this->_fields
[] = array(
135 'name' => 'url_recur',
136 'label' => ts('Recurring Payments URL'),
138 'msg' => ts('Enter a valid URL'),
142 if (!empty($this->_ppDAO
->url_button_default
)) {
143 $this->_fields
[] = array(
144 'name' => 'url_button',
145 'label' => ts('Button URL'),
147 'msg' => ts('Enter a valid URL'),
151 if (!empty($this->_ppDAO
->url_api_default
)) {
152 $this->_fields
[] = array(
154 'label' => ts('API URL'),
156 'msg' => ts('Enter a valid URL'),
162 * Build the form object.
166 public function buildQuickForm($check = FALSE) {
167 parent
::buildQuickForm();
169 if ($this->_action
& CRM_Core_Action
::DELETE
) {
173 $attributes = CRM_Core_DAO
::getAttribute('CRM_Financial_DAO_PaymentProcessor');
175 $this->add('text', 'name', ts('Name'),
176 $attributes['name'], TRUE
179 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array(
180 'CRM_Financial_DAO_PaymentProcessor',
184 $this->add('text', 'description', ts('Description'),
185 $attributes['description']
188 $types = CRM_Core_PseudoConstant
::paymentProcessorType();
189 $this->add('select', 'payment_processor_type_id', ts('Payment Processor Type'), $types, TRUE,
190 array('onchange' => "reload(true)")
193 // Financial Account of account type asset CRM-11515
194 $accountType = CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' ");
195 $financialAccount = CRM_Contribute_PseudoConstant
::financialAccount(NULL, key($accountType));
196 if ($fcount = count($financialAccount)) {
197 $this->assign('financialAccount', $fcount);
199 $this->add('select', 'financial_account_id', ts('Financial Account'),
200 array('' => ts('- select -')) +
$financialAccount,
203 // is this processor active ?
204 $this->add('checkbox', 'is_active', ts('Is this Payment Processor active?'));
205 $this->add('checkbox', 'is_default', ts('Is this Payment Processor the default?'));
207 foreach ($this->_fields
as $field) {
208 if (empty($field['label'])) {
212 $this->add('text', $field['name'],
213 $field['label'], $attributes[$field['name']]
215 $this->add('text', "test_{$field['name']}",
216 $field['label'], $attributes[$field['name']]
218 if (!empty($field['rule'])) {
219 $this->addRule($field['name'], $field['msg'], $field['rule']);
220 $this->addRule("test_{$field['name']}", $field['msg'], $field['rule']);
224 $this->addFormRule(array('CRM_Admin_Form_PaymentProcessor', 'formRule'));
232 public static function formRule($fields) {
234 // make sure that at least one of live or test is present
235 // and we have at least name and url_site
236 // would be good to make this processor specific
239 if (!(self
::checkSection($fields, $errors) ||
240 self
::checkSection($fields, $errors, 'test')
243 $errors['_qf_default'] = ts('You must have at least the test or live section filled');
246 if (!empty($errors)) {
250 return empty($errors) ?
TRUE : $errors;
256 * @param null $section
260 public static function checkSection(&$fields, &$errors, $section = NULL) {
261 $names = array('user_name');
265 foreach ($names as $name) {
267 $name = "{$section}_$name";
269 if (!empty($fields[$name])) {
279 $errors['_qf_default'] = ts('You must have at least the user_name specified');
288 public function setDefaultValues() {
290 if ($this->_ppType
) {
291 $defaults['payment_processor_type_id'] = $this->_ppType
;
294 $defaults['is_active'] = $defaults['is_default'] = 1;
295 $defaults['url_site'] = $this->_ppDAO
->url_site_default
;
296 $defaults['url_api'] = $this->_ppDAO
->url_api_default
;
297 $defaults['url_recur'] = $this->_ppDAO
->url_recur_default
;
298 $defaults['url_button'] = $this->_ppDAO
->url_button_default
;
299 $defaults['test_url_site'] = $this->_ppDAO
->url_site_test_default
;
300 $defaults['test_url_api'] = $this->_ppDAO
->url_api_test_default
;
301 $defaults['test_url_recur'] = $this->_ppDAO
->url_recur_test_default
;
302 $defaults['test_url_button'] = $this->_ppDAO
->url_button_test_default
;
305 $domainID = CRM_Core_Config
::domainID();
307 $dao = new CRM_Financial_DAO_PaymentProcessor();
308 $dao->id
= $this->_id
;
309 $dao->domain_id
= $domainID;
310 if (!$dao->find(TRUE)) {
314 CRM_Core_DAO
::storeValues($dao, $defaults);
317 $testDAO = new CRM_Financial_DAO_PaymentProcessor();
318 $testDAO->name
= $dao->name
;
319 $testDAO->is_test
= 1;
320 $testDAO->domain_id
= $domainID;
321 if ($testDAO->find(TRUE)) {
322 $this->_testID
= $testDAO->id
;
324 foreach ($this->_fields
as $field) {
325 $testName = "test_{$field['name']}";
326 $defaults[$testName] = $testDAO->{$field['name']};
329 $defaults['financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getFinancialAccount($dao->id
, 'civicrm_payment_processor', 'financial_account_id');
335 * Process the form submission.
337 public function postProcess() {
338 CRM_Utils_System
::flushCache('CRM_Financial_DAO_PaymentProcessor');
340 if ($this->_action
& CRM_Core_Action
::DELETE
) {
341 CRM_Financial_BAO_PaymentProcessor
::del($this->_id
);
342 CRM_Core_Session
::setStatus("", ts('Payment Processor Deleted.'), "success");
346 $values = $this->controller
->exportValues($this->_name
);
347 $domainID = CRM_Core_Config
::domainID();
349 if (!empty($values['is_default'])) {
350 $query = "UPDATE civicrm_payment_processor SET is_default = 0 WHERE domain_id = $domainID";
351 CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
354 $this->updatePaymentProcessor($values, $domainID, FALSE);
355 $this->updatePaymentProcessor($values, $domainID, TRUE);
356 CRM_Core_Session
::setStatus(ts('Payment processor %1 has been saved.', array(1 => "<em>{$values['name']}</em>")), ts('Saved'), 'success');
360 * Save a payment processor.
362 * @param array $values
363 * @param int $domainID
366 public function updatePaymentProcessor(&$values, $domainID, $test) {
367 $dao = new CRM_Financial_DAO_PaymentProcessor();
369 $dao->id
= $test ?
$this->_testID
: $this->_id
;
370 $dao->domain_id
= $domainID;
371 $dao->is_test
= $test;
372 $dao->is_default
= CRM_Utils_Array
::value('is_default', $values, 0);
374 $dao->is_active
= CRM_Utils_Array
::value('is_active', $values, 0);
376 $dao->name
= $values['name'];
377 $dao->description
= $values['description'];
378 $dao->payment_processor_type_id
= $values['payment_processor_type_id'];
380 foreach ($this->_fields
as $field) {
381 $fieldName = $test ?
"test_{$field['name']}" : $field['name'];
382 $dao->{$field['name']} = trim(CRM_Utils_Array
::value($fieldName, $values));
383 if (empty($dao->{$field['name']})) {
384 $dao->{$field['name']} = 'null';
388 // also copy meta fields from the info DAO
389 $dao->is_recur
= $this->_ppDAO
->is_recur
;
390 $dao->billing_mode
= $this->_ppDAO
->billing_mode
;
391 $dao->class_name
= $this->_ppDAO
->class_name
;
392 $dao->payment_type
= $this->_ppDAO
->payment_type
;
398 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
400 'entity_table' => 'civicrm_payment_processor',
401 'entity_id' => $dao->id
,
402 'account_relationship' => $relationTypeId,
403 'financial_account_id' => $values['financial_account_id'],
405 CRM_Financial_BAO_FinancialTypeAccount
::add($params);