From 9d8f43b1c8b3585609fe4c2900a62b2f0eb87791 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 2 Jul 2015 18:28:29 +1200 Subject: [PATCH] CRM-16402 towards standardising instantiation of payment processor Conflicts: tests/phpunit/api/v3/ContributionPageTest.php --- CRM/Contribute/Form/AdditionalPayment.php | 2 +- CRM/Contribute/Form/Contribution.php | 4 +- CRM/Contribute/Page/UserDashboard.php | 1 - CRM/Core/Payment/Form.php | 11 ++- CRM/Financial/BAO/PaymentProcessor.php | 100 +++++++--------------- CRM/Member/Form/MembershipRenewal.php | 6 +- api/v3/Contribution.php | 11 +-- 7 files changed, 43 insertions(+), 92 deletions(-) diff --git a/CRM/Contribute/Form/AdditionalPayment.php b/CRM/Contribute/Form/AdditionalPayment.php index d5bde9b779..eba618cbf5 100644 --- a/CRM/Contribute/Form/AdditionalPayment.php +++ b/CRM/Contribute/Form/AdditionalPayment.php @@ -565,7 +565,7 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract if ($paymentParams['amount'] > 0.0) { // force a reget of the payment processor in case the form changed it, CRM-7179 - $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this, TRUE); + $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); $result = $payment->doDirectPayment($paymentParams); } diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index ea734591eb..f8fc18b8fb 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -1212,8 +1212,8 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP if ($paymentParams['amount'] > 0.0) { // force a re-get of the payment processor in case the form changed it, CRM-7179 - // NOTE - I expect this is not obsolete. - $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this, TRUE); + // NOTE - I expect this is obsolete. + $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); try { $statuses = CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id'); $result = $payment->doPayment($paymentParams, 'contribute'); diff --git a/CRM/Contribute/Page/UserDashboard.php b/CRM/Contribute/Page/UserDashboard.php index ba8e373859..3d001aa707 100644 --- a/CRM/Contribute/Page/UserDashboard.php +++ b/CRM/Contribute/Page/UserDashboard.php @@ -54,7 +54,6 @@ class CRM_Contribute_Page_UserDashboard extends CRM_Contact_Page_View_UserDashBo $controller->run(); //add honor block - $params = array(); $params = CRM_Contribute_BAO_Contribution::getHonorContacts($this->_contactId); if (!empty($params)) { diff --git a/CRM/Core/Payment/Form.php b/CRM/Core/Payment/Form.php index 3490b2c064..ee2980b03a 100644 --- a/CRM/Core/Payment/Form.php +++ b/CRM/Core/Payment/Form.php @@ -210,7 +210,7 @@ class CRM_Core_Payment_Form { * @return array */ public static function getPaymentFields($paymentProcessor) { - $paymentProcessorObject = CRM_Core_Payment::singleton(($paymentProcessor['is_test'] ? 'test' : 'live'), $paymentProcessor); + $paymentProcessorObject = Civi\Payment\System::singleton()->getByProcessor($paymentProcessor); return $paymentProcessorObject->getPaymentFormFields(); } @@ -220,7 +220,7 @@ class CRM_Core_Payment_Form { * @return array */ public static function getPaymentFieldMetadata($paymentProcessor) { - $paymentProcessorObject = CRM_Core_Payment::singleton(($paymentProcessor['is_test'] ? 'test' : 'live'), $paymentProcessor); + $paymentProcessorObject = Civi\Payment\System::singleton()->getByProcessor($paymentProcessor); return $paymentProcessorObject->getPaymentFormFieldsMetadata(); } @@ -230,7 +230,7 @@ class CRM_Core_Payment_Form { * @return string */ public static function getPaymentTypeName($paymentProcessor) { - $paymentProcessorObject = CRM_Core_Payment::singleton(($paymentProcessor['is_test'] ? 'test' : 'live'), $paymentProcessor); + $paymentProcessorObject = Civi\Payment\System::singleton()->getByProcessor($paymentProcessor); return $paymentProcessorObject->getPaymentTypeName(); } @@ -240,7 +240,7 @@ class CRM_Core_Payment_Form { * @return string */ public static function getPaymentTypeLabel($paymentProcessor) { - $paymentProcessorObject = CRM_Core_Payment::singleton(($paymentProcessor['is_test'] ? 'test' : 'live'), $paymentProcessor); + $paymentProcessorObject = Civi\Payment\System::singleton()->getByProcessor($paymentProcessor); return ts(($paymentProcessorObject->getPaymentTypeLabel()) . ' Information'); } @@ -329,8 +329,7 @@ class CRM_Core_Payment_Form { public static function validatePaymentInstrument($payment_processor_id, $values, &$errors, $form) { // ignore if we don't have a payment instrument to validate (e.g. backend payments) if ($payment_processor_id > 0) { - $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($payment_processor_id, 'live'); - $payment = CRM_Core_Payment::singleton('live', $paymentProcessor, $form); + $payment = Civi\Payment\System::singleton()->getById($payment_processor_id); $payment->validatePaymentInstrument($values, $errors); } } diff --git a/CRM/Financial/BAO/PaymentProcessor.php b/CRM/Financial/BAO/PaymentProcessor.php index d8af267e89..34e761b481 100644 --- a/CRM/Financial/BAO/PaymentProcessor.php +++ b/CRM/Financial/BAO/PaymentProcessor.php @@ -175,7 +175,8 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces /** * Get the payment processor details. * - * @deprecated Use Civi\Payment\System::singleton->getByID(); + * This returns an array whereas Civi\Payment\System::singleton->getByID() returns an object. + * The object is a key in the array. * * @param int $paymentProcessorID * Payment processor id. @@ -186,30 +187,37 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces * associated array with payment processor related fields */ public static function getPayment($paymentProcessorID, $mode = 'based_on_id') { - if (!$paymentProcessorID) { - CRM_Core_Error::fatal(ts('Invalid value passed to getPayment function')); - } - - $dao = new CRM_Financial_DAO_PaymentProcessor(); - $dao->id = $paymentProcessorID; - $dao->is_active = 1; - if (!$dao->find(TRUE)) { - return NULL; - } - - if ($mode == 'test') { - $testDAO = new CRM_Financial_DAO_PaymentProcessor(); - $testDAO->name = $dao->name; - $testDAO->is_active = 1; - $testDAO->is_test = 1; - if (!$testDAO->find(TRUE)) { - CRM_Core_Error::fatal(ts('Could not retrieve payment processor details')); + $capabilities = ($mode == 'test') ? array('TestMode') : array(); + $processors = self::getPaymentProcessors($capabilities, array($paymentProcessorID)); + $processor = $processors[$paymentProcessorID]; + $fields = array( + 'id', + 'name', + 'payment_processor_type_id', + 'user_name', + 'password', + 'signature', + 'url_site', + 'url_api', + 'url_recur', + 'url_button', + 'subject', + 'class_name', + 'is_recur', + 'billing_mode', + 'is_test', + 'payment_type', + 'is_default', + ); + // Just to prevent e-Notices elsewhere we set all fields. + foreach ($fields as $name) { + if (!isset($processor)) { + $processor[$name] = NULL; } - return self::buildPayment($testDAO, $mode); - } - else { - return self::buildPayment($dao, $mode); } + $processor['payment_processor_type'] = CRM_Core_PseudoConstant::paymentProcessorType(FALSE, + $processor['payment_processor_type_id'], 'name'); + return $processors[$paymentProcessorID]; } /** @@ -249,50 +257,6 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces return ($p1 > $p2) ? -1 : 1; } - /** - * Build payment processor details. - * - * @deprecated - * - * @param object $dao - * Payment processor object. - * @param string $mode - * Payment mode ie test or live. - * - * @return array - * associated array with payment processor related fields - */ - protected static function buildPayment($dao, $mode) { - $fields = array( - 'id', - 'name', - 'payment_processor_type_id', - 'user_name', - 'password', - 'signature', - 'url_site', - 'url_api', - 'url_recur', - 'url_button', - 'subject', - 'class_name', - 'is_recur', - 'billing_mode', - 'is_test', - 'payment_type', - 'is_default', - ); - $result = array(); - foreach ($fields as $name) { - $result[$name] = $dao->$name; - } - $result['payment_processor_type'] = CRM_Core_PseudoConstant::paymentProcessorType(FALSE, $dao->payment_processor_type_id, 'name'); - - $result['instance'] = $result['object'] = CRM_Core_Payment::singleton($mode, $result); - - return $result; - } - /** * Get all payment processors as an array of objects. * @@ -482,7 +446,7 @@ INNER JOIN civicrm_contribution con ON ( mp.contribution_id = con.id ) } elseif ($type == 'obj') { $payment = self::getPayment($ppID, $mode); - $result = CRM_Core_Payment::singleton($mode, $payment); + $result = Civi\Payment\System::singleton()->getByProcessor($payment); } return $result; diff --git a/CRM/Member/Form/MembershipRenewal.php b/CRM/Member/Form/MembershipRenewal.php index bd812c5b9e..1773369618 100644 --- a/CRM/Member/Form/MembershipRenewal.php +++ b/CRM/Member/Form/MembershipRenewal.php @@ -505,9 +505,7 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { $formValues['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'financial_type_id'); } - $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($formValues['payment_processor_id'], - $this->_mode - ); + $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($formValues['payment_processor_id']); $fields = array(); @@ -574,7 +572,7 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE); - $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this); + $payment = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); if (!empty($paymentParams['auto_renew'])) { $contributionRecurParams = $this->processRecurringContribution($paymentParams); diff --git a/api/v3/Contribution.php b/api/v3/Contribution.php index 256fc68aa1..121a1d82e2 100644 --- a/api/v3/Contribution.php +++ b/api/v3/Contribution.php @@ -327,16 +327,7 @@ function civicrm_api3_contribution_transact($params) { $params['invoice_id'] = CRM_Utils_Array::value('invoice_id', $params, md5(uniqid(rand(), TRUE))); $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($params['payment_processor'], $params['payment_processor_mode']); - if (civicrm_error($paymentProcessor)) { - return $paymentProcessor; - } - - $payment = CRM_Core_Payment::singleton($params['payment_processor_mode'], $paymentProcessor); - if (civicrm_error($payment)) { - return $payment; - } - - $transaction = $payment->doPayment($params); + $paymentProcessor['object']->doPayment($params); $params['payment_instrument_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_PaymentProcessorType', $paymentProcessor['payment_processor_type_id'], 'payment_type') == 1 ? 'Credit Card' : 'Debit Card'; return civicrm_api('Contribution', 'create', $params); -- 2.25.1