3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id: PaymentProcessor.php 9702 2007-05-29 23:57:16Z lobo $
37 * This class generates form components for Payment Processor
40 class CRM_Admin_Form_PaymentProcessor
extends CRM_Admin_Form
{
41 protected $_id = NULL;
43 protected $_testID = NULL;
45 protected $_fields = NULL;
49 public function preProcess() {
50 if(!CRM_Core_Permission
::check('administer payment processors')) {
51 CRM_Core_Error
::statusBounce('The \'administer payment processors\' permission is required to add or edit a payment processor.');
55 CRM_Utils_System
::setTitle(ts('Settings - Payment Processor'));
57 // get the payment processor meta information
60 $this->_ppType
= CRM_Utils_Request
::retrieve('pp', 'String', $this, FALSE, NULL);
61 if (!$this->_ppType
) {
62 $this->_ppType
= CRM_Core_DAO
::getFieldValue( 'CRM_Financial_DAO_PaymentProcessor',
64 'payment_processor_type_id'
67 $this->set('pp', $this->_ppType
);
70 $this->_ppType
= CRM_Utils_Request
::retrieve('pp', 'String', $this, TRUE, NULL);
73 $this->assign('ppType', $this->_ppType
);
74 $ppTypeName = CRM_Core_DAO
::getFieldValue( 'CRM_Financial_DAO_PaymentProcessorType',
78 $this->assign('ppTypeName', $ppTypeName );
80 $this->_ppDAO
= new CRM_Financial_DAO_PaymentProcessorType( );
81 $this->_ppDAO
->id
= $this->_ppType
;
83 if (!$this->_ppDAO
->find(TRUE)) {
84 CRM_Core_Error
::fatal(ts('Could not find payment processor meta information'));
88 $refreshURL = CRM_Utils_System
::url('civicrm/admin/paymentProcessor',
89 "reset=1&action=update&id={$this->_id}",
94 $refreshURL = CRM_Utils_System
::url('civicrm/admin/paymentProcessor',
101 $destination = CRM_Utils_Request
::retrieve('civicrmDestination', 'String', $this);
103 $destination = urlencode($destination);
104 $refreshURL .= "&civicrmDestination=$destination";
107 $this->assign('refreshURL', $refreshURL);
109 $this->assign('is_recur', $this->_ppDAO
->is_recur
);
111 $this->_fields
= array(
113 'name' => 'user_name',
114 'label' => $this->_ppDAO
->user_name_label
,
117 'name' => 'password',
118 'label' => $this->_ppDAO
->password_label
,
121 'name' => 'signature',
122 'label' => $this->_ppDAO
->signature_label
,
126 'label' => $this->_ppDAO
->subject_label
,
129 'name' => 'url_site',
130 'label' => ts('Site URL'),
132 'msg' => ts('Enter a valid URL'),
136 if ($this->_ppDAO
->is_recur
) {
137 $this->_fields
[] = array(
138 'name' => 'url_recur',
139 'label' => ts('Recurring Payments URL'),
141 'msg' => ts('Enter a valid URL'),
145 if (!empty($this->_ppDAO
->url_button_default
)) {
146 $this->_fields
[] = array(
147 'name' => 'url_button',
148 'label' => ts('Button URL'),
150 'msg' => ts('Enter a valid URL'),
154 if (!empty($this->_ppDAO
->url_api_default
)) {
155 $this->_fields
[] = array(
157 'label' => ts('API URL'),
159 'msg' => ts('Enter a valid URL'),
165 * Build the form object
171 public function buildQuickForm($check = FALSE) {
172 parent
::buildQuickForm();
174 if ($this->_action
& CRM_Core_Action
::DELETE
) {
178 $attributes = CRM_Core_DAO
::getAttribute('CRM_Financial_DAO_PaymentProcessor');
180 $this->add('text', 'name', ts('Name'),
181 $attributes['name'], TRUE
184 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array('CRM_Financial_DAO_PaymentProcessor', $this->_id
));
186 $this->add('text', 'description', ts('Description'),
187 $attributes['description']
190 $types = CRM_Core_PseudoConstant
::paymentProcessorType();
191 $this->add( 'select', 'payment_processor_type_id', ts('Payment Processor Type'), $types, true,
192 array('onchange' => "reload(true)")
195 // Financial Account of account type asset CRM-11515
196 $accountType = CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' ");
197 $financialAccount = CRM_Contribute_PseudoConstant
::financialAccount(NULL, key($accountType));
198 if ($fcount = count($financialAccount)) {
199 $this->assign('financialAccount', $fcount);
201 $this->add('select', 'financial_account_id', ts('Financial Account'),
202 array('' => ts('- select -')) +
$financialAccount,
205 // is this processor active ?
206 $this->add('checkbox', 'is_active', ts('Is this Payment Processor active?'));
207 $this->add('checkbox', 'is_default', ts('Is this Payment Processor the default?'));
210 foreach ($this->_fields
as $field) {
211 if (empty($field['label'])) {
215 $this->add('text', $field['name'],
216 $field['label'], $attributes[$field['name']]
218 $this->add('text', "test_{$field['name']}",
219 $field['label'], $attributes[$field['name']]
221 if (!empty($field['rule'])) {
222 $this->addRule($field['name'], $field['msg'], $field['rule']);
223 $this->addRule("test_{$field['name']}", $field['msg'], $field['rule']);
227 $this->addFormRule(array('CRM_Admin_Form_PaymentProcessor', 'formRule'));
235 public static function formRule($fields) {
237 // make sure that at least one of live or test is present
238 // and we have at least name and url_site
239 // would be good to make this processor specific
242 if (!(self
::checkSection($fields, $errors) ||
243 self
::checkSection($fields, $errors, 'test')
245 $errors['_qf_default'] = ts('You must have at least the test or live section filled');
248 if (!empty($errors)) {
252 return empty($errors) ?
TRUE : $errors;
258 * @param null $section
262 public static function checkSection(&$fields, &$errors, $section = NULL) {
263 $names = array('user_name');
267 foreach ($names as $name) {
269 $name = "{$section}_$name";
271 if (!empty($fields[$name])) {
281 $errors['_qf_default'] = ts('You must have at least the user_name specified');
290 public function setDefaultValues() {
292 if ($this->_ppType
) {
293 $defaults['payment_processor_type_id'] = $this->_ppType
;
296 $defaults['is_active'] = $defaults['is_default'] = 1;
297 $defaults['url_site'] = $this->_ppDAO
->url_site_default
;
298 $defaults['url_api'] = $this->_ppDAO
->url_api_default
;
299 $defaults['url_recur'] = $this->_ppDAO
->url_recur_default
;
300 $defaults['url_button'] = $this->_ppDAO
->url_button_default
;
301 $defaults['test_url_site'] = $this->_ppDAO
->url_site_test_default
;
302 $defaults['test_url_api'] = $this->_ppDAO
->url_api_test_default
;
303 $defaults['test_url_recur'] = $this->_ppDAO
->url_recur_test_default
;
304 $defaults['test_url_button'] = $this->_ppDAO
->url_button_test_default
;
307 $domainID = CRM_Core_Config
::domainID();
309 $dao = new CRM_Financial_DAO_PaymentProcessor( );
310 $dao->id
= $this->_id
;
311 $dao->domain_id
= $domainID;
312 if (!$dao->find(TRUE)) {
316 CRM_Core_DAO
::storeValues($dao, $defaults);
319 $testDAO = new CRM_Financial_DAO_PaymentProcessor();
320 $testDAO->name
= $dao->name
;
321 $testDAO->is_test
= 1;
322 $testDAO->domain_id
= $domainID;
323 if ($testDAO->find(TRUE)) {
324 $this->_testID
= $testDAO->id
;
326 foreach ($this->_fields
as $field) {
327 $testName = "test_{$field['name']}";
328 $defaults[$testName] = $testDAO->{$field['name']};
331 $defaults['financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getFinancialAccount($dao->id
, 'civicrm_payment_processor', 'financial_account_id');
337 * Process the form submission
342 public function postProcess() {
343 CRM_Utils_System
::flushCache( 'CRM_Financial_DAO_PaymentProcessor' );
345 if ($this->_action
& CRM_Core_Action
::DELETE
) {
346 CRM_Financial_BAO_PaymentProcessor
::del($this->_id
);
347 CRM_Core_Session
::setStatus("", ts('Payment Processor Deleted.'), "success");
351 $values = $this->controller
->exportValues($this->_name
);
352 $domainID = CRM_Core_Config
::domainID();
354 if (!empty($values['is_default'])) {
355 $query = "UPDATE civicrm_payment_processor SET is_default = 0 WHERE domain_id = $domainID";
356 CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
359 $this->updatePaymentProcessor($values, $domainID, FALSE);
360 $this->updatePaymentProcessor($values, $domainID, TRUE);
361 CRM_Core_Session
::setStatus(ts('Payment processor %1 has been saved.', array(1 => "<em>{$values['name']}</em>")), ts('Saved'), 'success');
365 * Save a payment processor
368 * @param int $domainID
373 public function updatePaymentProcessor(&$values, $domainID, $test) {
374 $dao = new CRM_Financial_DAO_PaymentProcessor( );
376 $dao->id
= $test ?
$this->_testID
: $this->_id
;
377 $dao->domain_id
= $domainID;
378 $dao->is_test
= $test;
379 $dao->is_default
= CRM_Utils_Array
::value('is_default', $values, 0);
381 $dao->is_active
= CRM_Utils_Array
::value('is_active', $values, 0);
383 $dao->name
= $values['name'];
384 $dao->description
= $values['description'];
385 $dao->payment_processor_type_id
= $values['payment_processor_type_id'];
387 foreach ($this->_fields
as $field) {
388 $fieldName = $test ?
"test_{$field['name']}" : $field['name'];
389 $dao->{$field['name']} = trim(CRM_Utils_Array
::value($fieldName, $values));
390 if (empty($dao->{$field['name']})) {
391 $dao->{$field['name']} = 'null';
395 // also copy meta fields from the info DAO
396 $dao->is_recur
= $this->_ppDAO
->is_recur
;
397 $dao->billing_mode
= $this->_ppDAO
->billing_mode
;
398 $dao->class_name
= $this->_ppDAO
->class_name
;
399 $dao->payment_type
= $this->_ppDAO
->payment_type
;
405 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
407 'entity_table' => 'civicrm_payment_processor',
408 'entity_id' => $dao->id
,
409 'account_relationship' => $relationTypeId,
410 'financial_account_id' => $values['financial_account_id']
412 CRM_Financial_BAO_FinancialTypeAccount
::add($params);