Merge pull request #11540 from eileenmcnaughton/yahoo
[civicrm-core.git] / CRM / Admin / Form / PaymentProcessor.php
index efb4475355b203537b84eea7adc1c6d6ec59aa5c..b6ae84ac26e0d4f078cd8d5010c6f9ff22b6eecd 100644 (file)
@@ -43,16 +43,18 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
 
   protected $_ppDAO;
 
+  /**
+   * Get the name of the base entity being edited.
+   *
+   * @return string
+   */
+  public function getDefaultEntity() {
+    return 'PaymentProcessor';
+  }
+
   public function preProcess() {
-    if (!CRM_Core_Permission::check('administer payment processors')) {
-      CRM_Core_Error::statusBounce('The \'administer payment processors\' permission is required to add or edit a payment processor.');
-    }
     parent::preProcess();
 
-    CRM_Utils_System::setTitle(ts('Settings - Payment Processor'));
-
-    // get the payment processor meta information
-
     if ($this->_id) {
       $this->_ppType = CRM_Utils_Request::retrieve('pp', 'String', $this, FALSE, NULL);
       if (!$this->_ppType) {
@@ -77,9 +79,7 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
     $this->_ppDAO = new CRM_Financial_DAO_PaymentProcessorType();
     $this->_ppDAO->id = $this->_ppType;
 
-    if (!$this->_ppDAO->find(TRUE)) {
-      CRM_Core_Error::fatal(ts('Could not find payment processor meta information'));
-    }
+    $this->_ppDAO->find(TRUE);
 
     if ($this->_id) {
       $refreshURL = CRM_Utils_System::url('civicrm/admin/paymentProcessor',
@@ -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);
@@ -221,10 +222,13 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
         continue;
       }
 
-      $this->add('text', $field['name'],
-        $field['label'], $attributes[$field['name']]
-      );
-      $this->add('text', "test_{$field['name']}",
+      $this->addField($field['name'], array('label' => $field['label']));
+
+      $fieldSpec = civicrm_api3($this->getDefaultEntity(), 'getfield', array(
+        'name' => $field['name'],
+        'action' => 'create',
+      ));
+      $this->add($fieldSpec['values']['html']['type'], "test_{$field['name']}",
         $field['label'], $attributes[$field['name']]
       );
       if (!empty($field['rule'])) {
@@ -356,7 +360,7 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
         $defaults[$testName] = $testDAO->{$field['name']};
       }
     }
-    $defaults['financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getFinancialAccount($dao->id, 'civicrm_payment_processor', 'financial_account_id');
+    $defaults['financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($dao->id, NULL, 'civicrm_payment_processor');
 
     return $defaults;
   }
@@ -382,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');
+    }
   }
 
   /**