Merge pull request #12818 from mattwire/contributionsearch_byrecurtrxnprocessor
[civicrm-core.git] / CRM / Financial / BAO / PaymentProcessorType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33class CRM_Financial_BAO_PaymentProcessorType extends CRM_Financial_DAO_PaymentProcessorType {
34
35 /**
fe482240 36 * Static holder for the default payment processor.
6a488035
TO
37 */
38 static $_defaultPaymentProcessorType = NULL;
39
40 /**
fe482240 41 * Class constructor.
6a488035 42 */
00be9182 43 public function __construct() {
6a488035
TO
44 parent::__construct();
45 }
46
47 /**
fe482240 48 * Fetch object based on array of properties.
6a488035 49 *
ed5dd492
TO
50 * @param array $params
51 * (reference ) an assoc array of name/value pairs.
52 * @param array $defaults
53 * (reference ) an assoc array to hold the flattened values.
6a488035 54 *
cded2ebf 55 * @return CRM_Core_BAO_LocationType|null
16b10e64 56 * object on success, null otherwise
6a488035 57 */
00be9182 58 public static function retrieve(&$params, &$defaults) {
6a488035
TO
59 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
60 $paymentProcessorType->copyValues($params);
61 if ($paymentProcessorType->find(TRUE)) {
62 CRM_Core_DAO::storeValues($paymentProcessorType, $defaults);
63 return $paymentProcessorType;
64 }
65 return NULL;
66 }
67
68 /**
fe482240 69 * Update the is_active flag in the db.
6a488035 70 *
ed5dd492
TO
71 * @param int $id
72 * Id of the database record.
73 * @param bool $is_active
74 * Value we want to set the is_active field.
6a488035 75 *
8a4fede3 76 * @return bool
77 * true if we found and updated the object, else false
6a488035 78 */
00be9182 79 public static function setIsActive($id, $is_active) {
6a488035
TO
80 return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_PaymentProcessorType', $id, 'is_active', $is_active);
81 }
82
83 /**
fe482240 84 * Retrieve the default payment processor.
6a488035 85 *
a6c01b45
CW
86 * @return object
87 * The default payment processor object on success,
6a488035 88 * null otherwise
6a488035 89 */
00be9182 90 public static function &getDefault() {
6a488035
TO
91 if (self::$_defaultPaymentProcessorType == NULL) {
92 $params = array('is_default' => 1);
93 $defaults = array();
94 self::$_defaultPaymentProcessorType = self::retrieve($params, $defaults);
95 }
96 return self::$_defaultPaymentProcessorType;
97 }
98
99 /**
100fef9d 100 * Add the payment-processor type in the db
6a488035 101 *
ed5dd492
TO
102 * @param array $params
103 * (reference ) an assoc array of name/value pairs.
77b97be7
EM
104 *
105 * @throws Exception
c490a46a 106 * @return CRM_Financial_DAO_PaymentProcessorType
6a488035 107 */
00be9182 108 public static function create(&$params) {
6a488035
TO
109 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
110 $paymentProcessorType->copyValues($params);
111
006389de 112 /* @codingStandardsIgnoreStart
6a488035
TO
113 // adapted from CRM_Core_Extensions_Payment::install
114 foreach (array(
115 'class_name',
116 'title',
117 'name',
118 'description',
119 'user_name_label',
120 'password_label',
121 'signature_label',
122 'subject_label',
123 'url_site_default',
124 'url_api_default',
125 'url_recur_default',
126 'url_site_test_default',
127 'url_api_test_default',
128 'url_recur_test_default',
129 'url_button_default',
130 'url_button_test_default',
131 'billing_mode',
132 'is_recur',
133 'payment_type'
006389de 134 ) as $trimmable) {
6a488035
TO
135 if (isset($paymentProcessorType->{$trimmable})) {
136 $paymentProcessorType->{$trimmable} = trim($paymentProcessorType->{$trimmable});
137 }
138 }
006389de 139 @codingStandardsIgnoreEnd */
6a488035
TO
140
141 if (isset($paymentProcessorType->billing_mode)) {
142 // ugh unidirectional manipulation
143 if (!is_numeric($paymentProcessorType->billing_mode)) {
5973d43c 144 $billingModes = array_flip(self::buildOptions('billing_mode'));
6a488035
TO
145 if (array_key_exists($paymentProcessorType->billing_mode, $billingModes)) {
146 $paymentProcessorType->billing_mode = $billingModes[$paymentProcessorType->billing_mode];
147 }
148 }
5973d43c 149 if (!array_key_exists($paymentProcessorType->billing_mode, self::buildOptions('billing_mode'))) {
6a488035
TO
150 throw new Exception("Unrecognized billing_mode");
151 }
152 }
153
154 // FIXME handle is_default
155 if (!empty($paymentProcessorType->id)) {
156 $ppByName = self::getAllPaymentProcessorTypes('name');
157 if (array_key_exists($paymentProcessorType->name, $ppByName)) {
158 if ($ppByName[$paymentProcessorType->name] != $paymentProcessorType->id) {
159 CRM_Core_Error::fatal('This payment processor type already exists.');
160 }
161 }
162 }
163
164 return $paymentProcessorType->save();
165 }
166
167 /**
fe482240 168 * Delete payment processor.
6a488035 169 *
ed5dd492
TO
170 * @param int $paymentProcessorTypeId
171 * ID of the processor to be deleted.
6a488035 172 *
ae5ffbb7 173 * @return bool|NULL
6a488035 174 */
00be9182 175 public static function del($paymentProcessorTypeId) {
6a488035
TO
176 $query = "
177SELECT pp.id processor_id
178FROM civicrm_payment_processor pp, civicrm_payment_processor_type ppt
179WHERE pp.payment_processor_type_id = ppt.id AND ppt.id = %1";
180
181 $params = array(1 => array($paymentProcessorTypeId, 'Integer'));
182 $dao = CRM_Core_DAO::executeQuery($query, $params);
183
184 if ($dao->fetch()) {
185 CRM_Core_Session::setStatus(ts('There is a Payment Processor associated with selected Payment Processor type, hence it can not be deleted.'), ts('Deletion Error'), 'error');
ae5ffbb7 186 return NULL;
6a488035
TO
187 }
188
189 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
190 $paymentProcessorType->id = $paymentProcessorTypeId;
191 if ($paymentProcessorType->delete()) {
d3e86119 192 CRM_Core_Session::setStatus(ts('Selected Payment Processor type has been deleted.<br/>'), '', 'success');
6a488035
TO
193 return TRUE;
194 }
195 }
196
e0ef6999
EM
197 /**
198 * @param $attr
199 *
200 * @return array
201 */
6a488035
TO
202 static private function getAllPaymentProcessorTypes($attr) {
203 $ppt = array();
204 $dao = new CRM_Financial_DAO_PaymentProcessorType();
205 $dao->find();
206 while ($dao->fetch()) {
207 $ppt[$dao->$attr] = $dao->id;
208 }
209 return $ppt;
210 }
5973d43c 211
6a488035 212}