Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 | 16 | */ |
ee44263e | 17 | class CRM_Financial_BAO_PaymentProcessorType extends CRM_Financial_DAO_PaymentProcessorType implements \Civi\Core\HookInterface { |
6a488035 TO |
18 | |
19 | /** | |
fe482240 | 20 | * Static holder for the default payment processor. |
7b966967 | 21 | * @var object |
6a488035 | 22 | */ |
7b966967 | 23 | public static $_defaultPaymentProcessorType = NULL; |
6a488035 | 24 | |
6a488035 | 25 | /** |
4f940304 | 26 | * Retrieve DB object and copy to defaults array. |
6a488035 | 27 | * |
ed5dd492 | 28 | * @param array $params |
4f940304 | 29 | * Array of criteria values. |
ed5dd492 | 30 | * @param array $defaults |
4f940304 | 31 | * Array to be populated with found values. |
6a488035 | 32 | * |
4f940304 CW |
33 | * @return self|null |
34 | * The DAO object, if found. | |
35 | * | |
36 | * @deprecated | |
6a488035 | 37 | */ |
4f940304 CW |
38 | public static function retrieve($params, &$defaults) { |
39 | return self::commonRetrieve(self::class, $params, $defaults); | |
6a488035 TO |
40 | } |
41 | ||
42 | /** | |
fe482240 | 43 | * Update the is_active flag in the db. |
6a488035 | 44 | * |
ed5dd492 TO |
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. | |
6a488035 | 49 | * |
8a4fede3 | 50 | * @return bool |
51 | * true if we found and updated the object, else false | |
6a488035 | 52 | */ |
00be9182 | 53 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
54 | return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_PaymentProcessorType', $id, 'is_active', $is_active); |
55 | } | |
56 | ||
57 | /** | |
fe482240 | 58 | * Retrieve the default payment processor. |
6a488035 | 59 | * |
a6c01b45 CW |
60 | * @return object |
61 | * The default payment processor object on success, | |
6a488035 | 62 | * null otherwise |
6a488035 | 63 | */ |
00be9182 | 64 | public static function &getDefault() { |
6a488035 | 65 | if (self::$_defaultPaymentProcessorType == NULL) { |
be2fb01f CW |
66 | $params = ['is_default' => 1]; |
67 | $defaults = []; | |
6a488035 TO |
68 | self::$_defaultPaymentProcessorType = self::retrieve($params, $defaults); |
69 | } | |
70 | return self::$_defaultPaymentProcessorType; | |
71 | } | |
72 | ||
73 | /** | |
100fef9d | 74 | * Add the payment-processor type in the db |
6a488035 | 75 | * |
ed5dd492 TO |
76 | * @param array $params |
77 | * (reference ) an assoc array of name/value pairs. | |
77b97be7 EM |
78 | * |
79 | * @throws Exception | |
c490a46a | 80 | * @return CRM_Financial_DAO_PaymentProcessorType |
6a488035 | 81 | */ |
00be9182 | 82 | public static function create(&$params) { |
6a488035 TO |
83 | $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType(); |
84 | $paymentProcessorType->copyValues($params); | |
85 | ||
006389de | 86 | /* @codingStandardsIgnoreStart |
6a488035 TO |
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' | |
006389de | 108 | ) as $trimmable) { |
6a488035 TO |
109 | if (isset($paymentProcessorType->{$trimmable})) { |
110 | $paymentProcessorType->{$trimmable} = trim($paymentProcessorType->{$trimmable}); | |
111 | } | |
112 | } | |
006389de | 113 | @codingStandardsIgnoreEnd */ |
6a488035 TO |
114 | |
115 | if (isset($paymentProcessorType->billing_mode)) { | |
116 | // ugh unidirectional manipulation | |
117 | if (!is_numeric($paymentProcessorType->billing_mode)) { | |
5973d43c | 118 | $billingModes = array_flip(self::buildOptions('billing_mode')); |
6a488035 TO |
119 | if (array_key_exists($paymentProcessorType->billing_mode, $billingModes)) { |
120 | $paymentProcessorType->billing_mode = $billingModes[$paymentProcessorType->billing_mode]; | |
121 | } | |
122 | } | |
5973d43c | 123 | if (!array_key_exists($paymentProcessorType->billing_mode, self::buildOptions('billing_mode'))) { |
6a488035 TO |
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) { | |
4cf017d1 | 133 | throw new CRM_Core_Exception('This payment processor type already exists.'); |
6a488035 TO |
134 | } |
135 | } | |
136 | } | |
137 | ||
138 | return $paymentProcessorType->save(); | |
139 | } | |
140 | ||
141 | /** | |
fe482240 | 142 | * Delete payment processor. |
6a488035 | 143 | * |
ed5dd492 | 144 | * @param int $paymentProcessorTypeId |
921ad3b3 | 145 | * @deprecated |
ae5ffbb7 | 146 | * @return bool|NULL |
6a488035 | 147 | */ |
00be9182 | 148 | public static function del($paymentProcessorTypeId) { |
921ad3b3 CW |
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 = " | |
6a488035 TO |
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 | ||
921ad3b3 CW |
173 | $params = [1 => [$event->id, 'Integer']]; |
174 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
6a488035 | 175 | |
921ad3b3 CW |
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 | } | |
6a488035 TO |
179 | } |
180 | } | |
181 | ||
e0ef6999 | 182 | /** |
fa3fdebc | 183 | * @param string $attr |
e0ef6999 EM |
184 | * |
185 | * @return array | |
186 | */ | |
7b966967 | 187 | private static function getAllPaymentProcessorTypes($attr) { |
be2fb01f | 188 | $ppt = []; |
6a488035 TO |
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 | } | |
5973d43c | 196 | |
6a488035 | 197 | } |