From 847f13a835f8bf0e09721eb21746226199520f77 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sun, 27 Oct 2019 13:07:57 +1100 Subject: [PATCH] Implement PaymentProcessor and PaymentProcessorType APIv4 Entities Move default values to spec as per Coleman Fix class name in tests and fix default values on tables Mock financial_account_id field --- CRM/Financial/DAO/PaymentProcessorType.php | 6 +- .../Incremental/sql/5.20.alpha1.mysql.tpl | 11 ++++ Civi/Api4/Action/PaymentProcessor/Create.php | 43 +++++++++++++ .../PaymentProcessorSaveTrait.php | 62 ++++++++++++++++++ Civi/Api4/Action/PaymentProcessor/Save.php | 43 +++++++++++++ Civi/Api4/PaymentProcessor.php | 61 ++++++++++++++++++ Civi/Api4/PaymentProcessorType.php | 47 ++++++++++++++ .../PaymentProcessorCreationSpecProvider.php | 64 +++++++++++++++++++ ...ymentProcessorTypeCreationSpecProvider.php | 56 ++++++++++++++++ tests/phpunit/api/v3/PaymentProcessorTest.php | 5 ++ .../api/v3/PaymentProcessorTypeTest.php | 11 +++- xml/schema/Financial/PaymentProcessorType.xml | 2 +- 12 files changed, 406 insertions(+), 5 deletions(-) create mode 100644 Civi/Api4/Action/PaymentProcessor/Create.php create mode 100644 Civi/Api4/Action/PaymentProcessor/PaymentProcessorSaveTrait.php create mode 100644 Civi/Api4/Action/PaymentProcessor/Save.php create mode 100644 Civi/Api4/PaymentProcessor.php create mode 100644 Civi/Api4/PaymentProcessorType.php create mode 100644 Civi/Api4/Service/Spec/Provider/PaymentProcessorCreationSpecProvider.php create mode 100644 Civi/Api4/Service/Spec/Provider/PaymentProcessorTypeCreationSpecProvider.php diff --git a/CRM/Financial/DAO/PaymentProcessorType.php b/CRM/Financial/DAO/PaymentProcessorType.php index 732ec4b539a0..fa8674f00088 100644 --- a/CRM/Financial/DAO/PaymentProcessorType.php +++ b/CRM/Financial/DAO/PaymentProcessorType.php @@ -6,7 +6,7 @@ * * Generated from xml/schema/CRM/Financial/PaymentProcessorType.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:ea2020b03d32e0c0f1a2d4915f2a14b1) + * (GenCodeChecksum:53099eb7a742c6c3f4921806e3dc76f1) */ /** @@ -36,7 +36,7 @@ class CRM_Financial_DAO_PaymentProcessorType extends CRM_Core_DAO { public $id; /** - * Payment Processor Name. + * Payment Processor Type Name. * * @var string */ @@ -195,7 +195,7 @@ public static function &fields() { 'name' => 'name', 'type' => CRM_Utils_Type::T_STRING, 'title' => ts('Payment Processor variable name to be used in code'), - 'description' => ts('Payment Processor Name.'), + 'description' => ts('Payment Processor Type Name.'), 'required' => TRUE, 'maxlength' => 64, 'size' => CRM_Utils_Type::BIG, diff --git a/CRM/Upgrade/Incremental/sql/5.20.alpha1.mysql.tpl b/CRM/Upgrade/Incremental/sql/5.20.alpha1.mysql.tpl index 26b73a833bcc..1faf76c35bf3 100644 --- a/CRM/Upgrade/Incremental/sql/5.20.alpha1.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/5.20.alpha1.mysql.tpl @@ -1,3 +1,14 @@ {* file to handle db changes in 5.20.alpha1 during upgrade *} UPDATE civicrm_navigation SET url = "civicrm/api3" WHERE url = "civicrm/api" AND domain_id = {$domainID}; + +UPDATE civicrm_payment_processor SET is_default = 0 WHERE is_default IS NULL; +UPDATE civicrm_payment_processor SET is_active = 1 WHERE is_active IS NULL; +UPDATE civicrm_payment_processor SET is_test = 0 WHERE is_test IS NULL; +UPDATE civicrm_payment_processor_type SET is_active = 1 WHERE is_active IS NULL; +UPDATE civicrm_payment_processor_type SET is_default = 0 WHERE is_default IS NULL; +ALTER TABLE civicrm_payment_processor ALTER COLUMN is_default SET DEFAULT 0; +ALTER TABLE civicrm_payment_processor ALTER COLUMN is_active SET DEFAULT 1; +ALTER TABLE civicrm_payment_processor ALTER COLUMN is_test SET DEFAULT 0; +ALTER TABLE civicrm_payment_processor_type ALTER COLUMN is_active SET DEFAULT 1; +ALTER TABLE civicrm_payment_processor_type ALTER COLUMN is_default SET DEFAULT 0; diff --git a/Civi/Api4/Action/PaymentProcessor/Create.php b/Civi/Api4/Action/PaymentProcessor/Create.php new file mode 100644 index 000000000000..aeaba093bdcb --- /dev/null +++ b/Civi/Api4/Action/PaymentProcessor/Create.php @@ -0,0 +1,43 @@ +addWhere('id', '=', $item['payment_processor_type_id'])->execute(); + foreach ($paymentProcessorTypes as $paymentProcessorType) { + $item['payment_instrument_id'] = $paymentProcessorType['payment_instrument_id']; + } + } + } + return parent::writeObjects($items); + } + +} diff --git a/Civi/Api4/Action/PaymentProcessor/Save.php b/Civi/Api4/Action/PaymentProcessor/Save.php new file mode 100644 index 000000000000..30345529eab4 --- /dev/null +++ b/Civi/Api4/Action/PaymentProcessor/Save.php @@ -0,0 +1,43 @@ +getFieldByName('domain_id')->setRequired(FALSE)->setDefaultValue('current_domain'); + $financial_account_id = new FieldSpec('financial_account_id', 'PaymentProcessor', 'Integer'); + $financial_account_id + ->setTitle('Financial Account ID') + ->setDescription('The financial account that this payment processor is linked to') + ->setRequired(FALSE) + ->setDefaultValue(\CRM_Financial_BAO_PaymentProcessor::getDefaultFinancialAccountID()); + $spec->addFieldSpec($financial_account_id); + } + + /** + * @inheritDoc + */ + public function applies($entity, $action) { + return $entity === 'PaymentProcessor' && in_array($action, ['create']); + } + +} diff --git a/Civi/Api4/Service/Spec/Provider/PaymentProcessorTypeCreationSpecProvider.php b/Civi/Api4/Service/Spec/Provider/PaymentProcessorTypeCreationSpecProvider.php new file mode 100644 index 000000000000..f0ed1edf8767 --- /dev/null +++ b/Civi/Api4/Service/Spec/Provider/PaymentProcessorTypeCreationSpecProvider.php @@ -0,0 +1,56 @@ +getFieldByName('payment_instrument_id')->setRequired(FALSE)->setDefaultValue(1); + } + + /** + * @inheritDoc + */ + public function applies($entity, $action) { + return $entity === 'PaymentProcessorType' && in_array($action, ['create']); + } + +} diff --git a/tests/phpunit/api/v3/PaymentProcessorTest.php b/tests/phpunit/api/v3/PaymentProcessorTest.php index e3c8a713d72f..0ed3b303a824 100644 --- a/tests/phpunit/api/v3/PaymentProcessorTest.php +++ b/tests/phpunit/api/v3/PaymentProcessorTest.php @@ -47,6 +47,7 @@ public function setUp() { /** * Check create with no name specified. + * @dataProvider versionThreeAndFour */ public function testPaymentProcessorCreateWithoutName() { $this->callAPIFailure('payment_processor', 'create', ['is_active' => 1]); @@ -54,6 +55,7 @@ public function testPaymentProcessorCreateWithoutName() { /** * Create payment processor. + * @dataProvider versionThreeAndFour * * @throws \CRM_Core_Exception */ @@ -75,6 +77,7 @@ public function testPaymentProcessorCreate() { /** * Update payment processor. + * @dataProvider versionThreeAndFour * * @throws \CRM_Core_Exception */ @@ -120,6 +123,7 @@ public function testPaymentProcessorCreateExample() { /** * Check payment processor delete. + * @dataProvider versionThreeAndFour * * @throws \CRM_Core_Exception */ @@ -134,6 +138,7 @@ public function testPaymentProcessorDelete() { /** * Check with valid params array. + * @dataProvider versionThreeAndFour * * @throws \CRM_Core_Exception */ diff --git a/tests/phpunit/api/v3/PaymentProcessorTypeTest.php b/tests/phpunit/api/v3/PaymentProcessorTypeTest.php index 9cbd9e5326ab..79cfbd2a6729 100644 --- a/tests/phpunit/api/v3/PaymentProcessorTypeTest.php +++ b/tests/phpunit/api/v3/PaymentProcessorTypeTest.php @@ -22,7 +22,6 @@ public function setUp() { parent::setUp(); $this->useTransaction(TRUE); - $this->_apiversion = 3; } // function tearDown() { @@ -37,6 +36,7 @@ public function setUp() { /** * Check with no name. + * @dataProvider versionThreeAndFour */ public function testPaymentProcessorTypeCreateWithoutName() { $payProcParams = [ @@ -50,6 +50,7 @@ public function testPaymentProcessorTypeCreateWithoutName() { /** * Create payment processor type. + * @dataProvider versionThreeAndFour */ public function testPaymentProcessorTypeCreate() { $params = [ @@ -84,6 +85,7 @@ public function testPaymentProcessorTypeCreateExample() { /** * Check with empty array. + * @dataProvider versionThreeAndFour */ public function testPaymentProcessorTypeDeleteEmpty() { $params = []; @@ -92,6 +94,7 @@ public function testPaymentProcessorTypeDeleteEmpty() { /** * Check if required fields are not passed. + * @dataProvider versionThreeAndFour */ public function testPaymentProcessorTypeDeleteWithoutRequired() { $params = [ @@ -106,6 +109,7 @@ public function testPaymentProcessorTypeDeleteWithoutRequired() { /** * Check with incorrect required fields. + * @dataProvider versionThreeAndFour */ public function testPaymentProcessorTypeDeleteWithIncorrectData() { $result = $this->callAPIFailure('payment_processor_type', 'delete', ['id' => 'abcd']); @@ -113,6 +117,7 @@ public function testPaymentProcessorTypeDeleteWithIncorrectData() { /** * Check payment processor type delete. + * @dataProvider versionThreeAndFour */ public function testPaymentProcessorTypeDelete() { $payProcType = $this->paymentProcessorTypeCreate(); @@ -127,6 +132,7 @@ public function testPaymentProcessorTypeDelete() { /** * Check with empty array. + * @dataProvider versionThreeAndFour */ public function testPaymentProcessorTypeUpdateEmpty() { $params = []; @@ -136,6 +142,7 @@ public function testPaymentProcessorTypeUpdateEmpty() { /** * Check with all parameters. + * @dataProvider versionThreeAndFour */ public function testPaymentProcessorTypeUpdate() { // create sample payment processor type. @@ -161,6 +168,7 @@ public function testPaymentProcessorTypeUpdate() { /** * Check with empty array. + * @dataProvider versionThreeAndFour */ public function testPaymentProcessorTypesGetEmptyParams() { $results = $this->callAPISuccess('payment_processor_type', 'get', []); @@ -192,6 +200,7 @@ public function testPaymentProcessorTypesGetEmptyParams() { /** * Check with valid params array. + * @dataProvider versionThreeAndFour */ public function testPaymentProcessorTypesGet() { $firstRelTypeParams = [ diff --git a/xml/schema/Financial/PaymentProcessorType.xml b/xml/schema/Financial/PaymentProcessorType.xml index 27273978a9a1..b33bea6f9528 100644 --- a/xml/schema/Financial/PaymentProcessorType.xml +++ b/xml/schema/Financial/PaymentProcessorType.xml @@ -22,7 +22,7 @@ Payment Processor variable name to be used in code varchar 64 - Payment Processor Name. + Payment Processor Type Name. 1.8 true