Commit | Line | Data |
---|---|---|
914a49bf EM |
1 | <?php |
2 | ||
3 | namespace Civi\Payment; | |
4 | ||
5 | /** | |
6 | * Class System | |
7 | * @package Civi\Payment | |
8 | */ | |
9 | class System { | |
10 | ||
11 | /** | |
12 | * @var System | |
13 | */ | |
14 | private static $singleton; | |
15 | ||
16 | /** | |
0b0fefec | 17 | * @var array |
914a49bf | 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 | * |
4790df8a | 107 | * @return \CRM_Core_Payment |
c1797e86 | 108 | * |
4790df8a | 109 | * @throws \CiviCRM_API3_Exception|\CRM_Core_Exception |
914a49bf EM |
110 | */ |
111 | public function getById($id) { | |
4790df8a | 112 | if (isset($this->cache[$id])) { |
113 | return $this->cache[$id]; | |
114 | } | |
115 | if ((int) $id === 0) { | |
1d1fee72 | 116 | return new \CRM_Core_Payment_Manual(); |
117 | } | |
c64f69d9 | 118 | $processor = civicrm_api3('payment_processor', 'getsingle', ['id' => $id, 'is_test' => NULL]); |
4790df8a | 119 | return $this->getByProcessor($processor); |
914a49bf EM |
120 | } |
121 | ||
122 | /** | |
123 | * @param string $name | |
124 | * @param bool $is_test | |
1b9f9ca3 | 125 | * |
fe9cb36e | 126 | * @return \CRM_Core_Payment|NULL |
914a49bf EM |
127 | * @throws \CiviCRM_API3_Exception |
128 | */ | |
129 | public function getByName($name, $is_test) { | |
c64f69d9 | 130 | $processor = civicrm_api3('payment_processor', 'getsingle', ['name' => $name, 'is_test' => $is_test]); |
914a49bf EM |
131 | return self::getByProcessor($processor); |
132 | } | |
96025800 | 133 | |
1fee3ad2 EM |
134 | /** |
135 | * Flush processors from static cache. | |
136 | * | |
137 | * This is particularly used for tests. | |
1fee3ad2 EM |
138 | */ |
139 | public function flushProcessors() { | |
c64f69d9 | 140 | $this->cache = []; |
b04bcc45 | 141 | if (isset(\Civi::$statics['CRM_Contribute_BAO_ContributionRecur'])) { |
142 | unset(\Civi::$statics['CRM_Contribute_BAO_ContributionRecur']); | |
143 | } | |
576c8a85 | 144 | \CRM_Core_PseudoConstant::flush('paymentProcessor'); |
145 | civicrm_api3('PaymentProcessor', 'getfields', ['cache_clear' => 1]); | |
a160bb08 | 146 | \CRM_Financial_BAO_PaymentProcessor::getAllPaymentProcessors('all', TRUE); |
147 | \CRM_Financial_BAO_PaymentProcessor::getAllPaymentProcessors('live', TRUE); | |
d6944518 | 148 | \CRM_Financial_BAO_PaymentProcessor::getAllPaymentProcessors('test', TRUE); |
1fee3ad2 EM |
149 | } |
150 | ||
6b812400 EM |
151 | /** |
152 | * Sometimes we want to instantiate a processor object when no valid instance exists (eg. when uninstalling a | |
153 | * processor). | |
154 | * | |
155 | * This function does not load instance specific details for the processor. | |
156 | * | |
157 | * @param string $className | |
158 | * | |
159 | * @return \Civi\Payment\CRM_Core_Payment|NULL | |
160 | * @throws \CiviCRM_API3_Exception | |
161 | */ | |
162 | public function getByClass($className) { | |
c64f69d9 | 163 | return $this->getByProcessor([ |
6b812400 EM |
164 | 'class_name' => $className, |
165 | 'id' => 0, | |
166 | 'is_test' => 0, | |
c64f69d9 | 167 | ], |
6b812400 EM |
168 | TRUE); |
169 | } | |
170 | ||
914a49bf | 171 | } |