Convert Dummy payment processor to use doPayment function
[civicrm-core.git] / CRM / Core / Payment / Dummy.php
index 1f3d9336cd62ba7b2a599be1f4712e9f2438ba18..5e5947c9f8859b8c344c691b7626a83043bce56d 100644 (file)
@@ -55,17 +55,30 @@ class CRM_Core_Payment_Dummy extends CRM_Core_Payment {
   }
 
   /**
-   * Submit a payment using Advanced Integration Method.
+   * @param array|PropertyBag $params
    *
-   * @param array $params
-   *   Assoc array of input parameters for this transaction.
+   * @param string $component
    *
    * @return array
-   *   the result in a nice formatted array (or an error object)
+   *   Result array (containing at least the key payment_status_id)
+   *
    * @throws \Civi\Payment\Exception\PaymentProcessorException
    */
-  public function doDirectPayment(&$params) {
+  public function doPayment(&$params, $component = 'contribute') {
+    $this->_component = $component;
+    $statuses = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'validate');
+
     $propertyBag = PropertyBag::cast($params);
+
+    // If we have a $0 amount, skip call to processor and set payment_status to Completed.
+    // Conceivably a processor might override this - perhaps for setting up a token - but we don't
+    // have an example of that at the mome.
+    if ($propertyBag->getAmount() == 0) {
+      $result['payment_status_id'] = array_search('Completed', $statuses);
+      $result['payment_status'] = 'Completed';
+      return $result;
+    }
+
     // Invoke hook_civicrm_paymentProcessor
     // In Dummy's case, there is no translation of parameters into
     // the back-end's canonical set of parameters.  But if a processor
@@ -75,17 +88,14 @@ class CRM_Core_Payment_Dummy extends CRM_Core_Payment {
       $throwAnENoticeIfNotSetAsTheseAreRequired = $propertyBag->getRecurFrequencyInterval() . $propertyBag->getRecurFrequencyUnit();
     }
     // no translation in Dummy processor
-    CRM_Utils_Hook::alterPaymentProcessorParams($this,
-      $params,
-      $propertyBag
-    );
+    CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $propertyBag);
     // This means we can test failing transactions by setting a past year in expiry. A full expiry check would
     // be more complete.
     if (!empty($params['credit_card_exp_date']['Y']) && date('Y') >
       CRM_Core_Payment_Form::getCreditCardExpirationYear($params)) {
       throw new PaymentProcessorException(ts('Invalid expiry date'));
     }
-    //end of hook invocation
+
     if (!empty($this->_doDirectPaymentResult)) {
       $result = $this->_doDirectPaymentResult;
       if (CRM_Utils_Array::value('payment_status_id', $result) === 'failed') {
@@ -94,28 +104,30 @@ class CRM_Core_Payment_Dummy extends CRM_Core_Payment {
       $result['trxn_id'] = array_shift($this->_doDirectPaymentResult['trxn_id']);
       return $result;
     }
-    if ($this->_mode === 'test') {
-      $query = "SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE 'test\\_%'";
-      $p = [];
-      $trxn_id = (string) CRM_Core_DAO::singleValueQuery($query, $p);
-      $trxn_id = str_replace('test_', '', $trxn_id);
-      $trxn_id = (int) $trxn_id + 1;
-      $params['trxn_id'] = 'test_' . $trxn_id . '_' . uniqid();
-    }
-    else {
-      $query = "SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE 'live_%'";
-      $p = [];
-      $trxn_id = (string) CRM_Core_DAO::singleValueQuery($query, $p);
-      $trxn_id = str_replace('live_', '', $trxn_id);
-      $trxn_id = (int) $trxn_id + 1;
-      $params['trxn_id'] = 'live_' . $trxn_id . '_' . uniqid();
-    }
-    $params['gross_amount'] = $propertyBag->getAmount();
+
+    $result['trxn_id'] = $this->getTrxnID();
+
     // Add a fee_amount so we can make sure fees are handled properly in underlying classes.
-    $params['fee_amount'] = 1.50;
-    $params['description'] = $this->getPaymentDescription($params);
+    $result['fee_amount'] = 1.50;
+    $result['description'] = $this->getPaymentDescription($params);
+
+    if (!isset($result['payment_status_id'])) {
+      if (!empty($propertyBag->getIsRecur())) {
+        // See comment block.
+        $result['payment_status_id'] = array_search('Pending', $statuses);
+        $result['payment_status'] = 'Pending';
+      }
+      else {
+        $result['payment_status_id'] = array_search('Completed', $statuses);
+        $result['payment_status'] = 'Completed';
+      }
+    }
 
-    return $params;
+    // We shouldn't do this but it saves us changing the `testPayNowPayment` test to actually test the contribution
+    // like it should.
+    $result = array_merge($params, $result);
+
+    return $result;
   }
 
   /**
@@ -219,4 +231,21 @@ class CRM_Core_Payment_Dummy extends CRM_Core_Payment {
     return ['message' => ts('Recurring contribution cancelled')];
   }
 
+  /**
+   * Get a value for the transaction ID.
+   *
+   * Value is made up of the max existing value + a random string.
+   *
+   * Note the random string is likely a historical workaround.
+   *
+   * @return string
+   */
+  protected function getTrxnID() {
+    $string = $this->_mode;
+    $trxn_id = CRM_Core_DAO::singleValueQuery("SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id LIKE '{$string}_%'");
+    $trxn_id = str_replace($string, '', $trxn_id);
+    $trxn_id = (int) $trxn_id + 1;
+    return $string . '_' . $trxn_id . '_' . uniqid();
+  }
+
 }