Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement PaymentProcessor and PaymentProcessorType APIv4 Entities #15624

Merged
merged 1 commit into from
Jan 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
27 changes: 27 additions & 0 deletions Civi/Api4/PaymentProcessor.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 entity.
*
* @package Civi\Api4
*/
class PaymentProcessor extends Generic\DAOEntity {

}
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,52 @@
<?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');
// Billing mode is copied across from the payment processor type field in the BAO::create function.
$spec->getFieldByName('billing_mode')->setRequired(FALSE);

$financial_account_id = new FieldSpec('financial_account_id', 'PaymentProcessor', 'Integer');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eileenmcnaughton this may or may not be useful for you in understanding how to create pseudo fields in APIv4

$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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colemanw i tried using 'Credit Card' as the value but didn't seem to work right

}

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

}
28 changes: 23 additions & 5 deletions tests/phpunit/api/v3/PaymentProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,21 @@ public function setUp() {

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

/**
* Create payment processor.
* @dataProvider versionThreeAndFour
*
* @throws \CRM_Core_Exception
*/
public function testPaymentProcessorCreate() {
public function testPaymentProcessorCreate($version) {
$this->_apiversion = $version;
$params = $this->_params;
$result = $this->callAPIAndDocument('payment_processor', 'create', $params, __FUNCTION__, __FILE__);
$this->callAPISuccessGetSingle('EntityFinancialAccount', ['entity_table' => 'civicrm_payment_processor', 'entity_id' => $result['id']]);
Expand All @@ -77,10 +81,12 @@ public function testPaymentProcessorCreate() {

/**
* Update payment processor.
* @dataProvider versionThreeAndFour
*
* @throws \CRM_Core_Exception
*/
public function testPaymentProcessorUpdate() {
public function testPaymentProcessorUpdate($version) {
$this->_apiversion = $version;
$params = $this->_params;
$params['payment_instrument_id'] = 1;
$result = $this->callAPISuccess('payment_processor', 'create', $params);
Expand Down Expand Up @@ -108,6 +114,14 @@ public function testPaymentProcessorUpdate() {
'payment_instrument_id' => 1,
'is_active' => 1,
];
if ($version === 4) {
// In APIv3 If a field is default NULL it is not returned.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colemanw not sure if this is deliberate but it seems a noticeable change between the API versions, i assume it is documented somewhere?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is deliberate.
civicrm/org.civicrm.api4#116

You could simplify this patch by deleting NULL values from the api result instead of adding them to the expected result :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok have updated based on your suggestion @colemanw

foreach ($result['values'][$result['id']] as $field => $value) {
if (is_null($value)) {
unset($result['values'][$result['id']][$field]);
}
}
}
$this->checkArrayEquals($expectedResult, $result['values'][$result['id']]);
}

Expand All @@ -123,10 +137,12 @@ public function testPaymentProcessorCreateExample() {

/**
* Check payment processor delete.
* @dataProvider versionThreeAndFour
*
* @throws \CRM_Core_Exception
*/
public function testPaymentProcessorDelete() {
public function testPaymentProcessorDelete($version) {
$this->_apiversion = $version;
$result = $this->callAPISuccess('payment_processor', 'create', $this->_params);
$params = [
'id' => $result['id'],
Expand All @@ -137,10 +153,12 @@ public function testPaymentProcessorDelete() {

/**
* Check with valid params array.
* @dataProvider versionThreeAndFour
seamuslee001 marked this conversation as resolved.
Show resolved Hide resolved
*
* @throws \CRM_Core_Exception
*/
public function testPaymentProcessorsGet() {
public function testPaymentProcessorsGet($version) {
$this->_apiversion = $version;
$params = $this->_params;
$params['user_name'] = 'test@test.com';
$this->callAPISuccess('payment_processor', 'create', $params);
Expand Down
Loading