[REF] Move handling of default payment instruement for a payment processor into the...
authorSeamus Lee <seamuslee001@gmail.com>
Sat, 11 Jan 2020 20:53:12 +0000 (07:53 +1100)
committerSeamus Lee <seamuslee001@gmail.com>
Sat, 11 Jan 2020 20:53:12 +0000 (07:53 +1100)
CRM/Financial/BAO/PaymentProcessor.php
api/v3/PaymentProcessor.php
tests/phpunit/api/v3/PaymentProcessorTest.php

index 0ce7d3941ce0db45a6c07bf636e7aa5c55e9c766..65dae3da36241ca4ec63b36b561654afa8705904 100644 (file)
@@ -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);
 
index 4d7df87fe1e8043a28f920f83ede48b85731dc94..06587f00ca3120f50e4067ed510eda1c3544a814 100644 (file)
  *   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');
 }
 
index e3c8a713d72fca51a86f5f7f3e4d82b6c455a573..3bad456b3748ea00f292c184db2a51215df6ad32 100644 (file)
@@ -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']);