Merge pull request #23009 from seamuslee001/dev_core_3132
[civicrm-core.git] / CRM / Financial / BAO / PaymentProcessorType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Financial_BAO_PaymentProcessorType extends CRM_Financial_DAO_PaymentProcessorType implements \Civi\Core\HookInterface {
18
19 /**
20 * Static holder for the default payment processor.
21 * @var object
22 */
23 public static $_defaultPaymentProcessorType = NULL;
24
25 /**
26 * Retrieve DB object and copy to defaults array.
27 *
28 * @param array $params
29 * Array of criteria values.
30 * @param array $defaults
31 * Array to be populated with found values.
32 *
33 * @return self|null
34 * The DAO object, if found.
35 *
36 * @deprecated
37 */
38 public static function retrieve($params, &$defaults) {
39 return self::commonRetrieve(self::class, $params, $defaults);
40 }
41
42 /**
43 * Update the is_active flag in the db.
44 *
45 * @param int $id
46 * Id of the database record.
47 * @param bool $is_active
48 * Value we want to set the is_active field.
49 *
50 * @return bool
51 * true if we found and updated the object, else false
52 */
53 public static function setIsActive($id, $is_active) {
54 return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_PaymentProcessorType', $id, 'is_active', $is_active);
55 }
56
57 /**
58 * Retrieve the default payment processor.
59 *
60 * @return object
61 * The default payment processor object on success,
62 * null otherwise
63 */
64 public static function &getDefault() {
65 if (self::$_defaultPaymentProcessorType == NULL) {
66 $params = ['is_default' => 1];
67 $defaults = [];
68 self::$_defaultPaymentProcessorType = self::retrieve($params, $defaults);
69 }
70 return self::$_defaultPaymentProcessorType;
71 }
72
73 /**
74 * Add the payment-processor type in the db
75 *
76 * @param array $params
77 * (reference ) an assoc array of name/value pairs.
78 *
79 * @throws Exception
80 * @return CRM_Financial_DAO_PaymentProcessorType
81 */
82 public static function create(&$params) {
83 $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType();
84 $paymentProcessorType->copyValues($params);
85
86 /* @codingStandardsIgnoreStart
87 // adapted from CRM_Core_Extensions_Payment::install
88 foreach (array(
89 'class_name',
90 'title',
91 'name',
92 'description',
93 'user_name_label',
94 'password_label',
95 'signature_label',
96 'subject_label',
97 'url_site_default',
98 'url_api_default',
99 'url_recur_default',
100 'url_site_test_default',
101 'url_api_test_default',
102 'url_recur_test_default',
103 'url_button_default',
104 'url_button_test_default',
105 'billing_mode',
106 'is_recur',
107 'payment_type'
108 ) as $trimmable) {
109 if (isset($paymentProcessorType->{$trimmable})) {
110 $paymentProcessorType->{$trimmable} = trim($paymentProcessorType->{$trimmable});
111 }
112 }
113 @codingStandardsIgnoreEnd */
114
115 if (isset($paymentProcessorType->billing_mode)) {
116 // ugh unidirectional manipulation
117 if (!is_numeric($paymentProcessorType->billing_mode)) {
118 $billingModes = array_flip(self::buildOptions('billing_mode'));
119 if (array_key_exists($paymentProcessorType->billing_mode, $billingModes)) {
120 $paymentProcessorType->billing_mode = $billingModes[$paymentProcessorType->billing_mode];
121 }
122 }
123 if (!array_key_exists($paymentProcessorType->billing_mode, self::buildOptions('billing_mode'))) {
124 throw new Exception("Unrecognized billing_mode");
125 }
126 }
127
128 // FIXME handle is_default
129 if (!empty($paymentProcessorType->id)) {
130 $ppByName = self::getAllPaymentProcessorTypes('name');
131 if (array_key_exists($paymentProcessorType->name, $ppByName)) {
132 if ($ppByName[$paymentProcessorType->name] != $paymentProcessorType->id) {
133 throw new CRM_Core_Exception('This payment processor type already exists.');
134 }
135 }
136 }
137
138 return $paymentProcessorType->save();
139 }
140
141 /**
142 * Delete payment processor.
143 *
144 * @param int $paymentProcessorTypeId
145 * @deprecated
146 * @return bool|NULL
147 */
148 public static function del($paymentProcessorTypeId) {
149 try {
150 static::deleteRecord(['id' => $paymentProcessorTypeId]);
151 // This message is bad on so many levels
152 CRM_Core_Session::setStatus(ts('Selected Payment Processor type has been deleted.<br/>'), '', 'success');
153 return TRUE;
154 }
155 catch (CRM_Core_Exception $e) {
156 CRM_Core_Session::setStatus($e->getMessage(), ts('Deletion Error'), 'error');
157 return NULL;
158 }
159 }
160
161 /**
162 * Callback for hook_civicrm_pre().
163 * @param \Civi\Core\Event\PreEvent $event
164 * @throws CRM_Core_Exception
165 */
166 public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) {
167 if ($event->action === 'delete') {
168 $query = "
169 SELECT pp.id processor_id
170 FROM civicrm_payment_processor pp, civicrm_payment_processor_type ppt
171 WHERE pp.payment_processor_type_id = ppt.id AND ppt.id = %1";
172
173 $params = [1 => [$event->id, 'Integer']];
174 $dao = CRM_Core_DAO::executeQuery($query, $params);
175
176 if ($dao->fetch()) {
177 throw new CRM_Core_Exception(ts('There is a Payment Processor associated with selected Payment Processor type, hence it can not be deleted.'));
178 }
179 }
180 }
181
182 /**
183 * @param string $attr
184 *
185 * @return array
186 */
187 private static function getAllPaymentProcessorTypes($attr) {
188 $ppt = [];
189 $dao = new CRM_Financial_DAO_PaymentProcessorType();
190 $dao->find();
191 while ($dao->fetch()) {
192 $ppt[$dao->$attr] = $dao->id;
193 }
194 return $ppt;
195 }
196
197 }