Fix form to stop messing name field for test processor
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 24 Mar 2023 02:28:47 +0000 (15:28 +1300)
committerColeman Watts <coleman@civicrm.org>
Tue, 28 Mar 2023 18:26:33 +0000 (14:26 -0400)
CRM/Admin/Form/PaymentProcessor.php
CRM/Core/Payment.php

index 046fb6dec218b887f2a44a2a6a020942aacc81cd..641dac4c5ad6554ef8012460cbd7b0e8310a75f3 100644 (file)
@@ -42,7 +42,7 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
   /**
    * Set entity fields to be assigned to the form.
    */
-  protected function setEntityFields() {
+  protected function setEntityFields(): void {
     $this->entityFields = [
       'payment_processor_type_id' => [
         'name' => 'payment_processor_type_id',
@@ -103,7 +103,7 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
     }
     else {
       $refreshURL = CRM_Utils_System::url('civicrm/admin/paymentProcessor/edit',
-        "reset=1&action=add",
+        'reset=1&action=add',
         FALSE, NULL, FALSE
       );
     }
@@ -402,11 +402,10 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
       $this->_paymentProcessorDAO->id = $values['payment_processor_type_id'];
       $this->_paymentProcessorDAO->find(TRUE);
     }
-    $this->updatePaymentProcessor($values, $domainID, FALSE);
     $this->updatePaymentProcessor($values, $domainID, TRUE);
-
-    $processor = civicrm_api3('payment_processor', 'getsingle', ['name' => $values['name'], 'is_test' => 0]);
-    $errors = Civi\Payment\System::singleton()->checkProcessorConfig($processor);
+    $paymentProcessorID = $this->updatePaymentProcessor($values, $domainID, FALSE);
+    $processor = Civi\Payment\System::singleton()->getById($paymentProcessorID);
+    $errors = $processor->checkConfig();
     if ($errors) {
       CRM_Core_Session::setStatus($errors, ts('Payment processor configuration invalid'), 'error');
       Civi::log()->error('Payment processor configuration invalid: ' . $errors);
@@ -426,7 +425,7 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
    *
    * @throws \CRM_Core_Exception
    */
-  public function updatePaymentProcessor(&$values, $domainID, $test) {
+  public function updatePaymentProcessor($values, $domainID, $test) {
     if ($test) {
       foreach (['user_name', 'password', 'signature', 'url_site', 'url_recur', 'url_api', 'url_button', 'subject'] as $field) {
         $values[$field] = empty($values["test_{$field}"]) ? CRM_Utils_Array::value($field, $values) : $values["test_{$field}"];
@@ -443,9 +442,7 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
       }
       $creditCards = json_encode($creditCards);
     }
-    else {
-      $creditCards = "NULL";
-    }
+
     $params = array_merge([
       'id' => $test ? $this->_testID : $this->_id,
       'domain_id' => $domainID,
@@ -458,14 +455,12 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form {
       'payment_type' => $this->_paymentProcessorDAO->payment_type,
       'payment_instrument_id' => $this->_paymentProcessorDAO->payment_instrument_id,
       'financial_account_id' => $values['financial_account_id'],
-      'accepted_credit_cards' => $creditCards,
+      'accepted_credit_cards' => $creditCards ?? NULL,
     ], $values);
 
-    $result = civicrm_api4('PaymentProcessor', 'save', [
+    return civicrm_api4('PaymentProcessor', 'save', [
       'records' => [$params],
-    ]);
-    // Pass autogenerated name back
-    $values['name'] = $result->single()['name'];
+    ])->first()['id'];
   }
 
   /**
index 10afed4e59dcc2543c65413a3762ac5ecd0495b1..b8f80dd8a95f0ffd51f6aa77875f75e53cc4a1af 100644 (file)
@@ -1511,7 +1511,7 @@ abstract class CRM_Core_Payment {
    * @return string
    *   the error message if any
    */
-  abstract protected function checkConfig();
+  abstract public function checkConfig();
 
   /**
    * Redirect for paypal.