3 namespace Civi\Payment
;
7 * @package Civi\Payment
14 private static $singleton;
19 private $cache = array();
22 * @return \Civi\Payment\System
24 public static function singleton() {
25 if (!self
::$singleton) {
26 self
::$singleton = new self();
28 return self
::$singleton;
32 * Starting from the processor as an array retrieve the processor as an object.
34 * If there is no valid configuration it will not be retrieved.
36 * @param array $processor
38 * Override the config check. This is required in uninstall as no valid instances exist
39 * but will deliberately not work with any valid processors.
41 * @return \CRM_Core_Payment|NULL
43 * @throws \CRM_Core_Exception
45 public function getByProcessor($processor, $force = FALSE) {
46 $id = $force ?
0 : $processor['id'];
48 if (!isset($this->cache
[$id]) ||
$force) {
49 $ext = \CRM_Extension_System
::singleton()->getMapper();
50 if ($ext->isExtensionKey($processor['class_name'])) {
51 $paymentClass = $ext->keyToClass($processor['class_name'], 'payment');
52 require_once $ext->classToPath($paymentClass);
55 $paymentClass = 'CRM_Core_' . $processor['class_name'];
56 if (empty($paymentClass)) {
57 throw new \
CRM_Core_Exception('no class provided');
59 require_once str_replace('_', DIRECTORY_SEPARATOR
, $paymentClass) . '.php';
62 $processorObject = new $paymentClass(!empty($processor['is_test']) ?
'test' : 'live', $processor);
63 if (!$force && $processorObject->checkConfig()) {
64 $processorObject = NULL;
67 $processorObject->setPaymentProcessor($processor);
69 $this->cache
[$id] = $processorObject;
72 return $this->cache
[$id];
76 * Get payment processor by it's ID.
80 * @return \CRM_Core_Payment|NULL
81 * @throws \CiviCRM_API3_Exception
83 public function getById($id) {
85 return new \
CRM_Core_Payment_Manual();
87 $processor = civicrm_api3('payment_processor', 'getsingle', array('id' => $id, 'is_test' => NULL));
88 return self
::getByProcessor($processor);
93 * @param bool $is_test
95 * @return \CRM_Core_Payment|NULL
96 * @throws \CiviCRM_API3_Exception
98 public function getByName($name, $is_test) {
99 $processor = civicrm_api3('payment_processor', 'getsingle', array('name' => $name, 'is_test' => $is_test));
100 return self
::getByProcessor($processor);
104 * Flush processors from static cache.
106 * This is particularly used for tests.
108 public function flushProcessors() {
109 $this->cache
= array();
110 \CRM_Financial_BAO_PaymentProcessor
::getAllPaymentProcessors('all', TRUE);
111 \CRM_Financial_BAO_PaymentProcessor
::getAllPaymentProcessors('live', TRUE);
112 \CRM_Financial_BAO_PaymentProcessor
::getAllPaymentProcessors('test', TRUE);
116 * Sometimes we want to instantiate a processor object when no valid instance exists (eg. when uninstalling a
119 * This function does not load instance specific details for the processor.
121 * @param string $className
123 * @return \Civi\Payment\CRM_Core_Payment|NULL
124 * @throws \CiviCRM_API3_Exception
126 public function getByClass($className) {
127 return $this->getByProcessor(array(
128 'class_name' => $className,