3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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;
48 * Payment processor Type ID
50 protected $_paymentProcessorType;
53 * Get the name of the base entity being edited.
57 public function getDefaultEntity() {
58 return 'PaymentProcessor';
61 public function preProcess() {
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',
69 'payment_processor_type_id'
72 $this->set('pp', $this->_paymentProcessorType
);
75 $this->_paymentProcessorType
= CRM_Utils_Request
::retrieve('pp', 'String', $this, TRUE, NULL);
78 $this->assign('ppType', $this->_paymentProcessorType
);
79 $ppTypeName = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_PaymentProcessorType',
80 $this->_paymentProcessorType
,
83 $this->assign('ppTypeName', $ppTypeName);
85 $this->_paymentProcessorDAO
= new CRM_Financial_DAO_PaymentProcessorType();
86 $this->_paymentProcessorDAO
->id
= $this->_paymentProcessorType
;
88 $this->_paymentProcessorDAO
->find(TRUE);
91 $refreshURL = CRM_Utils_System
::url('civicrm/admin/paymentProcessor',
92 "reset=1&action=update&id={$this->_id}",
97 $refreshURL = CRM_Utils_System
::url('civicrm/admin/paymentProcessor',
104 $destination = CRM_Utils_Request
::retrieve('civicrmDestination', 'String', $this);
106 $destination = urlencode($destination);
107 $refreshURL .= "&civicrmDestination=$destination";
110 $this->refreshURL
= $refreshURL;
111 $this->assign('refreshURL', $refreshURL);
113 $this->assign('is_recur', $this->_paymentProcessorDAO
->is_recur
);
117 'name' => 'user_name',
118 'label' => $this->_paymentProcessorDAO
->user_name_label
,
121 'name' => 'password',
122 'label' => $this->_paymentProcessorDAO
->password_label
,
125 'name' => 'signature',
126 'label' => $this->_paymentProcessorDAO
->signature_label
,
130 'label' => $this->_paymentProcessorDAO
->subject_label
,
133 'name' => 'url_site',
134 'label' => ts('Site URL'),
136 'msg' => ts('Enter a valid URL'),
140 if ($this->_paymentProcessorDAO
->is_recur
) {
142 'name' => 'url_recur',
143 'label' => ts('Recurring Payments URL'),
145 'msg' => ts('Enter a valid URL'),
149 if (!empty($this->_paymentProcessorDAO
->url_button_default
)) {
151 'name' => 'url_button',
152 'label' => ts('Button URL'),
154 'msg' => ts('Enter a valid URL'),
158 if (!empty($this->_paymentProcessorDAO
->url_api_default
)) {
161 'label' => ts('API URL'),
163 'msg' => ts('Enter a valid URL'),
169 * Build the form object.
173 public function buildQuickForm($check = FALSE) {
174 parent
::buildQuickForm();
176 if ($this->_action
& CRM_Core_Action
::DELETE
) {
180 $attributes = CRM_Core_DAO
::getAttribute('CRM_Financial_DAO_PaymentProcessor');
182 $this->add('text', 'name', ts('Name'),
183 $attributes['name'], TRUE
186 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', [
187 'CRM_Financial_DAO_PaymentProcessor',
190 CRM_Core_Config
::domainID(),
193 $this->add('text', 'description', ts('Description'),
194 $attributes['description']
198 'payment_processor_type_id',
199 ts('Payment Processor Type'),
200 CRM_Financial_BAO_PaymentProcessor
::buildOptions('payment_processor_type_id'),
202 ['onchange' => "reload(true)"]
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);
211 $this->add('select', 'financial_account_id', ts('Financial Account'),
212 ['' => ts('- select -')] +
$financialAccount,
215 $this->addSelect('payment_instrument_id',
217 'entity' => 'contribution',
218 'label' => ts('Payment Method'),
219 'placeholder' => NULL,
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, ' ');
229 foreach ($this->_fields
as $field) {
230 if (empty($field['label'])) {
234 $this->addField($field['name'], ['label' => $field['label']]);
236 $fieldSpec = civicrm_api3($this->getDefaultEntity(), 'getfield', [
237 'name' => $field['name'],
238 'action' => 'create',
240 $this->add($fieldSpec['values']['html']['type'], "test_{$field['name']}",
241 $field['label'], $attributes[$field['name']]
243 if (!empty($field['rule'])) {
244 $this->addRule($field['name'], $field['msg'], $field['rule']);
245 $this->addRule("test_{$field['name']}", $field['msg'], $field['rule']);
249 $this->addFormRule(['CRM_Admin_Form_PaymentProcessor', 'formRule']);
257 public static function formRule($fields) {
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
264 if (!(self
::checkSection($fields, $errors) ||
265 self
::checkSection($fields, $errors, 'test')
268 $errors['_qf_default'] = ts('You must have at least the test or live section filled');
271 if (!empty($errors)) {
275 return empty($errors) ?
TRUE : $errors;
281 * @param null $section
285 public static function checkSection(&$fields, &$errors, $section = NULL) {
286 $names = ['user_name'];
290 foreach ($names as $name) {
292 $name = "{$section}_$name";
294 if (!empty($fields[$name])) {
304 $errors['_qf_default'] = ts('You must have at least the user_name specified');
313 public function setDefaultValues() {
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
;
331 $defaults['financial_account_id'] = CRM_Financial_BAO_PaymentProcessor
::getDefaultFinancialAccountID();
334 $domainID = CRM_Core_Config
::domainID();
336 $dao = new CRM_Financial_DAO_PaymentProcessor();
337 $dao->id
= $this->_id
;
338 $dao->domain_id
= $domainID;
339 if (!$dao->find(TRUE)) {
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
;
351 CRM_Core_Session
::setStatus('Payment Processor Type (ID=' . $this->_paymentProcessorType
. ') not found. Did you disable the payment processor extension?', 'Missing Payment Processor', 'alert');
354 $cards = json_decode(CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_PaymentProcessor',
356 'accepted_credit_cards'
359 if (!empty($cards)) {
360 foreach ($cards as $card => $val) {
361 $acceptedCards[$card] = 1;
364 $defaults['accept_credit_cards'] = $acceptedCards;
365 unset($defaults['accepted_credit_cards']);
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
;
374 foreach ($this->_fields
as $field) {
375 $testName = "test_{$field['name']}";
376 $defaults[$testName] = $testDAO->{$field['name']};
379 $defaults['financial_account_id'] = CRM_Contribute_PseudoConstant
::getRelationalFinancialAccount($dao->id
, NULL, 'civicrm_payment_processor');
385 * Process the form submission.
387 public function postProcess() {
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");
395 $values = $this->controller
->exportValues($this->_name
);
396 $domainID = CRM_Core_Config
::domainID();
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);
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);
410 $this->updatePaymentProcessor($values, $domainID, FALSE);
411 $this->updatePaymentProcessor($values, $domainID, TRUE);
413 $processor = civicrm_api3('payment_processor', 'getsingle', ['name' => $values['name'], 'is_test' => 0]);
414 $errors = Civi\Payment\System
::singleton()->checkProcessorConfig($processor);
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
);
421 CRM_Core_Session
::setStatus(ts('Payment processor %1 has been saved.', [1 => "<em>{$values['name']}</em>"]), ts('Saved'), 'success');
426 * Save a payment processor.
428 * @param array $values
429 * @param int $domainID
432 public function updatePaymentProcessor(&$values, $domainID, $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}"];
438 if (!empty($values['accept_credit_cards'])) {
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];
447 $creditCards = json_encode($creditCards);
450 $creditCards = "NULL";
452 $params = array_merge([
453 'id' => $test ?
$this->_testID
: $this->_id
,
454 'domain_id' => $domainID,
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,
467 civicrm_api3('PaymentProcessor', 'create', $params);