Merge pull request #13374 from cividesk/dev-627
[civicrm-core.git] / Civi / Payment / System.php
CommitLineData
914a49bf
EM
1<?php
2
3namespace Civi\Payment;
4
5/**
6 * Class System
7 * @package Civi\Payment
8 */
9class System {
10
11 /**
12 * @var System
13 */
14 private static $singleton;
15
16 /**
17 * @var array cache
18 */
c64f69d9 19 private $cache = [];
914a49bf
EM
20
21 /**
22 * @return \Civi\Payment\System
23 */
24 public static function singleton() {
25 if (!self::$singleton) {
26 self::$singleton = new self();
27 }
28 return self::$singleton;
29 }
30
31 /**
428e38a4
EM
32 * Starting from the processor as an array retrieve the processor as an object.
33 *
34 * If there is no valid configuration it will not be retrieved.
35 *
914a49bf 36 * @param array $processor
6b812400
EM
37 * @param bool $force
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.
428e38a4 40 *
81716ddb 41 * @return \CRM_Core_Payment|NULL
428e38a4 42 *
507d3b64 43 * @throws \CRM_Core_Exception
914a49bf 44 */
6b812400
EM
45 public function getByProcessor($processor, $force = FALSE) {
46 $id = $force ? 0 : $processor['id'];
914a49bf 47
6b812400
EM
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);
53 }
54 else {
55 $paymentClass = 'CRM_Core_' . $processor['class_name'];
8461467c 56 if (empty($processor['class_name'])) {
6b812400 57 throw new \CRM_Core_Exception('no class provided');
914a49bf 58 }
6b812400 59 }
914a49bf 60
8461467c
MW
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);
66 }
f92d1e2a 67 }
4ecbf127 68 $this->cache[$id] = $processorObject;
914a49bf 69 }
4ecbf127 70
ef1c2283 71 return $this->cache[$id];
914a49bf 72 }
46bcf597 73
3eecd5bc
MW
74 /**
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
77 *
78 * @param array $processor
79 *
80 * @return string|NULL
81 *
82 * @throws \CRM_Core_Exception
83 */
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);
89 }
90 else {
91 $paymentClass = 'CRM_Core_' . $processor['class_name'];
92 if (empty($paymentClass)) {
93 throw new \CRM_Core_Exception('no class provided');
94 }
95 require_once str_replace('_', DIRECTORY_SEPARATOR, $paymentClass) . '.php';
96 }
97
98 $processorObject = new $paymentClass(!empty($processor['is_test']) ? 'test' : 'live', $processor);
99 return $processorObject->checkConfig();
100 }
101
914a49bf 102 /**
1d1fee72 103 * Get payment processor by it's ID.
104 *
df8d3074 105 * @param int $id
1b9f9ca3 106 *
fe9cb36e 107 * @return \CRM_Core_Payment|NULL
914a49bf
EM
108 * @throws \CiviCRM_API3_Exception
109 */
110 public function getById($id) {
1d1fee72 111 if ($id == 0) {
112 return new \CRM_Core_Payment_Manual();
113 }
c64f69d9 114 $processor = civicrm_api3('payment_processor', 'getsingle', ['id' => $id, 'is_test' => NULL]);
914a49bf
EM
115 return self::getByProcessor($processor);
116 }
117
118 /**
119 * @param string $name
120 * @param bool $is_test
1b9f9ca3 121 *
fe9cb36e 122 * @return \CRM_Core_Payment|NULL
914a49bf
EM
123 * @throws \CiviCRM_API3_Exception
124 */
125 public function getByName($name, $is_test) {
c64f69d9 126 $processor = civicrm_api3('payment_processor', 'getsingle', ['name' => $name, 'is_test' => $is_test]);
914a49bf
EM
127 return self::getByProcessor($processor);
128 }
96025800 129
1fee3ad2
EM
130 /**
131 * Flush processors from static cache.
132 *
133 * This is particularly used for tests.
1fee3ad2
EM
134 */
135 public function flushProcessors() {
c64f69d9 136 $this->cache = [];
b04bcc45 137 if (isset(\Civi::$statics['CRM_Contribute_BAO_ContributionRecur'])) {
138 unset(\Civi::$statics['CRM_Contribute_BAO_ContributionRecur']);
139 }
576c8a85 140 \CRM_Core_PseudoConstant::flush('paymentProcessor');
141 civicrm_api3('PaymentProcessor', 'getfields', ['cache_clear' => 1]);
a160bb08 142 \CRM_Financial_BAO_PaymentProcessor::getAllPaymentProcessors('all', TRUE);
143 \CRM_Financial_BAO_PaymentProcessor::getAllPaymentProcessors('live', TRUE);
d6944518 144 \CRM_Financial_BAO_PaymentProcessor::getAllPaymentProcessors('test', TRUE);
1fee3ad2
EM
145 }
146
6b812400
EM
147 /**
148 * Sometimes we want to instantiate a processor object when no valid instance exists (eg. when uninstalling a
149 * processor).
150 *
151 * This function does not load instance specific details for the processor.
152 *
153 * @param string $className
154 *
155 * @return \Civi\Payment\CRM_Core_Payment|NULL
156 * @throws \CiviCRM_API3_Exception
157 */
158 public function getByClass($className) {
c64f69d9 159 return $this->getByProcessor([
6b812400
EM
160 'class_name' => $className,
161 'id' => 0,
162 'is_test' => 0,
c64f69d9 163 ],
6b812400
EM
164 TRUE);
165 }
166
914a49bf 167}