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