X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FCore%2FPayment%2FRealex.php;h=6fa22c9ccd867c3dcaebe88fb6aa1c36f7248fc4;hb=ddaa955ed4b89d8d3280316a7c902357a6e03793;hp=d1834686cb66ebd41af9a2ec95cc8e5f8801cbb0;hpb=aea2495c3afd1f728761c44e9937a85bfb106461;p=civicrm-core.git diff --git a/CRM/Core/Payment/Realex.php b/CRM/Core/Payment/Realex.php index d1834686cb..6fa22c9ccd 100644 --- a/CRM/Core/Payment/Realex.php +++ b/CRM/Core/Payment/Realex.php @@ -1,29 +1,14 @@ - * $Id$ */ class CRM_Core_Payment_Realex extends CRM_Core_Payment { const AUTH_APPROVED = '00'; - protected $_mode = NULL; + protected $_mode; protected $_params = []; @@ -50,14 +36,11 @@ class CRM_Core_Payment_Realex extends CRM_Core_Payment { * @param string $mode * The mode of operation: live or test. * - * @param $paymentProcessor - * - * @return \CRM_Core_Payment_Realex + * @param array $paymentProcessor */ public function __construct($mode, &$paymentProcessor) { $this->_mode = $mode; $this->_paymentProcessor = $paymentProcessor; - $this->_processorName = ts('Realex'); $this->_setParam('merchant_ref', $paymentProcessor['user_name']); $this->_setParam('secret', $paymentProcessor['password']); @@ -76,24 +59,22 @@ class CRM_Core_Payment_Realex extends CRM_Core_Payment { * * @return array * the result in a nice formatted array (or an error object) + * + * @throws \Civi\Payment\Exception\PaymentProcessorException */ public function doDirectPayment(&$params) { if (!defined('CURLOPT_SSLCERT')) { - return self::error(9001, ts('RealAuth requires curl with SSL support')); + throw new PaymentProcessorException(ts('RealAuth requires curl with SSL support'), 9001); } $result = $this->setRealexFields($params); - if ($result !== TRUE) { - return $result; - } - /********************************************************** * Check to see if we have a duplicate before we send **********************************************************/ if ($this->checkDupe($params['invoiceID'], CRM_Utils_Array::value('contributionID', $params))) { - return self::error(9004, ts('It appears that this transaction is a duplicate. Have you already submitted the form once? If so there may have been a connection problem. Check your email for a receipt from Authorize.net. If you do not receive a receipt within 2 hours you can try your transaction again. If you continue to have problems please contact the site administrator.')); + throw new PaymentProcessorException(ts('It appears that this transaction is a duplicate. Have you already submitted the form once? If so there may have been a connection problem. Check your email for a receipt from Authorize.net. If you do not receive a receipt within 2 hours you can try your transaction again. If you continue to have problems please contact the site administrator.'), 9004); } // Create sha1 hash for request @@ -136,7 +117,7 @@ class CRM_Core_Payment_Realex extends CRM_Core_Payment { $submit = curl_init($this->_paymentProcessor['url_site']); if (!$submit) { - return self::error(9002, ts('Could not initiate connection to payment gateway')); + throw new PaymentProcessorException(ts('Could not initiate connection to payment gateway'), 9002); } curl_setopt($submit, CURLOPT_HTTPHEADER, ['SOAPAction: ""']); @@ -156,7 +137,7 @@ class CRM_Core_Payment_Realex extends CRM_Core_Payment { $response_xml = curl_exec($submit); if (!$response_xml) { - return self::error(curl_errno($submit), curl_error($submit)); + throw new PaymentProcessorException(curl_error($submit), curl_errno($submit)); } curl_close($submit); @@ -168,7 +149,7 @@ class CRM_Core_Payment_Realex extends CRM_Core_Payment { // Parse the response xml $xml_parser = xml_parser_create(); if (!xml_parse($xml_parser, $response_xml)) { - return self::error(9003, 'XML Error'); + throw new PaymentProcessorException('XML Error', 9003); } $response = $this->xml_parse_into_assoc($response_xml); @@ -179,19 +160,7 @@ class CRM_Core_Payment_Realex extends CRM_Core_Payment { // Return an error if authentication was not successful if ($response['RESULT'] !== self::AUTH_APPROVED) { - return self::error($response['RESULT'], ' ' . $response['MESSAGE']); - } - - // Check the response hash - $hashme = "{$this->_getParam('timestamp')}.{$this->_getParam('merchant_ref')}.{$this->_getParam('order_id')}.{$response['RESULT']}.{$response['MESSAGE']}.{$response['PASREF']}.{$response['AUTHCODE']}"; - $sha1hash = sha1($hashme); - $hashme = "$sha1hash.{$this->_getParam('secret')}"; - $sha1hash = sha1($hashme); - - if ($response['SHA1HASH'] != $sha1hash) { - // FIXME: Need to actually check this - I couldn't get the - // hashes to match so I'm commenting out for now' - // return self::error( 9001, "Hash error, please report this to the webmaster" ); + throw new PaymentProcessorException($this->getDisplayError($response['RESULT'], ' ' . $response['MESSAGE'])); } // FIXME: We are using the trxn_result_code column to store all these extra details since there @@ -254,7 +223,8 @@ class CRM_Core_Payment_Realex extends CRM_Core_Payment { } /** - * Private helper for xml_parse_into_assoc, to recusively parsing the result + * Private helper for xml_parse_into_assoc, to recursively parsing the result. + * * @param $input * @param int $depth * @@ -294,11 +264,11 @@ class CRM_Core_Payment_Realex extends CRM_Core_Payment { * * @param array $params * - * @return bool + * @throws \Civi\Payment\Exception\PaymentProcessorException */ public function setRealexFields(&$params) { if ((int) $params['amount'] <= 0) { - return self::error(9001, ts('Amount must be positive')); + throw new PaymentProcessorException(ts('Amount must be positive'), 9001); } // format amount to be in smallest possible units @@ -335,7 +305,7 @@ class CRM_Core_Payment_Realex extends CRM_Core_Payment { break; default: - return self::error(9001, ts('Credit card type not supported by Realex:') . ' ' . $params['credit_card_type']); + throw new PaymentProcessorException(ts('Credit card type not supported by Realex:') . ' ' . $params['credit_card_type'], 9001); } // get the card holder name - cater cor customized billing forms @@ -361,11 +331,9 @@ class CRM_Core_Payment_Realex extends CRM_Core_Payment { $this->_setParam('varref', $params['contributionType_name']); $comment = $params['description'] . ' (page id:' . $params['contributionPageID'] . ')'; $this->_setParam('comments', $comment); - //$this->_setParam('currency', $params['currencyID']); // set the currency to the default which can be overrided. - $config = CRM_Core_Config::singleton(); - $this->_setParam('currency', $config->defaultCurrency); + $this->_setParam('currency', CRM_Core_Config::singleton()->defaultCurrency); // Format the expiry date to MMYY $expmonth = (string) $params['month']; @@ -383,10 +351,8 @@ class CRM_Core_Payment_Realex extends CRM_Core_Payment { } // Create timestamp - $timestamp = strftime("%Y%m%d%H%M%S"); + $timestamp = strftime('%Y%m%d%H%M%S'); $this->_setParam('timestamp', $timestamp); - - return TRUE; } /** @@ -403,9 +369,7 @@ class CRM_Core_Payment_Realex extends CRM_Core_Payment { if (isset($this->_params[$field])) { return $this->_params[$field]; } - else { - return ''; - } + return ''; } /** @@ -414,50 +378,12 @@ class CRM_Core_Payment_Realex extends CRM_Core_Payment { * * @param string $field * @param mixed $value - * - * @return bool - * false if value is not a scalar, true if successful */ public function _setParam($field, $value) { if (!is_scalar($value)) { - return FALSE; - } - else { - $this->_params[$field] = $value; - } - } - - /** - * @param null $errorCode - * @param null $errorMessage - * - * @return object - */ - public function &error($errorCode = NULL, $errorMessage = NULL) { - $e = CRM_Core_Error::singleton(); - - if ($errorCode) { - if ($errorCode == '101' || $errorCode == '102') { - $display_error = ts('Card declined by bank. Please try with a different card.'); - } - elseif ($errorCode == '103') { - $display_error = ts('Card reported lost or stolen. This incident will be reported.'); - } - elseif ($errorCode == '501') { - $display_error = ts("It appears that this transaction is a duplicate. Have you already submitted the form once? If so there may have been a connection problem. Check your email for a receipt for this transaction. If you do not receive a receipt within 2 hours you can try your transaction again. If you continue to have problems please contact the site administrator."); - } - elseif ($errorCode == '509') { - $display_error = $errorMessage; - } - else { - $display_error = ts('We were unable to process your payment at this time. Please try again later.'); - } - $e->push($errorCode, 0, NULL, $display_error); - } - else { - $e->push(9001, 0, NULL, ts('We were unable to process your payment at this time. Please try again later.')); + return; } - return $e; + $this->_params[$field] = $value; } /** @@ -479,9 +405,34 @@ class CRM_Core_Payment_Realex extends CRM_Core_Payment { if (!empty($error)) { return implode('
', $error); } + return NULL; + } + + /** + * Get the error to display. + * + * @param string $errorCode + * @param string $errorMessage + * + * @return string + */ + protected function getDisplayError($errorCode, $errorMessage): string { + if ($errorCode === '101' || $errorCode === '102') { + $display_error = ts('Card declined by bank. Please try with a different card.'); + } + elseif ($errorCode === '103') { + $display_error = ts('Card reported lost or stolen. This incident will be reported.'); + } + elseif ($errorCode === '501') { + $display_error = ts('It appears that this transaction is a duplicate. Have you already submitted the form once? If so there may have been a connection problem. Check your email for a receipt for this transaction. If you do not receive a receipt within 2 hours you can try your transaction again. If you continue to have problems please contact the site administrator.'); + } + elseif ($errorCode === '509') { + $display_error = $errorMessage; + } else { - return NULL; + $display_error = ts('We were unable to process your payment at this time. Please try again later.'); } + return $display_error; } }