From 42fcf5981744ff17381f1fd6f01bbe5952dc9fcf Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Sat, 10 Apr 2021 10:33:06 +0100 Subject: [PATCH] Initial refactor of PayPal core processor to stop using doDirectPayment/doTransferCheckout --- CRM/Core/Payment/PayPalImpl.php | 43 +++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/CRM/Core/Payment/PayPalImpl.php b/CRM/Core/Payment/PayPalImpl.php index 99dac01328..c60841e0f2 100644 --- a/CRM/Core/Payment/PayPalImpl.php +++ b/CRM/Core/Payment/PayPalImpl.php @@ -485,12 +485,46 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment { * @throws \Civi\Payment\Exception\PaymentProcessorException */ public function doPayment(&$params, $component = 'contribute') { + $this->_component = $component; if ($this->isPayPalType($this::PAYPAL_EXPRESS) || ($this->isPayPalType($this::PAYPAL_PRO) && !empty($params['token']))) { - $this->_component = $component; return $this->doExpressCheckout($params); } - return parent::doPayment($params, $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 mome. + if ($params['amount'] == 0) { + $result['payment_status_id'] = array_search('Completed', $statuses); + $result['payment_status'] = 'Completed'; + return $result; + } + + if ($this->_paymentProcessor['billing_mode'] == 4) { + $this->doPaymentRedirectToPayPal($params, $component); + // redirect calls CiviExit() so execution is stopped + } + else { + $result = $this->doPaymentPayPalButton($params, $component); + if (is_array($result) && !isset($result['payment_status_id'])) { + if (!empty($params['is_recur'])) { + // 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'; + } + } + } + if (is_a($result, 'CRM_Core_Error')) { + CRM_Core_Error::deprecatedFunctionWarning('payment processors should throw exceptions rather than return errors'); + throw new PaymentProcessorException(CRM_Core_Error::getMessages($result)); + } + return $result; } /** @@ -505,7 +539,7 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment { * the result in an nice formatted array (or an error object) * @throws \Civi\Payment\Exception\PaymentProcessorException */ - public function doDirectPayment(&$params, $component = 'contribute') { + public function doPaymentPayPalButton(&$params, $component = 'contribute') { $args = []; $this->initialize($args, 'DoDirectPayment'); @@ -856,8 +890,7 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment { * * @throws Exception */ - public function doTransferCheckout(&$params, $component = 'contribute') { - + public function doPaymentRedirectToPayPal(&$params, $component = 'contribute') { $notifyParameters = ['module' => $component]; $notifyParameterMap = [ 'contactID' => 'contactID', -- 2.25.1