From 5de5c50e548c1ce2d6b53eaa7ff9b945c580f879 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sun, 12 Jan 2020 07:53:12 +1100 Subject: [PATCH] [REF] Move handling of default payment instruement for a payment processor into the create function from API and expand tests --- CRM/Financial/BAO/PaymentProcessor.php | 7 +++++++ api/v3/PaymentProcessor.php | 6 ------ tests/phpunit/api/v3/PaymentProcessorTest.php | 3 +++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CRM/Financial/BAO/PaymentProcessor.php b/CRM/Financial/BAO/PaymentProcessor.php index 0ce7d3941c..65dae3da36 100644 --- a/CRM/Financial/BAO/PaymentProcessor.php +++ b/CRM/Financial/BAO/PaymentProcessor.php @@ -35,6 +35,13 @@ class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProces * @throws Exception */ public static function create($params) { + // If we are creating a new PaymentProcessor and have not specified the payment instrument to use, get the default from the Payment Processor Type. + if (empty($params['id']) && empty($params['payment_instrument_id'])) { + $params['payment_instrument_id'] = civicrm_api3('PaymentProcessorType', 'getvalue', [ + 'id' => $params['payment_processor_type_id'], + 'return' => 'payment_instrument_id', + ]); + } $processor = new CRM_Financial_DAO_PaymentProcessor(); $processor->copyValues($params); diff --git a/api/v3/PaymentProcessor.php b/api/v3/PaymentProcessor.php index 4d7df87fe1..06587f00ca 100644 --- a/api/v3/PaymentProcessor.php +++ b/api/v3/PaymentProcessor.php @@ -24,12 +24,6 @@ * API result array */ function civicrm_api3_payment_processor_create($params) { - if (empty($params['id']) && empty($params['payment_instrument_id'])) { - $params['payment_instrument_id'] = civicrm_api3('PaymentProcessorType', 'getvalue', [ - 'id' => $params['payment_processor_type_id'], - 'return' => 'payment_instrument_id', - ]); - } return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'PaymentProcessor'); } diff --git a/tests/phpunit/api/v3/PaymentProcessorTest.php b/tests/phpunit/api/v3/PaymentProcessorTest.php index e3c8a713d7..3bad456b37 100644 --- a/tests/phpunit/api/v3/PaymentProcessorTest.php +++ b/tests/phpunit/api/v3/PaymentProcessorTest.php @@ -33,6 +33,7 @@ class api_v3_PaymentProcessorTest extends CiviUnitTestCase { 'class_name' => 'CRM_Core_Payment_APITest', 'billing_mode' => 'form', 'is_recur' => 0, + 'payment_instrument_id' => 2, ]; $result = $this->callAPISuccess('payment_processor_type', 'create', $params); $this->_paymentProcessorType = $result['id']; @@ -71,6 +72,7 @@ class api_v3_PaymentProcessorTest extends CiviUnitTestCase { 'frequency_interval' => 1, ]); $this->getAndCheck($params, $result['id'], 'PaymentProcessor'); + $this->assertEquals(2, $result['values'][$result['id']]['payment_instrument_id']); } /** @@ -80,6 +82,7 @@ class api_v3_PaymentProcessorTest extends CiviUnitTestCase { */ public function testPaymentProcessorUpdate() { $params = $this->_params; + $params['payment_instrument_id'] = 1; $result = $this->callAPISuccess('payment_processor', 'create', $params); $this->assertNotNull($result['id']); -- 2.25.1