Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | * This class stores logic for managing CiviCRM extensions. | |
30 | * | |
31 | * @package CRM | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | class CRM_Extension_Manager_Payment extends CRM_Extension_Manager_Base { | |
37 | ||
38 | /** | |
39 | @var CRM_Extension_Mapper | |
40 | */ | |
41 | protected $mapper; | |
42 | ||
e0ef6999 EM |
43 | /** |
44 | * @param CRM_Extension_Mapper $mapper | |
45 | */ | |
6a488035 TO |
46 | public function __construct(CRM_Extension_Mapper $mapper) { |
47 | parent::__construct(TRUE); | |
48 | $this->mapper = $mapper; | |
49 | } | |
50 | ||
51 | /** | |
52 | * {@inheritdoc} | |
53 | */ | |
54 | public function onPreInstall(CRM_Extension_Info $info) { | |
55 | $paymentProcessorTypes = $this->_getAllPaymentProcessorTypes('class_name'); | |
56 | ||
57 | if (array_key_exists($info->key, $paymentProcessorTypes)) { | |
58 | CRM_Core_Error::fatal('This payment processor type is already installed.'); | |
59 | } | |
60 | ||
61 | $ppByName = $this->_getAllPaymentProcessorTypes('name'); | |
62 | if (array_key_exists($info->name, $ppByName)) { | |
63 | CRM_Core_Error::fatal('This payment processor type already exists.'); | |
64 | } | |
65 | ||
66 | $dao = new CRM_Financial_DAO_PaymentProcessorType(); | |
67 | ||
68 | $dao->is_active = 1; | |
69 | $dao->class_name = trim($info->key); | |
70 | $dao->title = trim($info->name) . ' (' . trim($info->key) . ')'; | |
71 | $dao->name = trim($info->name); | |
72 | $dao->description = trim($info->description); | |
73 | ||
74 | $dao->user_name_label = trim($info->typeInfo['userNameLabel']); | |
75 | $dao->password_label = trim($info->typeInfo['passwordLabel']); | |
76 | $dao->signature_label = trim($info->typeInfo['signatureLabel']); | |
77 | $dao->subject_label = trim($info->typeInfo['subjectLabel']); | |
78 | $dao->url_site_default = trim($info->typeInfo['urlSiteDefault']); | |
79 | $dao->url_api_default = trim($info->typeInfo['urlApiDefault']); | |
80 | $dao->url_recur_default = trim($info->typeInfo['urlRecurDefault']); | |
81 | $dao->url_site_test_default = trim($info->typeInfo['urlSiteTestDefault']); | |
82 | $dao->url_api_test_default = trim($info->typeInfo['urlApiTestDefault']); | |
83 | $dao->url_recur_test_default = trim($info->typeInfo['urlRecurTestDefault']); | |
84 | $dao->url_button_default = trim($info->typeInfo['urlButtonDefault']); | |
85 | $dao->url_button_test_default = trim($info->typeInfo['urlButtonTestDefault']); | |
86 | ||
87 | switch (trim($info->typeInfo['billingMode'])) { | |
88 | case 'form': | |
89 | $dao->billing_mode = CRM_Core_Payment::BILLING_MODE_FORM; | |
90 | break; | |
91 | ||
92 | case 'button': | |
93 | $dao->billing_mode = CRM_Core_Payment::BILLING_MODE_BUTTON; | |
94 | break; | |
95 | ||
96 | case 'notify': | |
97 | $dao->billing_mode = CRM_Core_Payment::BILLING_MODE_NOTIFY; | |
98 | break; | |
99 | ||
100 | default: | |
101 | CRM_Core_Error::fatal('Billing mode in info file has wrong value.'); | |
102 | } | |
103 | ||
104 | $dao->is_recur = trim($info->typeInfo['isRecur']); | |
105 | $dao->payment_type = trim($info->typeInfo['paymentType']); | |
106 | ||
107 | $dao->save(); | |
108 | } | |
109 | ||
110 | /** | |
111 | * {@inheritdoc} | |
112 | */ | |
113 | public function onPostInstall(CRM_Extension_Info $info) { | |
114 | $this->_runPaymentHook($info, 'install'); | |
115 | } | |
116 | ||
117 | /** | |
118 | * {@inheritdoc} | |
119 | */ | |
120 | public function onPreUninstall(CRM_Extension_Info $info) { | |
121 | $paymentProcessorTypes = $this->_getAllPaymentProcessorTypes('class_name'); | |
122 | if (!array_key_exists($info->key, $paymentProcessorTypes)) { | |
123 | CRM_Core_Error::fatal('This payment processor type is not registered.'); | |
124 | } | |
125 | ||
126 | $dao = new CRM_Financial_DAO_PaymentProcessor(); | |
127 | $dao->payment_processor_type_id = $paymentProcessorTypes[$info->key]; | |
128 | $dao->find(); | |
129 | while ($dao->fetch()) { | |
130 | throw new CRM_Extension_Exception_DependencyException('payment'); | |
131 | } | |
132 | ||
133 | $this->_runPaymentHook($info, 'uninstall'); | |
134 | return CRM_Financial_BAO_PaymentProcessorType::del($paymentProcessorTypes[$info->key]); | |
135 | } | |
136 | ||
137 | /** | |
138 | * {@inheritdoc} | |
139 | */ | |
140 | public function onPreDisable(CRM_Extension_Info $info) { | |
141 | // HMM? // if ($this->type == 'payment' && $this->status != 'missing') { | |
142 | $this->_runPaymentHook($info, 'disable'); | |
143 | ||
144 | $paymentProcessorTypes = $this->_getAllPaymentProcessorTypes('class_name'); | |
145 | CRM_Financial_BAO_PaymentProcessorType::setIsActive($paymentProcessorTypes[$info->key], 0); | |
146 | } | |
147 | ||
148 | /** | |
149 | * {@inheritdoc} | |
150 | */ | |
151 | public function onPreEnable(CRM_Extension_Info $info) { | |
152 | $paymentProcessorTypes = $this->_getAllPaymentProcessorTypes('class_name'); | |
153 | CRM_Financial_BAO_PaymentProcessorType::setIsActive($paymentProcessorTypes[$info->key], 1); | |
154 | } | |
155 | ||
156 | /** | |
157 | * {@inheritdoc} | |
158 | */ | |
159 | public function onPostEnable(CRM_Extension_Info $info) { | |
160 | // HMM? // if ($this->type == 'payment' && $this->status != 'missing') { | |
161 | $this->_runPaymentHook($info, 'enable'); | |
162 | } | |
163 | ||
164 | /** | |
165 | * @param string $attr the attribute used to key the array | |
166 | * @return array ($$attr => $id) | |
167 | */ | |
168 | private function _getAllPaymentProcessorTypes($attr) { | |
169 | $ppt = array(); | |
170 | $dao = new CRM_Financial_DAO_PaymentProcessorType(); | |
171 | $dao->find(); | |
172 | while ($dao->fetch()) { | |
173 | $ppt[$dao->$attr] = $dao->id; | |
174 | } | |
175 | return $ppt; | |
176 | } | |
177 | ||
178 | /** | |
179 | * Function to run hooks in the payment processor class | |
180 | * Load requested payment processor and call the method specified. | |
181 | * | |
da6b46f4 | 182 | * @param CRM_Extension_Info $info |
6a488035 | 183 | * @param string $method - the method to call in the payment processor class |
da6b46f4 | 184 | * |
6a488035 TO |
185 | * @private |
186 | */ | |
187 | private function _runPaymentHook(CRM_Extension_Info $info, $method) { | |
188 | // Not concerned about performance at this stage, as these are seldomly performed tasks | |
189 | // (payment processor enable/disable/install/uninstall). May wish to implement some | |
190 | // kind of registry/caching system if more hooks are added. | |
191 | ||
192 | try { | |
193 | $paymentClass = $this->mapper->keyToClass($info->key, 'payment'); | |
194 | $file = $this->mapper->classToPath($paymentClass); | |
195 | if (! file_exists($file)) { | |
196 | CRM_Core_Session::setStatus(ts('Failed to load file (%3) for payment processor (%1) while running "%2"', array(1 => $info->key, 2 => $method, 3 => $file)), '', 'error'); | |
197 | return; | |
198 | } else { | |
199 | require_once $file; | |
200 | } | |
201 | } catch (CRM_Extension_Exception $e) { | |
202 | CRM_Core_Session::setStatus(ts('Failed to determine file path for payment processor (%1) while running "%2"', array(1 => $info->key, 2 => $method)), '', 'error'); | |
203 | return; | |
204 | } | |
205 | ||
206 | // See if we have any instances of this PP defined .. | |
207 | if ($processor_id = CRM_Core_DAO::singleValueQuery(" | |
208 | SELECT pp.id | |
209 | FROM civicrm_extension ext | |
210 | INNER JOIN civicrm_payment_processor_type ppt | |
211 | ON ext.name = ppt.name | |
212 | INNER JOIN civicrm_payment_processor pp | |
213 | ON ppt.id = pp.payment_processor_type_id | |
214 | WHERE ext.type = 'payment' | |
215 | AND ext.full_name = %1 | |
216 | ", | |
217 | array( | |
218 | 1 => array($info->key, 'String'), | |
219 | ) | |
220 | )) { | |
221 | // If so, load params in the usual way .. | |
222 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($processor_id, NULL); | |
223 | } | |
224 | else { | |
225 | // Otherwise, do the best we can to construct some .. | |
226 | $dao = CRM_Core_DAO::executeQuery(" | |
227 | SELECT ppt.* | |
228 | FROM civicrm_extension ext | |
229 | INNER JOIN civicrm_payment_processor_type ppt | |
230 | ON ppt.name = ext.name | |
231 | WHERE ext.name = %1 | |
232 | AND ext.type = 'payment' | |
233 | ", | |
234 | array( | |
235 | 1 => array($info->name, 'String'), | |
236 | ) | |
237 | ); | |
238 | if ($dao->fetch()) $paymentProcessor = array( | |
239 | 'id' => -1, | |
240 | 'name' => $dao->title, | |
241 | 'payment_processor_type_id' => $dao->id, | |
242 | 'user_name' => 'nothing', | |
243 | 'password' => 'nothing', | |
244 | 'signature' => '', | |
245 | 'url_site' => $dao->url_site_default, | |
246 | 'url_api' => $dao->url_api_default, | |
247 | 'url_recur' => $dao->url_recur_default, | |
248 | 'url_button' => $dao->url_button_default, | |
249 | 'subject' => '', | |
250 | 'class_name' => $dao->class_name, | |
251 | 'is_recur' => $dao->is_recur, | |
252 | 'billing_mode' => $dao->billing_mode, | |
253 | 'payment_type' => $dao->payment_type, | |
254 | ); | |
255 | else CRM_Core_Error::fatal("Unable to find payment processor in " . __CLASS__ . '::' . __METHOD__); | |
256 | } | |
257 | ||
258 | // In the case of uninstall, check for instances of PP first. | |
259 | // Don't run hook if any are found. | |
260 | if ($method == 'uninstall' && $paymentProcessor['id'] > 0) { | |
261 | return; | |
262 | } | |
263 | ||
264 | switch ($method) { | |
265 | case 'install': | |
266 | case 'uninstall': | |
267 | case 'enable': | |
268 | case 'disable': | |
269 | ||
270 | // Instantiate PP | |
0e6e8724 | 271 | $processorInstance = $paymentClass::singleton(NULL, $paymentProcessor); |
6a488035 TO |
272 | |
273 | // Does PP implement this method, and can we call it? | |
274 | if (method_exists($processorInstance, $method) && is_callable(array( | |
275 | $processorInstance, $method))) { | |
276 | // If so, call it ... | |
277 | $processorInstance->$method(); | |
278 | } | |
279 | break; | |
280 | ||
281 | default: | |
282 | CRM_Core_Session::setStatus(ts( "Unrecognized payment hook (%1) in %2::%3", | |
283 | array(1 => $method, 2 => __CLASS__ , 3 => __METHOD__) ), | |
284 | '', 'error'); | |
285 | } | |
286 | } | |
287 | } |