| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 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-2018 |
| 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 $_paymentProcessorDAO; |
| 45 | |
| 46 | /** |
| 47 | * @var int $_paymentProcessorType Payment processor Type ID |
| 48 | */ |
| 49 | protected $_paymentProcessorType; |
| 50 | |
| 51 | /** |
| 52 | * Get the name of the base entity being edited. |
| 53 | * |
| 54 | * @return string |
| 55 | */ |
| 56 | public function getDefaultEntity() { |
| 57 | return 'PaymentProcessor'; |
| 58 | } |
| 59 | |
| 60 | public function preProcess() { |
| 61 | parent::preProcess(); |
| 62 | |
| 63 | if ($this->_id) { |
| 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', |
| 67 | $this->_id, |
| 68 | 'payment_processor_type_id' |
| 69 | ); |
| 70 | } |
| 71 | $this->set('pp', $this->_paymentProcessorType); |
| 72 | } |
| 73 | else { |
| 74 | $this->_paymentProcessorType = CRM_Utils_Request::retrieve('pp', 'String', $this, TRUE, NULL); |
| 75 | } |
| 76 | |
| 77 | $this->assign('ppType', $this->_paymentProcessorType); |
| 78 | $ppTypeName = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', |
| 79 | $this->_paymentProcessorType, |
| 80 | 'name' |
| 81 | ); |
| 82 | $this->assign('ppTypeName', $ppTypeName); |
| 83 | |
| 84 | $this->_paymentProcessorDAO = new CRM_Financial_DAO_PaymentProcessorType(); |
| 85 | $this->_paymentProcessorDAO->id = $this->_paymentProcessorType; |
| 86 | |
| 87 | $this->_paymentProcessorDAO->find(TRUE); |
| 88 | |
| 89 | if ($this->_id) { |
| 90 | $refreshURL = CRM_Utils_System::url('civicrm/admin/paymentProcessor', |
| 91 | "reset=1&action=update&id={$this->_id}", |
| 92 | FALSE, NULL, FALSE |
| 93 | ); |
| 94 | } |
| 95 | else { |
| 96 | $refreshURL = CRM_Utils_System::url('civicrm/admin/paymentProcessor', |
| 97 | "reset=1&action=add", |
| 98 | FALSE, NULL, FALSE |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | //CRM-4129 |
| 103 | $destination = CRM_Utils_Request::retrieve('civicrmDestination', 'String', $this); |
| 104 | if ($destination) { |
| 105 | $destination = urlencode($destination); |
| 106 | $refreshURL .= "&civicrmDestination=$destination"; |
| 107 | } |
| 108 | |
| 109 | $this->refreshURL = $refreshURL; |
| 110 | $this->assign('refreshURL', $refreshURL); |
| 111 | |
| 112 | $this->assign('is_recur', $this->_paymentProcessorDAO->is_recur); |
| 113 | |
| 114 | $this->_fields = array( |
| 115 | array( |
| 116 | 'name' => 'user_name', |
| 117 | 'label' => $this->_paymentProcessorDAO->user_name_label, |
| 118 | ), |
| 119 | array( |
| 120 | 'name' => 'password', |
| 121 | 'label' => $this->_paymentProcessorDAO->password_label, |
| 122 | ), |
| 123 | array( |
| 124 | 'name' => 'signature', |
| 125 | 'label' => $this->_paymentProcessorDAO->signature_label, |
| 126 | ), |
| 127 | array( |
| 128 | 'name' => 'subject', |
| 129 | 'label' => $this->_paymentProcessorDAO->subject_label, |
| 130 | ), |
| 131 | array( |
| 132 | 'name' => 'url_site', |
| 133 | 'label' => ts('Site URL'), |
| 134 | 'rule' => 'url', |
| 135 | 'msg' => ts('Enter a valid URL'), |
| 136 | ), |
| 137 | ); |
| 138 | |
| 139 | if ($this->_paymentProcessorDAO->is_recur) { |
| 140 | $this->_fields[] = array( |
| 141 | 'name' => 'url_recur', |
| 142 | 'label' => ts('Recurring Payments URL'), |
| 143 | 'rule' => 'url', |
| 144 | 'msg' => ts('Enter a valid URL'), |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | if (!empty($this->_paymentProcessorDAO->url_button_default)) { |
| 149 | $this->_fields[] = array( |
| 150 | 'name' => 'url_button', |
| 151 | 'label' => ts('Button URL'), |
| 152 | 'rule' => 'url', |
| 153 | 'msg' => ts('Enter a valid URL'), |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | if (!empty($this->_paymentProcessorDAO->url_api_default)) { |
| 158 | $this->_fields[] = array( |
| 159 | 'name' => 'url_api', |
| 160 | 'label' => ts('API URL'), |
| 161 | 'rule' => 'url', |
| 162 | 'msg' => ts('Enter a valid URL'), |
| 163 | ); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Build the form object. |
| 169 | * |
| 170 | * @param bool $check |
| 171 | */ |
| 172 | public function buildQuickForm($check = FALSE) { |
| 173 | parent::buildQuickForm(); |
| 174 | |
| 175 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_PaymentProcessor'); |
| 180 | |
| 181 | $this->add('text', 'name', ts('Name'), |
| 182 | $attributes['name'], TRUE |
| 183 | ); |
| 184 | |
| 185 | $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array( |
| 186 | 'CRM_Financial_DAO_PaymentProcessor', |
| 187 | $this->_id, |
| 188 | 'name', |
| 189 | CRM_Core_Config::domainID(), |
| 190 | )); |
| 191 | |
| 192 | $this->add('text', 'description', ts('Description'), |
| 193 | $attributes['description'] |
| 194 | ); |
| 195 | |
| 196 | $this->add('select', |
| 197 | 'payment_processor_type_id', |
| 198 | ts('Payment Processor Type'), |
| 199 | CRM_Financial_BAO_PaymentProcessor::buildOptions('payment_processor_type_id'), |
| 200 | TRUE, |
| 201 | array('onchange' => "reload(true)") |
| 202 | ); |
| 203 | |
| 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); |
| 209 | } |
| 210 | $this->add('select', 'financial_account_id', ts('Financial Account'), |
| 211 | array('' => ts('- select -')) + $financialAccount, |
| 212 | TRUE |
| 213 | ); |
| 214 | $this->addSelect('payment_instrument_id', |
| 215 | array( |
| 216 | 'entity' => 'contribution', |
| 217 | 'label' => ts('Payment Method'), |
| 218 | 'placeholder' => NULL, |
| 219 | ) |
| 220 | ); |
| 221 | |
| 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'])) { |
| 230 | continue; |
| 231 | } |
| 232 | |
| 233 | $this->addField($field['name'], array('label' => $field['label'])); |
| 234 | |
| 235 | $fieldSpec = civicrm_api3($this->getDefaultEntity(), 'getfield', array( |
| 236 | 'name' => $field['name'], |
| 237 | 'action' => 'create', |
| 238 | )); |
| 239 | $this->add($fieldSpec['values']['html']['type'], "test_{$field['name']}", |
| 240 | $field['label'], $attributes[$field['name']] |
| 241 | ); |
| 242 | if (!empty($field['rule'])) { |
| 243 | $this->addRule($field['name'], $field['msg'], $field['rule']); |
| 244 | $this->addRule("test_{$field['name']}", $field['msg'], $field['rule']); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | $this->addFormRule(array('CRM_Admin_Form_PaymentProcessor', 'formRule')); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * @param $fields |
| 253 | * |
| 254 | * @return array|bool |
| 255 | */ |
| 256 | public static function formRule($fields) { |
| 257 | |
| 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 |
| 261 | $errors = array(); |
| 262 | |
| 263 | if (!(self::checkSection($fields, $errors) || |
| 264 | self::checkSection($fields, $errors, 'test') |
| 265 | ) |
| 266 | ) { |
| 267 | $errors['_qf_default'] = ts('You must have at least the test or live section filled'); |
| 268 | } |
| 269 | |
| 270 | if (!empty($errors)) { |
| 271 | return $errors; |
| 272 | } |
| 273 | |
| 274 | return empty($errors) ? TRUE : $errors; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * @param $fields |
| 279 | * @param $errors |
| 280 | * @param null $section |
| 281 | * |
| 282 | * @return bool |
| 283 | */ |
| 284 | public static function checkSection(&$fields, &$errors, $section = NULL) { |
| 285 | $names = array('user_name'); |
| 286 | |
| 287 | $present = FALSE; |
| 288 | $allPresent = TRUE; |
| 289 | foreach ($names as $name) { |
| 290 | if ($section) { |
| 291 | $name = "{$section}_$name"; |
| 292 | } |
| 293 | if (!empty($fields[$name])) { |
| 294 | $present = TRUE; |
| 295 | } |
| 296 | else { |
| 297 | $allPresent = FALSE; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | if ($present) { |
| 302 | if (!$allPresent) { |
| 303 | $errors['_qf_default'] = ts('You must have at least the user_name specified'); |
| 304 | } |
| 305 | } |
| 306 | return $present; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * @return array |
| 311 | */ |
| 312 | public function setDefaultValues() { |
| 313 | $defaults = array(); |
| 314 | |
| 315 | if (!$this->_id) { |
| 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; |
| 329 | } |
| 330 | return $defaults; |
| 331 | } |
| 332 | $domainID = CRM_Core_Config::domainID(); |
| 333 | |
| 334 | $dao = new CRM_Financial_DAO_PaymentProcessor(); |
| 335 | $dao->id = $this->_id; |
| 336 | $dao->domain_id = $domainID; |
| 337 | if (!$dao->find(TRUE)) { |
| 338 | return $defaults; |
| 339 | } |
| 340 | |
| 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; |
| 347 | } |
| 348 | else { |
| 349 | CRM_Core_Session::setStatus('Payment Processor Type (ID=' . $this->_paymentProcessorType . ') not found. Did you disable the payment processor extension?', 'Missing Payment Processor', 'alert'); |
| 350 | } |
| 351 | |
| 352 | $cards = json_decode(CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessor', |
| 353 | $this->_id, |
| 354 | 'accepted_credit_cards' |
| 355 | ), TRUE); |
| 356 | $acceptedCards = array(); |
| 357 | if (!empty($cards)) { |
| 358 | foreach ($cards as $card => $val) { |
| 359 | $acceptedCards[$card] = 1; |
| 360 | } |
| 361 | } |
| 362 | $defaults['accept_credit_cards'] = $acceptedCards; |
| 363 | unset($defaults['accepted_credit_cards']); |
| 364 | // now get testID |
| 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; |
| 371 | |
| 372 | foreach ($this->_fields as $field) { |
| 373 | $testName = "test_{$field['name']}"; |
| 374 | $defaults[$testName] = $testDAO->{$field['name']}; |
| 375 | } |
| 376 | } |
| 377 | $defaults['financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($dao->id, NULL, 'civicrm_payment_processor'); |
| 378 | |
| 379 | return $defaults; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Process the form submission. |
| 384 | */ |
| 385 | public function postProcess() { |
| 386 | |
| 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"); |
| 390 | return NULL; |
| 391 | } |
| 392 | |
| 393 | $values = $this->controller->exportValues($this->_name); |
| 394 | $domainID = CRM_Core_Config::domainID(); |
| 395 | |
| 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); |
| 399 | } |
| 400 | |
| 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); |
| 407 | } |
| 408 | $this->updatePaymentProcessor($values, $domainID, FALSE); |
| 409 | $this->updatePaymentProcessor($values, $domainID, TRUE); |
| 410 | |
| 411 | $processor = civicrm_api3('payment_processor', 'getsingle', array('name' => $values['name'], 'is_test' => 0)); |
| 412 | $errors = Civi\Payment\System::singleton()->checkProcessorConfig($processor); |
| 413 | if ($errors) { |
| 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); |
| 417 | } |
| 418 | else { |
| 419 | CRM_Core_Session::setStatus(ts('Payment processor %1 has been saved.', array(1 => "<em>{$values['name']}</em>")), ts('Saved'), 'success'); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Save a payment processor. |
| 425 | * |
| 426 | * @param array $values |
| 427 | * @param int $domainID |
| 428 | * @param bool $test |
| 429 | */ |
| 430 | public function updatePaymentProcessor(&$values, $domainID, $test) { |
| 431 | if ($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}"]; |
| 434 | } |
| 435 | } |
| 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]; |
| 443 | } |
| 444 | } |
| 445 | $creditCards = json_encode($creditCards); |
| 446 | } |
| 447 | else { |
| 448 | $creditCards = "NULL"; |
| 449 | } |
| 450 | $params = array_merge(array( |
| 451 | 'id' => $test ? $this->_testID : $this->_id, |
| 452 | 'domain_id' => $domainID, |
| 453 | 'is_test' => $test, |
| 454 | 'is_active' => 0, |
| 455 | 'is_default' => 0, |
| 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, |
| 463 | ), $values); |
| 464 | |
| 465 | civicrm_api3('PaymentProcessor', 'create', $params); |
| 466 | } |
| 467 | |
| 468 | } |