Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class contains payment processor related functions. | |
38 | */ | |
39 | class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProcessor | |
40 | { | |
41 | /** | |
42 | * static holder for the default payment processor | |
43 | */ | |
44 | static $_defaultPaymentProcessor = NULL; | |
45 | ||
46 | /* | |
47 | * Create Payment Processor | |
48 | * | |
49 | * @params array parameters for Processor entity | |
50 | */ | |
e0ef6999 EM |
51 | /** |
52 | * @param $params | |
53 | * | |
54 | * @return CRM_Financial_DAO_PaymentProcessor | |
55 | * @throws Exception | |
56 | */ | |
6a488035 TO |
57 | static function create($params) { |
58 | // FIXME Reconcile with CRM_Admin_Form_PaymentProcessor::updatePaymentProcessor | |
59 | $processor = new CRM_Financial_DAO_PaymentProcessor(); | |
60 | $processor->copyValues($params); | |
61 | ||
62 | $ppTypeDAO = new CRM_Financial_DAO_PaymentProcessorType(); | |
63 | $ppTypeDAO->id = $params['payment_processor_type_id']; | |
64 | if (!$ppTypeDAO->find(TRUE)) { | |
65 | CRM_Core_Error::fatal(ts('Could not find payment processor meta information')); | |
66 | } | |
67 | ||
68 | // also copy meta fields from the info DAO | |
69 | $processor->is_recur = $ppTypeDAO->is_recur; | |
70 | $processor->billing_mode = $ppTypeDAO->billing_mode; | |
71 | $processor->class_name = $ppTypeDAO->class_name; | |
72 | $processor->payment_type = $ppTypeDAO->payment_type; | |
73 | ||
74 | $processor->save(); | |
03e04002 | 75 | // CRM-11826, add entry in civicrm_entity_financial_account |
6a488035 | 76 | // if financial_account_id is not NULL |
a7488080 | 77 | if (!empty($params['financial_account_id'])) { |
f743a6eb | 78 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' ")); |
6a488035 TO |
79 | $values = array( |
80 | 'entity_table' => 'civicrm_payment_processor', | |
81 | 'entity_id' => $processor->id, | |
82 | 'account_relationship' => $relationTypeId, | |
83 | 'financial_account_id' => $params['financial_account_id'] | |
84 | ); | |
03e04002 | 85 | CRM_Financial_BAO_FinancialTypeAccount::add($values); |
6a488035 TO |
86 | } |
87 | return $processor; | |
88 | } | |
89 | ||
90 | /** | |
91 | * class constructor | |
92 | */ | |
93 | function __construct() { | |
94 | parent::__construct(); | |
95 | } | |
96 | ||
97 | /** | |
98 | * Takes a bunch of params that are needed to match certain criteria and | |
99 | * retrieves the relevant objects. It also stores all the retrieved | |
100 | * values in the default array | |
101 | * | |
102 | * @param array $params (reference ) an assoc array of name/value pairs | |
103 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
104 | * | |
105 | * @return object CRM_Financial_DAO_PaymentProcessor object on success, null otherwise | |
106 | * @access public | |
107 | * @static | |
108 | */ | |
109 | static function retrieve(&$params, &$defaults) { | |
110 | $paymentProcessor = new CRM_Financial_DAO_PaymentProcessor(); | |
111 | $paymentProcessor->copyValues($params); | |
112 | if ($paymentProcessor->find(TRUE)) { | |
113 | CRM_Core_DAO::storeValues($paymentProcessor, $defaults); | |
114 | return $paymentProcessor; | |
115 | } | |
116 | return NULL; | |
117 | } | |
118 | ||
119 | /** | |
120 | * update the is_active flag in the db | |
121 | * | |
122 | * @param int $id id of the database record | |
123 | * @param boolean $is_active value we want to set the is_active field | |
124 | * | |
125 | * @return Object DAO object on sucess, null otherwise | |
126 | * | |
127 | * @access public | |
128 | * @static | |
129 | */ | |
130 | static function setIsActive($id, $is_active) { | |
131 | return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_PaymentProcessor', $id, 'is_active', $is_active); | |
132 | } | |
133 | ||
134 | /** | |
135 | * retrieve the default payment processor | |
136 | * | |
137 | * @param NULL | |
138 | * | |
139 | * @return object The default payment processor object on success, | |
140 | * null otherwise | |
141 | * @static | |
142 | * @access public | |
143 | */ | |
144 | static function &getDefault() { | |
145 | if (self::$_defaultPaymentProcessor == NULL) { | |
146 | $params = array('is_default' => 1); | |
147 | $defaults = array(); | |
148 | self::$_defaultPaymentProcessor = self::retrieve($params, $defaults); | |
149 | } | |
150 | return self::$_defaultPaymentProcessor; | |
151 | } | |
152 | ||
153 | /** | |
154 | * Function to delete payment processor | |
155 | * | |
77b97be7 EM |
156 | * @param $paymentProcessorID |
157 | * | |
158 | * @return null | |
159 | * @internal param int $paymentProcessorId ID of the processor to be deleted. | |
6a488035 TO |
160 | * |
161 | * @access public | |
162 | * @static | |
163 | */ | |
164 | static function del($paymentProcessorID) { | |
165 | if (!$paymentProcessorID) { | |
166 | CRM_Core_Error::fatal(ts('Invalid value passed to delete function')); | |
167 | } | |
168 | ||
169 | $dao = new CRM_Financial_DAO_PaymentProcessor(); | |
170 | $dao->id = $paymentProcessorID; | |
171 | if (!$dao->find(TRUE)) { | |
172 | return NULL; | |
173 | } | |
174 | ||
175 | $testDAO = new CRM_Financial_DAO_PaymentProcessor(); | |
176 | $testDAO->name = $dao->name; | |
177 | $testDAO->is_test = 1; | |
178 | $testDAO->delete(); | |
179 | ||
180 | $dao->delete(); | |
181 | } | |
182 | ||
183 | /** | |
184 | * Function to get the payment processor details | |
185 | * | |
186 | * @param int $paymentProcessorID payment processor id | |
187 | * @param string $mode payment mode ie test or live | |
188 | * | |
189 | * @return array associated array with payment processor related fields | |
190 | * @static | |
191 | * @access public | |
192 | */ | |
193 | static function getPayment($paymentProcessorID, $mode) { | |
194 | if (!$paymentProcessorID) { | |
195 | CRM_Core_Error::fatal(ts('Invalid value passed to getPayment function')); | |
196 | } | |
197 | ||
198 | $dao = new CRM_Financial_DAO_PaymentProcessor( ); | |
199 | $dao->id = $paymentProcessorID; | |
200 | $dao->is_active = 1; | |
201 | if (!$dao->find(TRUE)) { | |
202 | return NULL; | |
203 | } | |
204 | ||
205 | if ($mode == 'test') { | |
206 | $testDAO = new CRM_Financial_DAO_PaymentProcessor( ); | |
207 | $testDAO->name = $dao->name; | |
208 | $testDAO->is_active = 1; | |
209 | $testDAO->is_test = 1; | |
210 | if (!$testDAO->find(TRUE)) { | |
211 | CRM_Core_Error::fatal(ts('Could not retrieve payment processor details')); | |
212 | } | |
213 | return self::buildPayment($testDAO, $mode); | |
214 | } | |
215 | else { | |
216 | return self::buildPayment($dao, $mode); | |
217 | } | |
218 | } | |
219 | ||
e0ef6999 EM |
220 | /** |
221 | * @param $paymentProcessorIDs | |
222 | * @param $mode | |
223 | * | |
224 | * @return array | |
225 | * @throws Exception | |
226 | */ | |
6a488035 TO |
227 | static function getPayments($paymentProcessorIDs, $mode) { |
228 | if (!$paymentProcessorIDs) { | |
229 | CRM_Core_Error::fatal(ts('Invalid value passed to getPayment function')); | |
230 | } | |
231 | ||
232 | $payments = array( ); | |
233 | foreach ($paymentProcessorIDs as $paymentProcessorID) { | |
234 | $payment = self::getPayment($paymentProcessorID, $mode); | |
235 | $payments[$payment['id']] = $payment; | |
236 | } | |
237 | ||
13ac605f | 238 | uasort($payments, 'self::defaultComparison'); |
6a488035 TO |
239 | return $payments; |
240 | } | |
241 | ||
13ac605f DG |
242 | /** |
243 | * compare 2 payment processors to see which should go first based on is_default | |
244 | * (sort function for sortDefaultFirst) | |
245 | * @param array $processor1 | |
246 | * @param array_type $processor2 | |
247 | * @return number | |
248 | */ | |
249 | static function defaultComparison($processor1, $processor2){ | |
250 | $p1 = CRM_Utils_Array::value('is_default', $processor1); | |
251 | $p2 = CRM_Utils_Array::value('is_default', $processor2); | |
252 | if ($p1 == $p2) { | |
253 | return 0; | |
254 | } | |
255 | return ($p1 > $p2) ? -1 : 1; | |
256 | } | |
257 | ||
6a488035 TO |
258 | /** |
259 | * Function to build payment processor details | |
260 | * | |
261 | * @param object $dao payment processor object | |
262 | * @param string $mode payment mode ie test or live | |
263 | * | |
264 | * @return array associated array with payment processor related fields | |
265 | * @static | |
266 | * @access public | |
267 | */ | |
268 | static function buildPayment($dao, $mode) { | |
269 | $fields = array( | |
270 | 'id', 'name', 'payment_processor_type_id', 'user_name', 'password', | |
271 | 'signature', 'url_site', 'url_api', 'url_recur', 'url_button', | |
272 | 'subject', 'class_name', 'is_recur', 'billing_mode', | |
273 | 'payment_type', 'is_default', | |
274 | ); | |
275 | $result = array(); | |
276 | foreach ($fields as $name) { | |
277 | $result[$name] = $dao->$name; | |
278 | } | |
279 | $result['payment_processor_type'] = CRM_Core_PseudoConstant::paymentProcessorType(FALSE, $dao->payment_processor_type_id, 'name'); | |
280 | ||
281 | $result['instance'] =& CRM_Core_Payment::singleton($mode, $result); | |
282 | ||
283 | return $result; | |
284 | } | |
285 | ||
fbcb6fba EM |
286 | /** |
287 | * get all payment processors as an array of objects. | |
288 | * | |
289 | * @param $isExcludeTest | |
290 | * @param bool $reset | |
291 | * | |
292 | * @throws CiviCRM_API3_Exception | |
293 | * @return array | |
294 | */ | |
295 | static function getAllPaymentProcessors($isExcludeTest, $reset = FALSE) { | |
296 | /** | |
297 | * $cacheKey = 'CRM_Financial_BAO_Payment_Processor_' . ($isExcludeTest ? 'test' : 'all'); | |
298 | if (!$reset) { | |
299 | $processors = CRM_Utils_Cache::singleton()->get($cacheKey); | |
300 | if (!empty($processors)) { | |
301 | return $processors; | |
302 | } | |
303 | } | |
304 | * */ | |
305 | $retrievalParameters = array('is_active' => TRUE, 'options' => array('sort' => 'is_default, name')); | |
306 | if ($isExcludeTest) { | |
307 | $retrievalParameters['is_test'] = 0; | |
308 | } | |
309 | $processors = civicrm_api3('payment_processor', 'get', $retrievalParameters); | |
310 | foreach ($processors['values'] as $processor) { | |
311 | $processors['values'][$processor['id']]['object'] = CRM_Core_Payment::singleton(empty($processor['is_test']) ? 'live' : 'test', $processor); | |
312 | } | |
313 | /* | |
314 | CRM_Utils_Cache::singleton()->set($cacheKey, $processors); | |
315 | */ | |
316 | return $processors['values']; | |
317 | } | |
318 | ||
319 | /** | |
320 | * get Payment processors with specified capabilities. | |
321 | * Note that both the singleton & the pseudoconstant function have caching so we don't add | |
322 | * arguably this could go on the pseudoconstant class | |
323 | * | |
324 | * @param array $capabilities | |
325 | * @param bool $isIncludeTest | |
326 | * | |
327 | * @return array available processors | |
328 | */ | |
329 | static function getPaymentProcessors($capabilities = array(), $isIncludeTest = FALSE, $reset = FALSE) { | |
330 | $processors = self::getAllPaymentProcessors(!$isIncludeTest); | |
331 | if ($capabilities) { | |
332 | foreach ($processors as $index => $processor) { | |
333 | if (($error = $processor['object']->checkConfig()) != NULL) { | |
334 | unset ($processors[$index]); | |
335 | continue; | |
336 | } | |
337 | foreach ($capabilities as $capability) { | |
338 | if ($capability && !$processor['object']->supports($capability)) { | |
339 | unset ($processors[$index]); | |
340 | } | |
341 | } | |
342 | } | |
343 | } | |
344 | return $processors; | |
345 | } | |
346 | ||
347 | /** | |
348 | * Is there a processor on this site with the specified capability | |
349 | * @param array $capabilities | |
350 | * @param bool $isIncludeTest | |
351 | * | |
352 | * @return bool | |
353 | */ | |
354 | static function hasPaymentProcessorSupporting($capabilities = array(), $isIncludeTest = FALSE) { | |
355 | $result = self::getPaymentProcessors($capabilities, $isIncludeTest); | |
356 | return (!empty($result)) ? TRUE : FALSE; | |
357 | } | |
358 | ||
6a488035 TO |
359 | /** |
360 | * Function to retrieve payment processor id / info/ object based on component-id. | |
361 | * | |
da6b46f4 EM |
362 | * @param $entityID |
363 | * @param string $component component | |
364 | * @param string $type type of payment information to be retrieved | |
6a488035 | 365 | * |
da6b46f4 | 366 | * @internal param int $componentID id of a component |
6a488035 TO |
367 | * @return id / array / object based on type |
368 | * @static | |
369 | * @access public | |
370 | */ | |
371 | static function getProcessorForEntity($entityID, $component = 'contribute', $type = 'id') { | |
372 | $result = NULL; | |
373 | if (!in_array($component, array( | |
374 | 'membership', 'contribute', 'recur'))) { | |
375 | return $result; | |
376 | } | |
377 | //FIXME: | |
378 | if ($component == 'membership') { | |
379 | $sql = " | |
380 | SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test | |
381 | FROM civicrm_membership mem | |
382 | INNER JOIN civicrm_membership_payment mp ON ( mem.id = mp.membership_id ) | |
383 | INNER JOIN civicrm_contribution con ON ( mp.contribution_id = con.id ) | |
384 | LEFT JOIN civicrm_contribution_recur cr ON ( mem.contribution_recur_id = cr.id ) | |
385 | LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id ) | |
386 | WHERE mp.membership_id = %1"; | |
387 | } | |
388 | elseif ($component == 'contribute') { | |
389 | $sql = " | |
390 | SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test | |
391 | FROM civicrm_contribution con | |
392 | LEFT JOIN civicrm_contribution_recur cr ON ( con.contribution_recur_id = cr.id ) | |
393 | LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id ) | |
394 | WHERE con.id = %1"; | |
395 | } | |
396 | elseif ($component == 'recur') { | |
397 | $sql = " | |
398 | SELECT cr.payment_processor_id as ppID1, NULL as ppID2, cr.is_test | |
399 | FROM civicrm_contribution_recur cr | |
400 | WHERE cr.id = %1"; | |
401 | } | |
402 | ||
403 | //we are interesting in single record. | |
404 | $sql .= ' LIMIT 1'; | |
405 | ||
406 | $params = array(1 => array($entityID, 'Integer')); | |
407 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
408 | ||
409 | if (!$dao->fetch()) { | |
410 | ||
411 | return $result; | |
412 | ||
413 | } | |
414 | ||
415 | $ppID = (isset($dao->ppID1) && $dao->ppID1) ? $dao->ppID1 : (isset($dao->ppID2) ? $dao->ppID2 : NULL); | |
416 | $mode = (isset($dao->is_test) && $dao->is_test) ? 'test' : 'live'; | |
417 | if (!$ppID || $type == 'id') { | |
418 | $result = $ppID; | |
419 | } | |
420 | elseif ($type == 'info') { | |
421 | $result = self::getPayment($ppID, $mode); | |
422 | } | |
423 | elseif ($type == 'obj') { | |
424 | $payment = self::getPayment($ppID, $mode); | |
425 | $result = CRM_Core_Payment::singleton($mode, $payment); | |
426 | } | |
427 | ||
428 | return $result; | |
429 | } | |
430 | } | |
431 |