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 | * |
16b10e64 CW |
104 | * @return CRM_Financial_DAO_PaymentProcessor|null |
105 | * object on success, null otherwise | |
6a488035 | 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 | * |
16b10e64 CW |
125 | * @return CRM_Financial_DAO_PaymentProcessor|null |
126 | * DAO object on success, null otherwise | |
6a488035 | 127 | * |
6a488035 | 128 | */ |
00be9182 | 129 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
130 | return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_PaymentProcessor', $id, 'is_active', $is_active); |
131 | } | |
132 | ||
133 | /** | |
100fef9d | 134 | * Retrieve the default payment processor |
6a488035 TO |
135 | * |
136 | * @param NULL | |
137 | * | |
16b10e64 | 138 | * @return CRM_Financial_DAO_PaymentProcessor|null |
a6c01b45 | 139 | * The default payment processor object on success, |
16b10e64 | 140 | * null otherwise |
6a488035 | 141 | */ |
00be9182 | 142 | public static function &getDefault() { |
6a488035 TO |
143 | if (self::$_defaultPaymentProcessor == NULL) { |
144 | $params = array('is_default' => 1); | |
145 | $defaults = array(); | |
146 | self::$_defaultPaymentProcessor = self::retrieve($params, $defaults); | |
147 | } | |
148 | return self::$_defaultPaymentProcessor; | |
149 | } | |
150 | ||
151 | /** | |
16b10e64 | 152 | * Delete payment processor |
6a488035 | 153 | * |
c490a46a | 154 | * @param int $paymentProcessorID |
77b97be7 EM |
155 | * |
156 | * @return null | |
6a488035 | 157 | */ |
00be9182 | 158 | public static function del($paymentProcessorID) { |
6a488035 | 159 | if (!$paymentProcessorID) { |
1f21f2cf | 160 | CRM_Core_Error::fatal(ts('Invalid value passed to delete function.')); |
6a488035 TO |
161 | } |
162 | ||
163 | $dao = new CRM_Financial_DAO_PaymentProcessor(); | |
164 | $dao->id = $paymentProcessorID; | |
165 | if (!$dao->find(TRUE)) { | |
166 | return NULL; | |
167 | } | |
168 | ||
169 | $testDAO = new CRM_Financial_DAO_PaymentProcessor(); | |
170 | $testDAO->name = $dao->name; | |
171 | $testDAO->is_test = 1; | |
172 | $testDAO->delete(); | |
173 | ||
174 | $dao->delete(); | |
175 | } | |
176 | ||
177 | /** | |
100fef9d | 178 | * Get the payment processor details |
6a488035 | 179 | * |
ed5dd492 TO |
180 | * @param int $paymentProcessorID |
181 | * Payment processor id. | |
182 | * @param string $mode | |
183 | * Payment mode ie test or live. | |
6a488035 | 184 | * |
a6c01b45 CW |
185 | * @return array |
186 | * associated array with payment processor related fields | |
6a488035 | 187 | */ |
00be9182 | 188 | public static function getPayment($paymentProcessorID, $mode) { |
6a488035 TO |
189 | if (!$paymentProcessorID) { |
190 | CRM_Core_Error::fatal(ts('Invalid value passed to getPayment function')); | |
191 | } | |
192 | ||
353ffa53 TO |
193 | $dao = new CRM_Financial_DAO_PaymentProcessor(); |
194 | $dao->id = $paymentProcessorID; | |
6a488035 TO |
195 | $dao->is_active = 1; |
196 | if (!$dao->find(TRUE)) { | |
197 | return NULL; | |
198 | } | |
199 | ||
200 | if ($mode == 'test') { | |
481a74f4 | 201 | $testDAO = new CRM_Financial_DAO_PaymentProcessor(); |
353ffa53 | 202 | $testDAO->name = $dao->name; |
6a488035 | 203 | $testDAO->is_active = 1; |
353ffa53 | 204 | $testDAO->is_test = 1; |
6a488035 TO |
205 | if (!$testDAO->find(TRUE)) { |
206 | CRM_Core_Error::fatal(ts('Could not retrieve payment processor details')); | |
207 | } | |
208 | return self::buildPayment($testDAO, $mode); | |
209 | } | |
210 | else { | |
211 | return self::buildPayment($dao, $mode); | |
212 | } | |
213 | } | |
214 | ||
e0ef6999 EM |
215 | /** |
216 | * @param $paymentProcessorIDs | |
217 | * @param $mode | |
218 | * | |
219 | * @return array | |
220 | * @throws Exception | |
221 | */ | |
00be9182 | 222 | public static function getPayments($paymentProcessorIDs, $mode) { |
6a488035 TO |
223 | if (!$paymentProcessorIDs) { |
224 | CRM_Core_Error::fatal(ts('Invalid value passed to getPayment function')); | |
225 | } | |
226 | ||
045f52a3 | 227 | $payments = array(); |
6a488035 TO |
228 | foreach ($paymentProcessorIDs as $paymentProcessorID) { |
229 | $payment = self::getPayment($paymentProcessorID, $mode); | |
230 | $payments[$payment['id']] = $payment; | |
231 | } | |
232 | ||
13ac605f | 233 | uasort($payments, 'self::defaultComparison'); |
6a488035 TO |
234 | return $payments; |
235 | } | |
236 | ||
13ac605f | 237 | /** |
100fef9d | 238 | * Compare 2 payment processors to see which should go first based on is_default |
13ac605f DG |
239 | * (sort function for sortDefaultFirst) |
240 | * @param array $processor1 | |
16b10e64 | 241 | * @param array $processor2 |
13ac605f DG |
242 | * @return number |
243 | */ | |
9b873358 | 244 | public static function defaultComparison($processor1, $processor2) { |
045f52a3 TO |
245 | $p1 = CRM_Utils_Array::value('is_default', $processor1); |
246 | $p2 = CRM_Utils_Array::value('is_default', $processor2); | |
247 | if ($p1 == $p2) { | |
248 | return 0; | |
13ac605f | 249 | } |
045f52a3 TO |
250 | return ($p1 > $p2) ? -1 : 1; |
251 | } | |
13ac605f | 252 | |
6a488035 | 253 | /** |
100fef9d | 254 | * Build payment processor details |
6a488035 | 255 | * |
ed5dd492 TO |
256 | * @param object $dao |
257 | * Payment processor object. | |
258 | * @param string $mode | |
259 | * Payment mode ie test or live. | |
6a488035 | 260 | * |
a6c01b45 CW |
261 | * @return array |
262 | * associated array with payment processor related fields | |
6a488035 | 263 | */ |
00be9182 | 264 | public static function buildPayment($dao, $mode) { |
6a488035 | 265 | $fields = array( |
353ffa53 TO |
266 | 'id', |
267 | 'name', | |
268 | 'payment_processor_type_id', | |
269 | 'user_name', | |
270 | 'password', | |
271 | 'signature', | |
272 | 'url_site', | |
273 | 'url_api', | |
274 | 'url_recur', | |
275 | 'url_button', | |
276 | 'subject', | |
277 | 'class_name', | |
278 | 'is_recur', | |
279 | 'billing_mode', | |
280 | 'is_test', | |
281 | 'payment_type', | |
282 | 'is_default', | |
6a488035 TO |
283 | ); |
284 | $result = array(); | |
285 | foreach ($fields as $name) { | |
286 | $result[$name] = $dao->$name; | |
287 | } | |
288 | $result['payment_processor_type'] = CRM_Core_PseudoConstant::paymentProcessorType(FALSE, $dao->payment_processor_type_id, 'name'); | |
289 | ||
3392cb17 | 290 | $result['instance'] = $result['object'] =& CRM_Core_Payment::singleton($mode, $result); |
6a488035 TO |
291 | |
292 | return $result; | |
293 | } | |
294 | ||
fbcb6fba | 295 | /** |
100fef9d | 296 | * Get all payment processors as an array of objects. |
fbcb6fba | 297 | * |
52767de0 EM |
298 | * @param string|NULL $mode |
299 | * only return this mode - test|live or NULL for all | |
fbcb6fba EM |
300 | * @param bool $reset |
301 | * | |
302 | * @throws CiviCRM_API3_Exception | |
303 | * @return array | |
304 | */ | |
52767de0 | 305 | public static function getAllPaymentProcessors($mode, $reset = FALSE) { |
76e7a76c | 306 | /* |
353ffa53 TO |
307 | * $cacheKey = 'CRM_Financial_BAO_Payment_Processor_' . ($mode ? 'test' : 'all'); |
308 | * if (!$reset) { | |
76e7a76c CW |
309 | * $processors = CRM_Utils_Cache::singleton()->get($cacheKey); |
310 | * if (!empty($processors)) { | |
311 | * return $processors; | |
312 | * } | |
353ffa53 | 313 | * } |
76e7a76c | 314 | */ |
353ffa53 TO |
315 | $retrievalParameters = array( |
316 | 'is_active' => TRUE, | |
317 | 'options' => array('sort' => 'is_default DESC, name'), | |
318 | 'api.payment_processor_type.getsingle' => 1 | |
319 | ); | |
52767de0 EM |
320 | if ($mode == 'test') { |
321 | $retrievalParameters['is_test'] = 1; | |
322 | } | |
323 | elseif ($mode == 'live') { | |
fbcb6fba EM |
324 | $retrievalParameters['is_test'] = 0; |
325 | } | |
326 | $processors = civicrm_api3('payment_processor', 'get', $retrievalParameters); | |
327 | foreach ($processors['values'] as $processor) { | |
52767de0 EM |
328 | $fieldsToProvide = array('user_name', 'password', 'signature', 'subject'); |
329 | foreach ($fieldsToProvide as $field) { | |
330 | //prevent e-notices in processor classes when not configured | |
331 | if (!isset($processor[$field])) { | |
332 | $processor[$field] = NULL; | |
333 | } | |
334 | } | |
9d91a9c6 | 335 | $processors['values'][$processor['id']]['payment_processor_type'] = $processor['payment_processor_type'] = $processors['values'][$processor['id']]['api.payment_processor_type.getsingle']['name']; |
52767de0 EM |
336 | $mode = empty($processor['is_test']) ? 'live' : 'test'; |
337 | $processors['values'][$processor['id']]['object'] = CRM_Core_Payment::singleton($mode, $processor); | |
fbcb6fba EM |
338 | } |
339 | /* | |
340 | CRM_Utils_Cache::singleton()->set($cacheKey, $processors); | |
341 | */ | |
342 | return $processors['values']; | |
343 | } | |
344 | ||
345 | /** | |
100fef9d | 346 | * Get Payment processors with specified capabilities. |
fbcb6fba EM |
347 | * Note that both the singleton & the pseudoconstant function have caching so we don't add |
348 | * arguably this could go on the pseudoconstant class | |
349 | * | |
350 | * @param array $capabilities | |
16b10e64 CW |
351 | * capabilities of processor e.g |
352 | * - BackOffice | |
353 | * - TestMode | |
354 | * - LiveMode | |
355 | * - FutureStartDate | |
fbcb6fba | 356 | * |
dc913073 EM |
357 | * @param array $ids |
358 | * | |
a6c01b45 CW |
359 | * @return array |
360 | * available processors | |
fbcb6fba | 361 | */ |
52767de0 EM |
362 | public static function getPaymentProcessors($capabilities = array(), $ids = array()) { |
363 | $mode = NULL; | |
364 | if (in_array('TestMode', $capabilities)) { | |
365 | $mode = 'test'; | |
366 | } | |
367 | elseif (in_array('LiveMode', $capabilities)) { | |
368 | $mode = 'live'; | |
369 | } | |
370 | $processors = self::getAllPaymentProcessors($mode); | |
fbcb6fba EM |
371 | if ($capabilities) { |
372 | foreach ($processors as $index => $processor) { | |
dc913073 EM |
373 | if (!empty($ids) && !in_array($processor['id'], $ids)) { |
374 | unset ($processors[$index]); | |
375 | continue; | |
376 | } | |
fbcb6fba EM |
377 | if (($error = $processor['object']->checkConfig()) != NULL) { |
378 | unset ($processors[$index]); | |
379 | continue; | |
380 | } | |
381 | foreach ($capabilities as $capability) { | |
d8ce0d68 | 382 | if (($processor['object']->supports($capability)) == FALSE) { |
fbcb6fba EM |
383 | unset ($processors[$index]); |
384 | } | |
385 | } | |
386 | } | |
387 | } | |
388 | return $processors; | |
389 | } | |
390 | ||
391 | /** | |
392 | * Is there a processor on this site with the specified capability | |
393 | * @param array $capabilities | |
394 | * @param bool $isIncludeTest | |
395 | * | |
396 | * @return bool | |
397 | */ | |
00be9182 | 398 | public static function hasPaymentProcessorSupporting($capabilities = array(), $isIncludeTest = FALSE) { |
52767de0 EM |
399 | $mode = $isIncludeTest ? 'Test' : 'Live'; |
400 | $capabilities[] = $mode . 'Mode'; | |
401 | $result = self::getPaymentProcessors($capabilities); | |
fbcb6fba EM |
402 | return (!empty($result)) ? TRUE : FALSE; |
403 | } | |
404 | ||
6a488035 | 405 | /** |
100fef9d | 406 | * Retrieve payment processor id / info/ object based on component-id. |
6a488035 | 407 | * |
100fef9d | 408 | * @param int $entityID |
ed5dd492 TO |
409 | * @param string $component |
410 | * Component. | |
411 | * @param string $type | |
412 | * Type of payment information to be retrieved. | |
6a488035 | 413 | * |
16b10e64 | 414 | * @return int|array|object |
6a488035 | 415 | */ |
00be9182 | 416 | public static function getProcessorForEntity($entityID, $component = 'contribute', $type = 'id') { |
6a488035 TO |
417 | $result = NULL; |
418 | if (!in_array($component, array( | |
353ffa53 TO |
419 | 'membership', |
420 | 'contribute', | |
421 | 'recur' | |
422 | )) | |
423 | ) { | |
6a488035 TO |
424 | return $result; |
425 | } | |
426 | //FIXME: | |
427 | if ($component == 'membership') { | |
428 | $sql = " | |
429 | SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test | |
430 | FROM civicrm_membership mem | |
431 | INNER JOIN civicrm_membership_payment mp ON ( mem.id = mp.membership_id ) | |
432 | INNER JOIN civicrm_contribution con ON ( mp.contribution_id = con.id ) | |
433 | LEFT JOIN civicrm_contribution_recur cr ON ( mem.contribution_recur_id = cr.id ) | |
434 | LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id ) | |
435 | WHERE mp.membership_id = %1"; | |
436 | } | |
437 | elseif ($component == 'contribute') { | |
438 | $sql = " | |
439 | SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test | |
440 | FROM civicrm_contribution con | |
441 | LEFT JOIN civicrm_contribution_recur cr ON ( con.contribution_recur_id = cr.id ) | |
442 | LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id ) | |
443 | WHERE con.id = %1"; | |
444 | } | |
445 | elseif ($component == 'recur') { | |
446 | $sql = " | |
447 | SELECT cr.payment_processor_id as ppID1, NULL as ppID2, cr.is_test | |
448 | FROM civicrm_contribution_recur cr | |
449 | WHERE cr.id = %1"; | |
450 | } | |
451 | ||
452 | //we are interesting in single record. | |
453 | $sql .= ' LIMIT 1'; | |
454 | ||
455 | $params = array(1 => array($entityID, 'Integer')); | |
456 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
457 | ||
458 | if (!$dao->fetch()) { | |
459 | ||
460 | return $result; | |
461 | ||
462 | } | |
463 | ||
464 | $ppID = (isset($dao->ppID1) && $dao->ppID1) ? $dao->ppID1 : (isset($dao->ppID2) ? $dao->ppID2 : NULL); | |
465 | $mode = (isset($dao->is_test) && $dao->is_test) ? 'test' : 'live'; | |
466 | if (!$ppID || $type == 'id') { | |
467 | $result = $ppID; | |
468 | } | |
469 | elseif ($type == 'info') { | |
470 | $result = self::getPayment($ppID, $mode); | |
471 | } | |
472 | elseif ($type == 'obj') { | |
473 | $payment = self::getPayment($ppID, $mode); | |
474 | $result = CRM_Core_Payment::singleton($mode, $payment); | |
475 | } | |
476 | ||
477 | return $result; | |
478 | } | |
479 | } |