3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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;
44 protected $_paymentProcessorDAO;
47 * @var int $_paymentProcessorType Payment processor Type ID
49 protected $_paymentProcessorType;
52 * Get the name of the base entity being edited.
56 public function getDefaultEntity() {
57 return 'PaymentProcessor';
60 public function preProcess() {
64 $this->_paymentProcessorType
= CRM_Utils_Request
::retrieve('pp', 'String', $this, FALSE, NULL);
65 if (!$this->_paymentProcessorType
) {
66 $this->_paymentProcessorType
= CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_PaymentProcessor',
68 'payment_processor_type_id'
71 $this->set('pp', $this->_paymentProcessorType
);
74 $this->_paymentProcessorType
= CRM_Utils_Request
::retrieve('pp', 'String', $this, TRUE, NULL);
77 $this->assign('ppType', $this->_paymentProcessorType
);
78 $ppTypeName = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_PaymentProcessorType',
79 $this->_paymentProcessorType
,
82 $this->assign('ppTypeName', $ppTypeName);
84 $this->_paymentProcessorDAO
= new CRM_Financial_DAO_PaymentProcessorType();
85 $this->_paymentProcessorDAO
->id
= $this->_paymentProcessorType
;
87 $this->_paymentProcessorDAO
->find(TRUE);
90 $refreshURL = CRM_Utils_System
::url('civicrm/admin/paymentProcessor',
91 "reset=1&action=update&id={$this->_id}",
96 $refreshURL = CRM_Utils_System
::url('civicrm/admin/paymentProcessor',
103 $destination = CRM_Utils_Request
::retrieve('civicrmDestination', 'String', $this);
105 $destination = urlencode($destination);
106 $refreshURL .= "&civicrmDestination=$destination";
109 $this->refreshURL
= $refreshURL;
110 $this->assign('refreshURL', $refreshURL);
112 $this->assign('is_recur', $this->_paymentProcessorDAO
->is_recur
);
114 $this->_fields
= array(
116 'name' => 'user_name',
117 'label' => $this->_paymentProcessorDAO
->user_name_label
,
120 'name' => 'password',
121 'label' => $this->_paymentProcessorDAO
->password_label
,
124 'name' => 'signature',
125 'label' => $this->_paymentProcessorDAO
->signature_label
,
129 'label' => $this->_paymentProcessorDAO
->subject_label
,
132 'name' => 'url_site',
133 'label' => ts('Site URL'),
135 'msg' => ts('Enter a valid URL'),
139 if ($this->_paymentProcessorDAO
->is_recur
) {
140 $this->_fields
[] = array(
141 'name' => 'url_recur',
142 'label' => ts('Recurring Payments URL'),
144 'msg' => ts('Enter a valid URL'),
148 if (!empty($this->_paymentProcessorDAO
->url_button_default
)) {
149 $this->_fields
[] = array(
150 'name' => 'url_button',
151 'label' => ts('Button URL'),
153 'msg' => ts('Enter a valid URL'),
157 if (!empty($this->_paymentProcessorDAO
->url_api_default
)) {
158 $this->_fields
[] = array(
160 'label' => ts('API URL'),
162 'msg' => ts('Enter a valid URL'),
168 * Build the form object.
172 public function buildQuickForm($check = FALSE) {
173 parent
::buildQuickForm();
175 if ($this->_action
& CRM_Core_Action
::DELETE
) {
179 $attributes = CRM_Core_DAO
::getAttribute('CRM_Financial_DAO_PaymentProcessor');
181 $this->add('text', 'name', ts('Name'),
182 $attributes['name'], TRUE
185 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array(
186 'CRM_Financial_DAO_PaymentProcessor',
189 CRM_Core_Config
::domainID(),
192 $this->add('text', 'description', ts('Description'),
193 $attributes['description']
197 'payment_processor_type_id',
198 ts('Payment Processor Type'),
199 CRM_Financial_BAO_PaymentProcessor
::buildOptions('payment_processor_type_id'),
201 array('onchange' => "reload(true)")
204 // Financial Account of account type asset CRM-11515
205 $accountType = CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type', NULL, " AND v.name = 'Asset' ");
206 $financialAccount = CRM_Contribute_PseudoConstant
::financialAccount(NULL, key($accountType));
207 if ($fcount = count($financialAccount)) {
208 $this->assign('financialAccount', $fcount);
210 $this->add('select', 'financial_account_id', ts('Financial Account'),
211 array('' => ts('- select -')) +
$financialAccount,
214 $this->addSelect('payment_instrument_id',
216 'entity' => 'contribution',
217 'label' => ts('Payment Method'),
218 'placeholder' => NULL,
222 // is this processor active ?
223 $this->add('checkbox', 'is_active', ts('Is this Payment Processor active?'));
224 $this->add('checkbox', 'is_default', ts('Is this Payment Processor the default?'));
225 $creditCardTypes = CRM_Contribute_PseudoConstant
::creditCard();
226 $this->addCheckBox('accept_credit_cards', ts('Accepted Credit Card Type(s)'),
227 $creditCardTypes, NULL, NULL, NULL, NULL, ' ');
228 foreach ($this->_fields
as $field) {
229 if (empty($field['label'])) {
233 $this->addField($field['name'], array('label' => $field['label']));
235 $fieldSpec = civicrm_api3($this->getDefaultEntity(), 'getfield', array(
236 'name' => $field['name'],
237 'action' => 'create',
239 $this->add($fieldSpec['values']['html']['type'], "test_{$field['name']}",
240 $field['label'], $attributes[$field['name']]
242 if (!empty($field['rule'])) {
243 $this->addRule($field['name'], $field['msg'], $field['rule']);
244 $this->addRule("test_{$field['name']}", $field['msg'], $field['rule']);
248 $this->addFormRule(array('CRM_Admin_Form_PaymentProcessor', 'formRule'));
256 public static function formRule($fields) {
258 // make sure that at least one of live or test is present
259 // and we have at least name and url_site
260 // would be good to make this processor specific
263 if (!(self
::checkSection($fields, $errors) ||
264 self
::checkSection($fields, $errors, 'test')
267 $errors['_qf_default'] = ts('You must have at least the test or live section filled');
270 if (!empty($errors)) {
274 return empty($errors) ?
TRUE : $errors;
280 * @param null $section
284 public static function checkSection(&$fields, &$errors, $section = NULL) {
285 $names = array('user_name');
289 foreach ($names as $name) {
291 $name = "{$section}_$name";
293 if (!empty($fields[$name])) {
303 $errors['_qf_default'] = ts('You must have at least the user_name specified');
312 public function setDefaultValues() {
316 $defaults['is_active'] = $defaults['is_default'] = 1;
317 $defaults['url_site'] = $this->_paymentProcessorDAO
->url_site_default
;
318 $defaults['url_api'] = $this->_paymentProcessorDAO
->url_api_default
;
319 $defaults['url_recur'] = $this->_paymentProcessorDAO
->url_recur_default
;
320 $defaults['url_button'] = $this->_paymentProcessorDAO
->url_button_default
;
321 $defaults['test_url_site'] = $this->_paymentProcessorDAO
->url_site_test_default
;
322 $defaults['test_url_api'] = $this->_paymentProcessorDAO
->url_api_test_default
;
323 $defaults['test_url_recur'] = $this->_paymentProcessorDAO
->url_recur_test_default
;
324 $defaults['test_url_button'] = $this->_paymentProcessorDAO
->url_button_test_default
;
325 $defaults['payment_instrument_id'] = $this->_paymentProcessorDAO
->payment_instrument_id
;
326 // When user changes payment processor type, it is passed in via $this->_ppType so update defaults array.
327 if ($this->_paymentProcessorType
) {
328 $defaults['payment_processor_type_id'] = $this->_paymentProcessorType
;
332 $domainID = CRM_Core_Config
::domainID();
334 $dao = new CRM_Financial_DAO_PaymentProcessor();
335 $dao->id
= $this->_id
;
336 $dao->domain_id
= $domainID;
337 if (!$dao->find(TRUE)) {
341 CRM_Core_DAO
::storeValues($dao, $defaults);
342 // If payment processor ID does not exist, $paymentProcessorName will be FALSE
343 $paymentProcessorName = CRM_Core_PseudoConstant
::getName('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', $this->_paymentProcessorType
);
344 if ($this->_paymentProcessorType
&& $paymentProcessorName) {
345 // When user changes payment processor type, it is passed in via $this->_ppType so update defaults array.
346 $defaults['payment_processor_type_id'] = $this->_paymentProcessorType
;
349 CRM_Core_Session
::setStatus('Payment Processor Type (ID=' . $this->_paymentProcessorType
. ') not found. Did you disable the payment processor extension?', 'Missing Payment Processor', 'alert');
352 $cards = json_decode(CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_PaymentProcessor',
354 'accepted_credit_cards'
356 $acceptedCards = array();
357 if (!empty($cards)) {
358 foreach ($cards as $card => $val) {
359 $acceptedCards[$card] = 1;
362 $defaults['accept_credit_cards'] = $acceptedCards;
363 unset($defaults['accepted_credit_cards']);
365 $testDAO = new CRM_Financial_DAO_PaymentProcessor();
366 $testDAO->name
= $dao->name
;
367 $testDAO->is_test
= 1;
368 $testDAO->domain_id
= $domainID;
369 if ($testDAO->find(TRUE)) {
370 $this->_testID
= $testDAO->id
;
372 foreach ($this->_fields
as $field) {
373 $testName = "test_{$field['name']}";
374 $defaults[$testName] = $testDAO->{$field['name']};
377 $defaults['financial_account_id'] = CRM_Contribute_PseudoConstant
::getRelationalFinancialAccount($dao->id
, NULL, 'civicrm_payment_processor');
383 * Process the form submission.
385 public function postProcess() {
387 if ($this->_action
& CRM_Core_Action
::DELETE
) {
388 CRM_Financial_BAO_PaymentProcessor
::del($this->_id
);
389 CRM_Core_Session
::setStatus("", ts('Payment Processor Deleted.'), "success");
393 $values = $this->controller
->exportValues($this->_name
);
394 $domainID = CRM_Core_Config
::domainID();
396 if (!empty($values['is_default'])) {
397 $query = "UPDATE civicrm_payment_processor SET is_default = 0 WHERE domain_id = $domainID";
398 CRM_Core_DAO
::executeQuery($query);
401 if ($this->_paymentProcessorType
!== $values['payment_processor_type_id']) {
402 // If we changed the payment processor type, need to update the object as well
403 $this->_paymentProcessorType
= $values['payment_processor_type_id'];
404 $this->_paymentProcessorDAO
= new CRM_Financial_DAO_PaymentProcessorType();
405 $this->_paymentProcessorDAO
->id
= $values['payment_processor_type_id'];
406 $this->_paymentProcessorDAO
->find(TRUE);
408 $this->updatePaymentProcessor($values, $domainID, FALSE);
409 $this->updatePaymentProcessor($values, $domainID, TRUE);
411 $processor = civicrm_api3('payment_processor', 'getsingle', array('name' => $values['name'], 'is_test' => 0));
412 $errors = Civi\Payment\System
::singleton()->checkProcessorConfig($processor);
414 CRM_Core_Session
::setStatus($errors, 'Payment processor configuration invalid', 'error');
415 Civi
::log()->error('Payment processor configuration invalid: ' . $errors);
416 CRM_Core_Session
::singleton()->pushUserContext($this->refreshURL
);
419 CRM_Core_Session
::setStatus(ts('Payment processor %1 has been saved.', array(1 => "<em>{$values['name']}</em>")), ts('Saved'), 'success');
424 * Save a payment processor.
426 * @param array $values
427 * @param int $domainID
430 public function updatePaymentProcessor(&$values, $domainID, $test) {
432 foreach (array('user_name', 'password', 'signature', 'url_site', 'url_recur', 'url_api', 'url_button', 'subject') as $field) {
433 $values[$field] = empty($values["test_{$field}"]) ? CRM_Utils_Array
::value($field, $values) : $values["test_{$field}"];
436 if (!empty($values['accept_credit_cards'])) {
437 $creditCards = array();
438 $accptedCards = array_keys($values['accept_credit_cards']);
439 $creditCardTypes = CRM_Contribute_PseudoConstant
::creditCard();
440 foreach ($creditCardTypes as $type => $val) {
441 if (in_array($type, $accptedCards)) {
442 $creditCards[$type] = $creditCardTypes[$type];
445 $creditCards = json_encode($creditCards);
448 $creditCards = "NULL";
450 $params = array_merge(array(
451 'id' => $test ?
$this->_testID
: $this->_id
,
452 'domain_id' => $domainID,
456 'is_recur' => $this->_paymentProcessorDAO
->is_recur
,
457 'billing_mode' => $this->_paymentProcessorDAO
->billing_mode
,
458 'class_name' => $this->_paymentProcessorDAO
->class_name
,
459 'payment_type' => $this->_paymentProcessorDAO
->payment_type
,
460 'payment_instrument_id' => $this->_paymentProcessorDAO
->payment_instrument_id
,
461 'financial_account_id' => $values['financial_account_id'],
462 'accepted_credit_cards' => $creditCards,
465 civicrm_api3('PaymentProcessor', 'create', $params);