Skip to content

Commit

Permalink
Implement PaymentProcessor and PaymentProcessorType APIv4 Entities
Browse files Browse the repository at this point in the history
Move default values to spec as per Coleman

Fix class name in tests and fix default values on tables

Mock financial_account_id field

Fix headers and remove some unnessary code
  • Loading branch information
seamuslee001 committed Jan 10, 2020
1 parent 9c99288 commit 985815f
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CRM/Upgrade/Incremental/sql/5.23.alpha1.mysql.tpl
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{* file to handle db changes in 5.23.alpha1 during upgrade *}
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;
24 changes: 24 additions & 0 deletions Civi/Api4/Action/PaymentProcessor/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

namespace Civi\Api4\Action\PaymentProcessor;

class Create extends \Civi\Api4\Generic\DAOCreateAction {
use PaymentProcessorSaveTrait;

}
41 changes: 41 additions & 0 deletions Civi/Api4/Action/PaymentProcessor/PaymentProcessorSaveTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

namespace Civi\Api4\Action\PaymentProcessor;

/**
* @inheritDoc
*/
trait PaymentProcessorSaveTrait {

/**
* @inheritDoc
*/
protected function writeObjects($items) {
foreach ($items as &$item) {
if (empty($item['payment_instrument_id'])) {
$paymentProcessorTypes = \Civi\Api4\PaymentProcessorType::get()->addWhere('id', '=', $item['payment_processor_type_id'])->execute();
foreach ($paymentProcessorTypes as $paymentProcessorType) {
$item['payment_instrument_id'] = $paymentProcessorType['payment_instrument_id'];
}
}
}
return parent::writeObjects($items);
}

}
23 changes: 23 additions & 0 deletions Civi/Api4/Action/PaymentProcessor/Save.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

namespace Civi\Api4\Action\PaymentProcessor;

class Save extends \Civi\Api4\Generic\DAOSaveAction {
use PaymentProcessorSaveTrait;

}
41 changes: 41 additions & 0 deletions Civi/Api4/PaymentProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

namespace Civi\Api4;

/**
* Payment Processor entity.
*
* @package Civi\Api4
*/
class PaymentProcessor extends Generic\DAOEntity {

/**
* @return \Civi\Api4\Action\PaymentProcessor\Create
*/
public static function create() {
return new \Civi\Api4\Action\PaymentProcessor\Create(__CLASS__, __FUNCTION__);
}

/**
* @return \Civi\Api4\Action\PaymentProcessor\Save
*/
public static function save() {
return new \Civi\Api4\Action\PaymentProcessor\Save(__CLASS__, __FUNCTION__);
}

}
27 changes: 27 additions & 0 deletions Civi/Api4/PaymentProcessorType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

namespace Civi\Api4;

/**
* Payment Processor Type entity.
*
* @package Civi\Api4
*/
class PaymentProcessorType extends Generic\DAOEntity {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

namespace Civi\Api4\Service\Spec\Provider;

use Civi\Api4\Service\Spec\FieldSpec;
use Civi\Api4\Service\Spec\RequestSpec;

class PaymentProcessorCreationSpecProvider implements Generic\SpecProviderInterface {

/**
* This runs for both create and get actions
*
* @inheritDoc
*/
public function modifySpec(RequestSpec $spec) {
$spec->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())
->setFkEntity('FinancialAccount');
$spec->addFieldSpec($financial_account_id);
}

/**
* @inheritDoc
*/
public function applies($entity, $action) {
return $entity === 'PaymentProcessor' && in_array($action, ['create']);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2019 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2019
* $Id$
*
*/
namespace Civi\Api4\Service\Spec\Provider;

use Civi\Api4\Service\Spec\RequestSpec;

class PaymentProcessorTypeCreationSpecProvider implements Generic\SpecProviderInterface {

/**
* This runs for both create and get actions
*
* @inheritDoc
*/
public function modifySpec(RequestSpec $spec) {
$spec->getFieldByName('payment_instrument_id')->setRequired(FALSE)->setDefaultValue(1);
}

/**
* @inheritDoc
*/
public function applies($entity, $action) {
return $entity === 'PaymentProcessorType' && in_array($action, ['create']);
}

}
5 changes: 5 additions & 0 deletions tests/phpunit/api/v3/PaymentProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ public function setUp() {

/**
* Check create with no name specified.
* @dataProvider versionThreeAndFour
*/
public function testPaymentProcessorCreateWithoutName() {
$this->callAPIFailure('payment_processor', 'create', ['is_active' => 1]);
}

/**
* Create payment processor.
* @dataProvider versionThreeAndFour
*
* @throws \CRM_Core_Exception
*/
Expand All @@ -75,6 +77,7 @@ public function testPaymentProcessorCreate() {

/**
* Update payment processor.
* @dataProvider versionThreeAndFour
*
* @throws \CRM_Core_Exception
*/
Expand Down Expand Up @@ -120,6 +123,7 @@ public function testPaymentProcessorCreateExample() {

/**
* Check payment processor delete.
* @dataProvider versionThreeAndFour
*
* @throws \CRM_Core_Exception
*/
Expand All @@ -134,6 +138,7 @@ public function testPaymentProcessorDelete() {

/**
* Check with valid params array.
* @dataProvider versionThreeAndFour
*
* @throws \CRM_Core_Exception
*/
Expand Down
Loading

0 comments on commit 985815f

Please sign in to comment.