From 8563c1cea6e01de7129c602d4061922d4000c7c3 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 30 Nov 2018 13:57:32 +1300 Subject: [PATCH] Set default financial_type_id for creating new payment processors at form and api level --- CRM/Admin/Form/PaymentProcessor.php | 1 + CRM/Financial/BAO/PaymentProcessor.php | 12 ++++++++++++ api/v3/PaymentProcessor.php | 3 +++ tests/phpunit/api/v3/PaymentProcessorTest.php | 9 ++++----- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/CRM/Admin/Form/PaymentProcessor.php b/CRM/Admin/Form/PaymentProcessor.php index 16be41ca04..68cc32d37e 100644 --- a/CRM/Admin/Form/PaymentProcessor.php +++ b/CRM/Admin/Form/PaymentProcessor.php @@ -327,6 +327,7 @@ class CRM_Admin_Form_PaymentProcessor extends CRM_Admin_Form { if ($this->_paymentProcessorType) { $defaults['payment_processor_type_id'] = $this->_paymentProcessorType; } + $defaults['financial_account_id'] = CRM_Financial_BAO_PaymentProcessor::getDefaultFinancialAccountID(); return $defaults; } $domainID = CRM_Core_Config::domainID(); diff --git a/CRM/Financial/BAO/PaymentProcessor.php b/CRM/Financial/BAO/PaymentProcessor.php index e312d2ebc9..6e98ba9005 100644 --- a/CRM/Financial/BAO/PaymentProcessor.php +++ b/CRM/Financial/BAO/PaymentProcessor.php @@ -597,4 +597,16 @@ INNER JOIN civicrm_contribution con ON ( mp.contribution_id = con.id ) } } + /** + * Get the default financial account id for payment processor accounts. + * + * Note that there is only a 'name' field & no label field. If people customise + * name then this won't work. This is new best-effort functionality so that's non-regressive. + * + * The fix for that is to add a label value to the financial account table. + */ + public static function getDefaultFinancialAccountID() { + return CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_EntityFinancialAccount', 'financial_account_id', 'Payment Processor Account'); + } + } diff --git a/api/v3/PaymentProcessor.php b/api/v3/PaymentProcessor.php index 20a7e0a510..bee35c59a9 100644 --- a/api/v3/PaymentProcessor.php +++ b/api/v3/PaymentProcessor.php @@ -62,6 +62,9 @@ function _civicrm_api3_payment_processor_create_spec(&$params) { $params['is_default']['api.default'] = 0; $params['is_test']['api.default'] = 0; $params['domain_id']['api.default'] = CRM_Core_Config::domainID(); + $params['financial_account_id']['api.default'] = CRM_Financial_BAO_PaymentProcessor::getDefaultFinancialAccountID(); + $params['financial_account_id']['api.required'] = TRUE; + $params['financial_account_id']['title'] = ts('Financial Account for Processor'); } /** diff --git a/tests/phpunit/api/v3/PaymentProcessorTest.php b/tests/phpunit/api/v3/PaymentProcessorTest.php index 742404aed8..c71017308e 100644 --- a/tests/phpunit/api/v3/PaymentProcessorTest.php +++ b/tests/phpunit/api/v3/PaymentProcessorTest.php @@ -73,9 +73,8 @@ class api_v3_PaymentProcessorTest extends CiviUnitTestCase { public function testPaymentProcessorCreate() { $params = $this->_params; $result = $this->callAPIAndDocument('payment_processor', 'create', $params, __FUNCTION__, __FILE__); - $this->assertNotNull($result['id']); - $this->assertDBState('CRM_Financial_DAO_PaymentProcessor', $result['id'], $params); - return $result['id']; + $this->callAPISuccessGetSingle('EntityFinancialAccount', ['entity_table' => 'civicrm_payment_processor', 'entity_id' => $result['id']]); + $this->getAndCheck($params, $result['id'], 'PaymentProcessor'); } /** @@ -124,9 +123,9 @@ class api_v3_PaymentProcessorTest extends CiviUnitTestCase { * Check payment processor delete. */ public function testPaymentProcessorDelete() { - $id = $this->testPaymentProcessorCreate(); + $result = $this->callAPISuccess('payment_processor', 'create', $this->_params); $params = array( - 'id' => $id, + 'id' => $result['id'], ); $this->callAPIAndDocument('payment_processor', 'delete', $params, __FUNCTION__, __FILE__); -- 2.25.1