diff --git a/CRM/Financial/BAO/PaymentProcessor.php b/CRM/Financial/BAO/PaymentProcessor.php index 0ce7d3941ce0..65dae3da3624 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 4d7df87fe1e8..06587f00ca31 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 e3c8a713d72f..3bad456b3748 100644 --- a/tests/phpunit/api/v3/PaymentProcessorTest.php +++ b/tests/phpunit/api/v3/PaymentProcessorTest.php @@ -33,6 +33,7 @@ public function setUp() { '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 @@ public function testPaymentProcessorCreate() { 'frequency_interval' => 1, ]); $this->getAndCheck($params, $result['id'], 'PaymentProcessor'); + $this->assertEquals(2, $result['values'][$result['id']]['payment_instrument_id']); } /** @@ -80,6 +82,7 @@ public function testPaymentProcessorCreate() { */ public function testPaymentProcessorUpdate() { $params = $this->_params; + $params['payment_instrument_id'] = 1; $result = $this->callAPISuccess('payment_processor', 'create', $params); $this->assertNotNull($result['id']);