3 namespace Civi\Payment
;
7 * @package Civi\Payment
14 private static $singleton;
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($processor['class_name'])) {
57 throw new \
CRM_Core_Exception('no class provided');
61 $processorObject = NULL;
62 if (class_exists($paymentClass)) {
63 $processorObject = new $paymentClass(!empty($processor['is_test']) ?
'test' : 'live', $processor);
64 if ($force ||
!$processorObject->checkConfig()) {
65 $processorObject->setPaymentProcessor($processor);
68 $this->cache
[$id] = $processorObject;
71 return $this->cache
[$id];
75 * Execute checkConfig() on the payment processor Object.
76 * This function creates a new instance of the processor object and returns the output of checkConfig
78 * @param array $processor
82 * @throws \CRM_Core_Exception
84 public function checkProcessorConfig($processor) {
85 $ext = \CRM_Extension_System
::singleton()->getMapper();
86 if ($ext->isExtensionKey($processor['class_name'])) {
87 $paymentClass = $ext->keyToClass($processor['class_name'], 'payment');
88 require_once $ext->classToPath($paymentClass);
91 $paymentClass = 'CRM_Core_' . $processor['class_name'];
92 if (empty($paymentClass)) {
93 throw new \
CRM_Core_Exception('no class provided');
95 require_once str_replace('_', DIRECTORY_SEPARATOR
, $paymentClass) . '.php';
98 $processorObject = new $paymentClass(!empty($processor['is_test']) ?
'test' : 'live', $processor);
99 return $processorObject->checkConfig();
103 * Get payment processor by it's ID.
107 * @return \CRM_Core_Payment
109 * @throws \CiviCRM_API3_Exception|\CRM_Core_Exception
111 public function getById($id) {
112 if (isset($this->cache
[$id])) {
113 return $this->cache
[$id];
115 if ((int) $id === 0) {
116 return new \
CRM_Core_Payment_Manual();
118 $processor = civicrm_api3('payment_processor', 'getsingle', ['id' => $id, 'is_test' => NULL]);
119 return $this->getByProcessor($processor);
123 * @param string $name
124 * @param bool $is_test
126 * @return \CRM_Core_Payment|NULL
127 * @throws \CiviCRM_API3_Exception
129 public function getByName($name, $is_test) {
130 $processor = civicrm_api3('payment_processor', 'getsingle', ['name' => $name, 'is_test' => $is_test]);
131 return self
::getByProcessor($processor);
135 * Flush processors from static cache.
137 * This is particularly used for tests.
139 public function flushProcessors() {
141 if (isset(\Civi
::$statics['CRM_Contribute_BAO_ContributionRecur'])) {
142 unset(\Civi
::$statics['CRM_Contribute_BAO_ContributionRecur']);
144 \CRM_Core_PseudoConstant
::flush('paymentProcessor');
145 civicrm_api3('PaymentProcessor', 'getfields', ['cache_clear' => 1]);
146 \CRM_Financial_BAO_PaymentProcessor
::getAllPaymentProcessors('all', TRUE);
147 \CRM_Financial_BAO_PaymentProcessor
::getAllPaymentProcessors('live', TRUE);
148 \CRM_Financial_BAO_PaymentProcessor
::getAllPaymentProcessors('test', TRUE);
152 * Sometimes we want to instantiate a processor object when no valid instance exists (eg. when uninstalling a
155 * This function does not load instance specific details for the processor.
157 * @param string $className
159 * @return \Civi\Payment\CRM_Core_Payment|NULL
160 * @throws \CiviCRM_API3_Exception
162 public function getByClass($className) {
163 return $this->getByProcessor([
164 'class_name' => $className,