Use cached version of payment processor.
[civicrm-core.git] / CRM / Financial / BAO / PaymentProcessor.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33
34 /**
35 * This class contains payment processor related functions.
36 */
37 class CRM_Financial_BAO_PaymentProcessor extends CRM_Financial_DAO_PaymentProcessor {
38 /**
39 * Static holder for the default payment processor
40 */
41 static $_defaultPaymentProcessor = NULL;
42
43 /**
44 * Create Payment Processor.
45 *
46 * @param array $params
47 * Parameters for Processor entity.
48 *
49 * @return CRM_Financial_DAO_PaymentProcessor
50 * @throws Exception
51 */
52 public static function create($params) {
53 $processor = new CRM_Financial_DAO_PaymentProcessor();
54 $processor->copyValues($params);
55
56 if (empty($params['id'])) {
57 $ppTypeDAO = new CRM_Financial_DAO_PaymentProcessorType();
58 $ppTypeDAO->id = $params['payment_processor_type_id'];
59 if (!$ppTypeDAO->find(TRUE)) {
60 CRM_Core_Error::fatal(ts('Could not find payment processor meta information'));
61 }
62
63 // also copy meta fields from the info DAO
64 $processor->is_recur = $ppTypeDAO->is_recur;
65 $processor->billing_mode = $ppTypeDAO->billing_mode;
66 $processor->class_name = $ppTypeDAO->class_name;
67 $processor->payment_type = $ppTypeDAO->payment_type;
68 }
69
70 $processor->save();
71 // CRM-11826, add entry in civicrm_entity_financial_account
72 // if financial_account_id is not NULL
73 if (!empty($params['financial_account_id'])) {
74 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
75 $values = array(
76 'entity_table' => 'civicrm_payment_processor',
77 'entity_id' => $processor->id,
78 'account_relationship' => $relationTypeId,
79 'financial_account_id' => $params['financial_account_id'],
80 );
81 CRM_Financial_BAO_FinancialTypeAccount::add($values);
82 }
83
84 if (isset($params['id']) && isset($params['is_active']) && !isset($params['is_test'])) {
85 // check if is_active has changed & if so update test instance is_active too.
86 $test_id = self::getTestProcessorId($params['id']);
87 $testDAO = new CRM_Financial_DAO_PaymentProcessor();
88 $testDAO->id = $test_id;
89 if ($testDAO->find(TRUE)) {
90 $testDAO->is_active = $params['is_active'];
91 $testDAO->save();
92 }
93 }
94
95 Civi\Payment\System::singleton()->flushProcessors();
96 return $processor;
97 }
98
99 /**
100 * Class constructor.
101 */
102 public function __construct() {
103 parent::__construct();
104 }
105
106 /**
107 * Retrieve array of allowed credit cards for this payment processor.
108 * @param interger|null $paymentProcessorID id of processor.
109 * @return array
110 */
111 public static function getCreditCards($paymentProcessorID = NULL) {
112 if (!empty($paymentProcessorID)) {
113 $processor = new CRM_Financial_DAO_PaymentProcessor();
114 $processor->id = $paymentProcessorID;
115 $processor->find(TRUE);
116 $cards = json_decode($processor->accepted_credit_cards, TRUE);
117 return $cards;
118 }
119 return array();
120 }
121
122 /**
123 * Retrieve DB object based on input parameters.
124 *
125 * It also stores all the retrieved values in the default array.
126 *
127 * @param array $params
128 * (reference ) an assoc array of name/value pairs.
129 * @param array $defaults
130 * (reference ) an assoc array to hold the flattened values.
131 *
132 * @return CRM_Financial_DAO_PaymentProcessor|null
133 * object on success, null otherwise
134 */
135 public static function retrieve(&$params, &$defaults) {
136 $paymentProcessor = new CRM_Financial_DAO_PaymentProcessor();
137 $paymentProcessor->copyValues($params);
138 if ($paymentProcessor->find(TRUE)) {
139 CRM_Core_DAO::storeValues($paymentProcessor, $defaults);
140 return $paymentProcessor;
141 }
142 return NULL;
143 }
144
145 /**
146 * Update the is_active flag in the db.
147 *
148 * @param int $id
149 * Id of the database record.
150 * @param bool $is_active
151 * Value we want to set the is_active field.
152 *
153 * @return CRM_Financial_DAO_PaymentProcessor|null
154 * DAO object on success, null otherwise
155 *
156 */
157 public static function setIsActive($id, $is_active) {
158 return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_PaymentProcessor', $id, 'is_active', $is_active);
159 }
160
161 /**
162 * Retrieve the default payment processor.
163 *
164 * @return CRM_Financial_DAO_PaymentProcessor|null
165 * The default payment processor object on success,
166 * null otherwise
167 */
168 public static function &getDefault() {
169 if (self::$_defaultPaymentProcessor == NULL) {
170 $params = array('is_default' => 1);
171 $defaults = array();
172 self::$_defaultPaymentProcessor = self::retrieve($params, $defaults);
173 }
174 return self::$_defaultPaymentProcessor;
175 }
176
177 /**
178 * Delete payment processor.
179 *
180 * @param int $paymentProcessorID
181 *
182 * @return null
183 */
184 public static function del($paymentProcessorID) {
185 if (!$paymentProcessorID) {
186 CRM_Core_Error::fatal(ts('Invalid value passed to delete function.'));
187 }
188
189 $dao = new CRM_Financial_DAO_PaymentProcessor();
190 $dao->id = $paymentProcessorID;
191 if (!$dao->find(TRUE)) {
192 return NULL;
193 }
194
195 $testDAO = new CRM_Financial_DAO_PaymentProcessor();
196 $testDAO->name = $dao->name;
197 $testDAO->is_test = 1;
198 $testDAO->delete();
199
200 $dao->delete();
201 Civi\Payment\System::singleton()->flushProcessors();
202 }
203
204 /**
205 * Get the payment processor details.
206 *
207 * This returns an array whereas Civi\Payment\System::singleton->getByID() returns an object.
208 * The object is a key in the array.
209 *
210 * @param int $paymentProcessorID
211 * Payment processor id.
212 * @param string $mode
213 * Payment mode ie test or live.
214 *
215 * @return array
216 * associated array with payment processor related fields
217 */
218 public static function getPayment($paymentProcessorID, $mode = 'based_on_id') {
219 $capabilities = ($mode == 'test') ? array('TestMode') : array();
220 $processors = self::getPaymentProcessors($capabilities, array($paymentProcessorID));
221 return $processors[$paymentProcessorID];
222 }
223
224 /**
225 * Given a live processor ID get the test id.
226 *
227 * @param int $id
228 *
229 * @return int
230 * Test payment processor ID.
231 */
232 public static function getTestProcessorId($id) {
233 $liveProcessorName = civicrm_api3('payment_processor', 'getvalue', array(
234 'id' => $id,
235 'return' => 'name',
236 ));
237 return civicrm_api3('payment_processor', 'getvalue', array(
238 'return' => 'id',
239 'name' => $liveProcessorName,
240 'is_test' => 1,
241 'domain_id' => CRM_Core_Config::domainID(),
242 ));
243 }
244
245 /**
246 * Compare 2 payment processors to see which should go first based on is_default
247 * (sort function for sortDefaultFirst)
248 * @param array $processor1
249 * @param array $processor2
250 *
251 * @return int
252 */
253 public static function defaultComparison($processor1, $processor2) {
254 $p1 = CRM_Utils_Array::value('is_default', $processor1);
255 $p2 = CRM_Utils_Array::value('is_default', $processor2);
256 if ($p1 == $p2) {
257 return 0;
258 }
259 return ($p1 > $p2) ? -1 : 1;
260 }
261
262 /**
263 * Get all payment processors as an array of objects.
264 *
265 * @param string|NULL $mode
266 * only return this mode - test|live or NULL for all
267 * @param bool $reset
268 * @param bool $isCurrentDomainOnly
269 * Do we only want to load payment processors associated with the current domain.
270 *
271 * @throws CiviCRM_API3_Exception
272 * @return array
273 */
274 public static function getAllPaymentProcessors($mode = 'all', $reset = FALSE, $isCurrentDomainOnly = TRUE) {
275
276 $cacheKey = 'CRM_Financial_BAO_Payment_Processor_' . $mode . '_' . $isCurrentDomainOnly . '_' . CRM_Core_Config::domainID();
277 if (!$reset) {
278 $processors = CRM_Utils_Cache::singleton()->get($cacheKey);
279 if (!empty($processors)) {
280 return $processors;
281 }
282 }
283
284 $retrievalParameters = array(
285 'is_active' => TRUE,
286 'options' => array('sort' => 'is_default DESC, name', 'limit' => 0),
287 'api.payment_processor_type.getsingle' => 1,
288 );
289 if ($isCurrentDomainOnly) {
290 $retrievalParameters['domain_id'] = CRM_Core_Config::domainID();
291 }
292 if ($mode == 'test') {
293 $retrievalParameters['is_test'] = 1;
294 }
295 elseif ($mode == 'live') {
296 $retrievalParameters['is_test'] = 0;
297 }
298
299 $processors = civicrm_api3('payment_processor', 'get', $retrievalParameters);
300 foreach ($processors['values'] as $processor) {
301 $fieldsToProvide = array(
302 'id',
303 'name',
304 'payment_processor_type_id',
305 'user_name',
306 'password',
307 'signature',
308 'url_site',
309 'url_api',
310 'url_recur',
311 'url_button',
312 'subject',
313 'class_name',
314 'is_recur',
315 'billing_mode',
316 'is_test',
317 'payment_type',
318 'is_default',
319 );
320 foreach ($fieldsToProvide as $field) {
321 // Prevent e-notices in processor classes when not configured.
322 if (!isset($processor[$field])) {
323 $processors['values'][$processor['id']][$field] = NULL;
324 }
325 }
326 $processors['values'][$processor['id']]['payment_processor_type'] = $processor['payment_processor_type'] = $processors['values'][$processor['id']]['api.payment_processor_type.getsingle']['name'];
327 $processors['values'][$processor['id']]['object'] = Civi\Payment\System::singleton()->getByProcessor($processor);
328 }
329
330 // Add the pay-later pseudo-processor.
331 $processors['values'][0] = array(
332 'object' => new CRM_Core_Payment_Manual(),
333 'id' => 0,
334 'payment_processor_type_id' => 0,
335 // This shouldn't be required but there are still some processors hacked into core with nasty 'if's.
336 'payment_processor_type' => 'Manual',
337 'class_name' => 'Payment_Manual',
338 'name' => 'pay_later',
339 'billing_mode' => '',
340 'is_default' => 0,
341 'payment_instrument_id' => key(CRM_Core_OptionGroup::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1')),
342 // Making this optionally recur would give lots of options -but it should
343 // be a row in the payment processor table before we do that.
344 'is_recur' => FALSE,
345 'is_test' => FALSE,
346 );
347
348 CRM_Utils_Cache::singleton()->set($cacheKey, $processors['values']);
349
350 return $processors['values'];
351 }
352
353 /**
354 * Get Payment processors with specified capabilities.
355 * Note that both the singleton & the pseudoconstant function have caching so we don't add
356 * arguably this could go on the pseudoconstant class
357 *
358 * @param array $capabilities
359 * capabilities of processor e.g
360 * - BackOffice
361 * - TestMode
362 * - LiveMode
363 * - FutureStartDate
364 *
365 * @param array|bool $ids
366 *
367 * @return array
368 * available processors
369 */
370 public static function getPaymentProcessors($capabilities = array(), $ids = FALSE) {
371 $testProcessors = in_array('TestMode', $capabilities) ? self::getAllPaymentProcessors('test') : array();
372 if (is_array($ids)) {
373 $processors = self::getAllPaymentProcessors('all', FALSE, FALSE);
374 }
375 else {
376 $processors = self::getAllPaymentProcessors('all');
377 }
378
379 if (in_array('TestMode', $capabilities) && is_array($ids)) {
380 $possibleLiveIDs = array_diff($ids, array_keys($testProcessors));
381 foreach ($possibleLiveIDs as $possibleLiveID) {
382 if (isset($processors[$possibleLiveID]) && ($liveProcessorName = $processors[$possibleLiveID]['name']) != FALSE) {
383 foreach ($testProcessors as $index => $testProcessor) {
384 if ($testProcessor['name'] == $liveProcessorName) {
385 $ids[] = $testProcessor['id'];
386 }
387 }
388 }
389 }
390 $processors = $testProcessors;
391 }
392
393 foreach ($processors as $index => $processor) {
394 if (is_array($ids) && !in_array($processor['id'], $ids)) {
395 unset($processors[$index]);
396 continue;
397 }
398 // Invalid processors will store a null value in 'object' (e.g. if not all required config fields are present).
399 // This is determined by calling when loading the processor via the $processorObject->checkConfig() function.
400 if (!is_a($processor['object'], 'CRM_Core_Payment')) {
401 unset($processors[$index]);
402 continue;
403 }
404 foreach ($capabilities as $capability) {
405 if (($processor['object']->supports($capability)) == FALSE) {
406 unset($processors[$index]);
407 continue 1;
408 }
409 }
410 }
411
412 return $processors;
413 }
414
415 /**
416 * Is there a processor on this site with the specified capability.
417 *
418 * The capabilities are defined on CRM_Core_Payment and can be extended by
419 * processors.
420 *
421 * examples are
422 * - supportsBackOffice
423 * - supportsLiveMode
424 * - supportsFutureRecurDate
425 * - supportsRecurring
426 * - supportsCancelRecurring
427 * - supportsRecurContributionsForPledges
428 *
429 * They are passed as array('BackOffice');
430 *
431 * Details of specific functions are in the docblocks on the CRM_Core_Payment class.
432 *
433 * @param array $capabilities
434 *
435 * @return bool
436 */
437 public static function hasPaymentProcessorSupporting($capabilities = array()) {
438 $capabilitiesString = implode('', $capabilities);
439 if (!isset(\Civi::$statics[__CLASS__]['supported_capabilities'][$capabilitiesString])) {
440 $result = self::getPaymentProcessors($capabilities);
441 \Civi::$statics[__CLASS__]['supported_capabilities'][$capabilitiesString] = (!empty($result) && array_keys($result) !== array(0)) ? TRUE : FALSE;
442 }
443 return \Civi::$statics[__CLASS__]['supported_capabilities'][$capabilitiesString];
444 }
445
446 /**
447 * Retrieve payment processor id / info/ object based on component-id.
448 *
449 * @todo function needs revisiting. The whole 'info / obj' thing is an overload. Recommend creating new functions
450 * that are entity specific as there is little shared code specific to obj or info
451 *
452 * Also, it does not accurately derive the processor - for a completed contribution the best place to look is in the
453 * relevant financial_trxn record. For a recurring contribution it is in the contribution_recur table.
454 *
455 * For a membership the relevant contribution_recur should be derived & then resolved as above. The contribution page
456 * is never a reliable place to look as there can be more than one configured. For a pending contribution there is
457 * no way to derive the processor - but hey - what processor? it didn't go through!
458 *
459 * Query for membership might look something like:
460 * SELECT fte.payment_processor_id
461 * FROM civicrm_membership mem
462 * INNER JOIN civicrm_line_item li ON ( mem.id = li.entity_id AND li.entity_table = 'civicrm_membership')
463 * INNER JOIN civicrm_contribution con ON ( li.contribution_id = con.id )
464 * LEFT JOIN civicrm_entity_financial_trxn ft ON ft.entity_id = con.id AND ft.entity_table =
465 * 'civicrm_contribution'
466 * LEFT JOIN civicrm_financial_trxn fte ON fte.id = ft.financial_trxn_id
467 *
468 * @param int $entityID
469 * @param string $component
470 * Component.
471 * @param string $type
472 * Type of payment information to be retrieved.
473 *
474 * @return int|array|object
475 */
476 public static function getProcessorForEntity($entityID, $component = 'contribute', $type = 'id') {
477 $result = NULL;
478 if (!in_array($component, array(
479 'membership',
480 'contribute',
481 'recur',
482 ))
483 ) {
484 return $result;
485 }
486
487 if ($component == 'membership') {
488 $sql = "
489 SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test
490 FROM civicrm_membership mem
491 INNER JOIN civicrm_membership_payment mp ON ( mem.id = mp.membership_id )
492 INNER JOIN civicrm_contribution con ON ( mp.contribution_id = con.id )
493 LEFT JOIN civicrm_contribution_recur cr ON ( mem.contribution_recur_id = cr.id )
494 LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id )
495 WHERE mp.membership_id = %1";
496 }
497 elseif ($component == 'contribute') {
498 $sql = "
499 SELECT cr.payment_processor_id as ppID1, cp.payment_processor as ppID2, con.is_test
500 FROM civicrm_contribution con
501 LEFT JOIN civicrm_contribution_recur cr ON ( con.contribution_recur_id = cr.id )
502 LEFT JOIN civicrm_contribution_page cp ON ( con.contribution_page_id = cp.id )
503 WHERE con.id = %1";
504 }
505 elseif ($component == 'recur') {
506 // @deprecated - use getPaymentProcessorForRecurringContribution.
507 $sql = "
508 SELECT cr.payment_processor_id as ppID1, NULL as ppID2, cr.is_test
509 FROM civicrm_contribution_recur cr
510 WHERE cr.id = %1";
511 }
512
513 // We are interested in a single record.
514 $sql .= ' LIMIT 1';
515
516 $params = array(1 => array($entityID, 'Integer'));
517 $dao = CRM_Core_DAO::executeQuery($sql, $params);
518
519 if (!$dao->fetch()) {
520
521 return $result;
522
523 }
524
525 $ppID = (isset($dao->ppID1) && $dao->ppID1) ? $dao->ppID1 : (isset($dao->ppID2) ? $dao->ppID2 : NULL);
526 $mode = (isset($dao->is_test) && $dao->is_test) ? 'test' : 'live';
527 if (!$ppID || $type == 'id') {
528 $result = $ppID;
529 }
530 elseif ($type == 'info') {
531 $result = self::getPayment($ppID, $mode);
532 }
533 elseif ($type == 'obj' && is_numeric($ppID)) {
534 try {
535 $paymentProcessor = civicrm_api3('PaymentProcessor', 'getsingle', array('id' => $ppID));
536 }
537 catch (API_Exception $e) {
538 // Unable to load the processor because this function uses an unreliable method to derive it.
539 // The function looks to load the payment processor ID from the contribution page, which
540 // can support multiple processors.
541 }
542
543 $paymentProcessor['payment_processor_type'] = CRM_Core_PseudoConstant::getName('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', $paymentProcessor['payment_processor_type_id']);
544 $result = Civi\Payment\System::singleton()->getByProcessor($paymentProcessor);
545 }
546 return $result;
547 }
548
549 /**
550 * Get the payment processor associated with a recurring contribution series.
551 *
552 * @param int $contributionRecurID
553 *
554 * @return \CRM_Core_Payment
555 */
556 public static function getPaymentProcessorForRecurringContribution($contributionRecurID) {
557 $paymentProcessorId = civicrm_api3('ContributionRecur', 'getvalue', array(
558 'id' => $contributionRecurID,
559 'return' => 'payment_processor_id',
560 ));
561 return Civi\Payment\System::singleton()->getById($paymentProcessorId);
562 }
563
564 /**
565 * Get the name of the payment processor
566 *
567 * @param $paymentProcessorId
568 *
569 * @return null|string
570 */
571 public static function getPaymentProcessorName($paymentProcessorId) {
572 try {
573 $paymentProcessor = civicrm_api3('PaymentProcessor', 'getsingle', array(
574 'return' => array('name'),
575 'id' => $paymentProcessorId,
576 ));
577 return $paymentProcessor['name'];
578 }
579 catch (Exception $e) {
580 return ts('Unknown') . ' (' . $paymentProcessorId . ')';
581 }
582 }
583
584 /**
585 * Generate and assign an arbitrary value to a field of a test object.
586 *
587 * @param string $fieldName
588 * @param array $fieldDef
589 * @param int $counter
590 * The globally-unique ID of the test object.
591 */
592 protected function assignTestValue($fieldName, &$fieldDef, $counter) {
593 if ($fieldName === 'class_name') {
594 $this->class_name = 'Payment_Dummy';
595 }
596 else {
597 parent::assignTestValue($fieldName, $fieldDef, $counter);
598 }
599 }
600
601 }