Check configuration of payment processors when saving config
authorMatthew Wire <devel@mrwire.co.uk>
Mon, 29 Jan 2018 13:40:53 +0000 (20:40 +0700)
committerMatthew Wire <devel@mrwire.co.uk>
Tue, 30 Jan 2018 04:56:46 +0000 (11:56 +0700)
CRM/Admin/Form/PaymentProcessor.php
Civi/Payment/System.php

index 64fd697ea88a225d50f71076085d884fbfa558c7..b6ae84ac26e0d4f078cd8d5010c6f9ff22b6eecd 100644 (file)
@@ -101,6 +101,7 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
       $refreshURL .= "&civicrmDestination=$destination";
     }
 
+    $this->refreshURL = $refreshURL;
     $this->assign('refreshURL', $refreshURL);
 
     $this->assign('is_recur', $this->_ppDAO->is_recur);
@@ -385,7 +386,17 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
 
     $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');
+    }
   }
 
   /**
index e5327240f5caf7952b2ae878812c5cd32983c6f8..2ba7083c41e20487fade8a99269709a47b6d7a41 100644 (file)
@@ -72,6 +72,34 @@ class System {
     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.
    *