Skip to content

Commit

Permalink
Check configuration of payment processors when saving config
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Jan 30, 2018
1 parent 56d3977 commit 3eecd5b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
13 changes: 12 additions & 1 deletion CRM/Admin/Form/PaymentProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public function preProcess() {
$refreshURL .= "&civicrmDestination=$destination";
}

$this->refreshURL = $refreshURL;
$this->assign('refreshURL', $refreshURL);

$this->assign('is_recur', $this->_ppDAO->is_recur);
Expand Down Expand Up @@ -385,7 +386,17 @@ public function postProcess() {

$this->updatePaymentProcessor($values, $domainID, FALSE);
$this->updatePaymentProcessor($values, $domainID, TRUE);
CRM_Core_Session::setStatus(ts('Payment processor %1 has been saved.', array(1 => "<em>{$values['name']}</em>")), ts('Saved'), 'success');

$processor = civicrm_api3('payment_processor', 'getsingle', array('name' => $values['name'], 'is_test' => 0));
$errors = Civi\Payment\System::singleton()->checkProcessorConfig($processor);
if ($errors) {
CRM_Core_Session::setStatus($errors, 'Payment processor configuration invalid', 'error');
Civi::log()->error('Payment processor configuration invalid: ' . $errors);
CRM_Core_Session::singleton()->pushUserContext($this->refreshURL);
}
else {
CRM_Core_Session::setStatus(ts('Payment processor %1 has been saved.', array(1 => "<em>{$values['name']}</em>")), ts('Saved'), 'success');
}
}

/**
Expand Down
28 changes: 28 additions & 0 deletions Civi/Payment/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,34 @@ public function getByProcessor($processor, $force = FALSE) {
return $this->cache[$id];
}

/**
* Execute checkConfig() on the payment processor Object.
* This function creates a new instance of the processor object and returns the output of checkConfig
*
* @param array $processor
*
* @return string|NULL
*
* @throws \CRM_Core_Exception
*/
public function checkProcessorConfig($processor) {
$ext = \CRM_Extension_System::singleton()->getMapper();
if ($ext->isExtensionKey($processor['class_name'])) {
$paymentClass = $ext->keyToClass($processor['class_name'], 'payment');
require_once $ext->classToPath($paymentClass);
}
else {
$paymentClass = 'CRM_Core_' . $processor['class_name'];
if (empty($paymentClass)) {
throw new \CRM_Core_Exception('no class provided');
}
require_once str_replace('_', DIRECTORY_SEPARATOR, $paymentClass) . '.php';
}

$processorObject = new $paymentClass(!empty($processor['is_test']) ? 'test' : 'live', $processor);
return $processorObject->checkConfig();
}

/**
* Get payment processor by it's ID.
*
Expand Down

0 comments on commit 3eecd5b

Please sign in to comment.