From e4fcfa1dfd5ef935eb2a9a9426de34538e12536a Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Sat, 10 Apr 2021 10:23:09 +0100 Subject: [PATCH] Convert Dummy payment processor to use doPayment function --- CRM/Core/Payment/Dummy.php | 54 ++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/CRM/Core/Payment/Dummy.php b/CRM/Core/Payment/Dummy.php index 7961d56e94..5e5947c9f8 100644 --- a/CRM/Core/Payment/Dummy.php +++ b/CRM/Core/Payment/Dummy.php @@ -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') { @@ -95,13 +105,29 @@ class CRM_Core_Payment_Dummy extends CRM_Core_Payment { return $result; } - $params['trxn_id'] = $this->getTrxnID();; + $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'; + } + } + + // 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 $params; + return $result; } /** -- 2.25.1