Convert eway to doPayment
authorMatthew Wire <mjw@mjwconsult.co.uk>
Mon, 17 May 2021 15:20:31 +0000 (16:20 +0100)
committerMatthew Wire <mjw@mjwconsult.co.uk>
Mon, 17 May 2021 15:32:11 +0000 (16:32 +0100)
ext/ewaysingle/CRM/Core/Payment/eWAY.php

index a50389ffd33bbb7bb8d5232d441e41d487540d68..7dc7d233b51852a68cde30bda2f8790e5b9d7470 100644 (file)
@@ -128,12 +128,29 @@ class CRM_Core_Payment_eWAY extends CRM_Core_Payment {
   /**
    * Sends request and receive response from eWAY payment process.
    *
-   * @param array $params
+   * @param array|\Civi\Payment\PropertyBag $params
    *
-   * @return array|object
-   * @throws \Exception
+   * @param string $component
+   *
+   * @return array
+   *   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') {
+    $propertyBag = \Civi\Payment\PropertyBag::cast($params);
+    $this->_component = $component;
+    $statuses = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'validate');
+
+    // 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 moment.
+    if ($propertyBag->getAmount() == 0) {
+      $result['payment_status_id'] = array_search('Completed', $statuses);
+      $result['payment_status'] = 'Completed';
+      return $result;
+    }
+
     if (CRM_Utils_Array::value('is_recur', $params) == TRUE) {
       throw new CRM_Core_Exception(ts('eWAY - recurring payments not implemented'));
     }
@@ -319,6 +336,8 @@ class CRM_Core_Payment_eWAY extends CRM_Core_Payment {
     }
     $params['trxn_result_code'] = $eWAYResponse->Status() . $beaglestatus;
     $params['trxn_id'] = $eWAYResponse->TransactionNumber();
+    $params['payment_status_id'] = array_search('Completed', $statuses);
+    $params['payment_status'] = 'Completed';
 
     return $params;
   }